]> mj.ucw.cz Git - umpf.git/commitdiff
add arrows
authorAnicka Bernathova <anicka@anicka.net>
Wed, 15 Jul 2009 12:55:46 +0000 (14:55 +0200)
committerAnicka Bernathova <anicka@anicka.net>
Wed, 15 Jul 2009 12:55:46 +0000 (14:55 +0200)
code.c
cond.y
umpf.c
umpf.h

diff --git a/code.c b/code.c
index 4f665e1acfd110bb3afd300f418a009dfa133107..7c2296829c80268d092fb0cdc0dcfa47fa008c41 100644 (file)
--- 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 f46afe528f7b6869f7521ac0f10d1e5375677761..5d5751b3c32751bb3edf3c3af0245eae5164cc2b 100644 (file)
--- a/cond.y
+++ b/cond.y
@@ -19,8 +19,8 @@ static struct tree* tree_malloc(int type);
 %token <str> CONST
 %token <n> NUM
 %token <str> VAR
-%token <str> KW_DISCARD
-%token <str> KW_PIPE KW_MAIL KW_COPY
+%token <n> KW_DISCARD
+%token <n> 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 <tr> arrow 
 %type <tr> cond
 %type <n> rop 
-%type <str> left
-%type <str> right 
+%type <n> left
+%type <n> right 
 %type <tr> 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 f868775a6183f6e3541ca53320be67c724c23327..ab73285948a50ed8abacb53cbdc7bb8f63f34748 100644 (file)
--- 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 ff83a933e3eced743c2eebf19db44a44010d4dbc..c7d59a145ab3bff5b55c54a90df076ce063bb197 100644 (file)
--- 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;