]> mj.ucw.cz Git - umpf.git/blob - code.c
start with conditions
[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 /* return number of variable where lies result 
84  * pref_var < 0 => no preference
85  */
86 static int
87 evaluate(struct tree* t, int pref_var, struct list* where)
88 {
89         struct code ins;
90
91         if (t->st == ST_LEAF) { 
92                 return t->pt.leaf.n;
93         } else if (t->st == ST_OP) {
94                 int left, right;
95                 left = evaluate(t->pt.op.left, -1, where);
96                 right = evaluate(t->pt.op.right, -1, where);
97                 switch (t->pt.op.op) {
98                         case '.':
99                                 ins.opcode = CAT;
100                                 ins.u.cat.l = left;
101                                 ins.u.cat.r = right;
102                                 if (pref_var >= 0)
103                                         ins.u.cat.res = pref_var;
104                                 else
105                                         ins.u.cat.res = current_varcode++;;
106                                 new_instr(ins, where);
107                                 return ins.u.cat.res;
108                                 break;
109                         default:
110                                 die("evaluate: got to default");
111                 }
112         } else
113                 die("evaluate: I can evaluate only expressions but I got %d",
114                         t->st);
115 }
116
117 static void
118 do_ass(struct tree* t, struct list* where)
119 {
120         int var_l, var_r;
121         struct code ins;
122         var_l = t->pt.ass.left->pt.leaf.n;
123         var_r = evaluate(t->pt.ass.right, -1, where);
124         
125         ins.opcode = SET;
126         ins.u.set.l = var_l;
127         ins.u.set.r = var_r;
128         new_instr(ins, where);
129
130 }
131
132 static int
133 eval_cond(struct tree *t, int pref_var, struct list* where)
134 {
135         struct code ins;
136         int left, right;
137         if (t->pt.cond.type == JUST_BOOL) {
138                 if (t->pt.cond.left->st == ST_LEAF)
139                         return t->pt.cond.left->pt.leaf.n;
140                 if (t->pt.cond.left->st == ST_OP)
141                         return evaluate(t->pt.cond.left, -1, where);
142                 else
143                         die("eval_cond: %d cannot be JUST_BOOL\n", 
144                         t->pt.cond.left->st);
145         }
146         if (t->pt.cond.type == OP_REL) {
147                 left = evaluate(t->pt.cond.left, -1, where);
148                 right = evaluate(t->pt.cond.right, -1, where);
149
150                 switch (t->pt.cond.op) {                
151                         case '>':
152                                 ins.opcode = GT;
153                                 ins.u.gt.l = left;
154                                 ins.u.gt.r = right;
155                                 if (pref_var >= 0)
156                                         ins.u.gt.res = pref_var;
157                                 else
158                                         ins.u.gt.res = current_varcode++;;
159                                 new_instr(ins, where);
160                                 return ins.u.gt.res;
161                         break;
162                         /* fixme: do more of them */
163                         default:
164                                 die("eval_cond: unknown relation op %c\n", 
165                                 t->pt.cond.op);
166
167                 }
168         }
169 }
170
171 static void
172 do_if(struct tree *t, struct list* where)
173 {
174         int c;
175         struct code ins, nop, jmp;
176         struct list* if_branch = xmalloc(sizeof(struct list));
177         struct list* else_branch = xmalloc(sizeof(struct list));
178
179         list_init(if_branch);
180         list_init(else_branch);
181         nop.opcode  = NOP;
182         jmp.opcode = JUMP;
183
184         c = eval_cond(t->pt.tif.c, -1, where);
185
186         compile(t->pt.tif.i, if_branch);        
187         compile(t->pt.tif.i, else_branch);      
188         new_instr(nop, if_branch);
189         new_instr(nop, else_branch);
190         jmp.u.jump.target = list_last(else_branch);
191         new_instr(jmp, if_branch);
192         
193         ins.opcode = JUMP_UNLESS;
194         ins.u.jump_unless.cond = c;
195         ins.u.jump_unless.target = list_last(if_branch);
196         new_instr(ins, where);
197         list_cat(where, if_branch);
198         list_cat(where, else_branch);
199
200         free(if_branch);
201         free(else_branch);
202 }
203
204 static void
205 reset_temp_var_count(void)
206 {
207         current_varcode = temp_varcode_start;
208 }
209
210 void
211 compile(struct tree* t, struct list* where)
212 {
213         if (!t)
214                 return;
215         if (! where)
216                 where = &input_code;
217
218         switch(t->st) {
219                 case ST_BLOCK:
220                         reset_temp_var_count();
221                         compile(t->pt.block.head, where);
222                         compile(t->pt.block.tail, where);
223                         break;
224                 case ST_EMPTY:
225                         break;
226                 case ST_ASS:
227                         do_ass(t, where);
228                         break;
229                 case ST_OP:
230                         evaluate(t, -1, where); //emit warning?
231                         break;
232                 case ST_IF:
233                         do_if(t, where);
234                         break;
235                 case ST_COND:
236                         eval_cond(t, -1, where); // warn?
237                 default:
238                         die("compile: got to default");
239         }
240 }
241
242 void
243 print_code(void)
244 {
245         struct code* p;
246
247         LIST_FOREACH(p, &input_code) {
248                 switch (p->opcode) {
249                         case SET:
250                                 printf("SET %d %d\n", p->u.set.l, p->u.set.r);
251                                 break; 
252                         case CAT:
253                                 printf("CAT %d %d %d\n", p->u.cat.l,
254                                 p->u.cat.r, p->u.cat.res);
255                                 break;
256                         case JUMP:
257                                 printf("JUMP %d\n", (int) p->u.jump.target);
258                                 break;
259                         case JUMP_UNLESS:
260                                 printf("JUMP_UNLESS %d %d\n", p->u.jump_unless.cond,(int) p->u.jump_unless.target);
261                                 break;
262                         case GT:
263                                 printf("GT %d %d %d\n", p->u.gt.l, p->u.gt.r, p->u.gt.res);
264                                 break;
265                         case NOP:
266                                 puts("NOP");    
267                                 break;
268                         default:
269                                 printf("not implemented, opcode: %d\n",
270                                 p->opcode);
271                 }
272         }
273 }