Browse Source

Changed how captured fields are gotten, still needs improvement!

kindlich 6 years ago
parent
commit
5f4fb90d5d
No known key found for this signature in database

+ 35
- 27
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java View File

7
 import org.openzen.zencode.shared.CompileException;
7
 import org.openzen.zencode.shared.CompileException;
8
 import org.openzen.zencode.shared.CompileExceptionCode;
8
 import org.openzen.zencode.shared.CompileExceptionCode;
9
 import org.openzen.zenscript.codemodel.CompareType;
9
 import org.openzen.zenscript.codemodel.CompareType;
10
-import org.openzen.zenscript.codemodel.FunctionHeader;
11
-import org.openzen.zenscript.codemodel.FunctionParameter;
12
 import org.openzen.zenscript.codemodel.expression.*;
10
 import org.openzen.zenscript.codemodel.expression.*;
13
 import org.openzen.zenscript.codemodel.member.ref.ConstMemberRef;
11
 import org.openzen.zenscript.codemodel.member.ref.ConstMemberRef;
14
 import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
12
 import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
1575
 		final JavaWriter functionWriter = new JavaWriter(lambdaCW, methodInfo, null, signature, null, "java/lang/Override");
1573
 		final JavaWriter functionWriter = new JavaWriter(lambdaCW, methodInfo, null, signature, null, "java/lang/Override");
1576
 
1574
 
1577
 
1575
 
1578
-
1579
 		javaWriter.newObject(name);
1576
 		javaWriter.newObject(name);
1580
 		javaWriter.dup();
1577
 		javaWriter.dup();
1581
 
1578
 
1608
 		constructorWriter.end();
1605
 		constructorWriter.end();
1609
 
1606
 
1610
 
1607
 
1611
-
1612
         functionWriter.start();
1608
         functionWriter.start();
1613
 
1609
 
1614
 
1610
 
1615
-		final JavaStatementVisitor CSV = new JavaStatementVisitor(new JavaExpressionVisitor(functionWriter) {
1616
-			//@Override
1617
-			public Void visitGetLocalVariable(GetLocalVariableExpression varExpression) {
1618
-				final int position = calculateMemberPosition(varExpression, expression) ;
1619
-				if (position < 0)
1620
-					throw new CompileException(varExpression.position, CompileExceptionCode.INTERNAL_ERROR, "Captured Statement error");
1621
-				functionWriter.loadObject(0);
1622
-				functionWriter.getField(name, "captured" + position, varExpression.variable.type.accept(JavaTypeVisitor.INSTANCE).getDescriptor());
1623
-				return null;
1624
-			}
1625
-		});
1626
-
1611
+        final JavaStatementVisitor CSV = new JavaStatementVisitor(new JavaExpressionVisitor(functionWriter) {
1612
+            //@Override
1613
+            public Void visitGetLocalVariable(GetLocalVariableExpression varExpression) {
1614
+                final int position = calculateMemberPosition(varExpression, expression);
1615
+                functionWriter.loadObject(0);
1616
+                functionWriter.getField(name, "captured" + position, varExpression.variable.type.accept(JavaTypeVisitor.INSTANCE).getDescriptor());
1617
+                return null;
1618
+            }
1627
 
1619
 
1628
-		expression.body.accept(CSV);
1620
+            @Override
1621
+            public Void visitCapturedParameter(CapturedParameterExpression varExpression) {
1622
+                final int position = calculateMemberPosition(varExpression, expression);
1623
+                functionWriter.loadObject(0);
1624
+                functionWriter.getField(name, "captured" + position, varExpression.parameter.type.accept(JavaTypeVisitor.INSTANCE).getDescriptor());
1625
+                return null;
1626
+            }
1627
+        });
1629
 
1628
 
1630
 
1629
 
1630
+		expression.body.accept(CSV);
1631
         functionWriter.ret();
1631
         functionWriter.ret();
1632
 
1632
 
1633
 
1633
 
1634
-
1635
 		functionWriter.end();
1634
 		functionWriter.end();
1636
         lambdaCW.visitEnd();
1635
         lambdaCW.visitEnd();
1637
 
1636
 
1648
 
1647
 
1649
     //TODO replace with visitor?
1648
     //TODO replace with visitor?
1650
     private static int calculateMemberPosition(GetLocalVariableExpression localVariableExpression, FunctionExpression expression) {
1649
     private static int calculateMemberPosition(GetLocalVariableExpression localVariableExpression, FunctionExpression expression) {
1650
+        int h = 1;//expression.header.parameters.length;
1651
+            for (CapturedExpression capture : expression.closure.captures) {
1652
+                if (capture instanceof CapturedLocalVariableExpression && ((CapturedLocalVariableExpression) capture).variable == localVariableExpression.variable)
1653
+                    return h;
1654
+                if (capture instanceof CapturedClosureExpression && ((CapturedClosureExpression) capture).value instanceof CapturedLocalVariableExpression && ((CapturedLocalVariableExpression) ((CapturedClosureExpression) capture).value).variable == localVariableExpression.variable)
1655
+                    return h;
1656
+                h++;
1657
+            }
1658
+        throw new CompileException(localVariableExpression.position, CompileExceptionCode.INTERNAL_ERROR, "Captured Statement error");
1659
+    }
1660
+
1661
+    private static int calculateMemberPosition(CapturedParameterExpression functionParameterExpression, FunctionExpression expression) {
1651
         int h = 1;//expression.header.parameters.length;
1662
         int h = 1;//expression.header.parameters.length;
1652
         for (CapturedExpression capture : expression.closure.captures) {
1663
         for (CapturedExpression capture : expression.closure.captures) {
1653
-            if (capture instanceof CapturedLocalVariableExpression && ((CapturedLocalVariableExpression) capture).variable == localVariableExpression.variable)
1654
-                return h;
1655
-            if (capture instanceof CapturedClosureExpression && ((CapturedClosureExpression) capture).value instanceof CapturedLocalVariableExpression && ((CapturedLocalVariableExpression) ((CapturedClosureExpression) capture).value).variable == localVariableExpression.variable)
1664
+            if (capture instanceof CapturedParameterExpression && ((CapturedParameterExpression) capture).parameter == functionParameterExpression.parameter)
1656
                 return h;
1665
                 return h;
1657
             h++;
1666
             h++;
1658
         }
1667
         }
1659
-        return -1;
1668
+        throw new CompileException(functionParameterExpression.position, CompileExceptionCode.INTERNAL_ERROR, "Captured Statement error");
1660
     }
1669
     }
1661
 
1670
 
1671
+
1662
     private String calcFunctionSignature(LambdaClosure closure) {
1672
     private String calcFunctionSignature(LambdaClosure closure) {
1663
         StringJoiner joiner = new StringJoiner("", "(", ")V");
1673
         StringJoiner joiner = new StringJoiner("", "(", ")V");
1664
 
1674
 
1681
     public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
1691
     public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
1682
         JavaParameterInfo parameter = expression.parameter.getTag(JavaParameterInfo.class);
1692
         JavaParameterInfo parameter = expression.parameter.getTag(JavaParameterInfo.class);
1683
 
1693
 
1684
-        if(parameter == null) {
1685
-			System.err.println("NULL PARAMETER!!!");
1686
-			//FIXME
1687
-			parameter = new JavaParameterInfo(1, Type.INT_TYPE);
1688
-		}
1694
+        if (parameter == null) {
1695
+            throw new CompileException(expression.position, CompileExceptionCode.LAMBDA_HEADER_INVALID, "Could not resolve lambda parameter" + expression.parameter);
1696
+        }
1689
 
1697
 
1690
         javaWriter.load(expression.parameter.type.accept(JavaTypeVisitor.INSTANCE), parameter.index);
1698
         javaWriter.load(expression.parameter.type.accept(JavaTypeVisitor.INSTANCE), parameter.index);
1691
         return null;
1699
         return null;

Loading…
Cancel
Save