This page is no longer maintained — Please continue to the home page at www.scala-lang.org

scala source model in scala?

1 reply
Meredith Gregory
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Scalads and lasses,

Is there a scala model of scala source syntax?

Best wishes,

--greg

--
L.G. Meredith
Managing Partner
Biosimilarity LLC
806 55th St NE
Seattle, WA 98105

+1 206.650.3740

http://biosimilarity.blogspot.com
Johannes Rudolph
Joined: 2008-12-17,
User offline. Last seen 29 weeks 20 hours ago.
Re: scala source model in scala?

Hi,

On Sat, Feb 28, 2009 at 6:56 AM, Meredith Gregory
wrote:
> Scalads and lasses,
>
> Is there a scala model of scala source syntax?
There surely is an AST in the compiler [1], but I'm not sure, if
that's what you are looking for?!?

You might as well mean, if there is a AST equivalent available in the
Scala library and at runtime. Then you might want to look at the
classes in scala.reflect.Code, which are undocumented up to the point
that they are not even mentioned any more in the most recent scaladocs
[2]. It still works, though and the (probably outdated?)
"documentation" can still be found at [3]. See the appended scala
console session. Note, that the AST found in scala.reflect.Code is not
calculated at runtime by parsing the code, but lifted at compile-time.
Aside from that, you can crash the compiler with ease when lifting
given a complex enough expression (such as trying to define an local
method).

Johannes

[1] https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/compiler/scal...
[2] http://www.scala-lang.org/docu/files/api/index.html
[3] http://scala-tools.org/scaladocs/scala-library/2.7.1/

Welcome to Scala version 2.7.3.final (OpenJDK Server VM, Java 1.6.0_0).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.reflect.Code
import scala.reflect.Code

scala> Code.lift(5+3)
res0: scala.reflect.Code[Int] = scala.reflect.Code@1371566

scala> Code.lift(5+3).tree
res1: scala.reflect.Tree = Literal(8)

scala> Code.lift((x:Int) => x + 3).tree
res3: scala.reflect.Tree =
Function(List(LocalValue(NoSymbol,x,PrefixedType(ThisType(Class(scala)),Class(scala.Int)))),Apply(Select(Ident(LocalValue(NoSymbol,x,PrefixedType(ThisType(Class(scala)),Class(scala.Int)))),Method(scala.Int.$plus,MethodType(List(PrefixedType(ThisType(Class(scala)),Class(scala.Int))),PrefixedType(ThisType(Class(scala)),Class(scala.Int))))),List(Literal(3))))

scala> val x:Code[Int=>String] = y => y.toString
x: scala.reflect.Code[(Int) => String] = scala.reflect.Code@110195c

scala> x.tree
res4: scala.reflect.Tree =
Function(List(LocalValue(NoSymbol,y,PrefixedType(ThisType(Class(scala)),Class(scala.Int)))),Apply(Select(Ident(LocalValue(NoSymbol,y,PrefixedType(ThisType(Class(scala)),Class(scala.Int)))),Method(scala.Any.toString,MethodType(List(),PrefixedType(ThisType(Class(java.lang)),Class(java.lang.String))))),List()))

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland