Skip to content

Commit

Permalink
statement modifiers and few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashk committed May 14, 2013
1 parent 4da13fc commit bf1ea90
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 7 deletions.
20 changes: 16 additions & 4 deletions src/main/scala/org/moe/parser/MoeProductions.scala
Expand Up @@ -296,7 +296,7 @@ trait MoeProductions extends MoeLiterals with JavaTokenParsers with PackratParse
blockStatement
| declarationStatement
| terminatedStatement
| simpleStatement
| statement
| scopeBlock
)) ~ opt(statement) ^^ {
case stmts ~ Some(lastStmt) => StatementsNode(stmts ++ List(lastStmt))
Expand Down Expand Up @@ -476,9 +476,21 @@ trait MoeProductions extends MoeLiterals with JavaTokenParsers with PackratParse
| arrayElementAssignment
| hashElementAssignment
| expression
// | scopeBlock
)

lazy val statement: Parser[AST] = simpleStatement
lazy val terminatedStatement: Parser[AST] = simpleStatement <~ statementDelim
/**
* Statement modifiers
*/

lazy val modifiedStatement: Parser[AST] = simpleStatement ~ "if|unless|for(each)?|while|until".r ~ expression ^^ {
case stmt ~ "if" ~ cond => IfNode(new IfStruct(cond, StatementsNode(List(stmt))))
case stmt ~ "unless" ~ cond => UnlessNode(new UnlessStruct(cond, StatementsNode(List(stmt))))
case stmt ~ "foreach" ~ list => ForeachNode(VariableDeclarationNode("$_", UndefLiteralNode()), list, StatementsNode(List(stmt)))
case stmt ~ "for" ~ list => ForeachNode(VariableDeclarationNode("$_", UndefLiteralNode()), list, StatementsNode(List(stmt)))
case stmt ~ "while" ~ cond => WhileNode(cond, StatementsNode(List(stmt)))
case stmt ~ "until" ~ cond => WhileNode(PrefixUnaryOpNode(cond, "!"), StatementsNode(List(stmt)))
}

lazy val statement: Parser[AST] = ( modifiedStatement | simpleStatement )
lazy val terminatedStatement: Parser[AST] = statement <~ statementDelim
}
14 changes: 13 additions & 1 deletion t/006-statements/001-if.t
Expand Up @@ -24,4 +24,16 @@ use Test::More;
is($r, 30, '... got the expected value');
}

done_testing();
{
my $r = eval("10 if true");
ok(not($!.defined), '... the modifier statement worked correctly');
is($r, 10, '... got the expected value');
}

{
my $r = eval("my $a = 0; $a = $a + 1 if $a < 1; $a");
ok(not($!.defined), '... the modifier statement worked correctly');
is($r, 1, '... got the expected value');
}

done_testing();
22 changes: 21 additions & 1 deletion t/006-statements/002-for.t
Expand Up @@ -13,4 +13,24 @@ use Test::More;
is($r, 10, '... got the expected value');
}

done_testing();
{
my $r = eval(
"my $a = 0;" ~
"$a = $a + 1 for 1..5;" ~
"$a"
);
ok(not($!.defined), '... the modifier statement with range worked correctly');
is($r, 5, '... got the expected value');
}

{
my $r = eval(
"my $a = 0;" ~
"$a = $a + $_ for [1,2,3];" ~
"$a"
);
ok(not($!.defined), '... the modifier statement with list worked correctly');
is($r, 6, '... got the expected value');
}

done_testing();
22 changes: 21 additions & 1 deletion t/006-statements/003-foreach.t
Expand Up @@ -25,4 +25,24 @@ use Test::More;
is($r, 55, '... got the expected value');
}

done_testing();
{
my $r = eval(
"my $a = 0;" ~
"$a = $a + 1 foreach 1..5;" ~
"$a"
);
ok(not($!.defined), '... the modifier statement with range worked correctly');
is($r, 5, '... got the expected value');
}

{
my $r = eval(
"my $a = 0;" ~
"$a = $a + $_ foreach [1,2,3];" ~
"$a"
);
ok(not($!.defined), '... the modifier statement with list worked correctly');
is($r, 6, '... got the expected value');
}

done_testing();
12 changes: 12 additions & 0 deletions t/006-statements/004-unless.t
Expand Up @@ -30,4 +30,16 @@ use Test::More;
is($r, 30, '... got the expected value');
}

{
my $r = eval("10 unless false");
ok(not($!.defined), '... the modifier statement worked correctly');
is($r, 10, '... got the expected value');
}

{
my $r = eval("my $a = 0; $a = $a + 1 unless $a > 0; $a");
ok(not($!.defined), '... the modifier statement worked correctly');
is($r, 1, '... got the expected value');
}

done_testing();
25 changes: 25 additions & 0 deletions t/006-statements/005-while.t
@@ -0,0 +1,25 @@
use Test::More;

{
my $r = eval(
"my $x = 0;" ~
"while ($x < 10) {" ~
" $x = $x + 1;" ~
"}" ~
"$x"
);
ok(not($!.defined), '... the statement worked correctly');
is($r, 10, '... got the expected value');
}

{
my $r = eval(
"my $a = 0;" ~
"$a = $a + 1 while $a < 5;" ~
"$a"
);
ok(not($!.defined), '... the modifier statement worked correctly');
is($r, 5, '... got the expected value');
}

done_testing();
25 changes: 25 additions & 0 deletions t/006-statements/006-until.t
@@ -0,0 +1,25 @@
use Test::More;

{
my $r = eval(
"my $x = 0;" ~
"until ($x > 10) {" ~
" $x = $x + 1;" ~
"}" ~
"$x"
);
ok(not($!.defined), '... the statement worked correctly');
is($r, 11, '... got the expected value');
}

{
my $r = eval(
"my $a = 0;" ~
"$a = $a + 1 until $a > 5;" ~
"$a"
);
ok(not($!.defined), '... the modifier statement worked correctly');
is($r, 6, '... got the expected value');
}

done_testing();

0 comments on commit bf1ea90

Please sign in to comment.