From 8006c0cfbb0d615cec2a8cdf201ae59e8931f916 Mon Sep 17 00:00:00 2001 From: stefan schippers Date: Wed, 29 Apr 2026 23:02:46 +0200 Subject: [PATCH] eval_expr(): add comparison operators, !=. ==, <=, >=, <, > --- src/eval_expr.y | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/eval_expr.y b/src/eval_expr.y index eb9b5e81..4cc42556 100644 --- a/src/eval_expr.y +++ b/src/eval_expr.y @@ -78,6 +78,12 @@ double val; /* For returning numbers. */ symrec *tptr; /* For returning symbol-table pointers */ } +%token LE +%token GE +%token GT +%token LT +%token EQ +%token NE %token STREND 0 %token XCHAR %token EXPR /* expr( */ @@ -87,6 +93,8 @@ symrec *tptr; /* For returning symbol-table pointers */ %right ':' %right '?' %right '=' +%left LT GT LE GE +%left EQ NE %left '-' '+' %left '*' '/' '%' %left NEG /* Negation--unary minus */ @@ -108,7 +116,13 @@ line: exp: NUM {$$ = $1;} | FNCT '(' exp ')' {$$ = $1 ? ($1->fnctptr ? (*($1->fnctptr))($3) : 0.0) : 0.0;} | FNCT {$$ = $1 ? $1->value : 0.0;} - | exp '+' exp {$$ = $1 + $3; } + | exp EQ exp {$$ = ($1 == $3);} + | exp NE exp {$$ = ($1 != $3);} + | exp LT exp {$$ = ($1 < $3);} + | exp GT exp {$$ = ($1 > $3);} + | exp LE exp {$$ = ($1 <= $3);} + | exp GE exp {$$ = ($1 >= $3);} + | exp '+' exp {$$ = $1 + $3;} | exp '-' exp {$$ = $1 - $3;} | exp '*' exp {$$ = $1 * $3;} | exp '%' exp {$$ = (int)$1 % (int)$3;} @@ -319,6 +333,14 @@ static int kklex() return 0; /* error : undefined identifier */ } /* Any other character is a token by itself. */ + + if (c == '=' && *str == '=') {str++; return EQ;} + else if(c == '!' && *str == '=') {str++; return NE;} + else if(c == '<' && *str == '=') {str++; return LE;} + else if(c == '>' && *str == '=') {str++; return GE;} + else if(c == '<') {return LT;} + else if(c == '>') {return GT;} + return c; }