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