Skip to content

Commit

Permalink
for statement
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashk committed May 10, 2013
1 parent e18bed0 commit 6c3516f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/main/scala/org/moe/parser/MoeProductions.scala
Expand Up @@ -414,8 +414,6 @@ trait MoeProductions extends MoeLiterals with JavaTokenParsers with PackratParse
case cond ~ body => WhileNode(PrefixUnaryOpNode(cond, "!"), body)
}

// def forLoop = "for" ~ "(" ~> expression <~ ";" ~> expression <~ ";" ~> expression <~ ")" ~ block

def topicVariable = ("my".? ~> variableName) ^^ {
v => VariableDeclarationNode(v, UndefLiteralNode())
}
Expand All @@ -424,6 +422,10 @@ trait MoeProductions extends MoeLiterals with JavaTokenParsers with PackratParse
case None ~ list ~ block => ForeachNode(VariableDeclarationNode("$_", UndefLiteralNode()), list, block)
}

def forBlock = "for" ~> (("(" ~> variableDeclaration) <~ ";") ~ (expression <~ ";") ~ (statement <~ ")") ~ block ^^ {
case init ~ termCond ~ step ~ block => ForNode(init, termCond, step, block)
}

def tryBlock: Parser[TryNode] = ("try" ~> block) ~ rep(catchBlock) ~ rep(finallyBlock) ^^ {
case a ~ b ~ c => TryNode(a, b, c)
}
Expand All @@ -446,6 +448,7 @@ trait MoeProductions extends MoeLiterals with JavaTokenParsers with PackratParse
| whileBlock
| untilBlock
| foreachBlock
| forBlock
| doBlock
| tryBlock
) <~ opt(statementDelim)
Expand Down
18 changes: 18 additions & 0 deletions src/test/scala/org/moe/parser/ForStatementTestSuite.scala
@@ -0,0 +1,18 @@
package org.moe.parser

import org.scalatest.FunSuite
import org.scalatest.BeforeAndAfter

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

class ForStatementTestSuite extends FunSuite with BeforeAndAfter with ParserTestUtils {

test("... a for block") {
val result = interpretCode("my @a = [1,2,3]; my $sum = 0; for (my $i = 0; $i < @a.length; $i = $i + 1) { $sum = $sum + @a[$i] } $sum")
assert(result.unboxToInt.get === 6)
}

}
Expand Up @@ -10,7 +10,6 @@ import org.moe.parser._

class ForeachStatementTestSuite extends FunSuite with BeforeAndAfter with ParserTestUtils {

// TODO test environment stack via syntax when we are far enough
test("... a foreach block") {
val result = interpretCode("my @a = [1,2,3]; my $sum = 0; foreach (0..2) { $sum = $sum + @a[$_] } $sum")
assert(result.unboxToInt.get === 6)
Expand Down

0 comments on commit 6c3516f

Please sign in to comment.