A2

Appendix II

For your edification, here is the code produced by the little script we built to automate generating the syntax tree classes for jlox.

A2 . 1 Expressions

Expressions are the first syntax tree nodes we see, introduced in “Representing Code”. The main Expr class defines the visitor interface used to dispatch against the specific expression types, and contains the other expression subclasses as nested classes:

lox/Expr.java
create new file
package com.craftinginterpreters.lox;

import java.util.List;                      

abstract class Expr {                       
  interface Visitor<R> {                    
    R visitAssignExpr(Assign expr);         
    R visitBinaryExpr(Binary expr);         
    R visitCallExpr(Call expr);             
    R visitGetExpr(Get expr);               
    R visitGroupingExpr(Grouping expr);     
    R visitLiteralExpr(Literal expr);       
    R visitLogicalExpr(Logical expr);       
    R visitSetExpr(Set expr);               
    R visitSuperExpr(Super expr);           
    R visitThisExpr(This expr);             
    R visitUnaryExpr(Unary expr);           
    R visitVariableExpr(Variable expr);     
  }                                         

  // Nested Expr classes here...            

  abstract <R> R accept(Visitor<R> visitor);
}                                           
lox/Expr.java, create new file

A2 . 1 . 1 Assign expression

Variable assignment is introduced in “Statements and State”:

lox/Expr.java
nest inside class Expr
  static class Assign extends Expr {       
    Assign(Token name, Expr value) {       
      this.name = name;                    
      this.value = value;                  
    }

    <R> R accept(Visitor<R> visitor) {     
      return visitor.visitAssignExpr(this);
    }                                      

    final Token name;                      
    final Expr value;                      
  }                                        
lox/Expr.java, nest inside class Expr

A2 . 1 . 2 Binary expression

Binary operators are introduced in “Representing Code”:

lox/Expr.java
nest inside class Expr
  static class Binary extends Expr {               
    Binary(Expr left, Token operator, Expr right) {
      this.left = left;                            
      this.operator = operator;                    
      this.right = right;                          
    }

    <R> R accept(Visitor<R> visitor) {             
      return visitor.visitBinaryExpr(this);        
    }                                              

    final Expr left;                               
    final Token operator;                          
    final Expr right;                              
  }                                                
lox/Expr.java, nest inside class Expr

A2 . 1 . 3 Call expression

Function call expressions are introduced in “Functions”:

lox/Expr.java
nest inside class Expr
  static class Call extends Expr {                        
    Call(Expr callee, Token paren, List<Expr> arguments) {
      this.callee = callee;                               
      this.paren = paren;                                 
      this.arguments = arguments;                         
    }

    <R> R accept(Visitor<R> visitor) {                    
      return visitor.visitCallExpr(this);                 
    }                                                     

    final Expr callee;                                    
    final Token paren;                                    
    final List<Expr> arguments;                           
  }                                                       
lox/Expr.java, nest inside class Expr

A2 . 1 . 4 Get expression

Property access, or “get” expressions are introduced in “Classes”:

lox/Expr.java
nest inside class Expr
  static class Get extends Expr {       
    Get(Expr object, Token name) {      
      this.object = object;             
      this.name = name;                 
    }

    <R> R accept(Visitor<R> visitor) {  
      return visitor.visitGetExpr(this);
    }                                   

    final Expr object;                  
    final Token name;                   
  }                                     
lox/Expr.java, nest inside class Expr

A2 . 1 . 5 Grouping expression

Using parentheses to group expressions is introduced in “Representing Code”:

lox/Expr.java
nest inside class Expr
  static class Grouping extends Expr {       
    Grouping(Expr expression) {              
      this.expression = expression;          
    }

    <R> R accept(Visitor<R> visitor) {       
      return visitor.visitGroupingExpr(this);
    }                                        

    final Expr expression;                   
  }                                          
lox/Expr.java, nest inside class Expr

A2 . 1 . 6 Literal expression

Literal value expressions are introduced in “Representing Code”:

lox/Expr.java
nest inside class Expr
  static class Literal extends Expr {       
    Literal(Object value) {                 
      this.value = value;                   
    }

    <R> R accept(Visitor<R> visitor) {      
      return visitor.visitLiteralExpr(this);
    }                                       

    final Object value;                     
  }                                         
lox/Expr.java, nest inside class Expr

A2 . 1 . 7 Logical expression

The logical and and or operators are introduced in “Control Flow”:

