summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-12-06 22:25:50 +0000
committerPaul Phillips <paulp@improving.org>2009-12-06 22:25:50 +0000
commite1afd5b3230b70d8be9e299080fd15b358365b91 (patch)
tree0f2fa7fc2f1c9d8eb1960a795105d2686fd3cd7f /src/compiler/scala/tools
parent4532a5d7f1cd6bf11e2658843eabc49102a3a1b4 (diff)
downloadscala-e1afd5b3230b70d8be9e299080fd15b358365b91.tar.gz
scala-e1afd5b3230b70d8be9e299080fd15b358365b91.tar.bz2
scala-e1afd5b3230b70d8be9e299080fd15b358365b91.zip
Making it easier to explore the AST. Example:
scala> :power scala> val t = mkTree("def bip(x: Float) = x.toInt") t: interpreter.compiler.Tree = def bip(x: Float) = x.toInt
Diffstat (limited to 'src/compiler/scala/tools')
-rw-r--r--src/compiler/scala/tools/nsc/Interpreter.scala22
1 files changed, 14 insertions, 8 deletions
diff --git a/src/compiler/scala/tools/nsc/Interpreter.scala b/src/compiler/scala/tools/nsc/Interpreter.scala
index 77436fe55f..1f3518daf7 100644
--- a/src/compiler/scala/tools/nsc/Interpreter.scala
+++ b/src/compiler/scala/tools/nsc/Interpreter.scala
@@ -79,7 +79,8 @@ class Interpreter(val settings: Settings, out: PrintWriter)
import compiler.{ Traverser, CompilationUnit, Symbol, Name, Type }
import compiler.{
Tree, TermTree, ValOrDefDef, ValDef, DefDef, Assign, ClassDef,
- ModuleDef, Ident, Select, TypeDef, Import, MemberDef, DocDef }
+ ModuleDef, Ident, Select, TypeDef, Import, MemberDef, DocDef,
+ EmptyTree }
import compiler.{ nme, newTermName }
import nme.{
INTERPRETER_VAR_PREFIX, INTERPRETER_SYNTHVAR_PREFIX, INTERPRETER_LINE_PREFIX,
@@ -363,6 +364,11 @@ class Interpreter(val settings: Settings, out: PrintWriter)
}
}
+ /** For :power - create trees and type aliases from code snippets. */
+ def mkTree(code: String): Tree = mkTrees(code).headOption getOrElse EmptyTree
+ def mkTrees(code: String): List[Tree] = parse(code) getOrElse Nil
+ def mkType(name: String, what: String) = interpret("type " + name + " = " + what)
+
/** Compile an nsc SourceFile. Returns true if there are
* no compilation errors, or false othrewise.
*/
@@ -823,17 +829,17 @@ class Interpreter(val settings: Settings, out: PrintWriter)
def powerUser(): String = {
beQuietDuring {
- val mkTypeCmd =
- """def mkType(name: String, what: String) = interpreter.interpret("type " + name + " = " + what)"""
-
this.bind("interpreter", "scala.tools.nsc.Interpreter", this)
- interpret(mkTypeCmd)
+ this.bind("global", "scala.tools.nsc.Global", compiler)
+ interpret("""import interpreter.{ mkType, mkTree, mkTrees }""")
}
"""** Power User mode enabled - BEEP BOOP **
- |** New vals! Try interpreter.<tab> **
- |** New defs! Try mkType("T", "String") **
- |** New cmds! :help to discover them **""".stripMargin
+ |** New vals! Try interpreter, global **
+ |** New cmds! :help to discover them **
+ |** New defs! Give these a whirl: **
+ |** mkType("Fn", "(String, Int) => Int") **
+ |** mkTree("def f(x: Int, y: Int) = x+y") **""".stripMargin
}
def nameOfIdent(line: String): Option[Name] = {