Dynamic code creation

61 views
Skip to first unread message

Oliver Ruebenacker

unread,
Nov 18, 2013, 4:50:01 PM11/18/13
to treehugg...@googlegroups.com

     Hello,

  I have a list List("a", "b", "c", ...) and I want to use TreeHugger to create code such as:

  object MyObject {
    val a = f("a")
    val b = f("b")
    val c = f("c")
    ...
  }

  The list is not known at compile time, so I can not hard-code the members of MyObject. How can I do this programmatically? The only examples I can find are completely static DSL. The ScalaDoc has no explanations and to understand how the DSL works behind the scenes is extremely hard.

  Thanks!
    
     Best,
     Oliver

--
Oliver Ruebenacker
Director of Computational Biology at PanGenX (http://www.pangenx.com)
Be always grateful, but never satisfied.

eugene yokota

unread,
Nov 18, 2013, 5:49:52 PM11/18/13
to treehugg...@googlegroups.com
I'm a bit confused. The entire library of treehugger is about generating Scala source code dynamically.
The examples in the doc (http://eed3si9n.com/treehugger/) tends to be static, since they are examples.

Many of the constructs documented with "..." accept vararg, including BLOCK.
To make MyObject, you first need OBJECTDEF described here: http://eed3si9n.com/treehugger/object.html

import treehugger.forest._
import definitions._
import treehuggerDSL._
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_51).
Type in expressions to have them evaluated.
Type :help for more information.

scala> OBJECTDEF("MyObject") := BLOCK(
     |   LIT(0)
     | )
res0: treehugger.forest.ModuleDef = ModuleDef(Modifiers(, , Map()),MyObject,Template(List(),ValDef(Modifiers(private, , Map()),Ident(_),EmptyTree),List(Literal(Constant(0)))))

scala> treeToString(res0)
res1: String = 
object MyObject {
  0
}

We can define a function that turns your List[String] dynamically:

scala> def foo(valStrings: List[String]): Tree =
     |   OBJECTDEF("MyObject") := BLOCK(valStrings map { x =>
     |     VAL(x) := LIT(0)
     |   }: _*)
foo: (valStrings: List[String])treehugger.forest.Tree

scala> treeToString(foo(List("a", "b", "c")))
res2: String = 
object MyObject {
  val a = 0
  val b = 0
  val c = 0
}

Finally you have a function application: http://eed3si9n.com/treehugger/apply.html

scala> def foo(valStrings: List[String]): Tree =
     |   OBJECTDEF("MyObject") := BLOCK(valStrings map { x =>
     |     VAL(x) := REF("f") APPLY (LIT(x))
     |   }: _*)
foo: (valStrings: List[String])treehugger.forest.Tree

scala> treeToString(foo(List("a", "b", "c")))
res3: String = 
object MyObject {
  val a = f("a")
  val b = f("b")
  val c = f("c")
}

Hope this helps.

-eugene


--
You received this message because you are subscribed to the Google Groups "treehugger-scala" group.
To unsubscribe from this group and stop receiving emails from it, send an email to treehugger-sca...@googlegroups.com.
To post to this group, send email to treehugg...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/treehugger-scala/CAA%3DX4OA01qW23d%3DvyEoC1RZEHgk07R7D0eiipry9OxtcLr0gvQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages