next up previous contents
Next: 4.3 Building Abstract Syntax Up: 4 Abstract Syntax Previous: 4.1 Building Abstract Syntax   Contents

4.2 Building Abstract Syntax Trees in Scala

The previous example of expression ASTs can be written as follows in Scala using case classes:

sealed abstract class Exp
case class IntegerExp ( value: Int ) extends Exp
case class StringExp ( value: String ) extends Exp
case class VariableExp ( name: String ) extends Exp
case class BinaryExp ( operator: String, left: Exp, right: Exp ) extends Exp
case class UnaryExp ( operator: String, operand: Exp ) extends Exp
case class CallExp ( name: String, arguments: List[Exp] ) extends Exp
case class ProjectionExp ( record: Exp, attribute: String ) extends Exp
case class RecordExp ( arguments: List[(String,Exp)] ) extends Exp
For example,
BinaryExp("+",BinaryExp("-",VariableExp("x"),IntegerExp(2)),IntegerExp(3))
constructs the AST for the input (x-2)+3.



2015-01-20