I heard you like code, so… 

treehugger lets you treat Scala code as data (case classes), and data as code (treehugger DSL) so you can code while you code.

For example, here’s treehugger DSL for Hello World:

scala> import treehugger.forest._, definitions._, treehuggerDSL._
import treehugger.forest._
import definitions._
import treehuggerDSL._

scala> val tree: Tree = Predef_println APPLY LIT("Hello, world!")
tree: treehugger.forest.Tree = Apply(Ident(println),List(Literal(Constant(Hello, world!))))

scala> treeToString(tree)
res0: String = println("Hello, world!")

The result of the tree shows the structure, and treeToString(tree) shows the Scala source code represented by the tree. Because it is responsible for formatting of the String, your code generating code would be more clear using treehugger.

Classy example 

Let’s omit the import statements, and println statements from here. Here’s how treehugger DSL defines classes:

scala> :paste
// Entering paste mode (ctrl-D to finish)
object sym {
  val IntQueue = RootClass.newClass("IntQueue")
  val BasicIntQueue = RootClass.newClass("BasicIntQueue")
  val buf = BasicIntQueue.newValue("buf")
}

val tree1 = CLASSDEF(sym.IntQueue) withFlags(Flags.ABSTRACT) := BLOCK(
  DEF("get", IntClass),
  PROC("put") withParams(PARAM("x", IntClass))
)

val tree2 = CLASSDEF(sym.BasicIntQueue) withParents(sym.IntQueue) := BLOCK(
  VAL(sym.buf) withFlags(Flags.PRIVATE) :=
    NEW(ArrayBufferClass TYPE_OF IntClass),
  DEF("get", IntClass) := REF(sym.buf) DOT "remove" APPLY(),
  PROC("put") withParams(PARAM("x", IntClass)) := BLOCK(
    REF(sym.buf) INFIX("+=") APPLY REF("x")
  )
)

// Exiting paste mode, now interpreting.

defined module sym
tree1: treehugger.forest.ClassDef = ClassDef(Modifiers(abstract, , Map()),Modifiers(, , Map()),IntQueue,List(),List(),Template(List(),ValDef(Modifiers(private, , Map()),Ident(_),EmptyTree),List(DefDef(Modifiers(, , Map()),get,List(),List(),TypeTree(),EmptyTree), ProcDef(Modifiers(, , Map()),put,List(),List(List(ValDef(Modifiers(<param>, , Map()),Typed(Ident(x),TypeTree()),EmptyTree))),EmptyTree))))
tree2: treehugger.forest.ClassDef = ClassDef(Modifiers(, , Map()),Modifiers(, , Map()),BasicIntQueue,List(),List(),Template(List(TypeTree()),ValDef(Modifiers(private, , Map()),Ident(_),EmptyTree),List(ValDef(Modifiers(private, , Map()),Ident(buf),New(TypeTree())), DefDef(Modifiers(, , Map()),get,List(),List(),TypeTree(),Apply(Select(Ident(buf),remove),List())), ProcDef(Modi...

Let’s print them out:

scala> treeToString(tree1)
res1: String = 
abstract class IntQueue {
  def get: Int
  def put(x: Int)
}

scala> treeToString(tree2)
res2: String = 
class BasicIntQueue extends IntQueue {
  private val buf = new scala.collection.mutable.ArrayBuffer[Int]
  def get: Int = buf.remove()
  def put(x: Int) {
    buf += x
  }
}

Notice some of the symbols were defined upfront so we could avoid typing in string names.

Thanks scalac! 

To be clear where the credit is due, the majority of treehugger’s code is borrowed from the Scala compiler (scalac) written by Martin Odersky, Paul Phillips, and others. treehugger just took it further to fit its needs.