Skip to content

Commit

Permalink
moving the builtins into their own class (not 100% sure about this ap…
Browse files Browse the repository at this point in the history
…proach, but it works)
  • Loading branch information
Stevan Little committed Feb 12, 2013
1 parent de85abe commit 055c063
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 46 deletions.
2 changes: 2 additions & 0 deletions src/main/scala/org/moe/runtime/MoeErrors.scala
Expand Up @@ -14,6 +14,8 @@ object MoeErrors {
// MoeProblems is derived from MoeMoney - RIP B.I.G
class MoeProblems (msg: String) extends MoeMoney(msg)

class MoeStartupError (msg: String) extends MoeProblems(msg)

class NotAllowed (msg: String) extends MoeProblems(msg)
class MethodNotAllowed (msg: String) extends NotAllowed(msg)

Expand Down
49 changes: 3 additions & 46 deletions src/main/scala/org/moe/runtime/MoeRuntime.scala
Expand Up @@ -87,7 +87,7 @@ class MoeRuntime (
corePackage.addClass(numClass)
corePackage.addClass(exceptionClass)

addBuiltinMethods
setupBuiltins

/*
TODO:
Expand All @@ -99,52 +99,9 @@ class MoeRuntime (
}
}

private def addBuiltinMethods = {

// constructor
classClass.addMethod(
new MoeMethod(
"new",
{ (invocant, _) => invocant.asInstanceOf[MoeClass].newInstance }
)
)

// arithmetic
val int_class = ensureCoreClassFor("Int")
int_class.addMethod(
new MoeMethod(
"+",
{ (lhs, args) =>
val rhs = args(0)
val i = lhs.asInstanceOf[MoeIntObject]
rhs match {
case rhs_i: MoeIntObject => NativeObjects.getInt(i.getNativeValue + rhs_i.getNativeValue)
case rhs_n: MoeFloatObject => NativeObjects.getFloat(i.getNativeValue.toDouble + rhs_n.getNativeValue)
case _ => throw new MoeErrors.UnexpectedType(rhs.toString)
}
}
)
)

val num_class = ensureCoreClassFor("Num")
num_class.addMethod(
new MoeMethod(
"+",
{ (lhs, args) =>
val rhs = args(0)
val n = lhs.asInstanceOf[MoeFloatObject]
rhs match {
case rhs_n: MoeFloatObject => NativeObjects.getFloat(n.getNativeValue + rhs_n.getNativeValue)
case rhs_i: MoeIntObject => NativeObjects.getFloat(n.getNativeValue + rhs_i.getNativeValue.toDouble)
case _ => throw new MoeErrors.UnexpectedType(rhs.toString)
}
}
)
)
}

def getCoreClassFor (name: String): Option[MoeClass] = corePackage.getClass(name)
def ensureCoreClassFor (name: String): MoeClass = getCoreClassFor(name).getOrElse(unknownClass)

private def setupBuiltins = new MoeBuiltins(this)

object NativeObjects {

Expand Down

0 comments on commit 055c063

Please sign in to comment.