lox/Expr.java
nest inside class Expr
  static class Logical extends Expr {               
    Logical(Expr left, Token operator, Expr right) {
      this.left = left;                             
      this.operator = operator;                     
      this.right = right;                           
    }

    <R> R accept(Visitor<R> visitor) {              
      return visitor.visitLogicalExpr(this);        
    }                                               

    final Expr left;                                
    final Token operator;                           
    final Expr right;                               
  }                                                 
lox/Expr.java, nest inside class Expr

A2 . 1 . 8 Set expression

Property assignment, or “set” expressions are introduced in “Classes”:

lox/Expr.java
nest inside class Expr
  static class Set extends Expr {             
    Set(Expr object, Token name, Expr value) {
      this.object = object;                   
      this.name = name;                       
      this.value = value;                     
    }

    <R> R accept(Visitor<R> visitor) {        
      return visitor.visitSetExpr(this);      
    }                                         

    final Expr object;                        
    final Token name;                         
    final Expr value;                         
  }                                           
lox/Expr.java, nest inside class Expr

A2 . 1 . 9 Super expression

The super keyword is introduced in “Inheritance”:

lox/Expr.java
nest inside class Expr
  static class Super extends Expr {       
    Super(Token keyword, Token method) {  
      this.keyword = keyword;             
      this.method = method;               
    }

    <R> R accept(Visitor<R> visitor) {    
      return visitor.visitSuperExpr(this);
    }                                     

    final Token keyword;                  
    final Token method;                   
  }                                       
lox/Expr.java, nest inside class Expr

A2 . 1 . 10 This expression

The this keyword is introduced in “Classes”:

lox/Expr.java
nest inside class Expr
  static class This extends Expr {       
    This(Token keyword) {                
      this.keyword = keyword;            
    }

    <R> R accept(Visitor<R> visitor) {   
      return visitor.visitThisExpr(this);
    }                                    

    final Token keyword;                 
  }                                      
lox/Expr.java, nest inside class Expr

A2 . 1 . 11 Unary expression

Unary operators are introduced in “Representing Code”:

lox/Expr.java
nest inside class Expr
  static class Unary extends Expr {       
    Unary(Token operator, Expr right) {   
      this.operator = operator;           
      this.right = right;                 
    }

    <R> R accept(Visitor<R> visitor) {    
      return visitor.visitUnaryExpr(this);
    }                                     

    final Token operator;                 
    final Expr right;                     
  }                                       
lox/Expr.java, nest inside class Expr

A2 . 1 . 12 Variable expression

Variable access expressions are introduced in “Statements and State”:

lox/Expr.java
nest inside class Expr
  static class Variable extends Expr {       
    Variable(Token name) {                   
      this.name = name;                      
    }

    <R> R accept(Visitor<R> visitor) {       
      return visitor.visitVariableExpr(this);
    }                                        

    final Token name;                        
  }                                          
lox/Expr.java, nest inside class Expr

A2 . 2 Statements

Statements form a second hierarchy of syntax tree nodes independent of expressions. We add the first couple of them in “Statements and State”.

lox/Stmt.java
create new file
package com.craftinginterpreters.lox;

import java.util.List;                      

abstract class Stmt {                       
  interface Visitor<R> {                    
    R visitBlockStmt(Block stmt);           
    R visitClassStmt(Class stmt);           
    R visitExpressionStmt(Expression stmt); 
    R visitFunctionStmt(Function stmt);     
    R visitIfStmt(If stmt);                 
    R visitPrintStmt(Print stmt);           
    R visitReturnStmt(Return stmt);         
    R visitVarStmt(Var stmt);               
    R visitWhileStmt(While stmt);           
  }                                         

  // Nested Stmt classes here...            

  abstract <R> R accept(Visitor<R> visitor);
}                                           
lox/Stmt.java, create new file

A2 . 2 . 1 Block statement

Curly-braced block statements to define local scopes are introduced in “Statements and State”:

lox/Stmt.java
nest inside class Stmt
  static class Block extends Stmt {       
    Block(List<Stmt> statements) {        
      this.statements = statements;       
    }

    <R> R accept(Visitor<R> visitor) {    
      return visitor.visitBlockStmt(this);
    }                                     

    final List<Stmt> statements;          
  }                                       
lox/Stmt.java, nest inside class Stmt

A2 . 2 . 2 Class statement

