Skip to content

Commit

Permalink
Do not create new scope for each line of code in REPL. (there might b…
Browse files Browse the repository at this point in the history
…e a better way to do this?)
  • Loading branch information
prakashk committed May 6, 2013
1 parent df4800a commit 4b171a7
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/org/moe/Moe.scala
Expand Up @@ -173,7 +173,7 @@ object Moe {
case _ => if (isReplCommand(line))
replOptions = processReplCommand(line, replOptions)
else
evalLine(interpreter, runtime, partialInput + line, replOptions) match {
evalLine(interpreter, runtime, partialInput + line, replOptions + ("inREPL" -> true)) match {
case EvalResult.Partial => partialInput += line
case _ => partialInput = ""
}
Expand All @@ -185,7 +185,7 @@ object Moe {
try {
val nodes = MoeParser.parseFromEntry(line)
val ast = CompilationUnitNode(
ScopeNode(nodes)
ScopeNode(nodes, !options.getOrElse("inREPL", false))
)
if (options.getOrElse("dumpAST", false)) {
if (options.getOrElse("prettyPrintAST", false))
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/moe/ast/AST.scala
Expand Up @@ -7,7 +7,7 @@ abstract class AST
// AST containers

case class CompilationUnitNode(body: ScopeNode) extends AST
case class ScopeNode(body: StatementsNode) extends AST
case class ScopeNode(body: StatementsNode, makeNewEnv: Boolean = true) extends AST

case class StatementsNode(nodes: List[AST]) extends AST

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/moe/ast/Serializer.scala
Expand Up @@ -5,7 +5,7 @@ import scala.util.parsing.json._
object Serializer {
def toJSON(ast: AST): Any = ast match {
case CompilationUnitNode(body) => JSONObject(Map("CompilationUnitNode" -> toJSON(body)))
case ScopeNode(body) => JSONObject(Map("ScopeNode" -> toJSON(body)))
case ScopeNode(body, _) => JSONObject(Map("ScopeNode" -> toJSON(body)))
case StatementsNode(nodes) => JSONObject(Map("StatementsNode" -> JSONArray(nodes.map(toJSON(_)))))

case IntLiteralNode(value) => JSONObject(Map("IntLiteralNode" -> value))
Expand Down
Expand Up @@ -9,11 +9,12 @@ object CompilationUnits {

def apply (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
case (env, CompilationUnitNode(body)) => i.eval(r, env, body)
case (env, ScopeNode(body)) => i.compile_and_evaluate(new MoeEnvironment(Some(env)), body)
case (env, ScopeNode(body, false)) => i.compile_and_evaluate(env, body)
case (env, ScopeNode(body, true)) => i.compile_and_evaluate(new MoeEnvironment(Some(env)), body)
case (env, StatementsNode(nodes)) => {
nodes.foldLeft[MoeObject](r.NativeObjects.getUndef)(
(_, node) => i.compile_and_evaluate(env, node)
)
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/scala/org/moe/interpreter/guts/Utils.scala
Expand Up @@ -60,7 +60,7 @@ trait Utils {
callback(ast)
ast match {
case CompilationUnitNode(body) => walkAST(body, callback)
case ScopeNode(body) => walkAST(body, callback)
case ScopeNode(body, _) => walkAST(body, callback)
case StatementsNode(nodes) => nodes.foreach(walkAST(_, callback))

case PairLiteralNode(key, value) => {
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/org/moe/parser/SpecTestSuite.scala
Expand Up @@ -42,7 +42,7 @@ class SpecTestSuite extends FunSuite with BeforeAndAfter with ParserTestUtils {
try {
basicAST(MoeParser.parseStuff(moe_content)) match {
case CompilationUnitNode(scope) => scope match {
case ScopeNode(stmts) => {
case ScopeNode(stmts, _) => {
assert(Serializer.toJSON(stmts).toString().trim === ast_content.trim)
}
}
Expand Down

0 comments on commit 4b171a7

Please sign in to comment.