Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #108 from MoeOrganization/prakashk/for-loop
for loop etc.
- Loading branch information
Showing
7 changed files
with
111 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
src/test/scala/org/moe/parser/ForeachStatementTestSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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 ForeachStatementTestSuite extends FunSuite with BeforeAndAfter with ParserTestUtils { | ||
|
||
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) | ||
} | ||
|
||
test("... a for block with topic") { | ||
val result = interpretCode("my $sum = 0; for $x (1..3) { $sum = $sum + $x } $sum") | ||
assert(result.unboxToInt.get === 6) | ||
} | ||
|
||
test("... a for block with multiple statements") { | ||
val result = interpretCode("my $result = 0; for $x (1..3) { $result = $result + $x; $result = $result * 2 } $result") | ||
assert(result.unboxToInt.get === 22) | ||
} | ||
|
||
test("... a for block to modify array") { | ||
val result = interpretCode("my @a = [1,2,3]; for (0..2) { @a[$_] = @a[$_] + 1 } @a.join(',')") | ||
assert(result.unboxToString.get === "2,3,4") | ||
} | ||
|
||
// XXXX: this should work when binding the topic variable to array | ||
// elements is implemented | ||
|
||
// test("... a for block to modify array (take 2)") { | ||
// val result = interpretCode("my @a = [1,2,3]; for (@a) { $_ = $_ + 1 } @a.join(',')") | ||
// assert(result.unboxToString.get === "2,3,4") | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters