Skip to content

Commit

Permalink
getting a reference to a named sub now works
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevan Little committed Apr 10, 2013
1 parent 18c8f04 commit 30ce065
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/main/scala/org/moe/interpreter/Interpreter.scala
Expand Up @@ -489,13 +489,29 @@ class Interpreter {

// TODO context etc
case VariableAccessNode(name) => env.get(name).getOrElse(
throw new MoeErrors.VariableNotFound(name)
if (name.startsWith("&")) {
val function_name = name.drop(1)
val sub = runtime.lookupSubroutine(
function_name,
env.getCurrentPackage.getOrElse(
throw new MoeErrors.PackageNotFound("__PACKAGE__")
)
).getOrElse(
throw new MoeErrors.SubroutineNotFound(function_name)
)
if (!sub.hasAssociatedClass) sub.setAssociatedClass(runtime.getCoreClassFor("Code"))
sub
} else {
throw new MoeErrors.VariableNotFound(name)
}
)

case VariableAssignmentNode(name, expression) => {
env.set(name, eval(runtime, env, expression)).getOrElse(
throw new MoeErrors.VariableNotFound(name)
)
}

case VariableDeclarationNode(name, expression) => {
env.create(name, eval(runtime, env, expression)).get
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/scala/org/moe/parser/AnonCodeTestSuite.scala
Expand Up @@ -42,4 +42,14 @@ class AnonCodeTestSuite extends FunSuite with BeforeAndAfter with ParserTestUtil
result.unboxToInt.get should equal (30)
}

test("... a non-anon sub taken as a reference") {
val result = interpretCode("sub add($x, $y) { $x + $y }; &add.(1, 2)")
result.unboxToInt.get should equal (3)
}

test("... a non-anon sub taken as a reference w/ apply") {
val result = interpretCode("sub add($x, $y) { $x + $y }; &add.apply([1, 2])")
result.unboxToInt.get should equal (3)
}

}

0 comments on commit 30ce065

Please sign in to comment.