From: Anicka Bernathova Date: Wed, 15 Jul 2009 12:55:46 +0000 (+0200) Subject: add arrows X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=80e68c40229bccd38ec4d89737c4cae6ecb41e7c;p=umpf.git add arrows --- diff --git a/code.c b/code.c index 4f665e1..7c22968 100644 --- a/code.c +++ b/code.c @@ -225,6 +225,35 @@ do_if(struct tree *t, struct list* where) free(else_branch); } +static void +do_arrow(struct tree* t, struct list* where) +{ + int v; + struct code ins; + + v = evaluate(t->pt.arrow.s, -1, where); + ins.u.arrow.what = v; + + if (t->pt.arrow.left == K_COPY) + ins.u.arrow.copy = 1; + else + ins.u.arrow.copy = 0; + switch (t->pt.arrow.right) { + case K_EMPTY: + ins.opcode = STORE; + break; + case K_PIPE: + ins.opcode = PIPE; + break; + case K_MAIL: + ins.opcode = MAIL; + break; + default: + die("do_arrow: This cannot happen ;-)"); + } + new_instr(ins, where); +} + static void reset_temp_var_count(void) { @@ -247,6 +276,8 @@ compile(struct tree* t, struct list* where) break; case ST_EMPTY: break; + case ST_LEAF: //warn? + break; case ST_ASS: do_ass(t, where); break; @@ -258,6 +289,8 @@ compile(struct tree* t, struct list* where) break; case ST_COND: eval_cond(t, -1, where); // warn? + case ST_ARROW: + do_arrow(t, where); default: die("compile: got to default"); } diff --git a/cond.y b/cond.y index f46afe5..5d5751b 100644 --- a/cond.y +++ b/cond.y @@ -19,8 +19,8 @@ static struct tree* tree_malloc(int type); %token CONST %token NUM %token VAR -%token KW_DISCARD -%token KW_PIPE KW_MAIL KW_COPY +%token KW_DISCARD +%token KW_PIPE KW_MAIL KW_COPY %token '(' ')' '{' '}' ';' %nonassoc KW_IF %nonassoc KW_ELSE @@ -43,8 +43,8 @@ static struct tree* tree_malloc(int type); %type arrow %type cond %type rop -%type left -%type right +%type left +%type right %type leaves %% @@ -174,25 +174,25 @@ leaves: VAR { arrow: left ARROW right ass_right { $$ = tree_malloc(ST_ARROW); $$->pt.arrow.s = $4; - $$->pt.arrow.kw_left = $1; - $$->pt.arrow.kw_right = $3; + $$->pt.arrow.left = $1; + $$->pt.arrow.right = $3; } | left ARROW KW_DISCARD { //FIXME: actually left does not make sense here $$ = tree_malloc(ST_ARROW); $$->pt.arrow.s = NULL; - $$->pt.arrow.kw_left = NULL; - $$->pt.arrow.kw_right = "discard"; + $$->pt.arrow.left = K_EMPTY; + $$->pt.arrow.right = K_DISCARD; } ; -left: /* empty */ { $$ = NULL;} - | KW_COPY { $$ = "copy"; } +left: /* empty */ { $$ = K_EMPTY;} + | KW_COPY { $$ = K_COPY; } ; -right: /* empty */ { $$ = NULL; } - | KW_PIPE { $$ = "pipe"; } - | KW_MAIL { $$ = "mail"; } +right: /* empty */ { $$ = K_EMPTY; } + | KW_PIPE { $$ = K_PIPE; } + | KW_MAIL { $$ = K_MAIL; } ; ass_right: leaves diff --git a/umpf.c b/umpf.c index f868775..ab73285 100644 --- a/umpf.c +++ b/umpf.c @@ -37,7 +37,7 @@ main(int argc, char** argv) return res; temp_varcode_start = current_varcode; - compile(input_tree); + compile(input_tree, NULL); print_code(); diff --git a/umpf.h b/umpf.h index ff83a93..c7d59a1 100644 --- a/umpf.h +++ b/umpf.h @@ -4,6 +4,14 @@ int yylex (void); void yyerror (char const *); +enum keyword { + K_DISCARD, + K_COPY, + K_MAIL, + K_PIPE, + K_EMPTY +}; + struct tree { enum { ST_IF, @@ -53,8 +61,8 @@ struct tree { } leaf; struct { - char* kw_left; - char* kw_right; + enum keyword left; + enum keyword right; struct tree* s; } arrow; @@ -129,7 +137,11 @@ struct code { NOP, CAT, GT, - AND + AND, + PIPE, + MAIL, + STORE, + DISCARD } opcode; union { @@ -153,6 +165,10 @@ struct code { int r; int res; /* result */ } tpop; + struct { + int copy; + int what; + } arrow; struct { } nop; } u;