Skip to content

Commit

Permalink
adjusting all the code to work with the refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevan Little committed Feb 24, 2013
1 parent 818e04a commit 191dff1
Show file tree
Hide file tree
Showing 21 changed files with 167 additions and 125 deletions.
128 changes: 68 additions & 60 deletions src/main/scala/org/moe/interpreter/Interpreter.scala
Expand Up @@ -281,78 +281,92 @@ class Interpreter {
}
}

// TODO: constructor overloading
case ConstructorDeclarationNode(params, body) => {
case ParameterNode(name) => new MoeParameter(name)
case SignatureNode(params) => new MoeSignature(
params.map(eval(runtime, env, _).asInstanceOf[MoeParameter])
)

case ConstructorDeclarationNode(signature, body) => {
val klass = env.getCurrentClass.getOrElse(
throw new MoeErrors.ClassNotFound("__CLASS__")
)
throwForUndeclaredVars(env, params, body)
scoped { constructor_env =>
val method = new MoeMethod(
"new",
(invocant, args) => {
val param_pairs = params zip args
param_pairs.foreach({ case (param, arg) =>
constructor_env.create(param, arg)
})
val instance = klass.newInstance
constructor_env.setCurrentInvocant(instance)
eval(runtime, constructor_env, body)
instance
}
)
env.getCurrentClass.getOrElse(
throw new MoeErrors.ClassNotFound("__CLASS__")
).addMethod(method)
method
}
val sig = eval(runtime, env, signature).asInstanceOf[MoeSignature]
throwForUndeclaredVars(env, sig, body)

val constructor = new MoeMethod(
name = "BUILD",
signature = sig,
declaration_env = env,
body = (e) => eval(runtime, e, body)
)
klass.setConstructor(Some(constructor))

val new_method = new MoeMethod(
name = "new",
signature = sig,
declaration_env = env,
body = {
(env) =>
val c = env.getCurrentInvocant.get.asInstanceOf[MoeClass]
val i = c.newInstance
val e = new MoeEnvironment(Some(env))
e.setCurrentInvocant(i)
c.getConstructor.map(_.executeBody(e))
i
}
)
klass.addMethod(new_method)

constructor
}
case DestructorDeclarationNode(params, body) => stub

case MethodDeclarationNode(name, params, body) => {
case DestructorDeclarationNode(signature, body) => {
val klass = env.getCurrentClass.getOrElse(
throw new MoeErrors.ClassNotFound("__CLASS__")
)
throwForUndeclaredVars(env, params, body)
scoped { method_env =>
val method = new MoeMethod(
name,
(invocant, args) => {
val param_pairs = params zip args
param_pairs.foreach({ case (param, arg) =>
method_env.create(param, arg)
})
method_env.setCurrentInvocant(invocant)
eval(runtime, method_env, body)
}
)
env.getCurrentClass.getOrElse(
throw new MoeErrors.ClassNotFound("__CLASS__")
).addMethod(method)
method
}
val sig = eval(runtime, env, signature).asInstanceOf[MoeSignature]
throwForUndeclaredVars(env, sig, body)
val destructor = new MoeMethod(
name = "DEMOLISH",
signature = sig,
declaration_env = env,
body = (e) => eval(runtime, e, body)
)
klass.setDestructor(Some(destructor))
destructor
}

case ParameterNode(name) => new MoeParameter(name)
case SignatureNode(params) => new MoeSignature(
params.map(eval(runtime, env, _).asInstanceOf[MoeParameter])
)
case MethodDeclarationNode(name, signature, body) => {
val klass = env.getCurrentClass.getOrElse(
throw new MoeErrors.ClassNotFound("__CLASS__")
)
val sig = eval(runtime, env, signature).asInstanceOf[MoeSignature]
throwForUndeclaredVars(env, sig, body)
val method = new MoeMethod(
name = name,
signature = sig,
declaration_env = env,
body = (e) => eval(runtime, e, body)
)
klass.addMethod(method)
method
}

case SubroutineDeclarationNode(name, signature, body) => {
val sig = eval(runtime, env, signature).asInstanceOf[MoeSignature]
throwForUndeclaredVars(env, sig, body)
val sub = new MoeSubroutine(
name = name,
signature = sig,
captured_env = env,
body = (e) => eval(runtime, e, body)
name = name,
signature = sig,
declaration_env = env,
body = (e) => eval(runtime, e, body)
)
env.getCurrentPackage.getOrElse(
throw new MoeErrors.PackageNotFound("__PACKAGE__")
).addSubroutine( sub )
sub
}

case AttributeAccessNode(name) => {
val klass = env.getCurrentClass.getOrElse(
throw new MoeErrors.ClassNotFound("__CLASS__")
Expand Down Expand Up @@ -417,24 +431,18 @@ class Interpreter {
val meth = klass.getMethod(method_name).getOrElse(
throw new MoeErrors.MethodNotFound(method_name)
)
invocant_object.callMethod(meth, args.map(eval(runtime, env, _)))
obj.callMethod(meth, args.map(eval(runtime, env, _)))
case _ => throw new MoeErrors.MoeException("Object expected")
}
}

case SubroutineCallNode(function_name, args) => {
val sub = env.getCurrentPackage.getOrElse(
throw new MoeErrors.PackageNotFound("__PACKAGE__")
).getSubroutine(function_name).getOrElse(
throw new MoeErrors.SubroutineNotFound(function_name)
)
// NOTE:
// I think we might need to eval these
// in the context of the sub body, and
// we also need to make sure that we
// add the args to the environment as
// well.
// - SL
sub.execute(args.map(eval(runtime, env, _)))
sub.execute(new MoeArguments(args.map(eval(runtime, env, _))))
}

// statements
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/org/moe/runtime/builtins/AnyClass.scala
Expand Up @@ -8,6 +8,7 @@ import org.moe.runtime._
object AnyClass {

def apply(r: MoeRuntime): Unit = {
val env = new MoeEnvironment(Some(r.getCorePackage.getEnv))
val anyClass = r.getCoreClassFor("Any").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Any")
)
Expand Down
10 changes: 6 additions & 4 deletions src/main/scala/org/moe/runtime/builtins/ArrayClass.scala
Expand Up @@ -8,6 +8,7 @@ import org.moe.runtime._
object ArrayClass {

def apply(r: MoeRuntime): Unit = {
val env = new MoeEnvironment(Some(r.getCorePackage.getEnv))
val arrayClass = r.getCoreClassFor("Array").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Array")
)
Expand All @@ -27,10 +28,11 @@ object ArrayClass {
arrayClass.addMethod(
new MoeMethod(
"postcircumfix:<[]>",
{ (invocant, args) =>

var index = args(0).unboxToInt.get
val array = invocant.unboxToList.get
new MoeSignature(List(new MoeParameter("$i"))),
env,
{ (e) =>
var index = e.get("$i").get.unboxToInt.get
val array = e.getCurrentInvocant.get.unboxToList.get

while (index < 0) index += array.size
try {
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/org/moe/runtime/builtins/BoolClass.scala
Expand Up @@ -8,6 +8,7 @@ import org.moe.runtime._
object BoolClass {

def apply(r: MoeRuntime): Unit = {
val env = new MoeEnvironment(Some(r.getCorePackage.getEnv))
val boolClass = r.getCoreClassFor("Bool").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Bool")
)
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/org/moe/runtime/builtins/ClassClass.scala
Expand Up @@ -8,6 +8,7 @@ import org.moe.runtime._
object ClassClass {

def apply(r: MoeRuntime): Unit = {
val env = new MoeEnvironment(Some(r.getCorePackage.getEnv))
val classClass = r.getCoreClassFor("Class").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Class")
)
Expand All @@ -18,7 +19,9 @@ object ClassClass {
classClass.addMethod(
new MoeMethod(
"new",
{ (invocant, _) => invocant.asInstanceOf[MoeClass].newInstance }
new MoeSignature(List()),
env,
{ (e) => e.getCurrentInvocant.get.asInstanceOf[MoeClass].newInstance }
)
)

Expand Down
Expand Up @@ -8,6 +8,7 @@ import org.moe.runtime._
object ExceptionClass {

def apply(r: MoeRuntime): Unit = {
val env = new MoeEnvironment(Some(r.getCorePackage.getEnv))
val exceptionClass = r.getCoreClassFor("Exception").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Exception")
)
Expand Down
50 changes: 28 additions & 22 deletions src/main/scala/org/moe/runtime/builtins/HashClass.scala
Expand Up @@ -9,6 +9,7 @@ import org.moe.runtime.nativeobjects._
object HashClass {

def apply(r: MoeRuntime): Unit = {
val env = new MoeEnvironment(Some(r.getCorePackage.getEnv))
val hashClass = r.getCoreClassFor("Hash").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Hash")
)
Expand All @@ -19,56 +20,61 @@ object HashClass {

/**
* NOTE:
* This should also support lvalue assignment.
* This should also support slice and lvalue assignment.
*/
hashClass.addMethod(
new MoeMethod(
"postcircumfix:<{}>",
{ (self, args) =>
val hash = self.asInstanceOf[MoeHashObject]
if (args.length == 1) {
hash.at_key(r, args(0).asInstanceOf[MoeStrObject])
}
else {
hash.slice(r, args.map(_.asInstanceOf[MoeStrObject]))
}
new MoeSignature(List(new MoeParameter("$key"))),
env,
{ (e) =>
val hash = e.getCurrentInvocant.get.asInstanceOf[MoeHashObject]
hash.at_key(r, e.get("$key").get.asInstanceOf[MoeStrObject])
}
)
)

hashClass.addMethod(
new MoeMethod(
"at_key", // ($key)
(self, args) => self.asInstanceOf[MoeHashObject].at_key(r, args(0).asInstanceOf[MoeStrObject])
"at_key",
new MoeSignature(List(new MoeParameter("$key"))),
env,
(e) => e.getCurrentInvocant.get.asInstanceOf[MoeHashObject].at_key(r, e.get("$key").get.asInstanceOf[MoeStrObject])
)
)

hashClass.addMethod(
new MoeMethod(
"bind_key", // ($key, $value)
(self, args) => self.asInstanceOf[MoeHashObject].bind_key(r, args(0).asInstanceOf[MoeStrObject], args(1))
"bind_key",
new MoeSignature(List(new MoeParameter("$key"), new MoeParameter("$value"))),
env,
(e) => e.getCurrentInvocant.get.asInstanceOf[MoeHashObject].bind_key(r, e.get("$key").get.asInstanceOf[MoeStrObject], e.get("$value").get)
)
)

hashClass.addMethod(
new MoeMethod(
"exists", // ($key)
(self, args) => self.asInstanceOf[MoeHashObject].exists(r, args(0).asInstanceOf[MoeStrObject])
"exists",
new MoeSignature(List(new MoeParameter("$key"))),
env,
(e) => e.getCurrentInvocant.get.asInstanceOf[MoeHashObject].exists(r, e.get("$key").get.asInstanceOf[MoeStrObject])
)
)

hashClass.addMethod(
new MoeMethod(
"slice", // ($key)
(self, args) => self.asInstanceOf[MoeHashObject].slice(r, args.map(_.asInstanceOf[MoeStrObject]))
"slice",
new MoeSignature(List(new MoeParameter("@keys"))),
env,
(e) => e.getCurrentInvocant.get.asInstanceOf[MoeHashObject].slice(r, e.get("@keys").get.asInstanceOf[MoeArrayObject])
)
)

hashClass.addMethod(new MoeMethod("clear", (self, _) => self.asInstanceOf[MoeHashObject].clear(r)))
hashClass.addMethod(new MoeMethod("keys", (self, _) => self.asInstanceOf[MoeHashObject].keys(r)))
hashClass.addMethod(new MoeMethod("values", (self, _) => self.asInstanceOf[MoeHashObject].values(r)))
hashClass.addMethod(new MoeMethod("kv", (self, _) => self.asInstanceOf[MoeHashObject].kv(r)))
hashClass.addMethod(new MoeMethod("pairs", (self, _) => self.asInstanceOf[MoeHashObject].pairs(r)))
hashClass.addMethod(new MoeMethod("clear", new MoeSignature(List()), env, (e) => e.getCurrentInvocant.get.asInstanceOf[MoeHashObject].clear(r)))
hashClass.addMethod(new MoeMethod("keys", new MoeSignature(List()), env, (e) => e.getCurrentInvocant.get.asInstanceOf[MoeHashObject].keys(r)))
hashClass.addMethod(new MoeMethod("values", new MoeSignature(List()), env, (e) => e.getCurrentInvocant.get.asInstanceOf[MoeHashObject].values(r)))
hashClass.addMethod(new MoeMethod("kv", new MoeSignature(List()), env, (e) => e.getCurrentInvocant.get.asInstanceOf[MoeHashObject].kv(r)))
hashClass.addMethod(new MoeMethod("pairs", new MoeSignature(List()), env, (e) => e.getCurrentInvocant.get.asInstanceOf[MoeHashObject].pairs(r)))

/**
* List of Methods to support:
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/org/moe/runtime/builtins/IOClass.scala
Expand Up @@ -8,6 +8,7 @@ import org.moe.runtime._
object IOClass {

def apply(r: MoeRuntime): Unit = {
val env = new MoeEnvironment(Some(r.getCorePackage.getEnv))
val ioClass = r.getCoreClassFor("IO").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class IO")
)
Expand Down
25 changes: 15 additions & 10 deletions src/main/scala/org/moe/runtime/builtins/IntClass.scala
Expand Up @@ -9,6 +9,7 @@ import org.moe.runtime.nativeobjects._
object IntClass {

def apply(r: MoeRuntime): Unit = {
val env = new MoeEnvironment(Some(r.getCorePackage.getEnv))
val intClass = r.getCoreClassFor("Int").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Int")
)
Expand All @@ -22,11 +23,13 @@ object IntClass {
intClass.addMethod(
new MoeMethod(
"infix:<+>",
{ (invocant, args) =>
args(0) match {
case i: MoeIntObject => getInt(invocant.unboxToInt.get + i.unboxToInt.get)
case f: MoeNumObject => getNum(invocant.unboxToDouble.get + f.unboxToDouble.get)
case _ => throw new MoeErrors.UnexpectedType(args(0).toString)
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
e.get("$other").get match {
case i: MoeIntObject => getInt(e.getCurrentInvocant.get.unboxToInt.get + i.unboxToInt.get)
case f: MoeNumObject => getNum(e.getCurrentInvocant.get.unboxToDouble.get + f.unboxToDouble.get)
case _ => throw new MoeErrors.UnexpectedType(e.get("$other").get.toString)
}
}
)
Expand All @@ -35,11 +38,13 @@ object IntClass {
intClass.addMethod(
new MoeMethod(
"infix:<*>",
{ (invocant, args) =>
args(0) match {
case i: MoeIntObject => getInt(invocant.unboxToInt.get * i.unboxToInt.get)
case f: MoeNumObject => getNum(invocant.unboxToDouble.get * f.unboxToDouble.get)
case _ => throw new MoeErrors.UnexpectedType(args(0).toString)
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
e.get("$other").get match {
case i: MoeIntObject => getInt(e.getCurrentInvocant.get.unboxToInt.get * i.unboxToInt.get)
case f: MoeNumObject => getNum(e.getCurrentInvocant.get.unboxToDouble.get * f.unboxToDouble.get)
case _ => throw new MoeErrors.UnexpectedType(e.get("$other").get.toString)
}
}
)
Expand Down

0 comments on commit 191dff1

Please sign in to comment.