]> mj.ucw.cz Git - umpf.git/blob - cond.y
add nice conditions
[umpf.git] / cond.y
1 %{
2
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "umpf.h"
7
8 static struct tree* tree_malloc(int type);
9
10 %}
11 %error-verbose
12
13 %union {
14         int n;
15         char* str;
16         struct tree* tr;        
17 }
18
19 %token <str> CONST
20 %token <n> NUM
21 %token <str> VAR
22 %token <str> KW_DISCARD
23 %token <str> KW_PIPE KW_MAIL KW_COPY
24 %token '(' ')' '{' '}' ';'
25 %nonassoc KW_IF
26 %nonassoc KW_ELSE
27 %left ARROW
28 %left <n> EQ NEQ GE LE '<' '>' RE NRE
29 %left '='
30 %left '.'
31 %left '+' '-' 
32 %left '*' '/'
33 %left <n> '|'
34 %left <n> '^'
35 %left <n> '&'
36 %left <n> '!'
37 %type <tr> input 
38 %type <tr> command 
39 %type <tr> next 
40 %type <tr> ass 
41 %type <tr> ass_right 
42 %type <tr> cif
43 %type <tr> arrow 
44 %type <tr> cond
45 %type <n> rop 
46 %type <str> left
47 %type <str> right 
48 %type <tr> leaves 
49
50 %%
51 input:  /* empty */     { $$ = input_tree = tree_malloc(ST_EMPTY); }
52         | command input {       $$ = tree_malloc(ST_BLOCK); 
53                                 $$->pt.block.head = $1;
54                                 $$->pt.block.tail = $2;
55
56                                 input_tree = $$;
57                         } 
58 ;
59
60 command:         ';' { $$ = tree_malloc(ST_EMPTY); }
61                 | '{' command next '}'  {
62                                                 $$ = tree_malloc(ST_BLOCK); 
63                                                 $$->pt.block.head = $2;
64                                                 $$->pt.block.tail = $3; 
65                                         }
66                 | '{' '}' { $$ = tree_malloc(ST_EMPTY); }
67                 | cif
68                 | ass ';' { $$ = $1; }
69                 | arrow ';' { $$ = $1; }
70                 
71         
72 ;
73
74 next:   /* empty */ {$$ = tree_malloc(ST_EMPTY); }
75         | command
76         
77
78 ;
79
80 cif:    KW_IF cond command KW_ELSE command      { 
81                                         $$ = tree_malloc(ST_IF);
82                                         $$->pt.tif.c = $2;
83                                         $$->pt.tif.i = $3;
84                                         $$->pt.tif.e = $5;
85                                 }
86         | KW_IF cond command    { 
87                                         $$ = tree_malloc(ST_IF);
88                                         $$->pt.tif.c = $2;
89                                         $$->pt.tif.i = $3;
90                                         $$->pt.tif.e = tree_malloc(ST_EMPTY); 
91                                 }
92 ;
93
94 cond:   '!' cond {
95                                 $$ = tree_malloc(ST_COND);
96                                 $$->pt.cond.left = $2;  
97                                 $$->pt.cond.right = NULL; 
98                                 $$->pt.cond.op = $1;    
99                                 $$->pt.cond.type = OP_BOOL;     
100                 }
101         | cond '|' cond {
102                                 $$ = tree_malloc(ST_COND);
103                                 $$->pt.cond.left = $1;  
104                                 $$->pt.cond.right = $3; 
105                                 $$->pt.cond.op = $2;    
106                                 $$->pt.cond.type = OP_BOOL;     
107                         }
108         | cond '&' cond {
109                                 $$ = tree_malloc(ST_COND);
110                                 $$->pt.cond.left = $1;  
111                                 $$->pt.cond.right = $3; 
112                                 $$->pt.cond.op = $2;    
113                                 $$->pt.cond.type = OP_BOOL;     
114                         }
115         | cond '^' cond {
116                                 $$ = tree_malloc(ST_COND);
117                                 $$->pt.cond.left = $1;  
118                                 $$->pt.cond.right = $3; 
119                                 $$->pt.cond.op = $2;    
120                                 $$->pt.cond.type = OP_BOOL;     
121
122                         }
123         | '(' cond ')' { $$ = $2; }
124         | ass_right rop ass_right       {
125                                                 $$ = tree_malloc(ST_COND);
126                                                 $$->pt.cond.left = $1;  
127                                                 $$->pt.cond.right = $3; 
128                                                 $$->pt.cond.op = $2;    
129                                                 $$->pt.cond.type = OP_REL;      
130                                         }
131         | ass_right {
132                                 $$ = tree_malloc(ST_COND);
133                                 $$->pt.cond.left = $1;  
134                                 $$->pt.cond.type = JUST_BOOL;   
135                 }
136 ;
137
138 rop:    '>'
139         | '<'
140         | EQ
141         | NEQ
142         | LE
143         | GE
144         | RE
145         | NRE
146 ;
147
148 ass:
149         VAR '=' ass_right       {
150                                         $$ = tree_malloc(ST_ASS);
151
152                                         $$->pt.ass.left = tree_malloc(ST_LEAF);
153                                         $$->pt.ass.left->pt.leaf.type = L_VAR;
154                                         $$->pt.ass.left->pt.leaf.value = $1;
155                                         $$->pt.ass.left->pt.leaf.n = find_var($1, var_hash);
156                                         $$->pt.ass.right = $3;
157                                 }
158 ;
159
160 leaves:         VAR     {
161                                 $$ = tree_malloc(ST_LEAF);
162                                 $$->pt.leaf.type = L_VAR;
163                                 $$->pt.leaf.value = $1;
164                                 $$->pt.leaf.n = find_var($1, var_hash);
165                         }
166                 | CONST { 
167                                 $$ = tree_malloc(ST_LEAF);
168                                 $$->pt.leaf.type = L_CONST;
169                                 $$->pt.leaf.value = $1;
170                                 $$->pt.leaf.n = store_const($1);
171                         }
172 ;
173
174 arrow:  left ARROW right ass_right  {
175                                         $$ = tree_malloc(ST_ARROW);
176                                         $$->pt.arrow.s = $4;
177                                         $$->pt.arrow.kw_left = $1;
178                                         $$->pt.arrow.kw_right = $3;
179                                 }
180         | left ARROW KW_DISCARD         { //FIXME: actually left does not make sense here 
181                                         $$ = tree_malloc(ST_ARROW);
182                                         $$->pt.arrow.s = NULL;
183                                         $$->pt.arrow.kw_left = NULL;
184                                         $$->pt.arrow.kw_right = "discard";
185                                 }
186 ;
187
188 left:   /* empty */ { $$ = NULL;}
189         | KW_COPY { $$ = "copy"; }
190
191 ;
192
193 right:  /* empty */ { $$ = NULL; }
194         | KW_PIPE { $$ = "pipe"; }
195         | KW_MAIL { $$ = "mail"; }
196 ;
197
198 ass_right:      leaves
199                 | ass_right '.' ass_right       {
200                                         $$ = tree_malloc(ST_OP);
201                                         $$->pt.op.op = '.';
202                                         $$->pt.op.left = $1;    
203                                         $$->pt.op.right = $3;   
204                                 }
205 ;
206
207 %%
208
209 struct tree* 
210 tree_malloc(int type)
211 {
212         struct tree* temp;
213         temp = xmalloc(sizeof (struct tree));
214         temp->st=type;
215
216         return temp;
217 }
218
219 void
220 yyerror (char const *s)
221 {
222         fprintf (stderr, "Line %d: %s\n", line, s);
223 }