Skip to content

Commit

Permalink
Merge pull request #61 from MoeOrganization/prakashk/numclass-builtins
Browse files Browse the repository at this point in the history
arithmetic and relational operators on Nums
  • Loading branch information
Stevan Little committed Feb 25, 2013
2 parents c09aa21 + 2ac5fec commit 2f3cf11
Show file tree
Hide file tree
Showing 3 changed files with 337 additions and 12 deletions.
12 changes: 2 additions & 10 deletions src/main/scala/org/moe/runtime/builtins/IntClass.scala
Expand Up @@ -72,11 +72,7 @@ object IntClass {
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
e.get("$other").get match {
case i: MoeIntObject => getNum(e.getCurrentInvocant.get.unboxToInt.get / i.unboxToInt.get.toDouble)
case f: MoeNumObject => getNum(e.getCurrentInvocant.get.unboxToDouble.get / f.unboxToDouble.get)
case _ => throw new MoeErrors.UnexpectedType(e.get("$other").get.toString)
}
getNum(e.getCurrentInvocant.get.unboxToInt.get / e.get("$other").get.unboxToDouble.get)
}
)
)
Expand All @@ -87,11 +83,7 @@ object IntClass {
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
e.get("$other").get match {
case i: MoeIntObject => getInt(perlModuloOp(e.getCurrentInvocant.get.unboxToInt.get, i.unboxToInt.get))
case f: MoeNumObject => getInt(perlModuloOp(e.getCurrentInvocant.get.unboxToDouble.get.toInt, f.unboxToDouble.get.toInt))
case _ => throw new MoeErrors.UnexpectedType(e.get("$other").get.toString)
}
getInt(perlModuloOp(e.getCurrentInvocant.get.unboxToInt.get, e.get("$other").get.unboxToInt.get))
}
)
)
Expand Down
128 changes: 126 additions & 2 deletions src/main/scala/org/moe/runtime/builtins/NumClass.scala
@@ -1,6 +1,7 @@
package org.moe.runtime.builtins

import org.moe.runtime._
import org.moe.interpreter.InterpreterUtils._

