-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c43e48
commit 9c998c1
Showing
6 changed files
with
113 additions
and
20 deletions.
There are no files selected for viewing
52 changes: 43 additions & 9 deletions
52
snap-compile/src/test/java/org/snapscript/compile/ForInLoopTest.java
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 |
---|---|---|
@@ -1,20 +1,54 @@ | ||
package org.snapscript.compile; | ||
|
||
import junit.framework.TestCase; | ||
|
||
public class ForInLoopTest extends TestCase { | ||
public class ForInLoopTest extends ScriptTestCase { | ||
|
||
private static final String SOURCE_1 = | ||
"for(let i in 0..9){\n"+ | ||
" assert i > -1;\n"+ | ||
"}\n"+ | ||
"for(var i in 0..9){\n"+ | ||
"let l = [];\n"+ | ||
"for(i in 0 .. 9){\n"+ | ||
" l.add(i);\n"+ | ||
" assert i > -1;\n"+ | ||
"}\n"+ | ||
"println(l);\n"+ | ||
"assert l.length == 10;\n"+ | ||
"assert l[2] == 2;\n"+ | ||
"assert l[3] == 3;\n"+ | ||
"assert l[9] == 9;\n"; | ||
|
||
private static final String SOURCE_2 = | ||
"for(let i in 0 to 9){\n"+ | ||
" assert i > -1;\n"+ | ||
"}\n"+ | ||
"let l = [];\n"+ | ||
"for(var i in 0 to 9){\n"+ | ||
" l.add(i);\n"+ | ||
" assert i > -1;\n"+ | ||
"}\n"+ | ||
"println(l);\n"+ | ||
"assert l.length == 10;\n"+ | ||
"assert l[2] == 2;\n"+ | ||
"assert l[3] == 3;\n"+ | ||
"assert l[9] == 9;\n"; | ||
|
||
private static final String SOURCE_3 = | ||
"for(let i in 0 from 9){\n"+ | ||
" assert i > -1;\n"+ | ||
"}\n"; | ||
|
||
"}\n"+ | ||
"let l = [];\n"+ | ||
"for(var i in 0 from 9){\n"+ | ||
" l.add(i);\n"+ | ||
" assert i > -1;\n"+ | ||
"}\n"+ | ||
"println(l);\n"+ | ||
"assert l.length == 10;\n"+ | ||
"assert l[0] == 9;\n"+ | ||
"assert l[9] == 0;\n"; | ||
|
||
public void testForInLoop() throws Exception { | ||
Compiler compiler = ClassPathCompilerBuilder.createCompiler(); | ||
Executable executable = compiler.compile(SOURCE_1); | ||
executable.execute(); | ||
assertScriptExecutes(SOURCE_1); | ||
assertScriptExecutes(SOURCE_2); | ||
assertScriptExecutes(SOURCE_3); | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
snap-tree/src/main/java/org/snapscript/tree/collection/RangeOperator.java
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,44 @@ | ||
package org.snapscript.tree.collection; | ||
|
||
import org.snapscript.parse.StringToken; | ||
|
||
public enum RangeOperator { | ||
DOT("..", true), | ||
TO("to", true), | ||
FROM("from", false); | ||
|
||
public final String operator; | ||
public final boolean forward; | ||
|
||
private RangeOperator(String operator, boolean forward) { | ||
this.operator = operator; | ||
this.forward = forward; | ||
} | ||
|
||
public boolean isForward() { | ||
return forward; | ||
} | ||
|
||
public boolean isReverse() { | ||
return !forward; | ||
} | ||
|
||
public static RangeOperator resolveOperator(StringToken token) { | ||
if(token != null) { | ||
String value = token.getValue(); | ||
|
||
for(RangeOperator operator : VALUES) { | ||
if (operator.operator.equals(value)) { | ||
return operator; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static final RangeOperator[] VALUES = { | ||
DOT, | ||
TO, | ||
FROM | ||
}; | ||
} |
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