docs: update interpreter

This commit is contained in:
Ilkka Seppälä committed 2024-05-26 13:34:46 +03:00
1 parent d991970439
commit b5d280e4f5
1 file changed
+35 -8
+35 -8
View File
@@ -99,6 +99,11 @@ public class PlusExpression extends Expression {
Now, we are able to show the interpreter pattern in action parsing some simple math. Now, we are able to show the interpreter pattern in action parsing some simple math.
```java ```java
@Slf4j
public class App {
public static void main(String[] args) {
// the halfling kids are learning some basic math at school // the halfling kids are learning some basic math at school
// define the math string we want to parse // define the math string we want to parse
final var tokenString = "4 3 2 - 1 + *"; final var tokenString = "4 3 2 - 1 + *";
@@ -114,7 +119,8 @@ for (var s : tokenList) {
// the stack // the stack
var rightExpression = stack.pop(); var rightExpression = stack.pop();
var leftExpression = stack.pop(); var leftExpression = stack.pop();
LOGGER.info("popped from stack left: {} right: {}", leftExpression.interpret(),rightExpression.interpret()); LOGGER.info("popped from stack left: {} right: {}",
leftExpression.interpret(), rightExpression.interpret());
var operator = getOperatorInstance(s, leftExpression, rightExpression); var operator = getOperatorInstance(s, leftExpression, rightExpression);
LOGGER.info("operator: {}", operator); LOGGER.info("operator: {}", operator);
var result = operator.interpret(); var result = operator.interpret();
@@ -131,18 +137,39 @@ for (var s : tokenList) {
} }
// in the end, the final result lies on top of the stack // in the end, the final result lies on top of the stack
LOGGER.info("result: {}", stack.pop().interpret()); LOGGER.info("result: {}", stack.pop().interpret());
}
public static boolean isOperator(String s) {
return s.equals("+") || s.equals("-") || s.equals("*");
}
public static Expression getOperatorInstance(String s, Expression left, Expression right) {
return switch (s) {
case "+" -> new PlusExpression(left, right);
case "-" -> new MinusExpression(left, right);
default -> new MultiplyExpression(left, right);
};
}
}
``` ```
Executing the program produces the following console output. Executing the program produces the following console output.
``` ```
popped from stack left: 1 right: 1 13:33:15.437 [main] INFO com.iluwatar.interpreter.App -- push to stack: 4
operator: + 13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- push to stack: 3
push result to stack: 2 13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- push to stack: 2
popped from stack left: 4 right: 2 13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- popped from stack left: 3 right: 2
operator: * 13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- operator: -
push result to stack: 8 13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- push result to stack: 1
result: 8 13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- push to stack: 1
13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- popped from stack left: 1 right: 1
13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- operator: +
13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- push result to stack: 2
13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- popped from stack left: 4 right: 2
13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- operator: *
13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- push result to stack: 8
13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- result: 8
``` ```
## Class diagram ## Class diagram