Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
String builtins - concatenation operator
  • Loading branch information
prakashk committed Feb 28, 2013
1 parent 55eabd7 commit 0a82bfa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/org/moe/parser/Expressions.scala
Expand Up @@ -13,15 +13,15 @@ trait Expressions extends Literals with JavaTokenParsers with PackratParsers {
private lazy val hash_index_rule = "%" ~
(namespacedIdentifier <~ "{") ~
(expression <~ "}")

lazy val expression: PackratParser[AST] = relOp | addOp

// This is what I want
// def binOpResult = { case left ~ op ~ right => MethodCallNode(left, op, List(right)) }
// lazy val addOp: PackratParser[AST] = addOp ~ "[-+]".r ~ mulOp ^^ binOpResult | mulOp
// lazy val mulOp: PackratParser[AST] = mulOp ~ "[*/]".r ~ simpleExpression ^^ binOpResult | simpleExpression

lazy val addOp: PackratParser[AST] = addOp ~ "[-+]".r ~ mulOp ^^ {
lazy val addOp: PackratParser[AST] = addOp ~ "[-+.]".r ~ mulOp ^^ {
case left ~ op ~ right => BinaryOpNode(left, op, right)
}| mulOp

Expand Down
26 changes: 19 additions & 7 deletions src/main/scala/org/moe/runtime/builtins/StrClass.scala
Expand Up @@ -23,9 +23,9 @@ object StrClass {
strClass.addMethod(
new MoeMethod(
"prefix:<++>",
new MoeSignature(),
env,
{ (e) =>
new MoeSignature(),
env,
{ (e) =>
val inv = e.getCurrentInvocant.get.asInstanceOf[MoeStrObject]
inv.increment(r)
inv
Expand All @@ -36,9 +36,9 @@ object StrClass {
strClass.addMethod(
new MoeMethod(
"postfix:<++>",
new MoeSignature(),
env,
{ (e) =>
new MoeSignature(),
env,
{ (e) =>
val inv = e.getCurrentInvocant.get.asInstanceOf[MoeStrObject]
val old = getStr(inv.getNativeValue)
inv.increment(r)
Expand All @@ -47,9 +47,21 @@ object StrClass {
)
)

// concatenation

strClass.addMethod(
new MoeMethod(
"infix:<.>",
new MoeSignature(List(new MoeParameter("$other"))),
env,
(e) => e.getCurrentInvocant.get.asInstanceOf[MoeStrObject].concat(r, e.get("$other").get)
)
)

/**
* List of Operators to support:
* - infix:<.>
* - infix:<.>
* - infix:<x>
*
* List of Methods to support:
* - chomp
Expand Down
Expand Up @@ -12,18 +12,21 @@ class MoeStrObject(

def increment (r: MoeRuntime): Unit = setNativeValue(MoeUtil.magicalStringIncrement(getNativeValue))

def concat (r: MoeRuntime, other: MoeObject): MoeObject =
r.NativeObjects.getStr(getNativeValue + other.unboxToString.get)

// MoeObject overrides

override def isFalse: Boolean = getNativeValue match {
case "" | "0" => true
case _ => false
}
override def toString = "\"" + getNativeValue + "\""

// unboxing

override def unboxToString: Try[String] = Success(getNativeValue)
override def unboxToInt: Try[Int] = Try(getNativeValue.toInt)
override def unboxToDouble: Try[Double] = Try(getNativeValue.toDouble)

}
}
24 changes: 24 additions & 0 deletions src/test/scala/org/moe/parser/SimpleExpressionStrTestSuite.scala
@@ -0,0 +1,24 @@
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 SimpleExpressionStrTestSuite extends FunSuite with BeforeAndAfter with ParserTestUtils with ShouldMatchers {

test("... literal string concatenation ... [\"Hello \" . \"Moe\"]") {
val result = interpretCode(""""Hello " . "Moe"""")
result.unboxToString.get should equal ("Hello Moe")
}

test("... literal string concatenation ... ['foo' . 'bar']") {
val result = interpretCode("'foo' . 'bar'")
result.unboxToString.get should equal ("foobar")
}

}

0 comments on commit 0a82bfa

Please sign in to comment.