Class declaration statements are introduced in, unsurprisingly, “Classes”:

lox/Stmt.java
nest inside class Stmt
  static class Class extends Stmt {       
    Class(Token name,                     
          Expr.Variable superclass,       
          List<Stmt.Function> methods) {  
      this.name = name;                   
      this.superclass = superclass;       
      this.methods = methods;             
    }

    <R> R accept(Visitor<R> visitor) {    
      return visitor.visitClassStmt(this);
    }                                     

    final Token name;                     
    final Expr.Variable superclass;       
    final List<Stmt.Function> methods;    
  }                                       
lox/Stmt.java, nest inside class Stmt

A2 . 2 . 3 Expression statement

Expression statements are introduced in “Statements and State”:

lox/Stmt.java
nest inside class Stmt
  static class Expression extends Stmt {       
    Expression(Expr expression) {              
      this.expression = expression;            
    }

    <R> R accept(Visitor<R> visitor) {         
      return visitor.visitExpressionStmt(this);
    }                                          

    final Expr expression;                     
  }                                            
lox/Stmt.java, nest inside class Stmt

A2 . 2 . 4 Function statement

Function declaration statements are introduced in, you guessed it, “Functions”:

lox/Stmt.java
nest inside class Stmt
  static class Function extends Stmt {                         
    Function(Token name, List<Token> params, List<Stmt> body) {
      this.name = name;                                        
      this.params = params;                                    
      this.body = body;                                        
    }

    <R> R accept(Visitor<R> visitor) {                         
      return visitor.visitFunctionStmt(this);                  
    }                                                          

    final Token name;                                          
    final List<Token> params;                                  
    final List<Stmt> body;                                     
  }                                                            
lox/Stmt.java, nest inside class Stmt

A2 . 2 . 5 If statement

If statements are introduced in “Control Flow”:

lox/Stmt.java
nest inside class Stmt
  static class If extends Stmt {                          
    If(Expr condition, Stmt thenBranch, Stmt elseBranch) {
      this.condition = condition;                         
      this.thenBranch = thenBranch;                       
      this.elseBranch = elseBranch;                       
    }

    <R> R accept(Visitor<R> visitor) {                    
      return visitor.visitIfStmt(this);                   
    }                                                     

    final Expr condition;                                 
    final Stmt thenBranch;                                
    final Stmt elseBranch;                                
  }                                                       
lox/Stmt.java, nest inside class Stmt

A2 . 2 . 6 Print statement

Print statements are introduced in “Statements and State”:

lox/Stmt.java
nest inside class Stmt
  static class Print extends Stmt {       
    Print(Expr expression) {              
      this.expression = expression;       
    }

    <R> R accept(Visitor<R> visitor) {    
      return visitor.visitPrintStmt(this);
    }                                     

    final Expr expression;                
  }                                       
lox/Stmt.java, nest inside class Stmt

A2 . 2 . 7 Return statement

You need a function to return from, so return statements are introduced in “Functions”:

lox/Stmt.java
nest inside class Stmt
  static class Return extends Stmt {       
    Return(Token keyword, Expr value) {    
      this.keyword = keyword;              
      this.value = value;                  
    }

    <R> R accept(Visitor<R> visitor) {     
      return visitor.visitReturnStmt(this);
    }                                      

    final Token keyword;                   
    final Expr value;                      
  }                                        
lox/Stmt.java, nest inside class Stmt

A2 . 2 . 8 Variable statement

Variable declaration statements are introduced in “Statements and State”:

lox/Stmt.java
nest inside class Stmt
  static class Var extends Stmt {       
    Var(Token name, Expr initializer) { 
      this.name = name;                 
      this.initializer = initializer;   
    }

    <R> R accept(Visitor<R> visitor) {  
      return visitor.visitVarStmt(this);
    }                                   

    final Token name;                   
    final Expr initializer;             
  }                                     
lox/Stmt.java, nest inside class Stmt

A2 . 2 . 9 While statement

While statements are introduced in “Control Flow”:

lox/Stmt.java
nest inside class Stmt
  static class While extends Stmt {       
    While(Expr condition, Stmt body) {    
      this.condition = condition;         
      this.body = body;                   
    }

    <R> R accept(Visitor<R> visitor) {    
      return visitor.visitWhileStmt(this);
    }                                     

    final Expr condition;                 
    final Stmt body;                      
  }                                       
lox/Stmt.java, nest inside class Stmt