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 [2026/11/19 02:59] (current) – Fixed highlighting ahelwer | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ======= Handling TLA⁺ Statements ======= | ======= Handling TLA⁺ Statements ======= | ||
| - | Here we cover [[https:// | + | Here we cover [[https:// |
| We'll implement the ability to associate identifiers with values, then subsequently use those identifiers in expressions. | We'll implement the ability to associate identifiers with values, then subsequently use those identifiers in expressions. | ||
| While Lox was designed with a dual REPL/file execution model in mind, TLA⁺ was not - so the behavior of our interpreter will vary quite a bit from the book! | While Lox was designed with a dual REPL/file execution model in mind, TLA⁺ was not - so the behavior of our interpreter will vary quite a bit from the book! | ||
| We'll have to implement parser & interpreter //modes//. | We'll have to implement parser & interpreter //modes//. | ||
| When the user runs our TLA⁺ interpreter as a REPL, we want to support operator redefinition and the ability to see the value of simple expressions - like the Python REPL. | When the user runs our TLA⁺ interpreter as a REPL, we want to support operator redefinition and the ability to see the value of simple expressions - like the Python REPL. | ||
| - | In contast, when the user runs our TLA⁺ interpreter against a file it should be an error for multiple operators to use the same name. | + | In contrast, when the user runs our TLA⁺ interpreter against a file it should be an error for multiple operators to use the same name. |
| The parser also should not accept standalone expressions in the middle of the file. | The parser also should not accept standalone expressions in the middle of the file. | ||
| Line 24: | Line 24: | ||
| defineAst(outputDir, | defineAst(outputDir, | ||
| - | " | + | " |
| )); | )); | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | The '' | ||
| ===== Parsing statements ===== | ===== Parsing statements ===== | ||
| Line 44: | Line 46: | ||
| </ | </ | ||
| - | Modify | + | Entirely replace |
| <code java> | <code java> | ||
| Line 58: | Line 60: | ||
| Our '' | Our '' | ||
| - | We'll return | + | In other words, we automatically promote naked expressions into print statements. |
| + | Use '' | ||
| <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 70: | Line 72: | ||
| ===== Executing statements ===== | ===== Executing statements ===== | ||
| - | Add '' | + | Add the '' |
| <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 '' | + | Same as the book, overwrite |
| - | '' | + | The new method is identical to the book except for using '' |
| - | 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; | + | |
| - | } | + | |
| - | </ | + | |
| - | + | ||
| - | Same as the book, modify | + | |
| - | The changed | + | |
| <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; | ||
| + | System.out.println(new AstPrinter().print(statements)); | ||
| interpreter.interpret(statements); | interpreter.interpret(statements); | ||
| } | } | ||
| Line 165: | Line 147: | ||
| A bit more tidying up is necessary. | A bit more tidying up is necessary. | ||
| We need to detect when we're in REPL vs. file mode and propagate that information to our various methods and classes. | We need to detect when we're in REPL vs. file mode and propagate that information to our various methods and classes. | ||
| - | Remove the '' | + | Remove the '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| Line 176: | Line 158: | ||
| <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 183: | Line 165: | ||
| Similarly modify the '' | Similarly modify the '' | ||
| - | <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); | + | |
| - | BufferedReader reader = new BufferedReader(input); | + | |
| for (;;) { | for (;;) { | ||
| Line 196: | Line 176: | ||
| 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 205: | Line 193: | ||
| In [[https:// | In [[https:// | ||
| Operator definitions in TLA⁺ take the form '' | Operator definitions in TLA⁺ take the form '' | ||
| - | They can be parameterized with arguments, but we won't be implementing that until the tutorial covering chapter 10. | + | They can be parameterized with arguments, but we won't be implementing that until the tutorial covering chapter 10 of //Crafting Interpreters// |
| For now we are simply assigning a name to a value. | For now we are simply assigning a name to a value. | ||
| Line 212: | Line 200: | ||
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| defineAst(outputDir, | defineAst(outputDir, | ||
| - | " | + | " |
| " | " | ||
| )); | )); | ||
| </ | </ | ||
| - | Also generate | + | Also generate |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| Line 258: | Line 246: | ||
| That single line difference calls three separate functions we still need to define. | That single line difference calls three separate functions we still need to define. | ||
| While Lox is a nice unambiguous language with semicolon-delimited statements, TLA⁺ is a wholly different beast! | While Lox is a nice unambiguous language with semicolon-delimited statements, TLA⁺ is a wholly different beast! | ||
| - | In TLA⁺ it is quite difficult to know where each definition ends and another | + | In TLA⁺ it is quite difficult to know where each definition ends and another |
| We have to resort to the somewhat drastic measure of cloning our parser then looking several tokens ahead in the stream in search of a '' | We have to resort to the somewhat drastic measure of cloning our parser then looking several tokens ahead in the stream in search of a '' | ||
| This type of unbounded lookahead is inelegant and can lead to poor parser performance, | This type of unbounded lookahead is inelegant and can lead to poor parser performance, | ||
| Line 292: | Line 280: | ||
| </ | </ | ||
| - | So we clone the parser, | + | So we clone the parser, |
| Incidentally, | Incidentally, | ||
| Line 330: | Line 318: | ||
| In [[https:// | In [[https:// | ||
| - | Create a new file, '' | + | Create a new file, '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| Line 347: | Line 335: | ||
| This requires defining a constructor: | This requires defining a constructor: | ||
| - | <code java [highlight_lines_extra="" | + | <code java [highlight_lines_extra=" |
| class Environment { | class Environment { | ||
| private final boolean allowRedefinition; | private final boolean allowRedefinition; | ||
| Line 358: | Line 346: | ||
| </ | </ | ||
| - | Then, our '' | + | Then, our '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| Line 370: | Line 358: | ||
| </ | </ | ||
| - | Note that we changed the method signature to accept a '' | + | Compared to the book we changed the method signature to accept a '' |
| Our '' | Our '' | ||
| Line 386: | Line 374: | ||
| 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 391: | ||
| } | } | ||
| </ | </ | ||
| - | |||
| ====== Section 8.4: Assignment ====== | ====== Section 8.4: Assignment ====== | ||
| Line 424: | Line 407: | ||
| And that's it! | And that's it! | ||
| - | Your interpreter should now support definitions: | + | Your interpreter should now support definitions |
| < | < | ||
| > x == 2 | > x == 2 | ||
| Line 446: | Line 428: | ||
| </ | </ | ||
| - | Initialize it to '' | + | Initialize it to '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| Environment(boolean allowRedefinition) { | Environment(boolean allowRedefinition) { | ||
| Line 457: | Line 438: | ||
| this.enclosing = enclosing; | this.enclosing = enclosing; | ||
| this.allowRedefinition = enclosing.allowRedefinition; | this.allowRedefinition = enclosing.allowRedefinition; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Finally, update '' | ||
| + | <code java [highlight_lines_extra=" | ||
| + | Object get(Token name) { | ||
| + | if (values.containsKey(name.lexeme)) { | ||
| + | return values.get(name.lexeme); | ||
| + | } | ||
| + | |||
| + | if (enclosing != null) return enclosing.get(name); | ||
| + | |||
| + | throw new RuntimeError(name, | ||
| + | " | ||
| } | } | ||
| </ | </ | ||
| Line 463: | Line 458: | ||
| 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:// |
| - | ====== | + | ====== Challenges ====== |
| - | - Write some unit tests for your interpreter. | + | - Write some unit tests for your interpreter. |
| - The '' | - The '' | ||
| [[creating: | [[creating: | ||
| - | |||