Skip to content

Commit c397c9e

Browse files
committed
vtrutil: Add support for modulo operator to expression evaluator
1 parent b4bded5 commit c397c9e

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

libs/libvtrutil/src/vtr_expr_eval.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,17 @@ static void get_formula_object(const char* ch, int& ichar, const t_formula_data&
322322
fobj->type = E_FML_OPERATOR;
323323
fobj->data.op = E_OP_SUB;
324324
break;
325+
case '*':
326+
fobj->type = E_FML_OPERATOR;
327+
fobj->data.op = E_OP_MULT;
328+
break;
325329
case '/':
326330
fobj->type = E_FML_OPERATOR;
327331
fobj->data.op = E_OP_DIV;
328332
break;
329-
case '*':
333+
case '%':
330334
fobj->type = E_FML_OPERATOR;
331-
fobj->data.op = E_OP_MULT;
335+
fobj->data.op = E_OP_MOD;
332336
break;
333337
case '(':
334338
fobj->type = E_FML_BRACKET;
@@ -365,13 +369,14 @@ static int get_fobj_precedence(const Formula_Object& fobj) {
365369
precedence = 2;
366370
break;
367371
case E_OP_MULT: //fallthrough
368-
case E_OP_DIV:
372+
case E_OP_DIV: //fallthrough
373+
case E_OP_MOD:
369374
precedence = 3;
370375
break;
371376
case E_OP_MIN: //fallthrough
372377
case E_OP_MAX: //fallthrough
373378
case E_OP_LCM: //fallthrough
374-
case E_OP_GCD: //fallthrough
379+
case E_OP_GCD:
375380
precedence = 4;
376381
break;
377382
default:
@@ -606,6 +611,9 @@ static int apply_rpn_op(const Formula_Object& arg1, const Formula_Object& arg2,
606611
case E_OP_DIV:
607612
result = arg1.data.num / arg2.data.num;
608613
break;
614+
case E_OP_MOD:
615+
result = arg1.data.num % arg2.data.num;
616+
break;
609617
case E_OP_MAX:
610618
result = std::max(arg1.data.num, arg2.data.num);
611619
break;
@@ -643,8 +651,9 @@ static bool is_operator(const char ch) {
643651
switch (ch) {
644652
case '+': //fallthrough
645653
case '-': //fallthrough
646-
case '/': //fallthrough
647654
case '*': //fallthrough
655+
case '/': //fallthrough
656+
case '%': //fallthrough
648657
case ')': //fallthrough
649658
case '(': //fallthrough
650659
case ',':

libs/libvtrutil/src/vtr_expr_eval.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ typedef enum e_operator {
6060
E_OP_SUB,
6161
E_OP_MULT,
6262
E_OP_DIV,
63+
E_OP_MOD,
6364
E_OP_MIN,
6465
E_OP_MAX,
6566
E_OP_GCD,
@@ -108,6 +109,8 @@ class Formula_Object {
108109
return "*";
109110
} else if (data.op == E_OP_DIV) {
110111
return "/";
112+
} else if (data.op == E_OP_MOD) {
113+
return "%";
111114
} else if (data.op == E_OP_MIN) {
112115
return "min";
113116
} else if (data.op == E_OP_MAX) {

libs/libvtrutil/test/test_expr_eval.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ TEST_CASE("Simple Expressions", "[vtr_expr_eval]") {
2424
//Floor arithmetic
2525
REQUIRE(parser.parse_formula("5 / 10", vars) == 0);
2626
REQUIRE(parser.parse_formula("10 / 9", vars) == 1);
27+
28+
REQUIRE(parser.parse_formula("5 % 10", vars) == 5);
29+
REQUIRE(parser.parse_formula("10 % 9", vars) == 1);
2730
}
2831

2932
TEST_CASE("Negative Literals", "[vtr_expr_eval]") {

0 commit comments

Comments
 (0)