Skip to content

Commit

Permalink
[compiler] Added for in statement
Browse files Browse the repository at this point in the history
  • Loading branch information
mctaverna committed Sep 11, 2023
1 parent 5640d27 commit 5c0c0a3
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 26 deletions.
10 changes: 10 additions & 0 deletions compiler/src/main/java/ourtus/boxlang/parser/BoxCFParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,21 @@ private BoxStatement toAst( File file, CFParser.StatementContext node, Node pare
return toAst(file,node.continue_(),parent);
} else if ( node.switch_() != null ) {
return toAst(file,node.switch_(),parent);
} else if ( node.for_() != null ) {
return toAst(file,node.for_(),parent);
} else {
throw new IllegalStateException( "not implemented: " + node.getClass().getSimpleName() );
}
}

private BoxStatement toAst(File file, CFParser.ForContext node, Node parent) {
BoxExpr variable = toAst(file,node.identifier(),parent);
BoxExpr collection = toAst(file,node.expression(),parent);
List<BoxStatement> body = toAst(file,node.statementBlock(),parent);

return new BoxForIn(variable,collection,body,getPosition(node),getSourceText(node));
}

private BoxStatement toAst(File file, CFParser.SwitchContext node, Node parent) {
BoxExpr condition = toAst(file,node.expression(),parent);
List<BoxSwitchCase> cases = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ public class BoxLangTranspiler {
put(BoxLocalDeclaration.class,new BoxLocalDeclarationTransformer());
put(BoxIfElse.class,new BoxIfElseTransformer());
put(BoxWhile.class,new BoxWhileTransformer());
put(BoxSwitch.class,new BoxSwitchTransformer());
put(BoxBreak.class,new BoxBreakTransformer());
put(BoxContinue.class,new BoxContinueTransformer());
put(BoxForIn.class,new BoxForInTransformer());

}};
public BoxLangTranspiler() { }
Expand Down
138 changes: 112 additions & 26 deletions compiler/src/test/java/TestStatements.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class TestStatements extends TestBase {

public ParsingResult parseStatement(String statement) throws IOException {
BoxLangParser parser = new BoxLangParser();
ParsingResult result = parser.parseStatement( statement );
assertTrue(result.isCorrect());
return result;
}

@Test
public void invokeMethod() throws IOException {
String expression = """
String statement = """
myObject.myMethod( obj1, "foo", 42 )
""";

BoxLangParser parser = new BoxLangParser();
ParsingResult result = parser.parseStatement( expression );
ParsingResult result = parseStatement(statement);
Node javaAST = BoxLangTranspiler.transform( result.getRoot() );

assertEqualsNoWhiteSpaces( """
Expand All @@ -33,14 +41,13 @@ public void invokeMethod() throws IOException {
""", javaAST.toString() );
} @Test
public void invokeMethodWithKnownScope() throws IOException {
String expression = """
String statement = """
variables.system.out.println(
"hello world"
)
""";

BoxLangParser parser = new BoxLangParser();
ParsingResult result = parser.parseStatement( expression );
ParsingResult result = parseStatement(statement);
Node javaAST = BoxLangTranspiler.transform( result.getRoot() );

assertEqualsNoWhiteSpaces("""
Expand All @@ -57,27 +64,23 @@ public void invokeMethodWithKnownScope() throws IOException {

@Test
public void assigment() throws IOException {
String expression = """
String statement = """
variables["system"] = "Hello"
""";

BoxLangParser parser = new BoxLangParser();
ParsingResult result = parser.parseStatement( expression );

ParsingResult result = parseStatement(statement);
Node javaAST = BoxLangTranspiler.transform( result.getRoot() );

assertEquals( "variablesScope.put(Key.of(\"system\"), \"Hello\");", javaAST.toString() );

}

@Test
public void var() throws IOException {
String expression = """
String statement = """
var a = b = 1/0;
""";

BoxLangParser parser = new BoxLangParser();
ParsingResult result = parser.parseStatement( expression );
ParsingResult result = parseStatement(statement);

BlockStmt javaAST = (BlockStmt)BoxLangTranspiler.transform( result.getRoot() );
assertEquals( "context.getScopeNearby(Key.of(LocalScope.name)).put(Key.of(\"a\"), Divide.invoke(1, 0));", javaAST.getStatements().get(0).toString() );
Expand All @@ -95,8 +98,7 @@ public void ifElse() throws IOException {
}
""";

BoxLangParser parser = new BoxLangParser();
ParsingResult result = parser.parseStatement( statement );
ParsingResult result = parseStatement(statement);

Node javaAST = BoxLangTranspiler.transform( result.getRoot() );
assertEqualsNoWhiteSpaces(
Expand All @@ -118,8 +120,7 @@ public void while_() throws IOException {
}
""";

BoxLangParser parser = new BoxLangParser();
ParsingResult result = parser.parseStatement( statement );
ParsingResult result = parseStatement(statement);

Node javaAST = BoxLangTranspiler.transform( result.getRoot() );
assertEqualsNoWhiteSpaces(
Expand All @@ -131,21 +132,106 @@ public void while_() throws IOException {
, javaAST.toString());
}
@Test
public void case_() throws IOException {
public void case1_() throws IOException {
String statement = """
while( variables.a == true ) {
variables.a = false;
}
switch(variables['a']) {
case "9": {
variables['a'] = "0";
break;
}
case "1": {
variables['a'] = "1";
break;
}
default: {
variables['a'] = "default";
break;
}
}
""";

BoxLangParser parser = new BoxLangParser();
ParsingResult result = parser.parseStatement( statement );
ParsingResult result = parseStatement(statement);

Node javaAST = BoxLangTranspiler.transform( result.getRoot() );
System.out.println(javaAST);
assertEqualsNoWhiteSpaces(
"""
while(BooleanCaster.cast(EqualsEquals.invoke(variablesScope.get(Key.of("a")),true))){
variablesScope.put(Key.of("a"),false);
do {
if (BooleanCaster.cast(EqualsEquals.invoke((variablesScope.get(Key.of("a"))), "9"))) {
variablesScope.put(Key.of("a"), "0");
break;
}
if (BooleanCaster.cast(EqualsEquals.invoke((variablesScope.get(Key.of("a"))), "1"))) {
variablesScope.put(Key.of("a"), "1");
break;
}
variablesScope.put(Key.of("a"), "default");
break;
} while (false);
"""
, javaAST.toString());
}
@Test
public void case2_() throws IOException {
String statement = """
switch(0) {
case variables['a'] > "0": {
variables['a'] = "0";
break;
}
case variables['a'] < "1": {
variables['a'] = "1";
break;
}
default: {
variables['a'] = "default";
break;
}
}
""";

ParsingResult result = parseStatement(statement);

Node javaAST = BoxLangTranspiler.transform( result.getRoot() );
System.out.println(javaAST);
assertEqualsNoWhiteSpaces(
"""
do {
if (BooleanCaster.cast(GreaterThan.invoke(variablesScope.get(Key.of("a")), "0"))) {
variablesScope.put(Key.of("a"), "0");
break;
}
if (BooleanCaster.cast(LessThan.invoke(variablesScope.get(Key.of("a")), "1"))) {
variablesScope.put(Key.of("a"), "1");
break;
}
variablesScope.put(Key.of("a"), "default");
break;
} while (false);
"""
, javaAST.toString());
}

@Test
public void for_() throws IOException {
String statement = """
for( keyName in variables ) {
variables['a'] = variables['a'] + 1;
}
""";

ParsingResult result = parseStatement(statement);

Node javaAST = BoxLangTranspiler.transform( result.getRoot() );
System.out.println(javaAST);
assertEqualsNoWhiteSpaces(
"""
{
Iterator keyName = CollectionCaster.cast(variablesScope).iterator();
while (keyName.hasNext()) {
variablesScope.put(Key.of("keyName"), keyName.next());
variablesScope.put(Key.of("a"), Plus.invoke(variablesScope.get(Key.of("a")), 1));
}
}
"""
, javaAST.toString());
Expand Down

0 comments on commit 5c0c0a3

Please sign in to comment.