Interpreter: sync post code blocks to repo exactly (Javadoc wording), H6 file labels + explanatory paragraphs between every block, Java 25 note, README mapping
This commit is contained in:
31
03-behavioral/interpreter/README.md
Normal file
31
03-behavioral/interpreter/README.md
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# Interpreter Design Pattern — Java Example
|
||||||
|
|
||||||
|
**Pattern:** Behavioral → Interpreter
|
||||||
|
**Article:** https://ankurm.com/interpreter-design-pattern-java/
|
||||||
|
|
||||||
|
## What this example shows
|
||||||
|
|
||||||
|
A math expression evaluator built as an abstract syntax tree. `Expression` declares `interpret()` — every AST node implements it. `NumberExpression` is the only terminal: a leaf with no children that returns its value directly. `AddExpression`, `SubtractExpression`, and `MultiplyExpression` are non-terminals: each holds two child expressions and applies its operator after recursively interpreting them. `Main` is the client — it assembles three small trees by hand (`(5 + 3) * 2`, `10 - (4 + 2)`, `(3 * 4) + (10 - 6)`) and evaluates each by calling `interpret()` on the root.
|
||||||
|
|
||||||
|
## How to run
|
||||||
|
|
||||||
|
```bash
|
||||||
|
javac interpreter/*.java -d out/interpreter
|
||||||
|
java -cp out/interpreter interpreter.Main
|
||||||
|
```
|
||||||
|
|
||||||
|
Requires Java 25.
|
||||||
|
|
||||||
|
## Post Section ↔ File Mapping
|
||||||
|
|
||||||
|
| Post Section | File(s) |
|
||||||
|
|---|---|
|
||||||
|
| Implementation: Math Expression Evaluator — the AbstractExpression | `Expression.java` |
|
||||||
|
| Implementation: Math Expression Evaluator — the TerminalExpression | `NumberExpression.java` |
|
||||||
|
| Implementation: Math Expression Evaluator — AddExpression | `AddExpression.java` |
|
||||||
|
| Implementation: Math Expression Evaluator — SubtractExpression | `SubtractExpression.java` |
|
||||||
|
| Implementation: Math Expression Evaluator — MultiplyExpression | `MultiplyExpression.java` |
|
||||||
|
| Implementation: Math Expression Evaluator — wiring it together | `Main.java` |
|
||||||
|
|
||||||
|
Article: https://ankurm.com/interpreter-design-pattern-java/
|
||||||
|
All patterns: https://ankurm.com/design-patterns-java/
|
||||||
17
README.md
17
README.md
@@ -479,6 +479,23 @@ javac 03-behavioral/visitor/*.java -d out/visitor
|
|||||||
java -cp out/visitor visitor.Main
|
java -cp out/visitor visitor.Main
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Interpreter (`03-behavioral/interpreter/`)
|
||||||
|
|
||||||
|
| Post Section | File(s) |
|
||||||
|
|---|---|
|
||||||
|
| Implementation: Math Expression Evaluator — the AbstractExpression | `Expression.java` |
|
||||||
|
| Implementation: Math Expression Evaluator — the TerminalExpression | `NumberExpression.java` |
|
||||||
|
| Implementation: Math Expression Evaluator — AddExpression | `AddExpression.java` |
|
||||||
|
| Implementation: Math Expression Evaluator — SubtractExpression | `SubtractExpression.java` |
|
||||||
|
| Implementation: Math Expression Evaluator — MultiplyExpression | `MultiplyExpression.java` |
|
||||||
|
| Implementation: Math Expression Evaluator — wiring it together | `Main.java` |
|
||||||
|
|
||||||
|
Run it:
|
||||||
|
```bash
|
||||||
|
javac 03-behavioral/interpreter/*.java -d out/interpreter
|
||||||
|
java -cp out/interpreter interpreter.Main
|
||||||
|
```
|
||||||
|
|
||||||
## Reference
|
## Reference
|
||||||
|
|
||||||
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides
|
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides
|
||||||
|
|||||||
Reference in New Issue
Block a user