From 5c0c0a3a4479533344afce705f3e6bd6aace9f30 Mon Sep 17 00:00:00 2001 From: mctaverna Date: Mon, 11 Sep 2023 16:39:39 +0200 Subject: [PATCH] [compiler] Added for in statement --- .../ourtus/boxlang/parser/BoxCFParser.java | 10 ++ .../boxlang/transpiler/BoxLangTranspiler.java | 2 + compiler/src/test/java/TestStatements.java | 138 ++++++++++++++---- 3 files changed, 124 insertions(+), 26 deletions(-) diff --git a/compiler/src/main/java/ourtus/boxlang/parser/BoxCFParser.java b/compiler/src/main/java/ourtus/boxlang/parser/BoxCFParser.java index 5755a3106..bea9c9d3c 100644 --- a/compiler/src/main/java/ourtus/boxlang/parser/BoxCFParser.java +++ b/compiler/src/main/java/ourtus/boxlang/parser/BoxCFParser.java @@ -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 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 cases = new ArrayList<>(); diff --git a/compiler/src/main/java/ourtus/boxlang/transpiler/BoxLangTranspiler.java b/compiler/src/main/java/ourtus/boxlang/transpiler/BoxLangTranspiler.java index 509679656..fb9049ad0 100644 --- a/compiler/src/main/java/ourtus/boxlang/transpiler/BoxLangTranspiler.java +++ b/compiler/src/main/java/ourtus/boxlang/transpiler/BoxLangTranspiler.java @@ -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() { } diff --git a/compiler/src/test/java/TestStatements.java b/compiler/src/test/java/TestStatements.java index 920ca7ab5..2e9d1454b 100644 --- a/compiler/src/test/java/TestStatements.java +++ b/compiler/src/test/java/TestStatements.java @@ -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( """ @@ -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(""" @@ -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() ); @@ -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( @@ -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( @@ -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());