From 785f598db1da0406416cd6f184d929112bdd0e80 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sun, 17 Apr 2022 15:21:23 -0400 Subject: now multiplicationish things --- expr.l | 26 +++++++++++++------------- expr.y | 49 ++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/expr.l b/expr.l index d6aa239..57b3de2 100644 --- a/expr.l +++ b/expr.l @@ -17,22 +17,22 @@ DIGIT [0-9] %% -"|" return OR; -"&" return AND; -"=" return COMPARISON; -">" return COMPARISON; -">=" return COMPARISON; -"<" return COMPARISON; -"<=" return COMPARISON; -"!=" return COMPARISON; +"|" return '|'; +"&" return '&'; +"=" return '='; +">" return '>'; +">=" return GE; +"<" return '<'; +"<=" return LE; +"!=" return NE; "+" return '+'; -"-" return '-';; -"*" return MULTIPLICATION; -"/" return MULTIPLICATION; -"%" return MULTIPLICATION; +"-" return '-'; +"*" return '*'; +"/" return '/'; +"%" return '%'; "(" return '('; ")" return ')'; -":" return MATCH; +":" return ':'; {DIGIT}+ { yylval.u.i = atoi(yytext); return yylval.type = INTEGER; } -{DIGIT}+ { yylval.u.i = atoi(yytext); return yylval.type = INTEGER; } .+ { yylval.u.s = strdup(yytext); return yylval.type = STRING; } diff --git a/expr.y b/expr.y index 1060e27..785b004 100644 --- a/expr.y +++ b/expr.y @@ -12,9 +12,12 @@ static void breakpoint(void) { } %token INTEGER %token STRING %token '(' ')' -%left MATCH MULTIPLICATION +%left ':' +%left '*' '/' '%' %left '+' '-' -%left COMPARISON AND OR +%left '=' '<' '>' LE GE NE +%left '&' +%left '|' %start expr @@ -27,15 +30,25 @@ expr : STRING { } | '(' expr ')' { - printf("a parenthesized expression\n"); } - | expr MATCH expr { - printf("match two\n"); + | expr ':' expr { + printf("match\n"); } - | expr MULTIPLICATION expr { - printf("multiplicationish\n"); + | expr '*' expr { + $$.u.i = $1.u.i * $3.u.i; + printf("%d\n", $$.u.i); + } + + | expr '/' expr { + $$.u.i = $1.u.i / $3.u.i; + printf("%d\n", $$.u.i); + } + + | expr '%' expr { + $$.u.i = $1.u.i % $3.u.i; + printf("%d\n", $$.u.i); } | expr '+' expr { @@ -48,15 +61,29 @@ expr : STRING { printf("%d\n", $$.u.i); } - | expr COMPARISON expr { - printf("compare\n"); + | expr '=' expr { + } + + | expr NE expr { + } + + | expr '<' expr { + } + + | expr LE expr { + } + + | expr '>' expr { + } + + | expr GE expr { } - | expr AND expr { + | expr '&' expr { printf("and\n"); } - | expr OR expr { + | expr '|' expr { printf("or\n"); } ; -- cgit v1.2.1