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);
84 new_3par_instr(int opcode, int left, int right, int pref_var,
93 ins.u.tpop.res = pref_var;
95 ins.u.tpop.res = current_varcode++;;
96 new_instr(ins, where);
97 return ins.u.tpop.res;
100 /* return number of variable where lies result
101 * pref_var < 0 => no preference
104 evaluate(struct tree* t, int pref_var, struct list* where)
106 if (t->st == ST_LEAF) {
108 } else if (t->st == ST_OP) {
110 left = evaluate(t->pt.op.left, -1, where);
111 right = evaluate(t->pt.op.right, -1, where);
112 switch (t->pt.op.op) {
114 return new_3par_instr(OPC_CAT, left,
115 right, pref_var, where);
118 die("evaluate: got to default");
121 die("evaluate: I can evaluate only expressions but I got %d",
126 do_ass(struct tree* t, struct list* where)
130 var_l = t->pt.ass.left->pt.leaf.n;
131 var_r = evaluate(t->pt.ass.right, -1, where);
133 ins.opcode = OPC_SET;
136 new_instr(ins, where);
141 eval_cond(struct tree *t, int pref_var, struct list* where)
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);
150 die("eval_cond: %d cannot be JUST_BOOL\n",
151 t->pt.cond.left->st);
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);
157 switch (t->pt.cond.op) {
159 return new_3par_instr (OPC_GT, left,
160 right, pref_var, where);
163 return new_3par_instr (OPC_LT, left,
164 right, pref_var, where);
167 return new_3par_instr (OPC_LE, left,
168 right, pref_var, where);
171 return new_3par_instr (OPC_GE, left,
172 right, pref_var, where);
175 return new_3par_instr (OPC_NRE, left,
176 right, pref_var, where);
179 return new_3par_instr (OPC_RE, left,
180 right, pref_var, where);
183 return new_3par_instr (OPC_EQ, left,
184 right, pref_var, where);
187 return new_3par_instr (OPC_NEQ, left,
188 right, pref_var, where);
190 /* fixme: do more of them */
192 die("eval_cond: unknown relation op %c\n",
197 if (t->pt.cond.type == OP_BOOL) {
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) {
205 return new_3par_instr (OPC_AND, left,
206 right, pref_var, where);
209 return new_3par_instr (OPC_OR, left,
210 right, pref_var, where);
213 return new_3par_instr (OPC_XOR, left,
214 right, pref_var, where);
217 ins.opcode = OPC_NOT;
218 ins.u.dpop.par = left;
220 ins.u.dpop.res = pref_var;
222 ins.u.dpop.res = current_varcode++;;
223 new_instr(ins, where);
224 return ins.u.dpop.res;
227 die("eval_cond: unknown boolean op %c\n",
232 die("eval_cond: unknown condition type");
236 do_if(struct tree *t, struct list* where)
239 struct code ins, nop, jmp;
240 struct list* if_branch = xmalloc(sizeof(struct list));
241 struct list* else_branch = xmalloc(sizeof(struct list));
243 list_init(if_branch);
244 list_init(else_branch);
245 nop.opcode = OPC_NOP;
246 jmp.opcode = OPC_JUMP;
248 c = eval_cond(t->pt.tif.c, -1, where);
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);
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);
269 do_arrow(struct tree* t, struct list* where)
275 if (t->pt.arrow.left == K_COPY)
276 ins.u.arrow.copy = 1;
278 ins.u.arrow.copy = 0;
279 switch (t->pt.arrow.right) {
281 ins.opcode = OPC_STORE;
284 ins.opcode = OPC_PIPE;
287 ins.opcode = OPC_MAIL;
290 ins.opcode = OPC_DISCARD;
293 die("do_arrow: This cannot happen ;-)");
296 if (t->pt.arrow.right != K_DISCARD) {
297 v = evaluate(t->pt.arrow.s, -1, where);
298 ins.u.arrow.what = v;
301 new_instr(ins, where);
305 reset_temp_var_count(void)
307 current_varcode = temp_varcode_start;
311 compile(struct tree* t, struct list* where)
320 reset_temp_var_count();
321 compile(t->pt.block.head, where);
322 compile(t->pt.block.tail, where);
326 case ST_LEAF: //warn?
332 evaluate(t, -1, where); //emit warning?
338 eval_cond(t, -1, where); // warn?
343 die("compile: got to default, type: %d", t->st);
352 LIST_FOREACH(p, &input_code) {
355 printf("SET %d %d\n", p->u.set.l, p->u.set.r);
358 printf("CAT %d %d %d\n", p->u.tpop.l,
359 p->u.tpop.r, p->u.tpop.res);
362 printf("JUMP %d\n", (int) p->u.jump.target);
364 case OPC_JUMP_UNLESS:
365 printf("JUMP_UNLESS %d %d\n", p->u.jump_unless.cond,(int) p->u.jump_unless.target);
368 printf("GT %d %d %d\n", p->u.tpop.l, p->u.tpop.r, p->u.tpop.res);
371 printf("AND %d %d %d\n", p->u.tpop.l, p->u.tpop.r, p->u.tpop.res);
377 printf("PIPE %d %d\n", p->u.arrow.what, p->u.arrow.copy);
380 printf("STORE %d %d\n", p->u.arrow.what, p->u.arrow.copy);
383 printf("MAIL %d %d\n", p->u.arrow.what, p->u.arrow.copy);
389 printf("not implemented, opcode: %d\n",