Skip to content

Commit 362c131

Browse files
committed
fix: improve error handling in ExpressionParser and add missing test case for addition
1 parent 66ab3f2 commit 362c131

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/main/java/mindustrytool/workflow/expressions/ExpressionParser.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public ExpressionParser() {
5858
register("Multiplication", "*", (a, b) -> a * b);
5959
register("Division", "/", (a, b) -> a / b);
6060
register("Modulo", "%", (a, b) -> a % b);
61-
register("Integer Division", "idiv", (a, b) -> (double) ((int) (a / b)));
61+
register("Integer Division", "idiv", (a, b) -> Math.floor((a / b)));
6262
register("Equals", "==", (a, b) -> a.equals(b));
6363
register("Not Equals", "!=", (a, b) -> !a.equals(b));
6464
register("Less Than", "<", (a, b) -> a < b);
@@ -160,7 +160,7 @@ public Boolean evaluateAsBoolean(String expr, Map<String, Object> variables) {
160160
if (result instanceof String) {
161161
return Boolean.parseBoolean(result.toString());
162162
}
163-
163+
164164
throw new WorkflowError("Invalid boolean value: " + result.toString());
165165
}
166166

@@ -188,15 +188,23 @@ public <T> T evaluate(Class<T> type, String expr, Map<String, Object> variables)
188188
double b = (double) stack.pop();
189189
double a = (double) stack.pop();
190190
var operation = BINARY_OPERATORS.get(token);
191-
var result = operation.getFunction().apply(a, b);
192-
Log.debug(a + " " + operation.getSign() + " " + b + " = " + result);
193-
stack.push(result);
191+
try {
192+
var result = operation.getFunction().apply(a, b);
193+
stack.push(result);
194+
Log.debug(a + " " + operation.getSign() + " " + b + " = " + result);
195+
} catch (Exception e) {
196+
throw new WorkflowError("Invalid unary operation: " + a + " " + operation.getSign() + " " + b, e);
197+
}
194198
} else if (UNARY_OPERATORS.containsKey(token)) {
195199
double a = (double) stack.pop();
196200
var operation = UNARY_OPERATORS.get(token);
197-
var result = operation.getFunction().apply(a);
198-
Log.debug(a + " " + operation.getSign() + " = " + result);
199-
stack.push(result);
201+
try {
202+
var result = operation.getFunction().apply(a);
203+
stack.push(result);
204+
Log.debug(operation.getSign() + " " + a + " = " + result);
205+
} catch (Exception e) {
206+
throw new WorkflowError("Invalid unary operation: " + operation.getSign() + " " + a, e);
207+
}
200208
} else if (vars.containsKey(token)) {
201209
stack.push(vars.get(token));
202210
Log.debug("Add variable: " + token);

src/test/java/ExpressionParserTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public class ExpressionParserTest {
1414
@Test
1515
void testAddition() {
1616
assertEquals(parser.evaluate(Object.class, "1 + 2", variables), 3d);
17+
}
18+
19+
@Test
20+
void testAdditionWithVariable() {
1721
assertEquals(parser.evaluate(Object.class, "{{a}} + {{ b}}", variables), 3d);
1822
}
1923
}

0 commit comments

Comments
 (0)