]> mj.ucw.cz Git - umpf.git/blobdiff - cond.y
add arrow and .
[umpf.git] / cond.y
diff --git a/cond.y b/cond.y
index e69668b30b7fc2a94264b545b21c3e4bf1cfe781..8baaf2163306d7d1f58f2eae1a5061c15d1e20e1 100644 (file)
--- a/cond.y
+++ b/cond.y
@@ -22,7 +22,9 @@ struct tree {
                ST_BLOCK,
                ST_ASS,
                ST_LEAF,
-               ST_EMPTY
+               ST_EMPTY,
+               ST_ARROW,
+               ST_OP
        } st;   /* subtree type */
        union {
                struct {
@@ -59,6 +61,18 @@ struct tree {
                        } value;
                } leaf;
 
+               struct {
+                       char* kw_left;
+                       char* kw_right; 
+                       struct tree* s;
+               } arrow;
+
+               struct {
+                       int op;
+                       struct tree* left;
+                       struct tree* right;
+               } op;
+
        } pt;
 };
 
@@ -66,6 +80,8 @@ struct tree* input_tree;
 
 %}
 
+%error-verbose
+
 %union {
        int n;
        char* str;
@@ -75,7 +91,7 @@ struct tree* input_tree;
 %token <str> CONST
 %token <n> NUM
 %token <str> VAR
-%token KW_PIPE KW_MAIL KW_COPY
+%token <str> KW_PIPE KW_MAIL KW_COPY
 %token '(' ')' '{' '}' ';'
 %nonassoc KW_IF
 %nonassoc KW_ELSE
@@ -95,8 +111,12 @@ struct tree* input_tree;
 %type <tr> ass 
 %type <tr> ass_right 
 %type <tr> cif
+%type <tr> arrow 
 %type <tr> cond
 %type <n> rop 
+%type <str> left
+%type <str> right 
+%type <tr> leaves 
 
 %%
 input: /* empty */     { $$ = input_tree = NULL; }
@@ -117,6 +137,7 @@ command:     ';' { $$ = tree_malloc(ST_EMPTY); }
                | '{' '}' { $$ = tree_malloc(ST_EMPTY); }
                | cif
                | ass ';' { $$ = $1}
+               | arrow ';' { $$ = $1}
                
        
 ;
@@ -200,7 +221,7 @@ ass:
                                }
 ;
 
-ass_right:     NUM     { 
+leaves:                NUM     { 
                                $$ = tree_malloc(ST_LEAF);
                                $$->pt.leaf.type = L_NUM;
                                $$->pt.leaf.value.n = $1;
@@ -210,12 +231,38 @@ ass_right:        NUM     {
                                $$->pt.leaf.type = L_VAR;
                                $$->pt.leaf.value.s = $1;
                        }
-               | CONST {
+               | CONST { 
                                $$ = tree_malloc(ST_LEAF);
                                $$->pt.leaf.type = L_CONST;
                                $$->pt.leaf.value.s = $1;
                        }
+;
+
+arrow: left ARROW right ass_right  {
+                                       $$ = tree_malloc(ST_ARROW);
+                                       $$->pt.arrow.s = $4;
+                                       $$->pt.arrow.kw_left = $1;
+                                       $$->pt.arrow.kw_right = $3;
+                               }
+;
 
+left:  /* empty */ {$$=NULL}
+       | KW_COPY { $$ = "copy" }
+
+;
+
+right: /* empty */ {$$ = NULL}
+       | KW_PIPE { $$ = "pipe" }
+       | KW_MAIL { $$ = "mail" }
+;
+
+ass_right:     leaves
+               | ass_right '.' ass_right       {
+                                       $$ = tree_malloc(ST_OP);
+                                       $$->pt.op.op = '.';
+                                       $$->pt.op.left = $1;    
+                                       $$->pt.op.right = $3;   
+                               }
 ;
 
 %%
@@ -251,7 +298,7 @@ regex_cmp(char* s, char* r)
 void
 yyerror (char const *s)
 {
-       fprintf (stderr, "%s\n", s);
+       fprintf (stderr, "Line %d: %s\n", line, s);
 }
 
 void
@@ -317,6 +364,26 @@ print_tree(struct tree* t, int ind)
                                        break;
                        }
                        break;
+               case ST_ARROW:
+                       if (t->pt.arrow.kw_left){
+                               print_ind(ind+1, ' ');
+                               puts(t->pt.arrow.kw_left);
+                       }
+                       print_ind(ind, ' ');
+                       printf("->\n");
+                       if (t->pt.arrow.kw_right){
+                               print_ind(ind+1, ' ');
+                               puts(t->pt.arrow.kw_right);
+                       }
+                       print_tree(t->pt.arrow.s,ind+1);
+                       break;
+               case ST_OP:
+                       print_tree(t->pt.op.left, ind+1);
+                       print_ind(ind,' ');     
+                       putchar(t->pt.op.op);
+                       putchar('\n');
+                       print_tree(t->pt.op.right, ind+1);
+                       break;  
                case ST_EMPTY:
                        break;  
 
@@ -328,7 +395,7 @@ print_tree(struct tree* t, int ind)
 int
 main(void)
 {
-       yydebug=1;
+//     yydebug=1;
        yyparse ();
 
        print_tree(input_tree,0);