]> mj.ucw.cz Git - umpf.git/blobdiff - cond.y
unify BUFSIZE
[umpf.git] / cond.y
diff --git a/cond.y b/cond.y
index e69668b30b7fc2a94264b545b21c3e4bf1cfe781..f46afe528f7b6869f7521ac0f10d1e5375677761 100644 (file)
--- a/cond.y
+++ b/cond.y
@@ -2,69 +2,13 @@
 
 #include <stdio.h>
 #include <string.h>
-#include <pcre.h>
 
-#include "lex.h"
+#include "umpf.h"
 
-#define OVECCOUNT 3
-
-int yylex (void);
-void yyerror (char const *);
-
-int regex_cmp(char* s, char* r);
-struct tree* tree_malloc(int type);
-void print_tree(struct tree* t, int ind);
-
-struct tree {
-       enum {
-               ST_IF,
-               ST_COND,
-               ST_BLOCK,
-               ST_ASS,
-               ST_LEAF,
-               ST_EMPTY
-       } st;   /* subtree type */
-       union {
-               struct {
-                       struct tree* c; /* condition */
-                       struct tree* i; /* if */
-                       struct tree* e; /* else */
-               } tif;
-
-               struct {
-                       int op;
-                       struct tree* left;
-                       struct tree* right;
-               } cond; /* binary operator */
-
-               struct {
-                       struct tree* head;
-                       struct tree* tail;
-               } block;
-
-               struct {
-                       struct tree* left;
-                       struct tree* right;
-               } ass;
-
-               struct {
-                       enum {
-                               L_VAR,
-                               L_CONST,
-                               L_NUM
-                       } type;
-                       union {
-                               char* s;
-                               int n;
-                       } value;
-               } leaf;
-
-       } pt;
-};
-
-struct tree* input_tree;
+static struct tree* tree_malloc(int type);
 
 %}
+%error-verbose
 
 %union {
        int n;
@@ -75,7 +19,8 @@ struct tree* input_tree;
 %token <str> CONST
 %token <n> NUM
 %token <str> VAR
-%token KW_PIPE KW_MAIL KW_COPY
+%token <str> KW_DISCARD
+%token <str> KW_PIPE KW_MAIL KW_COPY
 %token '(' ')' '{' '}' ';'
 %nonassoc KW_IF
 %nonassoc KW_ELSE
@@ -95,11 +40,15 @@ 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; }
+input: /* empty */     { $$ = input_tree = tree_malloc(ST_EMPTY); }
        | command input {       $$ = tree_malloc(ST_BLOCK); 
                                $$->pt.block.head = $1;
                                $$->pt.block.tail = $2;
@@ -116,12 +65,13 @@ command:    ';' { $$ = tree_malloc(ST_EMPTY); }
                                        }
                | '{' '}' { $$ = tree_malloc(ST_EMPTY); }
                | cif
-               | ass ';' { $$ = $1}
+               | ass ';' { $$ = $1; }
+               | arrow ';' { $$ = $1; }
                
        
 ;
 
-next:  /* empty */ {$$ = NULL}
+next:  /* empty */ {$$ = tree_malloc(ST_EMPTY); }
        | command
        
 
@@ -137,36 +87,37 @@ cif:       KW_IF cond command KW_ELSE command      {
                                        $$ = tree_malloc(ST_IF);
                                        $$->pt.tif.c = $2;
                                        $$->pt.tif.i = $3;
-                                       $$->pt.tif.e = NULL;
+                                       $$->pt.tif.e = tree_malloc(ST_EMPTY); 
                                }
 ;
 
 cond:  '!' cond {
                                $$ = tree_malloc(ST_COND);
                                $$->pt.cond.left = $2;  
-                               $$->pt.cond.right = NULL;       
+                               $$->pt.cond.right = NULL; 
                                $$->pt.cond.op = $1;    
-
+                               $$->pt.cond.type = OP_BOOL;     
                }
        | cond '|' cond {
                                $$ = tree_malloc(ST_COND);
                                $$->pt.cond.left = $1;  
                                $$->pt.cond.right = $3; 
                                $$->pt.cond.op = $2;    
-
+                               $$->pt.cond.type = OP_BOOL;     
                        }
        | cond '&' cond {
                                $$ = tree_malloc(ST_COND);
                                $$->pt.cond.left = $1;  
                                $$->pt.cond.right = $3; 
                                $$->pt.cond.op = $2;    
-
+                               $$->pt.cond.type = OP_BOOL;     
                        }
        | cond '^' cond {
                                $$ = tree_malloc(ST_COND);
                                $$->pt.cond.left = $1;  
                                $$->pt.cond.right = $3; 
                                $$->pt.cond.op = $2;    
+                               $$->pt.cond.type = OP_BOOL;     
 
                        }
        | '(' cond ')' { $$ = $2; }
