Skip to content

Commit

Permalink
I think this is a better way to handle these
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevan Little committed Feb 12, 2013
1 parent 3bd6929 commit 4e3fd2a
Show file tree
Hide file tree
Showing 15 changed files with 277 additions and 64 deletions.
63 changes: 0 additions & 63 deletions src/main/scala/org/moe/runtime/MoeBuiltins.scala

This file was deleted.

20 changes: 19 additions & 1 deletion src/main/scala/org/moe/runtime/MoeRuntime.scala
Expand Up @@ -101,7 +101,25 @@ class MoeRuntime (

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

private def setupBuiltins = new MoeBuiltins(this)
private def setupBuiltins = {
import org.moe.runtime.builtins._

ClassClass(this)
ObjectClass(this)

AnyClass(this)
ScalarClass(this)
ArrayClass(this)
HashClass(this)
PairClass(this)

UndefClass(this)
BoolClass(this)
StrClass(this)
IntClass(this)
NumClass(this)
ExceptionClass(this)
}

object NativeObjects {

Expand Down
17 changes: 17 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/AnyClass.scala
@@ -0,0 +1,17 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Any
*/
object AnyClass {

def apply(r: MoeRuntime): Unit = {
val anyClass = r.getCoreClassFor("Any").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Any")
)

}

}
17 changes: 17 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/ArrayClass.scala
@@ -0,0 +1,17 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Array
*/
object ArrayClass {

def apply(r: MoeRuntime): Unit = {
val arrayClass = r.getCoreClassFor("Array").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Array")
)

}

}
17 changes: 17 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/BoolClass.scala
@@ -0,0 +1,17 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Bool
*/
object BoolClass {

def apply(r: MoeRuntime): Unit = {
val boolClass = r.getCoreClassFor("Bool").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Bool")
)

}

}
24 changes: 24 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/ClassClass.scala
@@ -0,0 +1,24 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Class
*/
object ClassClass {

def apply(r: MoeRuntime): Unit = {
val classClass = r.getCoreClassFor("Class").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Class")
)

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

}
17 changes: 17 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/ExceptionClass.scala
@@ -0,0 +1,17 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Exception
*/
object ExceptionClass {

def apply(r: MoeRuntime): Unit = {
val exceptionClass = r.getCoreClassFor("Exception").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Exception")
)

}

}
17 changes: 17 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/HashClass.scala
@@ -0,0 +1,17 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Hash
*/
object HashClass {

def apply(r: MoeRuntime): Unit = {
val hashClass = r.getCoreClassFor("Hash").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Hash")
)

}

}
33 changes: 33 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/IntClass.scala
@@ -0,0 +1,33 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Int
*/
object IntClass {

def apply(r: MoeRuntime): Unit = {
val intClass = r.getCoreClassFor("Int").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Int")
)

// arithmetic

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

}
31 changes: 31 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/NumClass.scala
@@ -0,0 +1,31 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Num
*/
object NumClass {

def apply(r: MoeRuntime): Unit = {
val numClass = r.getCoreClassFor("Num").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Num")
)

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

}
17 changes: 17 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/ObjectClass.scala
@@ -0,0 +1,17 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Object
*/
object ObjectClass {

def apply(r: MoeRuntime): Unit = {
val objectClass = r.getCoreClassFor("Object").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Object")
)

}

}
17 changes: 17 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/PairClass.scala
@@ -0,0 +1,17 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Pair
*/
object PairClass {

def apply(r: MoeRuntime): Unit = {
val pairClass = r.getCoreClassFor("Pair").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Pair")
)

}

}
17 changes: 17 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/ScalarClass.scala
@@ -0,0 +1,17 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Scalar
*/
object ScalarClass {

def apply(r: MoeRuntime): Unit = {
val scalarClass = r.getCoreClassFor("Scalar").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Scalar")
)

}

}
17 changes: 17 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/StrClass.scala
@@ -0,0 +1,17 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Str
*/
object StrClass {

def apply(r: MoeRuntime): Unit = {
val strClass = r.getCoreClassFor("Str").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Str")
)

}

}
17 changes: 17 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/UndefClass.scala
@@ -0,0 +1,17 @@
package org.moe.runtime.builtins

import org.moe.runtime._

/**
* setup class Undef
*/
object UndefClass {

def apply(r: MoeRuntime): Unit = {
val undefClass = r.getCoreClassFor("Undef").getOrElse(
throw new MoeErrors.MoeStartupError("Could not find class Undef")
)

}

}

0 comments on commit 4e3fd2a

Please sign in to comment.