Prettyprinting arithmetic expressions

26 views
Skip to first unread message

Jerry Swan

unread,
Feb 6, 2013, 9:54:53 AM2/6/13
to treehugg...@googlegroups.com
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:

"1.$plus(2.$times(3))"
 
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,
 
Jerry.

 

 

eugene yokota

unread,
Feb 6, 2013, 7:54:19 PM2/6/13
to treehugg...@googlegroups.com
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.

-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/msg/treehugger-scala/-/jw6RGFIp7MUJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages