Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
creating:statements [2025/04/19 19:47] – Use internal wiki links, add link to code repo ahelwer | creating:statements [2025/06/13 20:13] (current) – Removed PrintStream parameter from Interpreter constructor ahelwer | ||
---|---|---|---|
Line 71: | Line 71: | ||
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 78: | ||
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 90: | ||
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 103: | ||
Also add the '' | Also add the '' | ||
- | |||
<code java> | <code java> | ||
private void execute(Stmt stmt) { | private void execute(Stmt stmt) { | ||
Line 127: | Line 109: | ||
</ | </ | ||
- | 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 116: | ||
import java.util.HashSet; | import java.util.HashSet; | ||
import java.util.List; | import java.util.List; | ||
- | import java.io.PrintStream; | ||
</ | </ | ||
Line 154: | Line 134: | ||
</ | </ | ||
- | 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 155: | ||
<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 164: | ||
<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 175: | ||
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 386: | Line 373: | ||
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 390: | ||
} | } | ||
</ | </ | ||
- | |||
====== Section 8.4: Assignment ====== | ====== Section 8.4: Assignment ====== | ||
Line 463: | Line 445: | ||
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: | ||