Skip to content

Commit

Permalink
expose pair objects as first class objects (not just parts of a hash)…
Browse files Browse the repository at this point in the history
… and add a few runtime methods to them
  • Loading branch information
Stevan Little committed Mar 30, 2013
1 parent a0fb773 commit 37412d0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/scala/org/moe/parser/Expressions.scala
Expand Up @@ -103,6 +103,7 @@ trait Expressions extends Literals with JavaTokenParsers with PackratParsers {
| hashIndex
| hash
| array
| pair
| range
| literalValue
| declaration
Expand Down
25 changes: 25 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/PairClass.scala
@@ -1,6 +1,7 @@
package org.moe.runtime.builtins

import org.moe.runtime._
import org.moe.runtime.nativeobjects._

/**
* setup class Pair
Expand All @@ -13,8 +14,32 @@ object PairClass {
throw new MoeErrors.MoeStartupError("Could not find class Pair")
)

import r.NativeObjects._

def self(e: MoeEnvironment): MoePairObject = e.getCurrentInvocantAs[MoePairObject].getOrElse(
throw new MoeErrors.InvocantNotFound("Could not find invocant")
)

// MRO: Pair, Any, Object

pairClass.addMethod(
new MoeMethod(
"key",
new MoeSignature(),
env,
(e) => self(e).key(r)
)
)

pairClass.addMethod(
new MoeMethod(
"value",
new MoeSignature(),
env,
(e) => self(e).value(r)
)
)

/**
* List of Methods to support:
*
Expand Down
Expand Up @@ -9,6 +9,9 @@ class MoePairObject(
klass : Option[MoeClass] = None
) extends MoeNativeObject[(String, MoeObject)](v, klass) {

def key (r: MoeRuntime): MoeStrObject = r.NativeObjects.getStr(getNativeValue._1)
def value (r: MoeRuntime): MoeObject = getNativeValue._2

// MoeObject overrides

override def toString: String = getNativeValue match {
Expand Down

0 comments on commit 37412d0

Please sign in to comment.