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/06/18 20:42] – Fixed stale line of code due to print changes 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 46: | Line 46: | ||
| </ | </ | ||
| - | Modify | + | Entirely replace |
| <code java> | <code java> | ||
| Line 60: | Line 60: | ||
| Our '' | Our '' | ||
| + | In other words, we automatically promote naked expressions into print statements. | ||
| Use '' | Use '' | ||
| - | We'll return to this method later to add parsing logic for operator definitions. | ||
| <code java> | <code java> | ||
| private Stmt statement() { | private Stmt statement() { | ||
| Line 72: | 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 90: | Line 90: | ||
| </ | </ | ||
| - | Same as the book, modify | + | Same as the book, overwrite |
| - | The changed | + | The new 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 136: | Line 136: | ||
| </ | </ | ||
| - | Still in '' | + | 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 146: | 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 164: | 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(true); | interpreter = new Interpreter(true); | ||
| - | InputStreamReader input = new InputStreamReader(System.in); | ||
| - | BufferedReader reader = new BufferedReader(input); | ||
| for (;;) { | for (;;) { | ||
| Line 182: | Line 181: | ||
| We just used an '' | We just used an '' | ||
| Write that constructor now, although don't yet do anything with the parameter: | Write that constructor now, although don't yet do anything with the parameter: | ||
| - | < | + | < |
| public Interpreter(boolean replMode) { | public Interpreter(boolean replMode) { | ||
| Line 194: | 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 206: | Line 205: | ||
| </ | </ | ||
| - | Also generate | + | Also generate |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| Line 247: | 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 281: | Line 280: | ||
| </ | </ | ||
| - | So we clone the parser, | + | So we clone the parser, |
| Incidentally, | Incidentally, | ||
| Line 319: | 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 336: | 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 347: | Line 346: | ||
| </ | </ | ||
| - | Then, our '' | + | Then, our '' |
| <code java [highlight_lines_extra=" | <code java [highlight_lines_extra=" | ||
| Line 359: | 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 408: | 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 430: | 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 441: | 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 449: | Line 460: | ||
| 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. Capture the '' | - Write some unit tests for your interpreter. Capture the '' | ||
| Line 455: | Line 466: | ||
| [[creating: | [[creating: | ||
| - | |||