Skip to content

Commit

Permalink
[compiler] WIP BL-28
Browse files Browse the repository at this point in the history
  • Loading branch information
mctaverna committed Oct 17, 2023
1 parent 57cbe6a commit 7675769
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public Node transform( BoxNode node, TransformerContext context ) throws Illegal
* Escape a give String to make it safe to be printed or stored.
*
* @param s The input String.
*
*
* @return The output String.
**/
private String escape( String s ) {
String temp = s.replace( "\"\"", "\"" );
temp = temp.replace( "''", "\"" );
temp = temp.replace( "''", "'" );
temp = temp.replace( "''", "'" );
temp = temp.replace( "##", "#" );
return temp.replace( "\\", "\\\\" )
.replace( "\t", "\\t" )
Expand Down
191 changes: 134 additions & 57 deletions src/test/java/ortus/boxlang/compiler/TestExecution.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ortus.boxlang.compiler;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.File;
import java.io.IOException;
Expand All @@ -9,10 +11,15 @@

import com.github.javaparser.ast.Node;

import ortus.boxlang.executor.JavaRunner;
import ortus.boxlang.parser.BoxParser;
import ortus.boxlang.parser.BoxScriptType;
import ortus.boxlang.parser.BoxParser;
import ortus.boxlang.parser.ParsingResult;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.context.ScriptingBoxContext;
import ortus.boxlang.runtime.scopes.IScope;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.scopes.VariablesScope;
import ortus.boxlang.transpiler.BoxLangTranspiler;

/**
Expand All @@ -33,72 +40,142 @@
public class TestExecution extends TestBase {

@Test
public void executeWhile() throws IOException {
BoxRuntime instance = BoxRuntime.getInstance( true );
IBoxContext context = new ScriptingBoxContext( instance.getRuntimeContext() );
IScope variables = context.getScopeNearby( VariablesScope.name );

instance.executeSource( """
variables['system'] = createObject('java','java.lang.System');
a = 1;
while(a < 10) {
switch(variables.a) {
case 0: {
variables.system.out.println("zero");
break;
}
default: {
variables.system.out.println("non zero");
break;
}
}
if(!a % 2 == 0) {
variables.system.out.println("even and a=#variables.a#");
}
a +=1;
}
assert(variables["a"] == 10);
""" );

instance.shutdown();

public void executeFreeStyle() throws IOException {
BoxParser parser = new BoxParser();
ParsingResult result = parser.parse( new File( "examples/cf_to_java/freestyle/freestyle.cfm" ) );
result.getIssues().forEach( it -> System.out.println( it ) );
assertTrue( result.isCorrect() );
}

BoxLangTranspiler transpiler = new BoxLangTranspiler();
Node javaAST = transpiler.transpile( result.getRoot() );
new JavaRunner().run( transpiler.getStatements() );
@Test
public void executeFor() throws IOException {
BoxRuntime instance = BoxRuntime.getInstance( true );
IBoxContext context = new ScriptingBoxContext( instance.getRuntimeContext() );

instance.executeSource( """
variables['system'] = createObject('java','java.lang.System');
variables.a = 0;
for(a = 0; a < 10; a++){
variables.system.out.println(a);
}
assert(variables["a"] == 10);
""" );
instance.shutdown();
}

@Test
public void executeWhile() throws IOException {
public void comparison() throws IOException {
BoxRuntime instance = BoxRuntime.getInstance( true );
IBoxContext context = new ScriptingBoxContext( instance.getRuntimeContext() );

String statement = """
variables['system'] = createObject('java','java.lang.System');
a = 1;
while(a < 10) {
switch(variables.a) {
case 0: {
variables.system.out.println("zero");
break;
}
default: {
variables.system.out.println("non zero");
break;
}
}
if(!a % 2 == 0) {
variables.system.out.println("even and a=#variables.a#");
}
a +=1;
}
//assert(variables["a"] == 10);
""";
BoxParser parser = new BoxParser();
ParsingResult result = parser.parse( statement, BoxScriptType.CFSCRIPT );
assertTrue( result.isCorrect() );

BoxLangTranspiler transpiler = new BoxLangTranspiler();
Node javaAST = transpiler.transpile( result.getRoot() );
new JavaRunner().run( transpiler.getStatements() );
Object result = instance.executeStatement( "6 > 5", context );
assertThat( result ).isEqualTo( true );

result = instance.executeStatement( "5 LT 10", context );
assertThat( result ).isEqualTo( true );

// not implemented
result = instance.executeStatement( "5 LESS THAN 10", context );
assertThat( result ).isEqualTo( true );

instance.shutdown();
}

@Test
public void executeFor() throws IOException {
public void testDoWhileLoop() {
BoxRuntime instance = BoxRuntime.getInstance( true );
IBoxContext context = new ScriptingBoxContext( instance.getRuntimeContext() );
IScope variables = context.getScopeNearby( VariablesScope.name );
// Don't know what " no viable alternative at input" mwans
instance.executeSource(
"""
do {
result = variables.result + 1;
} while( result < 10 );
""",
context );

// assertThat( variables.dereference( result, false ) ).isEqualTo( 10 );

}

String statement = """
variables['system'] = createObject('java','java.lang.System');
variables.a = 0;
for(a = 0; a < 10; a++){
variables.system.out.println(a);
}
assert(variables["a"] == 10);
""";
BoxParser parser = new BoxParser();
ParsingResult result = parser.parse( statement, BoxScriptType.CFSCRIPT );
assertTrue( result.isCorrect() );

BoxLangTranspiler transpiler = new BoxLangTranspiler();
Node javaAST = transpiler.transpile( result.getRoot() );
new JavaRunner().run( transpiler.getStatements() );
@Test
public void testCastAs() {

BoxRuntime instance = BoxRuntime.getInstance( true );
IBoxContext context = new ScriptingBoxContext( instance.getRuntimeContext() );
// variable sdf should not exist, therefore an error needs to be thrown
assertThrows( Throwable.class, () -> instance.executeStatement( "5 castAs sdf",

context ) );

// castAs keyword doesn't seem to be implemented-- the parser is just directly returning the 5 as a literal
Object result = instance.executeStatement( "5 castAs 'String'", context );
assertThat( result ).isEqualTo( "5" );
assertThat( result.getClass().getName() ).isEqualTo( "java.lang.String" );
}

@Test
public void testTernary() {

BoxRuntime instance = BoxRuntime.getInstance( true );
IBoxContext context = new ScriptingBoxContext( instance.getRuntimeContext() );

// variable sdf should not exist, therefore an error needs to be thrown
instance.executeStatement(
"""
tmp = true;
result = tmp ? 'itwastrue' : 'itwasfalse'
""",

context );
assertThat( context.getScopeNearby( VariablesScope.name ).dereference( Key.of( "result" ), false ) ).isEqualTo( "itwastrue" );
}

@Test
public void testString3() {

BoxRuntime instance = BoxRuntime.getInstance( true );
IBoxContext context = new ScriptingBoxContext( instance.getRuntimeContext() );
IScope variables = context.getScopeNearby( VariablesScope.name );
// variable sdf should not exist, therefore an error needs to be thrown
instance.executeSource(
"""
// To escape a quote char, double it.
test4 = "Brad ""the guy"" Wood"
test5 = 'Luis ''the man'' Majano'
""",
context );

assertThat( variables.dereference( Key.of( "test4" ), false ) ).isEqualTo( "Brad \"the guy\" Wood" );
assertThat( variables.dereference( Key.of( "test5" ), false ) ).isEqualTo( "Luis 'the man' Majano" );
}

}

0 comments on commit 7675769

Please sign in to comment.