Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| creating:statements [2025/04/19 19:47] – Use internal wiki links, add link to code repo ahelwer | creating:statements [2025/06/18 20:42] (current) – Fixed stale line of code due to print changes ahelwer | ||
|---|---|---|---|
| Line 24: | Line 24: | ||
| defineAst(outputDir, | defineAst(outputDir, | ||
| - | " | + | " |
| )); | )); | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | The '' | ||
| ===== Parsing statements ===== | ===== Parsing statements ===== | ||
| Line 58: | Line 60: | ||
| Our '' | Our '' | ||
| + | Use '' | ||
| We'll return to this method later to add parsing logic for operator definitions. | We'll return to this method later to add parsing logic for operator definitions. | ||
| - | |||
| <code java> | <code java> | ||
| private Stmt statement() { | private Stmt statement() { | ||
| - | if (replMode) return new Stmt.Print(expression()); | + | if (replMode) return new Stmt.Print(peek(), |
| throw error(peek(), | throw error(peek(), | ||
| Line 71: | Line 73: | ||
| Add '' | Add '' | ||
| - | |||
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| class Interpreter implements Expr.Visitor< | class Interpreter implements Expr.Visitor< | ||
| Line 79: | Line 80: | ||
| Now add a visitor method for the '' | Now add a visitor method for the '' | ||
| - | This is identical to the book except for one thing, which we will change to improve the testability of our interpreter: | + | This is identical to the book: |
| - | + | <code java> | |
| - | <code java [highlight_lines_extra=" | + | |
| @Override | @Override | ||
| public Void visitPrintStmt(Stmt.Print stmt) { | public Void visitPrintStmt(Stmt.Print stmt) { | ||
| Object value = evaluate(stmt.expression); | Object value = evaluate(stmt.expression); | ||
| - | out.println(stringify(value)); | + | |
| return null; | return null; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Instead of printing directly to '' | ||
| - | '' | ||
| - | Add a constructor for the '' | ||
| - | |||
| - | <code java [highlight_lines_extra=" | ||
| - | class Interpreter implements Expr.Visitor< | ||
| - | | ||
| - | private final PrintStream out; | ||
| - | |||
| - | public Interpreter(PrintStream out, boolean replMode) { | ||
| - | this.out = out; | ||
| } | } | ||
| </ | </ | ||
| Line 106: | Line 92: | ||
| Same as the book, modify the old '' | Same as the book, modify the old '' | ||
| The changed method is identical to the book except for using '' | The changed method is identical to the book except for using '' | ||
| - | |||
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| void interpret(List< | void interpret(List< | ||
| Line 120: | Line 105: | ||
| Also add the '' | Also add the '' | ||
| - | |||
| <code java> | <code java> | ||
| private void execute(Stmt stmt) { | private void execute(Stmt stmt) { | ||
| Line 127: | Line 111: | ||
| </ | </ | ||
| - | At the top of '' | + | At the top of '' |
| - | + | <code java [highlight_lines_extra=" | |
| - | <code java [highlight_lines_extra=" | + | |
| package tla; | package tla; | ||
| Line 135: | Line 118: | ||
| import java.util.HashSet; | import java.util.HashSet; | ||
| import java.util.List; | import java.util.List; | ||
| - | import java.io.PrintStream; | ||
| </ | </ | ||
| Line 154: | Line 136: | ||
| </ | </ | ||
| - | Then replace the call to the interpreter with this: | + | Still in '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| if (hadError) return; | if (hadError) return; | ||
| Line 176: | Line 157: | ||
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| private static void runFile(String path) throws IOException { | private static void runFile(String path) throws IOException { | ||
| - | interpreter = new Interpreter(System.out, | + | interpreter = new Interpreter(false); |
| byte[] bytes = Files.readAllBytes(Paths.get(path)); | byte[] bytes = Files.readAllBytes(Paths.get(path)); | ||
| run(new String(bytes, | run(new String(bytes, | ||
| Line 185: | Line 166: | ||
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| private static void runPrompt() throws IOException { | private static void runPrompt() throws IOException { | ||
| - | interpreter = new Interpreter(System.out, | + | interpreter = new Interpreter(true); |
| InputStreamReader input = new InputStreamReader(System.in); | InputStreamReader input = new InputStreamReader(System.in); | ||
| BufferedReader reader = new BufferedReader(input); | BufferedReader reader = new BufferedReader(input); | ||
| Line 196: | Line 177: | ||
| hadError = false; | hadError = false; | ||
| } | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | We just used an '' | ||
| + | Write that constructor now, although don't yet do anything with the parameter: | ||
| + | <code Java> | ||
| + | public Interpreter(boolean replMode) { | ||
| + | |||
| } | } | ||
| </ | </ | ||
| Line 212: | Line 201: | ||
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| defineAst(outputDir, | defineAst(outputDir, | ||
| - | " | + | " |
| " | " | ||
| )); | )); | ||
| Line 386: | Line 375: | ||
| Now add an '' | Now add an '' | ||
| - | + | <code java [highlight_lines_extra=" | |
| - | <code java [highlight_lines_extra=" | + | |
| class Interpreter implements Expr.Visitor< | class Interpreter implements Expr.Visitor< | ||
| | | ||
| private Environment environment; | private Environment environment; | ||
| - | private final PrintStream out; | ||
| - | public Interpreter(PrintStream out, boolean replMode) { | + | public Interpreter(boolean replMode) { |
| this.environment = new Environment(replMode); | this.environment = new Environment(replMode); | ||
| - | this.out = out; | ||
| } | } | ||
| </ | </ | ||
| - | Now add a visitor method in the '' | + | Add a visitor method in the '' |
| <code java> | <code java> | ||
| @Override | @Override | ||
| Line 407: | Line 392: | ||
| } | } | ||
| </ | </ | ||
| - | |||
| ====== Section 8.4: Assignment ====== | ====== Section 8.4: Assignment ====== | ||
| Line 463: | Line 447: | ||
| Next up, our greatest parsing challenge yet: [[creating: | Next up, our greatest parsing challenge yet: [[creating: | ||
| - | If your code got out of sync during this tutorial, you can find a snapshot of its expected state in [[https:// | + | If your code got out of sync during this tutorial, you can find a snapshot of its expected state in [[https:// |
| ====== Section 8.5: Challenges ====== | ====== Section 8.5: Challenges ====== | ||
| - | - Write some unit tests for your interpreter. | + | - Write some unit tests for your interpreter. |
| - The '' | - The '' | ||
| [[creating: | [[creating: | ||