Skip to content

Commit

Permalink
Pass arguments into subroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmay committed Feb 6, 2013
1 parent 17532f6 commit 8224f91
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 21 deletions.
12 changes: 10 additions & 2 deletions src/main/scala/org/moe/interpreter/Interpreter.scala
Expand Up @@ -230,10 +230,18 @@ class Interpreter {
case MethodDeclarationNode(name, params, body) => stub
// TODO: handle arguments
case SubroutineDeclarationNode(name, params, body) => {
scoped { newEnv =>
scoped { sub_env =>
val sub = new MoeSubroutine(
name,
params => eval(runtime, newEnv, body)
args => {
// TODO make a run through after parse-time to check
// for argument counts at the call sites
val param_pairs = params zip args
param_pairs.foreach({ case (param, arg) =>
sub_env.create(param, arg)
})
eval(runtime, sub_env, body)
}
)
env.getCurrentPackage.getOrElse(
throw new MoeErrors.PackageNotFound("__PACKAGE__")
Expand Down
26 changes: 7 additions & 19 deletions src/test/scala/org/moe/interpreter/PackageNodeTestSuite.scala
Expand Up @@ -6,8 +6,7 @@ import org.moe.runtime._
import org.moe.ast._

class PackageNodeTestSuite extends FunSuite with InterpreterTestUtils {

test("... basic test with package and closure") {
test("... basic test with package and single sub") {
// package Foo { my $foo = 0; sub add_foo { $foo++ }
// add_foo(); add_foo(); add_foo(); $foo }
val ast = wrapSimpleAST(
Expand All @@ -16,30 +15,19 @@ class PackageNodeTestSuite extends FunSuite with InterpreterTestUtils {
"Foo",
StatementsNode(
List(
VariableDeclarationNode(
"$foo",
IntLiteralNode(0)
),
SubroutineDeclarationNode(
"add_foo",
"my_sub",
List(),
StatementsNode(
List(
IncrementNode(VariableAccessNode("$foo"))
)
)
),
SubroutineCallNode("add_foo", List()),
SubroutineCallNode("add_foo", List()),
SubroutineCallNode("add_foo", List()),
VariableAccessNode("$foo")
StatementsNode(List())
)
)
)
)
)
)
val result = interpreter.eval(runtime, runtime.getRootEnv, ast)
assert(result.asInstanceOf[MoeIntObject].getNativeValue === 3)
interpreter.eval(runtime, runtime.getRootEnv, ast)
assert(runtime.getRootPackage.hasSubPackage("Foo"), "core package has package Foo")
assert(runtime.getRootPackage.getSubPackage("Foo").exists(_.hasSubroutine("my_sub")), "Foo has sub my_sub")
}

}
71 changes: 71 additions & 0 deletions src/test/scala/org/moe/interpreter/SubroutineNodeTestSuite.scala
@@ -0,0 +1,71 @@
package org.moe.interpreter

import org.scalatest.FunSuite

import org.moe.runtime._
import org.moe.ast._

class SubroutineNodeTestSuite extends FunSuite with InterpreterTestUtils {

test("... basic test with one-arg sub") {
// package Foo { sub inc($n) { ++$n }
// my $n = 1; inc(inc(n)) }
val ast = wrapSimpleAST(
List(
SubroutineDeclarationNode(
"inc",
List("$n"),
StatementsNode(
List(
// TODO just add one when we have addition
IncrementNode(VariableAccessNode("$n"))
)
)
),
VariableDeclarationNode("$n", IntLiteralNode(1)),
SubroutineCallNode(
"inc",
List(
SubroutineCallNode(
"inc",
List(
VariableAccessNode("$n")
)
)
)
)
)
)
val result = interpreter.eval(runtime, runtime.getRootEnv, ast)
assert(result.asInstanceOf[MoeIntObject].getNativeValue === 3)
}

test("... basic test with closure") {
// package Foo { my $foo = 0; sub add_foo { $foo++ }
// add_foo(); add_foo(); add_foo(); $foo }
val ast = wrapSimpleAST(
List(
VariableDeclarationNode(
"$foo",
IntLiteralNode(0)
),
SubroutineDeclarationNode(
"add_foo",
List(),
StatementsNode(
List(
IncrementNode(VariableAccessNode("$foo"))
)
)
),
SubroutineCallNode("add_foo", List(VariableAccessNode("$foo"))),
SubroutineCallNode("add_foo", List(VariableAccessNode("$foo"))),
SubroutineCallNode("add_foo", List(VariableAccessNode("$foo"))),
VariableAccessNode("$foo")
)
)
val result = interpreter.eval(runtime, runtime.getRootEnv, ast)
assert(result.asInstanceOf[MoeIntObject].getNativeValue === 3)
}

}

0 comments on commit 8224f91

Please sign in to comment.