Support des égalités et des inégalités entre entiers
This commit is contained in:
parent
80abf67355
commit
9fefbe57df
|
@ -31,7 +31,7 @@ typedef struct varlist // variable reference (used for print statement)
|
||||||
|
|
||||||
typedef struct expr // boolean expression
|
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;
|
var *var;
|
||||||
struct expr *left, *right;
|
struct expr *left, *right;
|
||||||
} expr;
|
} expr;
|
||||||
|
@ -71,6 +71,7 @@ var* find_ident (char *s)
|
||||||
while (v && strcmp(v->name,s))
|
while (v && strcmp(v->name,s))
|
||||||
v = v->next;
|
v = v->next;
|
||||||
if (!v) {
|
if (!v) {
|
||||||
|
// Check in int vars
|
||||||
v = program_ints;
|
v = program_ints;
|
||||||
while (v && strcmp(v->name, s))
|
while (v && strcmp(v->name, s))
|
||||||
v = v->next;
|
v = v->next;
|
||||||
|
@ -158,6 +159,8 @@ stmt* make_stmt (int type, var *var, expr *expr,
|
||||||
%left EQUIV
|
%left EQUIV
|
||||||
%left PLUS
|
%left PLUS
|
||||||
%left TIMES
|
%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); }
|
| INT { $$ = make_expr(INT, make_int($1), NULL, NULL); }
|
||||||
| expr TIMES expr { $$ = make_expr(TIMES, NULL, $1, $3); }
|
| expr TIMES expr { $$ = make_expr(TIMES, NULL, $1, $3); }
|
||||||
| expr PLUS expr { $$ = make_expr(PLUS, 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);
|
return eval(e->left) + eval(e->right);
|
||||||
case TIMES:
|
case TIMES:
|
||||||
return eval(e->left) * eval(e->right);
|
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 INT:
|
||||||
case 0:
|
case 0:
|
||||||
return e->var->value;
|
return e->var->value;
|
||||||
|
|
|
@ -26,6 +26,8 @@ DIG [0-9]
|
||||||
"<=>" { return EQUIV; }
|
"<=>" { return EQUIV; }
|
||||||
"+" { return PLUS; }
|
"+" { return PLUS; }
|
||||||
"*" { return TIMES; }
|
"*" { return TIMES; }
|
||||||
|
"==" { return EQUALS; }
|
||||||
|
"<" { return LT; }
|
||||||
|
|
||||||
[a-z_][a-z0-9_]* { yylval.i = strdup(yytext); return IDENT; }
|
[a-z_][a-z0-9_]* { yylval.i = strdup(yytext); return IDENT; }
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
bool y;
|
bool y;
|
||||||
int x;
|
int x;
|
||||||
x := 42 + 21;
|
x := 42 + 21;
|
||||||
y := true;
|
y := x == (7 * (5 + 4));
|
||||||
print x,y,x
|
print x,y,x
|
||||||
|
|
Loading…
Reference in New Issue