problems in the domain of language processing. Problem Domain-specific language Structure representation Algebraic data types Text to structure Context-free grammars Output Pretty printing Transformation Term rewriting Decoration Attribute grammars Aim: to bring these high-level processing mechanisms closer to real programmers.
: List [(JName ,JValue)]) extends JValue case class JName (s : String) case class JArray (values: Vector[JValue ]) extends JValue case class JString (value : String) extends JValue case class JNumber (value : Double) extends JValue case class JTrue () extends JValue case class JFalse () extends JValue case class JNull () extends JValue
: List[Dept ]) extends Node case class Dept (n : Name , m : Manager , su : List[SubUnit ]) extends Node type Manager = Employee case class Employee (n : Name , a : Address , s : Salary) extends Node abstract class SubUnit extends Node case class PU (e : Employee) extends SubUnit case class DU (d : Dept) extends SubUnit type Name = String type Address = String type Salary = Double
traversal patterns to satisfy those dependencies, and attribute storage. Figure: An attribute of a tree node depends on attributes of other nodes Solution: functional attribute definitions with memoisation.
==> Int = attr { case Company (ds) => (ds map numemp).sum case Dept (_, _, sus) => 1 + (sus map numemp).sum case DU (d) => d- >numemp case _ => 1 } val salary : Node ==> Double = attr { case Company (ds) => (ds map salary).sum case Dept (_, Employee (_, _, s), sus) => s + (sus map salary).sum case PU (e) => e- >salary case DU (d) => d- >salary case Employee (_, _, s) => s }
= attr { case n if n isRoot => Double.MaxValue case Dept (_, m, _) => m- >salary case e : Employee => e.parent[Node] match { case p @ Dept (_, m, _) if m eq e => // Avoid comparing manager ’s salary with itself p.parent[Node]->bosssalary case p => p- >bosssalary } }
case class instance with new children without using reflection. def dup[T <: Product] ( t : T, children : Array[AnyRef] ) : T For example, if Foo is a case class with two constructor parameters dup (Foo (a, b), Array (c, d)) should return Foo (c, d)