/**
* setup class Num
Expand All @@ -20,14 +21,137 @@ object NumClass {
numClass.addMethod(
new MoeMethod(
"infix:<+>",
new MoeSignature(List(new MoeParameter("$other"))),
env,
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
getNum(e.getCurrentInvocant.get.unboxToDouble.get + e.get("$other").get.unboxToDouble.get)
}
)
)

numClass.addMethod(
new MoeMethod(
"infix:<->",
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
getNum(e.getCurrentInvocant.get.unboxToDouble.get - e.get("$other").get.unboxToDouble.get)
}
)
)

numClass.addMethod(
new MoeMethod(
"infix:<*>",
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
getNum(e.getCurrentInvocant.get.unboxToDouble.get * e.get("$other").get.unboxToDouble.get)
}
)
)

numClass.addMethod(
new MoeMethod(
"infix:</>",
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
getNum(e.getCurrentInvocant.get.unboxToDouble.get / e.get("$other").get.unboxToDouble.get)
}
)
)

numClass.addMethod(
new MoeMethod(
"infix:<%>",
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
getInt(perlModuloOp(e.getCurrentInvocant.get.unboxToInt.get, e.get("$other").get.unboxToInt.get))
}
)
)

numClass.addMethod(
new MoeMethod(
"infix:<**>",
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
getNum(Math.pow(e.getCurrentInvocant.get.unboxToDouble.get, e.get("$other").get.unboxToDouble.get))
}
)
)

// relational operators

numClass.addMethod(
new MoeMethod(
"infix:<<>",
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
getBool(e.getCurrentInvocant.get.unboxToDouble.get < e.get("$other").get.unboxToDouble.get)
}
)
)

numClass.addMethod(
new MoeMethod(
"infix:<>>",
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
getBool(e.getCurrentInvocant.get.unboxToDouble.get > e.get("$other").get.unboxToDouble.get)
}
)
)

numClass.addMethod(
new MoeMethod(
"infix:<<=>",
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
getBool(e.getCurrentInvocant.get.unboxToDouble.get <= e.get("$other").get.unboxToDouble.get)
}
)
)

numClass.addMethod(
new MoeMethod(
"infix:<>=>",
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
getBool(e.getCurrentInvocant.get.unboxToDouble.get >= e.get("$other").get.unboxToDouble.get)
}
)
)

numClass.addMethod(
new MoeMethod(
"infix:<==>",
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
getBool(e.getCurrentInvocant.get.unboxToDouble.get == e.get("$other").get.unboxToDouble.get)
}
)
)

numClass.addMethod(
new MoeMethod(
"infix:<!=>",
new MoeSignature(List(new MoeParameter("$other"))),
env,
{ (e) =>
getBool(e.getCurrentInvocant.get.unboxToDouble.get != e.get("$other").get.unboxToDouble.get)
}
)
)

/**
* List of Operators to support:
* - infix:<*>
Expand Down
209 changes: 209 additions & 0 deletions src/test/scala/org/moe/parser/SimpleExpressionNumTestSuite.scala
@@ -0,0 +1,209 @@
package org.moe.parser

import org.scalatest.FunSuite
import org.scalatest.BeforeAndAfter
import org.scalatest.matchers.ShouldMatchers

import org.moe.runtime._
import org.moe.interpreter._
import org.moe.ast._
import org.moe.parser._

class SimpleExpressionNumTestSuite extends FunSuite with BeforeAndAfter with ParserTestUtils with ShouldMatchers {

val delta = 2e-8

// addition

test("... literal float addition ... [2.3+2]") {
val result = interpretCode("2.3+2")
result.unboxToDouble.get should be (4.3 plusOrMinus delta)
}

test("... literal float addition ... [2.3+3.2]") {
val result = interpretCode("2.3+3.2")
result.unboxToDouble.get should be (5.5 plusOrMinus delta)
}

test("... literal float addition ... [-2.3+3.2]") {
val result = interpretCode("-2.3+3.2")
result.unboxToDouble.get should be (0.9 plusOrMinus delta)
}

test("... literal float addition ... [2.3+-3.2]") {
val result = interpretCode("2.3+-3.2")
result.unboxToDouble.get should be (-0.9 plusOrMinus delta)
}

test("... literal float addition ... [2.3 + +3.2]") {
val result = interpretCode("2.3 + +3.2")
result.unboxToDouble.get should be (5.5 plusOrMinus delta)
}

test("... literal float addition ... [2.3e-2 + 3.2e2]") {
val result = interpretCode("2.3e-2 + 3.2e2")
result.unboxToDouble.get should be (320.023 plusOrMinus delta)
}

// subtraction

test("... literal float subtraction ... [2.3-2]") {
val result = interpretCode("2.3-2")
result.unboxToDouble.get should be (0.3 plusOrMinus delta)
}

test("... literal float subtraction ... [2.3-3.2]") {
val result = interpretCode("2.3-3.2")
result.unboxToDouble.get should be (-0.9 plusOrMinus delta)
}

test("... literal float subtraction ... [-2.3-3.2]") {
val result = interpretCode("-2.3-3.2")
result.unboxToDouble.get should be (-5.5 plusOrMinus delta)
}

test("... literal float subtraction ... [2.3--3.2]") {
val result = interpretCode("2.3--3.2")
result.unboxToDouble.get should be (5.5 plusOrMinus delta)
}

test("... literal float subtraction ... [2.3 - +3.2]") {
val result = interpretCode("2.3 - +3.2")
result.unboxToDouble.get should be (-0.9 plusOrMinus delta)
}

test("... literal float subtraction ... [2.3e-2 - 3.2e2]") {
val result = interpretCode("2.3e-2 - 3.2e2")
result.unboxToDouble.get should be (-319.977 plusOrMinus delta)
}

// multiplication

test("... literal float multiplication ... [2.3*2]") {
val result = interpretCode("2.3*2")
result.unboxToDouble.get should be (4.6 plusOrMinus delta)
}

// division

test("... literal float division ... [2.3/2]") {
val result = interpretCode("2.3/2")
result.unboxToDouble.get should be (1.15 plusOrMinus delta)
}

// modulo

test("... literal float modulo operation ... [13.0 % 4.0]") {
val result = interpretCode("13.0 % 4.0")
result.unboxToDouble.get should equal (1)
}

test("... literal float modulo operation ... [-13.0 % 4.0]") {
val result = interpretCode("-13.0 % 4.0")
result.unboxToDouble.get should equal (3)
}

test("... literal float modulo operation ... [13.0 % -4.0]") {
val result = interpretCode("13.0 % -4.0")
result.unboxToDouble.get should equal (-3)
}

test("... literal float modulo operation ... [-13.0 % -4.0]") {
val result = interpretCode("-13.0 % -4.0")
result.unboxToDouble.get should equal (-1)
}

// exponentiation

test("... literal float exponentiation operation ... [2.2**2]") {
val result = interpretCode("2.2**2")
result.unboxToDouble.get should be (4.84 plusOrMinus 0.00000001)
}

test("... literal float exponentiation operation ... [2.2**2.2]") {
val result = interpretCode("2.2**2.2")
result.unboxToDouble.get should be (5.66669577 plusOrMinus 0.00000001)
}

// relational

test("... literal float relational operation ... [1.1 < 2]") {
val result = interpretCode("1.1 < 2")
result.unboxToBoolean.get should equal (true)
}

test("... literal float relational operation ... [2.0 < 1]") {
val result = interpretCode("2.0 < 1")
result.unboxToBoolean.get should equal (false)
}

test("... literal float relational operation ... [2.1 < 2.5]") {
val result = interpretCode("2.1 < 2.5")
result.unboxToBoolean.get should equal (true)
}

test("... literal float relational operation ... [2.1 > 1]") {
val result = interpretCode("2.1 > 1")
result.unboxToBoolean.get should equal (true)
}

test("... literal float relational operation ... [2.0 > 3.5]") {
val result = interpretCode("2.0 > 3.5")
result.unboxToBoolean.get should equal (false)
}

test("... literal float relational operation ... [2.9 > 2.5]") {
val result = interpretCode("2.9 > 2.5")
result.unboxToBoolean.get should equal (true)
}

test("... literal float relational operation ... [1.1 <= 2]") {
val result = interpretCode("1.1 <= 2")
result.unboxToBoolean.get should equal (true)
}

test("... literal float relational operation ... [1.1 <= 1.1]") {
val result = interpretCode("1.1 <= 1.1")
result.unboxToBoolean.get should equal (true)
}

test("... literal float relational operation ... [2.0 <= 1]") {
val result = interpretCode("2.0 <= 1")
result.unboxToBoolean.get should equal (false)
}

test("... literal float relational operation ... [2.1 <= 2.5]") {
val result = interpretCode("2.1 <= 2.5")
result.unboxToBoolean.get should equal (true)
}

test("... literal float relational operation ... [2.1 >= 1]") {
val result = interpretCode("2.1 >= 1")
result.unboxToBoolean.get should equal (true)
}

test("... literal float relational operation ... [2.0 >= 3.5]") {
val result = interpretCode("2.0 >= 3.5")
result.unboxToBoolean.get should equal (false)
}

test("... literal float relational operation ... [2.9 >= 2.5]") {
val result = interpretCode("2.9 >= 2.5")
result.unboxToBoolean.get should equal (true)
}

test("... literal float relational operation ... [2.9 >= 2.9]") {
val result = interpretCode("2.9 >= 2.9")
result.unboxToBoolean.get should equal (true)
}

test("... literal float relational operation ... [2.9 == 2.9]") {
val result = interpretCode("2.9 == 2.9")
result.unboxToBoolean.get should equal (true)
}

test("... literal float relational operation ... [2.9 != 2.9]") {
val result = interpretCode("2.9 != 2.9")
result.unboxToBoolean.get should equal (false)
}
}

0 comments on commit 2f3cf11

Please sign in to comment.