]> mj.ucw.cz Git - umpf.git/blob - code.c
add more binary operators
[umpf.git] / code.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5
6 #include "umpf.h"
7
8 #define HASHSIZE 103
9 #define MAGIC 19
10
11 struct list* 
12 new_var_hash(void)
13 {
14         struct list* res;
15         int i;
16
17         res = xmalloc (HASHSIZE * sizeof(struct list));
18         for (i = 0; i < HASHSIZE; i++)
19                 list_init(res + i);
20         
21         return res;
22 }
23
24 static int
25 get_bucket_number(char* name)
26 {
27         unsigned int n = 0;
28         unsigned char* p = name;
29
30         while (*p != '\0'){
31                 n = n * MAGIC + toupper(*p++);
32         }
33         n %= HASHSIZE;
34
35         return n;
36 }
37
38 /* if not found, variable with value "" is created  */ 
39 int
40 find_var(char* name, struct list* hash)
41 {
42         int n;
43         struct variable *p;
44
45         n = get_bucket_number(name);
46         int nocase = isupper(*name);
47         LIST_FOREACH(p, hash + n)
48                 if (!(nocase ? strcasecmp : strcmp)(p->name,name))
49                         return p->varcode;
50
51         p = xmalloc(sizeof(struct variable));
52         p->name = xstrdup(name);
53         p->varcode = current_varcode++;
54         list_add_last(hash+n, &p->car);
55
56         return p->varcode;
57 }
58
59 int
60 store_const(char* c)
61 {
62         if (cur_const_n >= cur_const_s) {
63                 cur_const_s *= 2;
64                 const_tab = xrealloc(const_tab, cur_const_s);
65         }
66
67         const_tab[cur_const_n] = c;
68
69         return -cur_const_n++;  
70 }
71
72 static void
73 new_instr(struct code c, struct list* where)
74 {
75         struct code* p = xmalloc(sizeof(struct code));
76         *p = c;
77         if (where)
78                 list_add_last(where, &p->car);
79         else
80                 list_add_last(&input_code, &p->car);
81 }
82
83 static int
84 new_3par_instr(int opcode, int left, int right, int pref_var, 
85                  struct list* where)
86 {
87         struct code ins;
88
89         ins.opcode = opcode;
90         ins.u.tpop.l = left;
91         ins.u.tpop.r = right;
92         if (pref_var >= 0)
93                 ins.u.tpop.res = pref_var;
94         else
95                 ins.u.tpop.res = current_varcode++;;
96         new_instr(ins, where);
97         return ins.u.tpop.res;
98 }
99
100 /* return number of variable where lies result 
101  * pref_var < 0 => no preference
102  */
103 static int
104 evaluate(struct tree* t, int pref_var, struct list* where)
105 {
106         if (t->st == ST_LEAF) { 
107                 return t->pt.leaf.n;
108         } else if (t->st == ST_OP) {
109                 int left, right;
110                 left = evaluate(t->pt.op.left, -1, where);
111                 right = evaluate(t->pt.op.right, -1, where);
112                 switch (t->pt.op.op) {
113                         case '.':
114                                 return new_3par_instr(OPC_CAT, left, 
115                                         right, pref_var, where);
116                                 break;
117                         default:
118                                 die("evaluate: got to default");
119                 }
120         } else
121                 die("evaluate: I can evaluate only expressions but I got %d",
122                         t->st);
123 }
124
125 static void
126 do_ass(struct tree* t, struct list* where)
127 {
128         int var_l, var_r;
129         struct code ins;
130         var_l = t->pt.ass.left->pt.leaf.n;
131         var_r = evaluate(t->pt.ass.right, -1, where);
132         
133         ins.opcode = OPC_SET;
134         ins.u.set.l = var_l;
135         ins.u.set.r = var_r;
136         new_instr(ins, where);
137
138 }
139
140 static int
141 eval_cond(struct tree *t, int pref_var, struct list* where)
142 {
143         int left, right;
144         if (t->pt.cond.type == JUST_BOOL) {
145                 if (t->pt.cond.left->st == ST_LEAF)
146                         return t->pt.cond.left->pt.leaf.n;
147                 if (t->pt.cond.left->st == ST_OP)
148                         return evaluate(t->pt.cond.left, -1, where);
149                 else
150                         die("eval_cond: %d cannot be JUST_BOOL\n", 
151                         t->pt.cond.left->st);
152         }
153         if (t->pt.cond.type == OP_REL) {
154                 left = evaluate(t->pt.cond.left, -1, where);
155                 right = evaluate(t->pt.cond.right, -1, where);
156
157                 switch (t->pt.cond.op) {                
158                         case '>':
159                                 return new_3par_instr (OPC_GT, left, 
160                                         right, pref_var, where);
161                                 break;
162                         case '<':
163                                 return new_3par_instr (OPC_LT, left, 
164                                         right, pref_var, where);
165                                 break;
166                         case CC('<','='):
167                                 return new_3par_instr (OPC_LE, left, 
168                                         right, pref_var, where);
169                                 break;
170                         case CC('>','='):
171                                 return new_3par_instr (OPC_GE, left, 
172                                         right, pref_var, where);
173                                 break;
174                         case CC('!','~'):
175                                 return new_3par_instr (OPC_NRE, left, 
176                                         right, pref_var, where);
177                                 break;
178                         case CC('~','~'):
179                                 return new_3par_instr (OPC_RE, left, 
180                                         right, pref_var, where);
181                                 break;
182                         case CC('=','='):
183                                 return new_3par_instr (OPC_EQ, left, 
184                                         right, pref_var, where);
185                                 break;
186                         case CC('!','='):
187                                 return new_3par_instr (OPC_NEQ, left, 
188                                         right, pref_var, where);
189                                 break;
190                         /* fixme: do more of them */
191                         default:
192                                 die("eval_cond: unknown relation op %c\n", 
193                                 t->pt.cond.op);
194
195                 }
196         }
197         if (t->pt.cond.type == OP_BOOL) {
198                 struct code ins;
199
200                 left = eval_cond(t->pt.cond.left, -1, where);
201                 if (t->pt.cond.op != '!') /* ! is unary */
202                         right = eval_cond(t->pt.cond.right, -1, where);
203                 switch (t->pt.cond.op) {
204                         case '&':
205                                 return new_3par_instr (OPC_AND, left, 
206                                         right, pref_var, where);
207                                 break;
208                         case '|':
209                                 return new_3par_instr (OPC_OR, left, 
210                                         right, pref_var, where);
211                                 break;
212                         case '^':
213                                 return new_3par_instr (OPC_XOR, left, 
214                                         right, pref_var, where);
215                                 break;
216                         case '!':
217                                 ins.opcode = OPC_NOT;
218                                 ins.u.dpop.par = left;
219                                 if (pref_var >= 0)
220                                         ins.u.dpop.res = pref_var;
221                                 else
222                                         ins.u.dpop.res = current_varcode++;;
223                                 new_instr(ins, where);
224                                 return ins.u.dpop.res;
225                                 break;
226                         default:
227                                 die("eval_cond: unknown boolean op %c\n", 
228                                 t->pt.cond.op);
229                 }
230         }
231         
232         die("eval_cond: unknown condition type");
233 }
234
235 static void
236 do_if(struct tree *t, struct list* where)
237 {
238         int c;
239         struct code ins, nop, jmp;
240         struct list* if_branch = xmalloc(sizeof(struct list));
241         struct list* else_branch = xmalloc(sizeof(struct list));
242
243         list_init(if_branch);
244         list_init(else_branch);
245         nop.opcode  = OPC_NOP;
246         jmp.opcode = OPC_JUMP;
247
248         c = eval_cond(t->pt.tif.c, -1, where);
249
250         compile(t->pt.tif.i, if_branch);        
251         compile(t->pt.tif.i, else_branch);      
252         new_instr(nop, if_branch);
253         new_instr(nop, else_branch);
254         jmp.u.jump.target = list_last(else_branch);
255         new_instr(jmp, if_branch);
256         
257         ins.opcode = OPC_JUMP_UNLESS;
258         ins.u.jump_unless.cond = c;
259         ins.u.jump_unless.target = list_last(if_branch);
260         new_instr(ins, where);
261         list_cat(where, if_branch);
262         list_cat(where, else_branch);
263
264         free(if_branch);
265         free(else_branch);
266 }
267
268 static void
269 do_arrow(struct tree* t, struct list* where)
270 {
271         int v;
272         struct code ins;
273
274
275         if (t->pt.arrow.left == K_COPY)
276                 ins.u.arrow.copy = 1;
277         else
278                 ins.u.arrow.copy = 0;
279         switch (t->pt.arrow.right) {
280                 case K_EMPTY:
281                         ins.opcode = OPC_STORE;
282                         break;
283                 case K_PIPE:
284                         ins.opcode = OPC_PIPE;
285                         break;
286                 case K_MAIL:
287                         ins.opcode = OPC_MAIL;
288                         break;
289                 case K_DISCARD:
290                         ins.opcode = OPC_DISCARD;
291                         break;
292                 default:
293                         die("do_arrow: This cannot happen ;-)");
294         }
295
296         if (t->pt.arrow.right != K_DISCARD) {
297                 v = evaluate(t->pt.arrow.s, -1, where);
298                 ins.u.arrow.what = v;
299         }
300
301         new_instr(ins, where);
302 }
303
304 static void
305 reset_temp_var_count(void)
306 {
307         current_varcode = temp_varcode_start;
308 }
309
310 void
311 compile(struct tree* t, struct list* where)
312 {
313         if (!t)
314                 return;
315         if (! where)
316                 where = &input_code;
317
318         switch(t->st) {
319                 case ST_BLOCK:
320                         reset_temp_var_count();
321                         compile(t->pt.block.head, where);
322                         compile(t->pt.block.tail, where);
323                         break;
324                 case ST_EMPTY:
325                         break;
326                 case ST_LEAF: //warn?
327                         break;
328                 case ST_ASS:
329                         do_ass(t, where);
330                         break;
331                 case ST_OP:
332                         evaluate(t, -1, where); //emit warning?
333                         break;
334                 case ST_IF:
335                         do_if(t, where);
336                         break;
337                 case ST_COND:
338                         eval_cond(t, -1, where); // warn?
339                 case ST_ARROW:
340                         do_arrow(t, where);
341                         break;
342                 default:
343                         die("compile: got to default, type: %d", t->st);
344         }
345 }
346
347 void
348 print_code(void)
349 {
350         struct code* p;
351
352         LIST_FOREACH(p, &input_code) {
353                 switch (p->opcode) {
354                         case OPC_SET:
355                                 printf("SET %d %d\n", p->u.set.l, p->u.set.r);
356                                 break; 
357                         case OPC_CAT:
358                                 printf("CAT %d %d %d\n", p->u.tpop.l,
359                                 p->u.tpop.r, p->u.tpop.res);
360                                 break;
361                         case OPC_JUMP:
362                                 printf("JUMP %d\n", (int) p->u.jump.target);
363                                 break;
364                         case OPC_JUMP_UNLESS:
365                                 printf("JUMP_UNLESS %d %d\n", p->u.jump_unless.cond,(int) p->u.jump_unless.target);
366                                 break;
367                         case OPC_GT:
368                                 printf("GT %d %d %d\n", p->u.tpop.l, p->u.tpop.r, p->u.tpop.res);
369                                 break;
370                         case OPC_AND:
371                                 printf("AND %d %d %d\n", p->u.tpop.l, p->u.tpop.r, p->u.tpop.res);
372                                 break;
373                         case OPC_NOP:
374                                 puts("NOP");    
375                                 break;
376                         case OPC_PIPE:
377                                 printf("PIPE %d %d\n", p->u.arrow.what, p->u.arrow.copy);
378                                 break;
379                         case OPC_STORE:
380                                 printf("STORE %d %d\n", p->u.arrow.what, p->u.arrow.copy);
381                                 break;
382                         case OPC_MAIL:
383                                 printf("MAIL %d %d\n", p->u.arrow.what, p->u.arrow.copy);
384                                 break;
385                         case OPC_DISCARD:
386                                 puts("DISCARD");
387                                 break;
388                         default:
389                                 printf("not implemented, opcode: %d\n",
390                                 p->opcode);
391                 }
392         }
393 }