Skip to content

Commit

Permalink
Add integer addition to the interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmay committed Feb 9, 2013
1 parent b121b45 commit f8bde81
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/scala/org/moe/runtime/MoeNativeObject.scala
Expand Up @@ -22,6 +22,25 @@ abstract class MoeNativeObject[A] (
class MoeIntObject(v: Int, klass : Option[MoeClass] = None) extends MoeNativeObject[Int](v, klass) {
override def isFalse: Boolean = getNativeValue == 0
override def toString = getNativeValue.toString

klass.map({klass =>
klass.addMethod(
new MoeMethod(
"+",
{ (lhs, args) =>
val rhs = args(0)
lhs match {
case i: MoeIntObject =>
rhs match {
case rhs_i: MoeIntObject => new MoeIntObject(i.getNativeValue + rhs_i.getNativeValue)
case rhs_n: MoeFloatObject => new MoeIntObject(i.getNativeValue + rhs_n.getNativeValue.toInt)
case _ => throw new MoeErrors.UnexpectedType(rhs.toString)
}
}
}
)
)
})
}

class MoeFloatObject(v: Double, klass : Option[MoeClass] = None) extends MoeNativeObject[Double](v, klass) {
Expand Down
15 changes: 15 additions & 0 deletions src/test/scala/org/moe/interpreter/ClassNodeTestSuite.scala
Expand Up @@ -166,6 +166,21 @@ class ClassNodeTestSuite
counter_class should haveMethod("inc")
}

test("... basic test with builtin method call") {
// class Foo { method zzz { 42 } } Foo->new->zzz()
val ast = wrapSimpleAST(
List(
MethodCallNode(
IntLiteralNode(2),
"+",
List(IntLiteralNode(2))
)
)
)
val result = interpreter.eval(runtime, runtime.getRootEnv, ast)
result.asInstanceOf[MoeIntObject].getNativeValue should equal (4)
}

test("... basic test with method call") {
// class Foo { method zzz { 42 } } Foo->new->zzz()
val ast = wrapSimpleAST(
Expand Down

0 comments on commit f8bde81

Please sign in to comment.