From 4cb7e4eb4a9213304ee5b83c9aaa0427e9a8fe31 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Wed, 21 May 2025 18:23:35 -0500 Subject: ExpressionParser: use correct operand order Most of the internal problems with the expression parser happened because I was accepting the arguments for binary operators in the wrong order. For example, '2 - 1' became '1 2 -', not '2 1 -'. The likely cause of this error is the following sequence of events: - In commit 6dbd32cd, I created the code for interpreting RPN. I accepted two arguments from the stack (o1 and o2), then performed o1 o2. However, because stacks are in LIFO order, I should have actually done o2 o1. - Later, in commit 94349688, I created the code for converting an infix expression to RPN. Creating the expressions in the correct order did not work, because my interpreter used the incorrect order. To 'fix' this problem, I created the expressions in the incorrect order. I did not notice any discrepancy, probably because I was not testing the individual methods, only the two-step whole (which found no errors). --- src/main/java/sevenUnits/utils/ExpressionParser.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java/sevenUnits/utils/ExpressionParser.java') diff --git a/src/main/java/sevenUnits/utils/ExpressionParser.java b/src/main/java/sevenUnits/utils/ExpressionParser.java index e248ff0..2bc8952 100644 --- a/src/main/java/sevenUnits/utils/ExpressionParser.java +++ b/src/main/java/sevenUnits/utils/ExpressionParser.java @@ -675,7 +675,7 @@ public final class ExpressionParser { final String operand2 = components .remove(highestPriorityOperatorPosition - 1); components.add(highestPriorityOperatorPosition - 1, - operand2 + " " + operand1 + " " + binaryOperator); + operand1 + " " + operand2 + " " + binaryOperator); break; default: throw new AssertionError("Expected operator, found non-operator."); @@ -826,7 +826,7 @@ public final class ExpressionParser { final BinaryOperator binaryOperator = this.binaryOperators .get(item); - stack.push(binaryOperator.apply(o1, o2)); + stack.push(binaryOperator.apply(o2, o1)); break; case NUMERIC_OPERATOR: -- cgit v1.2.3