Skip to content

Commit

Permalink
Merge pull request #138 from ortus-boxlang/development
Browse files Browse the repository at this point in the history
Beta 22
  • Loading branch information
jclausen authored Nov 15, 2024
2 parents 0a9369c + 5bd1fff commit ba8664d
Show file tree
Hide file tree
Showing 78 changed files with 1,341 additions and 275 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
java-version: ${{ env.JDK }}

- name: Setup Gradle
uses: gradle/gradle-build-action@v3
uses: gradle/actions/setup-gradle@v4
with:
gradle-version: ${{ env.GRADLE }}

Expand Down Expand Up @@ -218,7 +218,7 @@ jobs:
path: .tmp

- name: Setup Gradle
uses: gradle/gradle-build-action@v3
uses: gradle/actions/setup-gradle@v4
with:
gradle-version: ${{ env.GRADLE }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scans.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
java-version: ${{ env.JDK }}

- name: Setup Gradle
uses: gradle/gradle-build-action@v3
uses: gradle/actions/setup-gradle@v4
with:
gradle-version: "8.7"

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
java-version: ${{ matrix.jdkVersion }}

- name: Setup Gradle
uses: gradle/gradle-build-action@v3
uses: gradle/actions/setup-gradle@v4
with:
gradle-version: "8.7"

Expand Down Expand Up @@ -72,8 +72,8 @@ jobs:

- name: Test Module
run: |
gradle :src:modules:test:build -Dboxlang.experimental.compiler=${{ matrix.compiler }} --stacktrace --console=plain
gradle test --stacktrace --console=plain
gradle :src:modules:test:build "-Dboxlang.experimental.compiler=${{ matrix.compiler }}" --stacktrace --console=plain
gradle test "-Dboxlang.experimental.compiler=${{ matrix.compiler }}" --stacktrace --console=plain
- name: Upload Test Results
if: always()
Expand Down
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,19 @@ dependencies {
// https://mvnrepository.com/artifact/org.apache.commons/commons-cli
implementation "commons-cli:commons-cli:1.9.0"
// https://mvnrepository.com/artifact/com.fasterxml.jackson.jr/jackson-jr-objects
implementation 'com.fasterxml.jackson.jr:jackson-jr-objects:2.18.0'
implementation 'com.fasterxml.jackson.jr:jackson-jr-objects:2.18.1'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.jr/jackson-jr-extension-javatime
implementation 'com.fasterxml.jackson.jr:jackson-jr-extension-javatime:2.18.0'
implementation 'com.fasterxml.jackson.jr:jackson-jr-extension-javatime:2.18.1'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.jr/jackson-jr-stree
implementation 'com.fasterxml.jackson.jr:jackson-jr-stree:2.18.0'
implementation 'com.fasterxml.jackson.jr:jackson-jr-stree:2.18.1'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.jr/jackson-jr-annotation-support
implementation 'com.fasterxml.jackson.jr:jackson-jr-annotation-support:2.18.0'
implementation 'com.fasterxml.jackson.jr:jackson-jr-annotation-support:2.18.1'
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
implementation 'org.slf4j:slf4j-api:2.0.16'
// https://mvnrepository.com/artifact/ch.qos.logback/logback-classic
implementation 'ch.qos.logback:logback-classic:1.5.12'
// https://mvnrepository.com/artifact/com.zaxxer/HikariCP
implementation 'com.zaxxer:HikariCP:6.0.0'
implementation 'com.zaxxer:HikariCP:6.1.0'
// https://mvnrepository.com/artifact/org.ow2.asm/asm-tree
implementation 'org.ow2.asm:asm-tree:9.7.1'
// https://mvnrepository.com/artifact/org.ow2.asm/asm-util
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Fri Oct 25 14:06:18 UTC 2024
#Fri Nov 01 20:41:48 UTC 2024
antlrVersion=4.13.1
jdkVersion=21
version=1.0.0-beta21
version=1.0.0-beta22
32 changes: 32 additions & 0 deletions src/main/java/ortus/boxlang/compiler/asmboxpiler/AsmHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.LineNumberNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.TypeInsnNode;
Expand Down Expand Up @@ -58,6 +60,36 @@

public class AsmHelper {

public record LineNumberIns( List<AbstractInsnNode> start, List<AbstractInsnNode> end ) {

}

public static LineNumberIns translatePosition( BoxNode node ) {
LabelNode start = new LabelNode();
LabelNode end = new LabelNode();

return new LineNumberIns(
List.of( start, new LineNumberNode( node.getPosition().getStart().getLine(), start ) ),
List.of( end, new LineNumberNode( node.getPosition().getEnd().getLine(), end ) )
);
}

public static List<AbstractInsnNode> addLineNumberLabels( List<AbstractInsnNode> nodes, BoxNode node ) {
LabelNode start = new LabelNode();
LabelNode end = new LabelNode();

if ( node.getPosition() == null ) {
return nodes;
}

nodes.add( 0, start );
nodes.add( 1, new LineNumberNode( node.getPosition().getStart().getLine(), start ) );
nodes.add( end );
nodes.add( new LineNumberNode( node.getPosition().getStart().getLine(), end ) );

return nodes;
}

public static List<AbstractInsnNode> generateMapOfAbstractMethodNames( Transpiler transpiler, BoxNode classOrInterface ) {
List<List<AbstractInsnNode>> methodKeyLists = classOrInterface.getDescendantsOfType( BoxFunctionDeclaration.class )
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import ortus.boxlang.compiler.asmboxpiler.transformer.expression.BoxExpressionStatementTransformer;
import ortus.boxlang.compiler.asmboxpiler.transformer.expression.BoxFQNTransformer;
import ortus.boxlang.compiler.asmboxpiler.transformer.expression.BoxFunctionInvocationTransformer;
import ortus.boxlang.compiler.asmboxpiler.transformer.expression.BoxFunctionalBIFAccessTransformer;
import ortus.boxlang.compiler.asmboxpiler.transformer.expression.BoxFunctionalMemberAccessTransformer;
import ortus.boxlang.compiler.asmboxpiler.transformer.expression.BoxIdentifierTransformer;
import ortus.boxlang.compiler.asmboxpiler.transformer.expression.BoxImportTransformer;
import ortus.boxlang.compiler.asmboxpiler.transformer.expression.BoxIntegerLiteralTransformer;
Expand Down Expand Up @@ -95,6 +97,8 @@
import ortus.boxlang.compiler.ast.expression.BoxExpressionInvocation;
import ortus.boxlang.compiler.ast.expression.BoxFQN;
import ortus.boxlang.compiler.ast.expression.BoxFunctionInvocation;
import ortus.boxlang.compiler.ast.expression.BoxFunctionalBIFAccess;
import ortus.boxlang.compiler.ast.expression.BoxFunctionalMemberAccess;
import ortus.boxlang.compiler.ast.expression.BoxIdentifier;
import ortus.boxlang.compiler.ast.expression.BoxIntegerLiteral;
import ortus.boxlang.compiler.ast.expression.BoxLambda;
Expand Down Expand Up @@ -215,6 +219,8 @@ public AsmTranspiler() {
registry.put( BoxTemplateIsland.class, new BoxTemplateIslandTransformer( this ) );
registry.put( BoxExpressionInvocation.class, new BoxExpressionInvocationTransformer( this ) );
registry.put( BoxParam.class, new BoxParamTransformer( this ) );
registry.put( BoxFunctionalBIFAccess.class, new BoxFunctionalBIFAccessTransformer( this ) );
registry.put( BoxFunctionalMemberAccess.class, new BoxFunctionalMemberAccessTransformer( this ) );
}

@Override
Expand All @@ -228,6 +234,8 @@ public ClassNode transpile( BoxScript boxScript ) throws BoxRuntimeException {
String relativePath = getProperty( "relativePath" );
Source source = boxScript.getPosition().getSource();
String filePath = source instanceof SourceFile file && file.getFile() != null ? file.getFile().getAbsolutePath() : "unknown";
setProperty( "filePath", filePath );
classNode.visitSource( filePath, null );

String baseClassName = getProperty( "baseclass" ) != null ? getProperty( "baseclass" ) : "BoxScript";

Expand Down Expand Up @@ -311,7 +319,7 @@ public ClassNode transpile( BoxScript boxScript ) throws BoxRuntimeException {

methodVisitor.visitFieldInsn( Opcodes.GETSTATIC,
Type.getInternalName( BoxSourceType.class ),
"BOXSCRIPT",
getProperty( "sourceType" ).toUpperCase(),
Type.getDescriptor( BoxSourceType.class ) );
methodVisitor.visitFieldInsn( Opcodes.PUTSTATIC,
type.getInternalName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import ortus.boxlang.runtime.operators.Plus;
import ortus.boxlang.runtime.scopes.IScope;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.scopes.LocalScope;
import ortus.boxlang.runtime.types.exceptions.ExpressionException;

public class BoxAssignmentTransformer extends AbstractTransformer {
Expand All @@ -68,14 +69,22 @@ public BoxAssignmentTransformer( AsmTranspiler transpiler ) {

@Override
public List<AbstractInsnNode> transform( BoxNode node, TransformerContext context, ReturnValueContext returnContext ) throws IllegalStateException {
BoxAssignment assigment = ( BoxAssignment ) node;
BoxAssignment assignment = ( BoxAssignment ) node;
List<AbstractInsnNode> nodes = null;

if ( assigment.getOp() == BoxAssignmentOperator.Equal ) {
List<AbstractInsnNode> jRight = transpiler.transform( assigment.getRight(), TransformerContext.NONE, ReturnValueContext.VALUE );
nodes = transformEquals( assigment.getLeft(), jRight, assigment.getOp(), assigment.getModifiers() );
if ( assignment.getOp() == null ) {
if ( ! ( assignment.getLeft() instanceof BoxIdentifier ) ) {
throw new ExpressionException( "You cannot declare a variable using " + assignment.getLeft().getClass().getSimpleName(),
assignment.getPosition(),
assignment.getSourceText() );
}

nodes = assignNullValue( ( BoxIdentifier ) assignment.getLeft() );
} else if ( assignment.getOp() == BoxAssignmentOperator.Equal ) {
List<AbstractInsnNode> jRight = transpiler.transform( assignment.getRight(), TransformerContext.NONE, ReturnValueContext.VALUE );
nodes = transformEquals( assignment.getLeft(), jRight, assignment.getOp(), assignment.getModifiers() );
} else {
nodes = transformCompoundEquals( assigment );
nodes = transformCompoundEquals( assignment );
}

if ( returnContext.empty ) {
Expand Down Expand Up @@ -433,4 +442,40 @@ private Class<?> getMethodCallTemplate( BoxAssignment assignment ) {
};
}

private List<AbstractInsnNode> assignNullValue( BoxIdentifier name ) {
List<AbstractInsnNode> nodes = new ArrayList<>();
transpiler.getCurrentMethodContextTracker().ifPresent( t -> nodes.addAll( t.loadCurrentContext() ) );

nodes.add( new InsnNode( Opcodes.DUP ) );
nodes.add( new FieldInsnNode( Opcodes.GETSTATIC,
Type.getInternalName( LocalScope.class ),
"name",
Type.getDescriptor( Key.class ) ) );
nodes.add( new InsnNode( Opcodes.ACONST_NULL ) );

nodes.add( new MethodInsnNode( Opcodes.INVOKEINTERFACE,
Type.getInternalName( IBoxContext.class ),
"scopeFindNearby",
Type.getMethodDescriptor( Type.getType( IBoxContext.ScopeSearchResult.class ),
Type.getType( Key.class ),
Type.getType( IScope.class ) ),
true ) );

nodes.add( new InsnNode( Opcodes.ACONST_NULL ) );
nodes.addAll( AsmHelper.array( Type.getType( Key.class ), List.of( transpiler.createKey( ( name.getName() ) ) ) ) );

nodes.add( new MethodInsnNode(
Opcodes.INVOKESTATIC,
Type.getInternalName( Referencer.class ),
"setDeep",
Type.getMethodDescriptor( Type.getType( Object.class ),
Type.getType( IBoxContext.class ),
Type.getType( IBoxContext.ScopeSearchResult.class ),
Type.getType( Object.class ),
Type.getType( Key[].class ) ),
false ) );

return nodes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,23 @@ public List<AbstractInsnNode> transform( BoxNode node, TransformerContext contex
MethodContextTracker tracker = transpiler.getCurrentMethodContextTracker().get();
List<AbstractInsnNode> nodes = new ArrayList<AbstractInsnNode>();

if ( returnContext.nullable || exitsAllowed.equals( ExitsAllowed.FUNCTION ) ) {
BoxNode labelTarget = tracker.getStringLabel( breakNode.getLabel() );
if ( labelTarget == null ) {
labelTarget = getTargetAncestor( breakNode );
}

int intermediateCount = countIntermediateLoops( labelTarget, breakNode );

for ( int i = 0; i < intermediateCount; i++ ) {
nodes.add( new InsnNode( Opcodes.POP ) );
}

if ( returnContext.nullable
|| exitsAllowed.equals( ExitsAllowed.FUNCTION ) ) {
nodes.add( new InsnNode( Opcodes.ACONST_NULL ) );
}

BoxNode labelTarget = tracker.getStringLabel( breakNode.getLabel() );
LabelNode currentBreak = tracker.getBreak( labelTarget != null ? labelTarget : getTargetAncestor( breakNode ) );
LabelNode currentBreak = tracker.getBreak( labelTarget != null ? labelTarget : getTargetAncestor( breakNode ) );

if ( currentBreak != null ) {
if ( returnContext.nullable && nodes.size() == 0 ) {
Expand Down Expand Up @@ -106,4 +117,20 @@ public BoxNode getTargetAncestor( BoxNode node ) {
BoxForIndex.class, BoxForIn.class,
BoxWhile.class );
}

public int countIntermediateLoops( BoxNode target, BoxBreak breakNode ) {
int count = 0;

BoxNode parent = breakNode.getParent();

while ( parent != target ) {
if ( parent instanceof BoxWhile || parent instanceof BoxDo || parent instanceof BoxForIn || parent instanceof BoxForIndex ) {
count++;
}

parent = parent.getParent();
}

return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import java.util.List;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.JumpInsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;

import ortus.boxlang.compiler.asmboxpiler.AsmHelper;
import ortus.boxlang.compiler.asmboxpiler.MethodContextTracker;
Expand All @@ -39,6 +42,7 @@
import ortus.boxlang.compiler.ast.statement.BoxFunctionDeclaration;
import ortus.boxlang.compiler.ast.statement.BoxWhile;
import ortus.boxlang.compiler.ast.statement.component.BoxComponent;
import ortus.boxlang.runtime.components.Component;

public class BoxContinueTransformer extends AbstractTransformer {

Expand Down Expand Up @@ -71,20 +75,23 @@ public List<AbstractInsnNode> transform( BoxNode node, TransformerContext contex
}

if ( exitsAllowed.equals( ExitsAllowed.COMPONENT ) ) {
// template = "if(true) return Component.BodyResult.ofBreak(" + componentLabel + ");";
nodes.add( new LdcInsnNode( "" ) );
nodes.add( new MethodInsnNode( Opcodes.INVOKESTATIC,
Type.getInternalName( Component.BodyResult.class ),
"ofBreak",
Type.getMethodDescriptor( Type.getType( Component.BodyResult.class ), Type.getType( String.class ) ),
false )
);
nodes.add( new InsnNode( Opcodes.ARETURN ) );
return nodes;
} else if ( exitsAllowed.equals( ExitsAllowed.LOOP ) ) {
nodes.add( new JumpInsnNode( Opcodes.GOTO, currentBreak ) );
return nodes;
// template = "if(true) break " + breakLabel + ";";
} else if ( exitsAllowed.equals( ExitsAllowed.FUNCTION ) ) {
nodes.add( new InsnNode( Opcodes.ARETURN ) );
return nodes;
} else {
// template = "if(true) return;";
}
// if ( currentBreak == null ) {
// throw new RuntimeException( "Cannot break from current location" );
// }

throw new RuntimeException( "Cannot continue from current location - processing: " + transpiler.getProperty( "relativePath" ) );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@
*/
package ortus.boxlang.compiler.asmboxpiler.transformer.expression;

import java.math.BigDecimal;
import java.util.List;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.TypeInsnNode;

import ortus.boxlang.compiler.asmboxpiler.Transpiler;
import ortus.boxlang.compiler.asmboxpiler.transformer.AbstractTransformer;
import ortus.boxlang.compiler.asmboxpiler.transformer.ReturnValueContext;
import ortus.boxlang.compiler.asmboxpiler.transformer.TransformerContext;
import ortus.boxlang.compiler.ast.BoxNode;
import ortus.boxlang.compiler.ast.expression.BoxDecimalLiteral;
import ortus.boxlang.runtime.types.util.MathUtil;

public class BoxDecimalLiteralTransformer extends AbstractTransformer {

Expand All @@ -38,6 +42,21 @@ public BoxDecimalLiteralTransformer( Transpiler transpiler ) {
@Override
public List<AbstractInsnNode> transform( BoxNode node, TransformerContext context, ReturnValueContext returnContext ) throws IllegalStateException {
BoxDecimalLiteral literal = ( BoxDecimalLiteral ) node;

if ( MathUtil.isHighPrecisionMath() ) {
return List.of(
new TypeInsnNode( Opcodes.NEW, Type.getInternalName( BigDecimal.class ) ),
new InsnNode( Opcodes.DUP ),
new LdcInsnNode( literal.getValue() ),
new MethodInsnNode( Opcodes.INVOKESPECIAL,
Type.getInternalName( BigDecimal.class ),
"<init>",
Type.getMethodDescriptor( Type.VOID_TYPE, Type.getType( String.class ) ),
false
)
);
}

return List.of( new LdcInsnNode( Double.valueOf( literal.getValue() ) ), new MethodInsnNode( Opcodes.INVOKESTATIC,
Type.getInternalName( Double.class ),
"valueOf",
Expand Down
Loading

0 comments on commit ba8664d

Please sign in to comment.