Skip to content

Commit

Permalink
adding in Coercion for Native objects, and adding in a few new except…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
Stevan Little committed Feb 13, 2013
1 parent 94dee14 commit 5eef999
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/main/scala/org/moe/runtime/MoeErrors.scala
Expand Up @@ -14,8 +14,11 @@ object MoeErrors {
// MoeProblems is derived from MoeMoney - RIP B.I.G
class MoeProblems (msg: String) extends MoeMoney(msg)

// Moe internal errors
class MoeStartupError (msg: String) extends MoeProblems(msg)
class MoeTypeError (msg: String) extends MoeProblems(msg)

// Runtime errors
class NotAllowed (msg: String) extends MoeProblems(msg)
class MethodNotAllowed (msg: String) extends NotAllowed(msg)

Expand All @@ -39,5 +42,6 @@ object MoeErrors {
class MissingValue (msg: String) extends MoeProblems(msg)
class MissingClass (msg: String) extends MissingValue(msg)

class UnexpectedType (msg: String) extends MoeProblems(msg)
class UnexpectedType (msg: String) extends MoeTypeError(msg)
class BadTypeCoercion (msg: String) extends MoeTypeError(msg)
}
31 changes: 28 additions & 3 deletions src/main/scala/org/moe/runtime/MoeRuntime.scala
Expand Up @@ -141,9 +141,34 @@ class MoeRuntime (
def getArray (value: List[MoeObject]) = new MoeArrayObject(value, getCoreClassFor("Array"))
def getArray () = new MoeArrayObject(List(), getCoreClassFor("Array"))
def getPair (value: (MoeObject, MoeObject)) = new MoePairObject(
(value._1.asInstanceOf[MoeStringObject].getNativeValue, value._2),
getCoreClassFor("Pair")
)
(value._1.asInstanceOf[MoeStringObject].getNativeValue, value._2),
getCoreClassFor("Pair")
)

object Coercions {
def toDouble(obj: MoeObject): Double = obj match {
case i: MoeIntObject => i.getNativeValue.toDouble
case n: MoeFloatObject => n.getNativeValue
case s: MoeStringObject => try { s.getNativeValue.toDouble } catch { case e => 0 } // TODO: warn
case _ => throw new MoeErrors.BadTypeCoercion("to Double")
}

def toInt(obj: MoeObject): Int = obj match {
case i: MoeIntObject => i.getNativeValue
case n: MoeFloatObject => n.getNativeValue.toInt
case s: MoeStringObject => try { s.getNativeValue.toInt } catch { case e => 0 } // TODO: warn
case _ => throw new MoeErrors.BadTypeCoercion("to Int")
}

def toString(obj: MoeObject): String = obj match {
case i: MoeIntObject => i.getNativeValue.toString
case n: MoeFloatObject => n.getNativeValue.toString
case s: MoeStringObject => s.getNativeValue
case _ => throw new MoeErrors.BadTypeCoercion("to String")
}

}

}

}

0 comments on commit 5eef999

Please sign in to comment.