17 res = xmalloc (HASHSIZE * sizeof(struct list));
18 for (i = 0; i < HASHSIZE; i++)
25 get_bucket_number(char* name)
28 unsigned char* p = name;
31 n = n * MAGIC + toupper(*p++);
38 /* if not found, variable with value "" is created */
40 find_var(char* name, struct list* hash)
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))
51 p = xmalloc(sizeof(struct variable));
52 p->name = xstrdup(name);
53 p->varcode = current_varcode++;
54 list_add_last(hash+n, &p->car);
62 if (cur_const_n >= cur_const_s) {
64 const_tab = xrealloc(const_tab, cur_const_s);
67 const_tab[cur_const_n] = c;
69 return -cur_const_n++;
73 new_instr(struct code c, struct list* where)
75 struct code* p = xmalloc(sizeof(struct code));
78 list_add_last(where, &p->car);
80 list_add_last(&input_code, &p->car);
83 /* return number of variable where lies result
84 * pref_var < 0 => no preference
87 evaluate(struct tree* t, int pref_var, struct list* where)
91 if (t->st == ST_LEAF) {
93 } else if (t->st == ST_OP) {
95 left = evaluate(t->pt.op.left, -1, where);
96 right = evaluate(t->pt.op.right, -1, where);
97 switch (t->pt.op.op) {
101 ins.u.tpop.r = right;
103 ins.u.tpop.res = pref_var;
105 ins.u.tpop.res = current_varcode++;;
106 new_instr(ins, where);
107 return ins.u.tpop.res;
110 die("evaluate: got to default");
113 die("evaluate: I can evaluate only expressions but I got %d",
118 do_ass(struct tree* t, struct list* where)
122 var_l = t->pt.ass.left->pt.leaf.n;
123 var_r = evaluate(t->pt.ass.right, -1, where);
128 new_instr(ins, where);
133 new_cond_instr(int opcode, int left, int right, int pref_var,
140 ins.u.tpop.r = right;
142 ins.u.tpop.res = pref_var;
144 ins.u.tpop.res = current_varcode++;;
145 new_instr(ins, where);
146 return ins.u.tpop.res;
150 eval_cond(struct tree *t, int pref_var, struct list* where)
153 if (t->pt.cond.type == JUST_BOOL) {
154 if (t->pt.cond.left->st == ST_LEAF)
155 return t->pt.cond.left->pt.leaf.n;
156 if (t->pt.cond.left->st == ST_OP)
157 return evaluate(t->pt.cond.left, -1, where);
159 die("eval_cond: %d cannot be JUST_BOOL\n",
160 t->pt.cond.left->st);
162 if (t->pt.cond.type == OP_REL) {
163 left = evaluate(t->pt.cond.left, -1, where);
164 right = evaluate(t->pt.cond.right, -1, where);
166 switch (t->pt.cond.op) {
168 return new_cond_instr(GT, left,
169 right, pref_var, where);
171 /* fixme: do more of them */
173 die("eval_cond: unknown relation op %c\n",
178 if (t->pt.cond.type == OP_BOOL) {
179 left = eval_cond(t->pt.cond.left, -1, where);
180 right = eval_cond(t->pt.cond.right, -1, where);
181 switch (t->pt.cond.op) {
183 return new_cond_instr(AND, left,
184 right, pref_var, where);
187 die("eval_cond: unknown boolean op %c\n",
192 die("eval_cond: unknown condition type");
196 do_if(struct tree *t, struct list* where)
199 struct code ins, nop, jmp;
200 struct list* if_branch = xmalloc(sizeof(struct list));
201 struct list* else_branch = xmalloc(sizeof(struct list));
203 list_init(if_branch);
204 list_init(else_branch);
208 c = eval_cond(t->pt.tif.c, -1, where);
210 compile(t->pt.tif.i, if_branch);
211 compile(t->pt.tif.i, else_branch);
212 new_instr(nop, if_branch);
213 new_instr(nop, else_branch);
214 jmp.u.jump.target = list_last(else_branch);
215 new_instr(jmp, if_branch);
217 ins.opcode = JUMP_UNLESS;
218 ins.u.jump_unless.cond = c;
219 ins.u.jump_unless.target = list_last(if_branch);
220 new_instr(ins, where);
221 list_cat(where, if_branch);
222 list_cat(where, else_branch);
229 do_arrow(struct tree* t, struct list* where)
234 v = evaluate(t->pt.arrow.s, -1, where);
235 ins.u.arrow.what = v;
237 if (t->pt.arrow.left == K_COPY)
238 ins.u.arrow.copy = 1;
240 ins.u.arrow.copy = 0;
241 switch (t->pt.arrow.right) {
252 die("do_arrow: This cannot happen ;-)");
254 new_instr(ins, where);
258 reset_temp_var_count(void)
260 current_varcode = temp_varcode_start;
264 compile(struct tree* t, struct list* where)
273 reset_temp_var_count();
274 compile(t->pt.block.head, where);
275 compile(t->pt.block.tail, where);
279 case ST_LEAF: //warn?
285 evaluate(t, -1, where); //emit warning?
291 eval_cond(t, -1, where); // warn?
295 die("compile: got to default");
304 LIST_FOREACH(p, &input_code) {
307 printf("SET %d %d\n", p->u.set.l, p->u.set.r);
310 printf("CAT %d %d %d\n", p->u.tpop.l,
311 p->u.tpop.r, p->u.tpop.res);
314 printf("JUMP %d\n", (int) p->u.jump.target);
317 printf("JUMP_UNLESS %d %d\n", p->u.jump_unless.cond,(int) p->u.jump_unless.target);
320 printf("GT %d %d %d\n", p->u.tpop.l, p->u.tpop.r, p->u.tpop.res);
323 printf("AND %d %d %d\n", p->u.tpop.l, p->u.tpop.r, p->u.tpop.res);
329 printf("not implemented, opcode: %d\n",