summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2025-05-21 18:23:35 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2025-05-30 19:54:17 -0500
commit4cb7e4eb4a9213304ee5b83c9aaa0427e9a8fe31 (patch)
tree827c42e6823aca0705962c62d3aacfda75580c43
parent779bf983226f9799dc19d84a3f2cb9a3e26cd540 (diff)
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 <op> o2. However, because stacks are in LIFO order, I should have actually done o2 <op> 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).
-rw-r--r--src/main/java/sevenUnits/utils/ExpressionParser.java4
-rw-r--r--src/test/java/sevenUnits/utils/ExpressionParserTest.java1
2 files changed, 2 insertions, 3 deletions
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<T> {
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<T> {
final BinaryOperator<T> binaryOperator = this.binaryOperators
.get(item);
- stack.push(binaryOperator.apply(o1, o2));
+ stack.push(binaryOperator.apply(o2, o1));
break;
case NUMERIC_OPERATOR:
diff --git a/src/test/java/sevenUnits/utils/ExpressionParserTest.java b/src/test/java/sevenUnits/utils/ExpressionParserTest.java
index ede931f..15701ce 100644
--- a/src/test/java/sevenUnits/utils/ExpressionParserTest.java
+++ b/src/test/java/sevenUnits/utils/ExpressionParserTest.java
@@ -27,7 +27,6 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
-// TODO add tests for expression-to-RPN and RPN-to-result
/**
* A test for the {@code ExpressionParser} class. This is NOT part of this
* program's public API.