Prettyprinting arithmetic expressions

Skip to first unread message

Jerry Swan

Feb 6, 2013, 9:54:53 AM2/6/13
Hi Eugene,
Treehugger certainly looks like the product of a lot of hours effort!
Does it provide any support for turning arithmetic operations back into their source code form?
For example, if I start with
val inputExpr = "1 + ( 2 * 3 )"

val expr = toolbox.parse( inputExpr )

then the corresponding Scala reflection Tree prints out as:

which AFAICS is also what is returned by treehugger's treeToString() method, whearas what I'd obviously like to be able to do is to recover inputExpr.
Best wishes,



eugene yokota

Feb 6, 2013, 7:54:19 PM2/6/13
The quick answer is "not really.."
treehugger.scala is a fork of earlier version of Scala 2.10 code that provides some its power as library in Scala 2.8 and 2.9.
Since I have expanded tree to incorporate syntax sugars, treehugger trees are no longer compatible with Scalac 2.10 trees.

As a proof of concept I did start a bridge project a while back to demonstrate conversion between the two, but I didn't complete it:

Another issue is the lossy parsing. Since Scalac tree's job is to output bytecode, and not interested in how it used to look in source code,
it throws away things like method infix notation and translates your "1 + ( 2 * 3 )" into "1.$plus(2.$times(3))".
So even if I had implemented tree conversion, it's still not going to recover the infix notation from the original inputExpr,
whereas if you wrote it in treehugger DSL it's able to express that.

So, to be able to parse "1 + ( 2 * 3 )", treehugger would need to provide its own parsing method, which is kind of sketchy and something I didn't see a big return.



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
To post to this group, send email to
To view this discussion on the web visit
For more options, visit

Reply all
Reply to author
0 new messages