creating:statements

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
creating:statements [2025/06/13 20:13] – Removed PrintStream parameter from Interpreter constructor ahelwercreating:statements [2025/06/18 20:42] (current) – Fixed stale line of code due to print changes ahelwer
Line 24: Line 24:
  
     defineAst(outputDir, "Stmt", Arrays.asList(     defineAst(outputDir, "Stmt", Arrays.asList(
-      "Print    : Expr expression"+      "Print    : Token location, Expr expression"
     ));     ));
   }   }
 </code> </code>
 +
 +The ''Print'' class contains a ''Token'' instance to support reporting error locations.
  
 ===== Parsing statements ===== ===== Parsing statements =====
Line 58: Line 60:
  
 Our ''statement()'' method is different from the book; if we're in REPL mode we parse & return a statement wrapped in a ''Stmt.Print'' object, and error otherwise. Our ''statement()'' method is different from the book; if we're in REPL mode we parse & return a statement wrapped in a ''Stmt.Print'' object, and error otherwise.
 +Use ''peek()'' to get the first token in the expression for error location reporting.
 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(), expression());
  
     throw error(peek(), "Expected statement.");     throw error(peek(), "Expected statement.");
Line 199: Line 201:
 <code java [highlight_lines_extra="3"]> <code java [highlight_lines_extra="3"]>
     defineAst(outputDir, "Stmt", Arrays.asList(     defineAst(outputDir, "Stmt", Arrays.asList(
-      "Print    : Expr expression",+      "Print    : Token location, Expr expression",
       "OpDef    : Token name, Expr body"       "OpDef    : Token name, Expr body"
     ));     ));
  • creating/statements.1749845591.txt.gz
  • Last modified: 2025/06/13 20:13
  • by ahelwer