@@ -175,7 +126,13 @@ cond:      '!' cond {
                                                $$->pt.cond.left = $1;  
                                                $$->pt.cond.right = $3; 
                                                $$->pt.cond.op = $2;    
+                                               $$->pt.cond.type = OP_REL;      
                                        }
+       | ass_right {
+                               $$ = tree_malloc(ST_COND);
+                               $$->pt.cond.left = $1;  
+                               $$->pt.cond.type = JUST_BOOL;   
+               }
 ;
 
 rop:   '>'
@@ -194,28 +151,57 @@ ass:
 
                                        $$->pt.ass.left = tree_malloc(ST_LEAF);
                                        $$->pt.ass.left->pt.leaf.type = L_VAR;
-                                       $$->pt.ass.left->pt.leaf.value.s = $1;
-
+                                       $$->pt.ass.left->pt.leaf.value = $1;
+                                       $$->pt.ass.left->pt.leaf.n = find_var($1, var_hash);
                                        $$->pt.ass.right = $3;
                                }
 ;
 
-ass_right:     NUM     { 
-                               $$ = tree_malloc(ST_LEAF);
-                               $$->pt.leaf.type = L_NUM;
-                               $$->pt.leaf.value.n = $1;
-                       }
-               | VAR   {
+leaves:                VAR     {
                                $$ = tree_malloc(ST_LEAF);
                                $$->pt.leaf.type = L_VAR;
-                               $$->pt.leaf.value.s = $1;
+                               $$->pt.leaf.value = $1;
+                               $$->pt.leaf.n = find_var($1, var_hash);
                        }
-               | CONST {
+               | CONST { 
                                $$ = tree_malloc(ST_LEAF);
                                $$->pt.leaf.type = L_CONST;
-                               $$->pt.leaf.value.s = $1;
+                               $$->pt.leaf.value = $1;
+                               $$->pt.leaf.n = store_const($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 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";
+                               }
+;
+
+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;   
+                               }
 ;
 
 %%
@@ -230,107 +216,8 @@ tree_malloc(int type)
        return temp;
 }
 
-int 
-regex_cmp(char* s, char* r)
-{
-       pcre *brum;
-       int erroroffset;
-       const char* error;
-       int ovector[OVECCOUNT];
-       
-       brum = pcre_compile(r,0,&error,&erroroffset,NULL);
-       if (!brum)
-               return -1;
-       
-       int res = pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT);
-       pcre_free(brum);
-
-       return res;
-}
-
 void
 yyerror (char const *s)
 {
-       fprintf (stderr, "%s\n", s);
-}
-
-void
-print_ind(int num, char c)
-{
-       int i;
-
-       for (i = 0; i < num; i++){
-               putchar(c);
-       }
-}
-
-void
-print_tree(struct tree* t, int ind)
-{
-       if (!t)
-               return; 
-
-       switch (t->st){
-               case ST_IF:
-                       print_ind(ind,' ');
-                       puts("if");
-                       print_tree(t->pt.tif.c,ind+1);
-                       print_ind(ind,' ');
-                       puts("then");
-                       print_tree(t->pt.tif.i,ind+1);
-                       print_ind(ind,' ');
-                       puts("else");
-                       print_tree(t->pt.tif.e,ind+1);
-                       break;
-               case ST_COND:
-#define UPPER(a) ((a) >> 8)
-#define LOWER(a) ((a) & 0xFF)
-                       print_tree(t->pt.cond.left, ind+1);
-                       print_ind(ind,' ');
-
-                       if (UPPER(t->pt.cond.op) > 0)
-                               putchar(UPPER(t->pt.cond.op));
-                       putchar(LOWER(t->pt.cond.op));
-                       putchar('\n'); 
-                       print_tree(t->pt.cond.right, ind+1);    
-                       break;
-               case ST_BLOCK:
-                       print_tree(t->pt.block.head,ind);
-                       print_tree(t->pt.block.tail,ind);
-                       break;
-               case ST_ASS:
-                       print_tree(t->pt.ass.left, ind+1);
-                       print_ind(ind,' ');     
-                       puts("=");
-                       print_tree(t->pt.ass.right, ind+1);
-                       break;
-               case ST_LEAF:
-                       print_ind(ind, ' ');
-                       switch (t->pt.leaf.type){
-                               case L_VAR:
-                                       putchar('$');
-                               case L_CONST:
-                                       puts(t->pt.leaf.value.s);
-                                       break;
-                               case L_NUM:
-                                       printf("%d\n",t->pt.leaf.value.n);
-                                       break;
-                       }
-                       break;
-               case ST_EMPTY:
-                       break;  
-
-
-       }
-}
-
-
-int
-main(void)
-{
-       yydebug=1;
-       yyparse ();
-
-       print_tree(input_tree,0);
-       return 0;
+       fprintf (stderr, "Line %d: %s\n", line, s);
 }