]> mj.ucw.cz Git - umpf.git/blobdiff - cond.y
unify BUFSIZE
[umpf.git] / cond.y
diff --git a/cond.y b/cond.y
index 6473557660d944e472b17612517f7440092b6a4c..f46afe528f7b6869f7521ac0f10d1e5375677761 100644 (file)
--- a/cond.y
+++ b/cond.y
 
 #include <stdio.h>
 #include <string.h>
-#include <pcre.h>
 
-#define OVECCOUNT 3
+#include "umpf.h"
 
-int yylex (void);
-void yyerror (char const *);
-
-int regex_cmp(char* s, char* r);
+static struct tree* tree_malloc(int type);
 
 %}
+%error-verbose
 
 %union {
-       int b;  
        int n;
        char* str;
+       struct tree* tr;        
 }
 
 %token <str> CONST
 %token <n> NUM
 %token <str> VAR
-%token KW_IF KW_ELSE KW_PIPE KW_MAIL KW_COPY
-%token '(' ')' '{' '}'
+%token <str> KW_DISCARD
+%token <str> KW_PIPE KW_MAIL KW_COPY
+%token '(' ')' '{' '}' ';'
+%nonassoc KW_IF
+%nonassoc KW_ELSE
 %left ARROW
-%left EQ NEQ GE LE '<' '>' RE NRE
+%left <n> EQ NEQ GE LE '<' '>' RE NRE
+%left '='
+%left '.'
 %left '+' '-' 
 %left '*' '/'
-%left '|' '&' '^'
-%left '!'
-%left '=' 
-%type <b> boo
+%left <n> '|'
+%left <n> '^'
+%left <n> '&'
+%left <n> '!'
+%type <tr> input 
+%type <tr> command 
+%type <tr> next 
+%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 line
+input: /* empty */     { $$ = input_tree = tree_malloc(ST_EMPTY); }
+       | command input {       $$ = tree_malloc(ST_BLOCK); 
+                               $$->pt.block.head = $1;
+                               $$->pt.block.tail = $2;
+
+                               input_tree = $$;
+                       } 
+;
+
+command:        ';' { $$ = tree_malloc(ST_EMPTY); }
+               | '{' command next '}'  {
+                                               $$ = tree_malloc(ST_BLOCK); 
+                                               $$->pt.block.head = $2;
+                                               $$->pt.block.tail = $3; 
+                                       }
+               | '{' '}' { $$ = tree_malloc(ST_EMPTY); }
+               | cif
+               | ass ';' { $$ = $1; }
+               | arrow ';' { $$ = $1; }
+               
+       
+;
+
+next:  /* empty */ {$$ = tree_malloc(ST_EMPTY); }
+       | command
+       
+
+;
+
+cif:   KW_IF cond command KW_ELSE command      { 
+                                       $$ = tree_malloc(ST_IF);
+                                       $$->pt.tif.c = $2;
+                                       $$->pt.tif.i = $3;
+                                       $$->pt.tif.e = $5;
+                               }
+       | KW_IF cond command    { 
+                                       $$ = tree_malloc(ST_IF);
+                                       $$->pt.tif.c = $2;
+                                       $$->pt.tif.i = $3;
+                                       $$->pt.tif.e = tree_malloc(ST_EMPTY); 
+                               }
+;
+
+cond:  '!' cond {
+                               $$ = tree_malloc(ST_COND);
+                               $$->pt.cond.left = $2;  
+                               $$->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; }
+       | ass_right rop ass_right       {
+                                               $$ = tree_malloc(ST_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:   '>'
+       | '<'
+       | EQ
+       | NEQ
+       | LE
+       | GE
+       | RE
+       | NRE
 ;
 
-line:  '\n'
-       | boo '\n'              { printf("%s\n",$1?"true":"false"); }
-       | error '\n'            { yyerrok; }
+ass:
+       VAR '=' ass_right       {
+                                       $$ = tree_malloc(ST_ASS);
+
+                                       $$->pt.ass.left = tree_malloc(ST_LEAF);
+                                       $$->pt.ass.left->pt.leaf.type = L_VAR;
+                                       $$->pt.ass.left->pt.leaf.value = $1;
+                                       $$->pt.ass.left->pt.leaf.n = find_var($1, var_hash);
+                                       $$->pt.ass.right = $3;
+                               }
+;
+
+leaves:                VAR     {
+                               $$ = tree_malloc(ST_LEAF);
+                               $$->pt.leaf.type = L_VAR;
+                               $$->pt.leaf.value = $1;
+                               $$->pt.leaf.n = find_var($1, var_hash);
+                       }
+               | CONST { 
+                               $$ = tree_malloc(ST_LEAF);
+                               $$->pt.leaf.type = L_CONST;
+                               $$->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"; }
+
 ;
 
-boo:    CONST EQ CONST         { $$ = ! strcmp($1, $3); }
-       | CONST NEQ CONST       { $$ = !! strcmp($1, $3); }
-       | CONST RE CONST        { $$ = regex_cmp($1,$3) >= 0 }
-       | CONST NRE CONST       { $$ = regex_cmp($1,$3) < 0 }
-       | NUM EQ NUM            { $$ = $1 == $3 }
-       | NUM NEQ NUM           { $$ = $1 != $3 }
-       | NUM GE NUM            { $$ = $1 >= $3 }
-       | NUM LE NUM            { $$ = $1 <= $3 }
-       | NUM '>' NUM           { $$ = $1 > $3 }
-       | NUM '<' NUM           { $$ = $1 < $3 }
-       | boo '|' boo           { $$ = $1 || $3 }
-       | boo '&' boo           { $$ = $1 && $3 }
-       | boo '^' boo           { $$ = ($1 || $3) && !($1 && $3) }
-       | '!' boo               { $$ = ! $2 }
+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;   
+                               }
 ;
+
 %%
 
-int 
-regex_cmp(char* s, char* r)
+struct tree* 
+tree_malloc(int type)
 {
-       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);
+       struct tree* temp;
+       temp = xmalloc(sizeof (struct tree));
+       temp->st=type;
 
-       return res;
+       return temp;
 }
 
 void
 yyerror (char const *s)
 {
-       fprintf (stderr, "%s\n", s);
-}
-
-int
-main(void)
-{
-//     yydebug=1;
-       return yyparse ();
+       fprintf (stderr, "Line %d: %s\n", line, s);
 }