Skip to content

Commit 012d602

Browse files
committed
vtrutil: Add gt/lt operators to expression eval
1 parent c397c9e commit 012d602

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

libs/libvtrutil/src/vtr_expr_eval.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,14 @@ static void get_formula_object(const char* ch, int& ichar, const t_formula_data&
334334
fobj->type = E_FML_OPERATOR;
335335
fobj->data.op = E_OP_MOD;
336336
break;
337+
case '>':
338+
fobj->type = E_FML_OPERATOR;
339+
fobj->data.op = E_OP_GT;
340+
break;
341+
case '<':
342+
fobj->type = E_FML_OPERATOR;
343+
fobj->data.op = E_OP_LT;
344+
break;
337345
case '(':
338346
fobj->type = E_FML_BRACKET;
339347
fobj->data.left_bracket = true;
@@ -364,12 +372,16 @@ static int get_fobj_precedence(const Formula_Object& fobj) {
364372
} else if (E_FML_OPERATOR == fobj.type) {
365373
t_operator op = fobj.data.op;
366374
switch (op) {
375+
case E_OP_GT: //fallthrough
376+
case E_OP_LT:
377+
precedence = 1;
378+
break;
367379
case E_OP_ADD: //fallthrough
368380
case E_OP_SUB:
369381
precedence = 2;
370382
break;
371383
case E_OP_MULT: //fallthrough
372-
case E_OP_DIV: //fallthrough
384+
case E_OP_DIV: //fallthrough
373385
case E_OP_MOD:
374386
precedence = 3;
375387
break;
@@ -614,6 +626,12 @@ static int apply_rpn_op(const Formula_Object& arg1, const Formula_Object& arg2,
614626
case E_OP_MOD:
615627
result = arg1.data.num % arg2.data.num;
616628
break;
629+
case E_OP_GT:
630+
result = arg1.data.num > arg2.data.num;
631+
break;
632+
case E_OP_LT:
633+
result = arg1.data.num < arg2.data.num;
634+
break;
617635
case E_OP_MAX:
618636
result = std::max(arg1.data.num, arg2.data.num);
619637
break;
@@ -656,6 +674,8 @@ static bool is_operator(const char ch) {
656674
case '%': //fallthrough
657675
case ')': //fallthrough
658676
case '(': //fallthrough
677+
case '<': //fallthrough
678+
case '>': //fallthrough
659679
case ',':
660680
return true;
661681
default:

libs/libvtrutil/src/vtr_expr_eval.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ typedef enum e_operator {
6161
E_OP_MULT,
6262
E_OP_DIV,
6363
E_OP_MOD,
64+
E_OP_GT,
65+
E_OP_LT,
6466
E_OP_MIN,
6567
E_OP_MAX,
6668
E_OP_GCD,
@@ -111,6 +113,10 @@ class Formula_Object {
111113
return "/";
112114
} else if (data.op == E_OP_MOD) {
113115
return "%";
116+
} else if (data.op == E_OP_GT) {
117+
return ">";
118+
} else if (data.op == E_OP_LT) {
119+
return "<";
114120
} else if (data.op == E_OP_MIN) {
115121
return "min";
116122
} else if (data.op == E_OP_MAX) {

libs/libvtrutil/test/test_expr_eval.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ TEST_CASE("Simple Expressions", "[vtr_expr_eval]") {
1616
REQUIRE(parser.parse_formula("5 - 2", vars) == 3);
1717
REQUIRE(parser.parse_formula("5 - 10", vars) == -5);
1818

19-
20-
2119
REQUIRE(parser.parse_formula("5 * 5", vars) == 25);
2220
REQUIRE(parser.parse_formula("5 / 5", vars) == 1);
2321

@@ -27,6 +25,12 @@ TEST_CASE("Simple Expressions", "[vtr_expr_eval]") {
2725

2826
REQUIRE(parser.parse_formula("5 % 10", vars) == 5);
2927
REQUIRE(parser.parse_formula("10 % 9", vars) == 1);
28+
29+
REQUIRE(parser.parse_formula("5 < 10", vars) == 1);
30+
REQUIRE(parser.parse_formula("20 < 10", vars) == 0);
31+
32+
REQUIRE(parser.parse_formula("5 > 10", vars) == 0);
33+
REQUIRE(parser.parse_formula("20 > 10", vars) == 1);
3034
}
3135

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

0 commit comments

Comments
 (0)