Skip to content

Commit

Permalink
[evaluator] implement modulo
Browse files Browse the repository at this point in the history
Implement evaluation of the modulo operator.

Fixes: #18.
  • Loading branch information
hexagonal-sun committed Sep 14, 2019
1 parent 4b177fd commit 9e58a2e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/evaluate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,28 @@ static tree eval_div(tree t, int depth)
return ret;
}

static tree eval_mod(tree t, int depth)
{
tree left = __evaluate_1(tMOD_LHS(t), depth + 1);
tree right = __evaluate_1(tMOD_RHS(t), depth + 1);

tree ret = tree_make(T_INTEGER);
mpz_init(tINT_VAL(ret));

if (is_T_LIVE_VAR(left))
left = make_int_from_live_var(left);

if (is_T_LIVE_VAR(right))
right = make_int_from_live_var(right);

if (!(is_T_INTEGER(left) && is_T_INTEGER(right)))
eval_die(t, "Could not modulus to non integer type\n");

mpz_mod(tINT_VAL(ret), tINT_VAL(left), tINT_VAL(right));

return ret;
}

static tree eval_comma(tree t, int depth)
{
__evaluate_1(tCOMMA_LHS(t), depth + 1);
Expand Down Expand Up @@ -2408,6 +2430,7 @@ static tree __evaluate_1(tree t, int depth)
case T_RSHIFT: result = eval_rshift(t, depth + 1); break;
case T_MUL: result = eval_mul(t, depth + 1); break;
case T_DIV: result = eval_div(t, depth + 1); break;
case T_MOD: result = eval_mod(t, depth + 1); break;
case T_COMMA: result = eval_comma(t, depth + 1); break;
case T_LT: result = eval_lt(t, depth + 1); break;
case T_GT: result = eval_gt(t, depth + 1); break;
Expand Down
1 change: 1 addition & 0 deletions testsuite/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ for-loop-init-decl
sizeof-deref
break
comma
modulo

*.trs
2 changes: 1 addition & 1 deletion testsuite/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ check_PROGRAMS = puts strcat pass_parameter_printf decl_with_assign \
if_stmts eqneq union-struct casts shift loops negate \
ior function_ptr_typedef array_ptr \
struct_array_alignment iand factorial char-decl-and-assignment \
for-loop-init-decl sizeof-deref break comma
for-loop-init-decl sizeof-deref break comma modulo

expect_scripts = fileio.exp strtok.exp decl.exp chars.exp infix.exp \
repl-interrupt.exp
Expand Down
11 changes: 11 additions & 0 deletions testsuite/modulo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int printf(const char *s, ...);

int main()
{
printf("%d\n",30 % 50);
printf("%d\n",2 % 27);
printf("%d\n",3 % 87);
printf("%d\n",9 % 18);
printf("%d\n",100 % 200);
printf("%d\n",1024 % 8190);
}

0 comments on commit 9e58a2e

Please sign in to comment.