mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 10:58:42 +00:00
docs: update interpreter
This commit is contained in:
+63
-36
@@ -99,50 +99,77 @@ public class PlusExpression extends Expression {
|
||||
Now, we are able to show the interpreter pattern in action parsing some simple math.
|
||||
|
||||
```java
|
||||
// the halfling kids are learning some basic math at school
|
||||
// define the math string we want to parse
|
||||
final var tokenString="4 3 2 - 1 + *";
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
// the stack holds the parsed expressions
|
||||
var stack = new Stack<Expression>();
|
||||
public static void main(String[] args) {
|
||||
|
||||
// tokenize the string and go through them one by one
|
||||
var tokenList = tokenString.split(" ");
|
||||
for (var s : tokenList) {
|
||||
if(isOperator(s)) {
|
||||
// when an operator is encountered we expect that the numbers can be popped from the top of
|
||||
// the stack
|
||||
var rightExpression = stack.pop();
|
||||
var leftExpression = stack.pop();
|
||||
LOGGER.info("popped from stack left: {} right: {}", leftExpression.interpret(),rightExpression.interpret());
|
||||
var operator = getOperatorInstance(s, leftExpression, rightExpression);
|
||||
LOGGER.info("operator: {}", operator);
|
||||
var result = operator.interpret();
|
||||
// the operation result is pushed on top of the stack
|
||||
var resultExpression = new NumberExpression(result);
|
||||
stack.push(resultExpression);
|
||||
LOGGER.info("push result to stack: {}", resultExpression.interpret());
|
||||
} else {
|
||||
// numbers are pushed on top of the stack
|
||||
var i = new NumberExpression(s);
|
||||
stack.push(i);
|
||||
LOGGER.info("push to stack: {}", i.interpret());
|
||||
// the halfling kids are learning some basic math at school
|
||||
// define the math string we want to parse
|
||||
final var tokenString = "4 3 2 - 1 + *";
|
||||
|
||||
// the stack holds the parsed expressions
|
||||
var stack = new Stack<Expression>();
|
||||
|
||||
// tokenize the string and go through them one by one
|
||||
var tokenList = tokenString.split(" ");
|
||||
for (var s : tokenList) {
|
||||
if (isOperator(s)) {
|
||||
// when an operator is encountered we expect that the numbers can be popped from the top of
|
||||
// the stack
|
||||
var rightExpression = stack.pop();
|
||||
var leftExpression = stack.pop();
|
||||
LOGGER.info("popped from stack left: {} right: {}",
|
||||
leftExpression.interpret(), rightExpression.interpret());
|
||||
var operator = getOperatorInstance(s, leftExpression, rightExpression);
|
||||
LOGGER.info("operator: {}", operator);
|
||||
var result = operator.interpret();
|
||||
// the operation result is pushed on top of the stack
|
||||
var resultExpression = new NumberExpression(result);
|
||||
stack.push(resultExpression);
|
||||
LOGGER.info("push result to stack: {}", resultExpression.interpret());
|
||||
} else {
|
||||
// numbers are pushed on top of the stack
|
||||
var i = new NumberExpression(s);
|
||||
stack.push(i);
|
||||
LOGGER.info("push to stack: {}", i.interpret());
|
||||
}
|
||||
}
|
||||
// in the end, the final result lies on top of the stack
|
||||
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);
|
||||
};
|
||||
}
|
||||
}
|
||||
// in the end, the final result lies on top of the stack
|
||||
LOGGER.info("result: {}",stack.pop().interpret());
|
||||
```
|
||||
```
|
||||
|
||||
Executing the program produces the following console output.
|
||||
|
||||
```
|
||||
popped from stack left: 1 right: 1
|
||||
operator: +
|
||||
push result to stack: 2
|
||||
popped from stack left: 4 right: 2
|
||||
operator: *
|
||||
push result to stack: 8
|
||||
result: 8
|
||||
13:33:15.437 [main] INFO com.iluwatar.interpreter.App -- push to stack: 4
|
||||
13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- push to stack: 3
|
||||
13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- push to stack: 2
|
||||
13:33:15.440 [main] INFO com.iluwatar.interpreter.App -- popped from stack left: 3 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: 1
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user