From ba4d142be2b8c0aa8a24d501731c2746c982728c Mon Sep 17 00:00:00 2001 From: Ankur Date: Wed, 24 Jun 2026 17:40:29 +0530 Subject: [PATCH] Interpreter: sync post code blocks to repo exactly (Javadoc wording), H6 file labels + explanatory paragraphs between every block, Java 25 note, README mapping --- 03-behavioral/interpreter/README.md | 31 +++++++++++++++++++++++++++++ README.md | 17 ++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 03-behavioral/interpreter/README.md diff --git a/03-behavioral/interpreter/README.md b/03-behavioral/interpreter/README.md new file mode 100644 index 0000000..c8db249 --- /dev/null +++ b/03-behavioral/interpreter/README.md @@ -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/ diff --git a/README.md b/README.md index 4866088..0885be9 100644 --- a/README.md +++ b/README.md @@ -479,6 +479,23 @@ javac 03-behavioral/visitor/*.java -d out/visitor 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 - *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides