X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=cond.y;h=4c1d3d221d188a976ff7fd96d5b42e3e8c07a714;hb=8628def3891660a2ebfce358dc65cf6b0f0a92d1;hp=7355a429a88bb4b9f4ef9c112fee39f165ab1d8b;hpb=d4bc03c024891aadf8e963736be114d4d023987f;p=umpf.git diff --git a/cond.y b/cond.y index 7355a42..4c1d3d2 100644 --- a/cond.y +++ b/cond.y @@ -2,10 +2,12 @@ #include #include +#include -#include "brum.h" +#include "umpf.h" static struct tree* tree_malloc(int type); +static enum var_type get_var_type(char* var); %} %error-verbose @@ -16,38 +18,40 @@ static struct tree* tree_malloc(int type); struct tree* tr; } +%right '!' %token CONST %token NUM %token VAR -%token KW_PIPE KW_MAIL KW_COPY +%token KW_DISCARD +%token KW_PIPE KW_MAIL KW_COPY KW_FILTER %token '(' ')' '{' '}' ';' %nonassoc KW_IF %nonassoc KW_ELSE %left ARROW %left EQ NEQ GE LE '<' '>' RE NRE -%left '=' -%left '.' -%left '+' '-' -%left '*' '/' +%left '=' +%left '.' +%left '+' '-' +%left '*' '/' %left '|' %left '^' %left '&' -%left '!' %type input %type command %type next %type ass %type ass_right +%type ass_right_p %type cif %type arrow %type cond %type rop -%type left -%type right +%type left +%type right %type 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; @@ -70,7 +74,7 @@ command: ';' { $$ = tree_malloc(ST_EMPTY); } ; -next: /* empty */ {$$ = NULL; } +next: /* empty */ {$$ = tree_malloc(ST_EMPTY); } | command @@ -86,36 +90,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; } @@ -124,7 +129,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: '>' @@ -138,51 +149,88 @@ rop: '>' ; ass: - VAR '=' ass_right { + VAR '=' ass_right_p { $$ = 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, get_var_type($1), var_hash); $$->pt.ass.right = $3; } ; -leaves: | VAR { +leaves: VAR { $$ = tree_malloc(ST_LEAF); $$->pt.leaf.type = L_VAR; $$->pt.leaf.value = $1; + $$->pt.leaf.n = find_var($1, get_var_type($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; + $$->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.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; } + | KW_FILTER { $$ = K_FILTER; } +; + +ass_right_p: '(' ass_right ')' {$$ = $2; } + | ass_right {$$ = $1; } ; ass_right: leaves | ass_right '.' ass_right { $$ = tree_malloc(ST_OP); - $$->pt.op.op = '.'; + $$->pt.op.op = $2; + $$->pt.op.left = $1; + $$->pt.op.right = $3; + } + | ass_right '+' ass_right { + $$ = tree_malloc(ST_OP); + $$->pt.op.op = $2; + $$->pt.op.left = $1; + $$->pt.op.right = $3; + } + | ass_right '-' ass_right { + $$ = tree_malloc(ST_OP); + $$->pt.op.op = $2; + $$->pt.op.left = $1; + $$->pt.op.right = $3; + } + | ass_right '*' ass_right { + $$ = tree_malloc(ST_OP); + $$->pt.op.op = $2; + $$->pt.op.left = $1; + $$->pt.op.right = $3; + } + | ass_right '/' ass_right { + $$ = tree_malloc(ST_OP); + $$->pt.op.op = $2; $$->pt.op.left = $1; $$->pt.op.right = $3; } @@ -200,6 +248,28 @@ tree_malloc(int type) return temp; } +enum var_type +get_var_type(char* var) +{ + int upper = 0; + int lower = 0; + + if (islower(*var)) + return VAR_USER; + + while (*var) { + if (isupper(*var)) + upper++; + if (islower(*var)) + lower++; + var++; + } + if (upper && lower) + return VAR_HEADER; + + return VAR_INTERN; +} + void yyerror (char const *s) {