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