Support des égalités et des inégalités entre entiers

This commit is contained in:
Yohann D'ANELLO 2020-05-17 00:21:25 +02:00
parent 80abf67355
commit 9fefbe57df
3 changed files with 14 additions and 2 deletions

View File

@ -31,7 +31,7 @@ typedef struct varlist // variable reference (used for print statement)
typedef struct expr // boolean expression
{
int type; // TRUE, FALSE, OR, AND, EQUIV, NOT, 0 (variable), PLUS, TIMES, INT
int type; // TRUE, FALSE, OR, AND, EQUIV, NOT, 0 (variable), PLUS, TIMES, INT, EQUALS, LT
var *var;
struct expr *left, *right;
} expr;
@ -71,6 +71,7 @@ var* find_ident (char *s)
while (v && strcmp(v->name,s))
v = v->next;
if (!v) {
// Check in int vars
v = program_ints;
while (v && strcmp(v->name, s))
v = v->next;
@ -158,6 +159,8 @@ stmt* make_stmt (int type, var *var, expr *expr,
%left EQUIV
%left PLUS
%left TIMES
%left EQUALS
%left LT
%%
@ -206,6 +209,8 @@ expr : IDENT { $$ = make_expr(0,find_ident($1),NULL,NULL); }
| INT { $$ = make_expr(INT, make_int($1), NULL, NULL); }
| expr TIMES expr { $$ = make_expr(TIMES, NULL, $1, $3); }
| expr PLUS expr { $$ = make_expr(PLUS, NULL, $1, $3); }
| expr EQUALS expr { $$ = make_expr(EQUALS, NULL, $1, $3); }
| expr LT expr { $$ = make_expr(LT, NULL, $1, $3); }
%%
@ -235,6 +240,11 @@ int eval (expr *e)
return eval(e->left) + eval(e->right);
case TIMES:
return eval(e->left) * eval(e->right);
case EQUALS:
printf("%d, %d\n", eval(e->left), eval(e->right));
return eval(e->left) == eval(e->right) ? 1 : 0;
case LT:
return eval(e->left) < eval(e->right) ? 1 : 0;
case INT:
case 0:
return e->var->value;

View File

@ -26,6 +26,8 @@ DIG [0-9]
"<=>" { return EQUIV; }
"+" { return PLUS; }
"*" { return TIMES; }
"==" { return EQUALS; }
"<" { return LT; }
[a-z_][a-z0-9_]* { yylval.i = strdup(yytext); return IDENT; }

View File

@ -1,5 +1,5 @@
bool y;
int x;
x := 42 + 21;
y := true;
y := x == (7 * (5 + 4));
print x,y,x