Browse Source

Merge remote-tracking branch 'kindlich/development' into development

Jared 4 years ago
parent
commit
eaa9db6cad
No account linked to committer's email address
40 changed files with 641 additions and 845 deletions
  1. 4
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/NativeDefinitionAnnotation.java
  2. 6
    4
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialGlobalExpression.java
  3. 2
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/BlockStatement.java
  4. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/ReturnStatement.java
  5. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GlobalTypeRegistry.java
  6. 44
    30
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMembers.java
  7. 5
    1
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeRunUnit.java
  8. 3
    2
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java
  9. 18
    4
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java
  10. 11
    5
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaStatementVisitor.java
  11. 7
    2
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaWriter.java
  12. 251
    206
      JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java
  13. 1
    1
      JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaClass.java
  14. 10
    4
      JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareDefinitionVisitor.java
  15. 124
    0
      Parser/src/main/java/org/openzen/zenscript/parser/EscapableBracketParser.java
  16. 19
    9
      Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionFunction.java
  17. 0
    4
      ScriptingExample/scripts/arithmetic.zs
  18. 0
    75
      ScriptingExample/scripts/arrays.zs
  19. 0
    83
      ScriptingExample/scripts/classes.zs
  20. 0
    58
      ScriptingExample/scripts/conditionals.zs
  21. 0
    17
      ScriptingExample/scripts/functionalInterfaces.zs
  22. 0
    20
      ScriptingExample/scripts/functions.zs
  23. 0
    19
      ScriptingExample/scripts/functions2.zs
  24. 0
    1
      ScriptingExample/scripts/functions3.zs
  25. 0
    22
      ScriptingExample/scripts/functions_lambdas.zs
  26. 0
    12
      ScriptingExample/scripts/helloworld.zs
  27. 0
    54
      ScriptingExample/scripts/integration.zs
  28. 0
    17
      ScriptingExample/scripts/interfaces.zs
  29. 0
    94
      ScriptingExample/scripts/moreHellos.zs
  30. 0
    3
      ScriptingExample/scripts/scriptparameters.zs
  31. 0
    2
      ScriptingExample/scripts/statements.zs
  32. 0
    18
      ScriptingExample/scripts/switch.zs
  33. 0
    70
      ScriptingExample/scripts/variants.zs
  34. 4
    0
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Globals.java
  35. 19
    7
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java
  36. 24
    0
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/EventManager.java
  37. 41
    0
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/IEvent.java
  38. 4
    0
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/SomeMCEvent.java
  39. 25
    0
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/impl/CTStringedEvent.java
  40. 12
    0
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/impl/StringedEvent.java

+ 4
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/NativeDefinitionAnnotation.java View File

@@ -40,4 +40,8 @@ public class NativeDefinitionAnnotation implements DefinitionAnnotation {
40 40
 	public void serialize(CodeSerializationOutput output, HighLevelDefinition definition, TypeContext context) {
41 41
 		output.writeString(identifier);
42 42
 	}
43
+
44
+	public String getIdentifier() {
45
+		return identifier;
46
+	}
43 47
 }

+ 6
- 4
CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialGlobalExpression.java View File

@@ -9,10 +9,7 @@ import java.util.List;
9 9
 import org.openzen.zencode.shared.CodePosition;
10 10
 import org.openzen.zencode.shared.CompileException;
11 11
 import org.openzen.zenscript.codemodel.FunctionHeader;
12
-import org.openzen.zenscript.codemodel.expression.CallArguments;
13
-import org.openzen.zenscript.codemodel.expression.Expression;
14
-import org.openzen.zenscript.codemodel.expression.GlobalCallExpression;
15
-import org.openzen.zenscript.codemodel.expression.GlobalExpression;
12
+import org.openzen.zenscript.codemodel.expression.*;
16 13
 import org.openzen.zenscript.codemodel.scope.TypeScope;
17 14
 import org.openzen.zenscript.codemodel.GenericName;
18 15
 import org.openzen.zenscript.codemodel.type.StoredType;
@@ -63,4 +60,9 @@ public class PartialGlobalExpression implements IPartialExpression {
63 60
 	public StoredType[] getTypeArguments() {
64 61
 		return typeArguments;
65 62
 	}
63
+
64
+	@Override
65
+	public IPartialExpression capture(CodePosition position, LambdaClosure closure) {
66
+		return this;
67
+	}
66 68
 }

+ 2
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/BlockStatement.java View File

@@ -7,6 +7,7 @@ package org.openzen.zenscript.codemodel.statement;
7 7
 
8 8
 import java.util.Arrays;
9 9
 import java.util.List;
10
+import java.util.Objects;
10 11
 import java.util.function.Consumer;
11 12
 import java.util.stream.Collectors;
12 13
 
@@ -96,6 +97,7 @@ public class BlockStatement extends Statement {
96 97
 	public StoredType getReturnType() {
97 98
 		final List<StoredType> collect = Arrays.stream(statements)
98 99
 				.map(Statement::getReturnType)
100
+				.filter(Objects::nonNull)
99 101
 				.distinct()
100 102
 				.collect(Collectors.toList());
101 103
 		if(collect.isEmpty())

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/ReturnStatement.java View File

@@ -11,6 +11,7 @@ import org.openzen.zencode.shared.ConcatMap;
11 11
 import org.openzen.zenscript.codemodel.expression.Expression;
12 12
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
13 13
 import org.openzen.zenscript.codemodel.scope.TypeScope;
14
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
14 15
 import org.openzen.zenscript.codemodel.type.StoredType;
15 16
 
16 17
 /**
@@ -28,7 +29,7 @@ public class ReturnStatement extends Statement {
28 29
 	
29 30
 	@Override
30 31
 	public StoredType getReturnType() {
31
-		return value.type;
32
+		return value != null ? value.type : BasicTypeID.VOID.stored();
32 33
 	}
33 34
 	
34 35
 	@Override

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GlobalTypeRegistry.java View File

@@ -5,6 +5,7 @@
5 5
  */
6 6
 package org.openzen.zenscript.codemodel.type;
7 7
 
8
+import java.util.Collection;
8 9
 import java.util.HashMap;
9 10
 import java.util.Map;
10 11
 import org.openzen.zenscript.codemodel.FunctionHeader;
@@ -110,4 +111,8 @@ public class GlobalTypeRegistry {
110 111
 			return id;
111 112
 		}
112 113
 	}
114
+
115
+	public Collection<DefinitionTypeID> getDefinitions() {
116
+		return definitionTypes.keySet();
117
+	}
113 118
 }

+ 44
- 30
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMembers.java View File

@@ -11,41 +11,16 @@ import org.openzen.zencode.shared.CompileExceptionCode;
11 11
 import org.openzen.zenscript.codemodel.CompareType;
12 12
 import org.openzen.zenscript.codemodel.GenericName;
13 13
 import org.openzen.zenscript.codemodel.OperatorType;
14
-import org.openzen.zenscript.codemodel.expression.CallArguments;
15
-import org.openzen.zenscript.codemodel.expression.CheckNullExpression;
16
-import org.openzen.zenscript.codemodel.expression.Expression;
17
-import org.openzen.zenscript.codemodel.expression.InterfaceCastExpression;
18
-import org.openzen.zenscript.codemodel.expression.InvalidExpression;
19
-import org.openzen.zenscript.codemodel.expression.NullExpression;
20
-import org.openzen.zenscript.codemodel.expression.StorageCastExpression;
21
-import org.openzen.zenscript.codemodel.expression.SupertypeCastExpression;
22
-import org.openzen.zenscript.codemodel.expression.WrapOptionalExpression;
14
+import org.openzen.zenscript.codemodel.expression.*;
23 15
 import org.openzen.zenscript.codemodel.member.EnumConstantMember;
24 16
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
25 17
 import org.openzen.zenscript.codemodel.member.InnerDefinition;
26
-import org.openzen.zenscript.codemodel.member.ref.CasterMemberRef;
27
-import org.openzen.zenscript.codemodel.member.ref.ConstMemberRef;
28
-import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
29
-import org.openzen.zenscript.codemodel.member.ref.FieldMemberRef;
30
-import org.openzen.zenscript.codemodel.member.ref.FunctionalMemberRef;
31
-import org.openzen.zenscript.codemodel.member.ref.GetterMemberRef;
32
-import org.openzen.zenscript.codemodel.member.ref.ImplementationMemberRef;
33
-import org.openzen.zenscript.codemodel.member.ref.IteratorMemberRef;
34
-import org.openzen.zenscript.codemodel.member.ref.SetterMemberRef;
35
-import org.openzen.zenscript.codemodel.member.ref.VariantOptionRef;
36
-import org.openzen.zenscript.codemodel.partial.IPartialExpression;
37
-import org.openzen.zenscript.codemodel.partial.PartialMemberGroupExpression;
38
-import org.openzen.zenscript.codemodel.partial.PartialStaticMemberGroupExpression;
39
-import org.openzen.zenscript.codemodel.partial.PartialTypeExpression;
40
-import org.openzen.zenscript.codemodel.partial.PartialVariantOptionExpression;
18
+import org.openzen.zenscript.codemodel.member.ref.*;
19
+import org.openzen.zenscript.codemodel.partial.*;
41 20
 import org.openzen.zenscript.codemodel.scope.TypeScope;
42 21
 import org.openzen.zenscript.codemodel.type.*;
43 22
 
44
-import java.util.ArrayList;
45
-import java.util.HashMap;
46
-import java.util.List;
47
-import java.util.Map;
48
-import java.util.Set;
23
+import java.util.*;
49 24
 
50 25
 /**
51 26
  *
@@ -81,6 +56,36 @@ public final class TypeMembers {
81 56
 	
82 57
 	public boolean extendsOrImplements(TypeID other) {
83 58
 		other = other.getNormalized();
59
+		checkBoundaries:
60
+		if(this.type.type instanceof DefinitionTypeID && other instanceof DefinitionTypeID) {
61
+			DefinitionTypeID thisTypeId = (DefinitionTypeID) this.type.type;
62
+			DefinitionTypeID otherTypeId = (DefinitionTypeID) other;
63
+
64
+			if(thisTypeId.definition != otherTypeId.definition){
65
+				break checkBoundaries;
66
+			}
67
+
68
+			if(thisTypeId.definition.typeParameters.length != otherTypeId.typeArguments.length){
69
+				break checkBoundaries;
70
+			}
71
+
72
+			for (int i = 0; i < thisTypeId.definition.typeParameters.length; i++) {
73
+				final TypeID type = otherTypeId.typeArguments[i].type;
74
+				if (type == BasicTypeID.UNDETERMINED) {
75
+					continue;
76
+				}
77
+				if (type instanceof InvalidTypeID && ((InvalidTypeID) type).code == CompileExceptionCode.TYPE_ARGUMENTS_NOT_INFERRABLE) {
78
+					continue;
79
+				}
80
+				if (thisTypeId.definition.typeParameters[i].matches(cache, type)) {
81
+					continue;
82
+				}
83
+				break checkBoundaries;
84
+			}
85
+			return true;
86
+		}
87
+
88
+
84 89
 		TypeID superType = type.type.getSuperType(cache.getRegistry());
85 90
 		if (superType != null) {
86 91
 			if (superType == other)
@@ -430,8 +435,17 @@ public final class TypeMembers {
430 435
    
431 436
 		if( getImplicitCaster(toType) != null || extendsOrImplements(toType.type))
432 437
 		    return true;
433
-		
438
+
439
+		if(type.type.isGeneric() && type.type instanceof GenericTypeID) {
440
+			final GenericTypeID genericTypeID = (GenericTypeID) type.type;
441
+			if(genericTypeID.parameter.matches(cache, toType.type)) {
442
+				return true;
443
+			}
444
+		}
445
+
446
+
434 447
         final StoredType accept = type.type.accept(new TagRemovingTypeVisitor(cache));
448
+
435 449
         if(!this.type.type.equals(accept.type) && cache.get(accept).canCastImplicit(toType)){
436 450
             return true;
437 451
         }

+ 5
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeRunUnit.java View File

@@ -92,7 +92,11 @@ public class JavaBytecodeRunUnit {
92 92
 			directory.mkdirs();
93 93
 
94 94
 		for (Map.Entry<String, byte[]> classEntry : classes.entrySet()) {
95
-			File output = new File(directory, classEntry.getKey() + ".class");
95
+			File output = new File(directory, classEntry.getKey().replace('.', File.separatorChar) + ".class");
96
+			if (!output.getParentFile().exists() && !output.getParentFile().mkdirs()) {
97
+				//Throw error?
98
+				continue;
99
+			}
96 100
 			try (FileOutputStream outputStream = new FileOutputStream(output)) {
97 101
 				outputStream.write(classEntry.getValue());
98 102
 			} catch (IOException ex) {

+ 3
- 2
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java View File

@@ -48,8 +48,9 @@ public class JavaCompiler {
48 48
 		context.addModule(module.module, target);
49 49
 		
50 50
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
51
-			String filename = getFilename(definition) + "_" + (definition.name == null ? "generated" : definition.name) + "_" + expansionCounter++;
52
-			JavaPrepareDefinitionVisitor definitionPreparer = new JavaPrepareDefinitionVisitor(context, target, filename, null);
51
+			final String className = getFilename(definition);
52
+			String filename = className + "_" + (definition.name == null ? "generated" : definition.name) + "_" + expansionCounter++;
53
+			JavaPrepareDefinitionVisitor definitionPreparer = new JavaPrepareDefinitionVisitor(context, target, filename, null, className);
53 54
 			definition.accept(definitionPreparer);
54 55
 		}
55 56
 		

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

@@ -159,8 +159,8 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
159 159
     private final JavaBoxingTypeVisitor boxingTypeVisitor;
160 160
     private final JavaUnboxingTypeVisitor unboxingTypeVisitor;
161 161
     private final JavaCapturedExpressionVisitor capturedExpressionVisitor = new JavaCapturedExpressionVisitor(this);
162
-    private final JavaBytecodeContext context;
163
-    private final JavaCompiledModule module;
162
+    final JavaBytecodeContext context;
163
+    final JavaCompiledModule module;
164 164
 
165 165
 	public JavaExpressionVisitor(JavaBytecodeContext context, JavaCompiledModule module, JavaWriter javaWriter) {
166 166
 		this.javaWriter = javaWriter;
@@ -1961,6 +1961,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
1961 1961
 		final String signature;
1962 1962
 		final String[] interfaces;
1963 1963
 		final String className = context.getLambdaCounter();
1964
+		final String descriptor;
1964 1965
 		
1965 1966
 		{//Fill the info above
1966 1967
 			final StorageTag actualStorage = expression.type.getActualStorage();
@@ -1970,10 +1971,12 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
1970 1971
 				
1971 1972
 				//Should be the same, should it not?
1972 1973
 				signature = context.getMethodSignature(expression.header);
1974
+				descriptor = context.getMethodDescriptor(expression.header);
1973 1975
 				interfaces = new String[]{Type.getInternalName(functionalInterfaceMethod.getDeclaringClass())};
1974 1976
 			} else {
1975 1977
 				//Normal way, no casting to functional interface
1976 1978
 				signature = context.getMethodSignature(expression.header);
1979
+				descriptor = context.getMethodDescriptor(expression.header);
1977 1980
 				interfaces = new String[]{context.getInternalName(new FunctionTypeID(null, expression.header).stored(UniqueStorageTag.INSTANCE))};
1978 1981
 			}
1979 1982
 		}
@@ -1989,7 +1992,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
1989 1992
 		final JavaWriter functionWriter;
1990 1993
 		
1991 1994
 		//Bridge method!!!
1992
-		if (!Objects.equals(methodInfo.descriptor, signature)) {
1995
+		if (!Objects.equals(methodInfo.descriptor, descriptor)) {
1993 1996
 			final JavaMethod bridgeMethodInfo = new JavaMethod(methodInfo.cls, methodInfo.kind, methodInfo.name, methodInfo.compile, methodInfo.descriptor, methodInfo.modifiers | JavaModifiers.BRIDGE | JavaModifiers.SYNTHETIC, methodInfo.genericResult, methodInfo.typeParameterArguments);
1994 1997
 			final JavaWriter bridgeWriter = new JavaWriter(expression.position, lambdaCW, bridgeMethodInfo, null, methodInfo.descriptor, null, "java/lang/Override");
1995 1998
 			bridgeWriter.start();
@@ -2016,7 +2019,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
2016 2019
 			
2017 2020
 			
2018 2021
 			
2019
-			final JavaMethod actualMethod = new JavaMethod(methodInfo.cls, methodInfo.kind, methodInfo.name, methodInfo.compile, signature, methodInfo.modifiers, methodInfo.genericResult, methodInfo.typeParameterArguments);
2022
+			final JavaMethod actualMethod = new JavaMethod(methodInfo.cls, methodInfo.kind, methodInfo.name, methodInfo.compile, context.getMethodDescriptor(expression.header), methodInfo.modifiers, methodInfo.genericResult, methodInfo.typeParameterArguments);
2020 2023
 			//No @Override
2021 2024
 			functionWriter = new JavaWriter(expression.position, lambdaCW, actualMethod, null, signature, null);
2022 2025
 		} else {
@@ -2061,6 +2064,17 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
2061 2064
 		final JavaStatementVisitor CSV = new JavaStatementVisitor(context, new JavaExpressionVisitor(context, module, functionWriter) {
2062 2065
 			@Override
2063 2066
 			public Void visitGetLocalVariable(GetLocalVariableExpression varExpression) {
2067
+				final JavaLocalVariableInfo localVariable = functionWriter.tryGetLocalVariable(varExpression.variable.variable);
2068
+				if(localVariable != null) {
2069
+					final Label label = new Label();
2070
+					localVariable.end = label;
2071
+					functionWriter.label(label);
2072
+					functionWriter.load(localVariable);
2073
+					return null;
2074
+				}
2075
+
2076
+
2077
+
2064 2078
 				final int position = calculateMemberPosition(varExpression, expression);
2065 2079
 				functionWriter.loadObject(0);
2066 2080
 				functionWriter.getField(className, "captured" + position, context.getDescriptor(varExpression.variable.type));

+ 11
- 5
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaStatementVisitor.java View File

@@ -16,8 +16,8 @@ import org.openzen.zenscript.javashared.JavaCompiledModule;
16 16
 public class JavaStatementVisitor implements StatementVisitor<Boolean> {
17 17
     private final JavaWriter javaWriter;
18 18
 	final JavaBytecodeContext context;
19
-    public JavaExpressionVisitor expressionVisitor;
20
-	public JavaNonPushingExpressionVisitor nonPushingExpressionVisitor;
19
+    public final JavaExpressionVisitor expressionVisitor;
20
+	public final JavaNonPushingExpressionVisitor nonPushingExpressionVisitor;
21 21
 
22 22
     /**
23 23
      * @param javaWriter the method writer that compiles the statement
@@ -26,13 +26,14 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
26 26
         this.javaWriter = javaWriter;
27 27
 		this.context = context;
28 28
         this.expressionVisitor = new JavaExpressionVisitor(context, module, javaWriter);
29
-		nonPushingExpressionVisitor = new JavaNonPushingExpressionVisitor(context, module, javaWriter, expressionVisitor);
29
+		this.nonPushingExpressionVisitor = new JavaNonPushingExpressionVisitor(context, module, javaWriter, expressionVisitor);
30 30
     }
31 31
 
32 32
     public JavaStatementVisitor(JavaBytecodeContext context, JavaExpressionVisitor expressionVisitor) {
33 33
         this.javaWriter = expressionVisitor.getJavaWriter();
34 34
 		this.context = context;
35 35
         this.expressionVisitor = expressionVisitor;
36
+		this.nonPushingExpressionVisitor = new JavaNonPushingExpressionVisitor(expressionVisitor.context, expressionVisitor.module, expressionVisitor.javaWriter, expressionVisitor);
36 37
     }
37 38
 
38 39
 	@Override
@@ -189,8 +190,13 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
189 190
 	@Override
190 191
 	public Boolean visitReturn(ReturnStatement statement) {
191 192
     	javaWriter.position(statement.position.fromLine);
192
-		statement.value.accept(expressionVisitor);
193
-		javaWriter.returnType(context.getType(statement.value.type));
193
+    	if(statement.value == null) {
194
+    		javaWriter.ret();
195
+		} else {
196
+			statement.value.accept(expressionVisitor);
197
+			javaWriter.returnType(context.getType(statement.value.type));
198
+		}
199
+
194 200
 		return true;
195 201
 	}
196 202
 

+ 7
- 2
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaWriter.java View File

@@ -11,6 +11,7 @@ import org.openzen.zenscript.javashared.JavaField;
11 11
 import org.openzen.zenscript.javashared.JavaMethod;
12 12
 import org.openzen.zenscript.javashared.JavaParameterInfo;
13 13
 
14
+import javax.lang.model.element.VariableElement;
14 15
 import java.util.ArrayList;
15 16
 import java.util.HashMap;
16 17
 import java.util.List;
@@ -85,9 +86,13 @@ public class JavaWriter {
85 86
 	public void setLocalVariable(VariableID variable, JavaLocalVariableInfo info) {
86 87
 		localVariables.put(variable, info);
87 88
 	}
88
-	
89
+
90
+	public JavaLocalVariableInfo tryGetLocalVariable(VariableID variable) {
91
+		return localVariables.get(variable);
92
+	}
93
+
89 94
 	public JavaLocalVariableInfo getLocalVariable(VariableID variable) {
90
-		JavaLocalVariableInfo result = localVariables.get(variable);
95
+		JavaLocalVariableInfo result = tryGetLocalVariable(variable);
91 96
 		if (result == null)
92 97
 			throw new IllegalStateException("Local variable unknown");
93 98
 		

+ 251
- 206
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java View File

@@ -10,40 +10,16 @@ import org.openzen.zencode.shared.CompileException;
10 10
 import org.openzen.zencode.shared.LiteralSourceFile;
11 11
 import org.openzen.zenscript.codemodel.*;
12 12
 import org.openzen.zenscript.codemodel.annotations.AnnotationDefinition;
13
+import org.openzen.zenscript.codemodel.annotations.DefinitionAnnotation;
14
+import org.openzen.zenscript.codemodel.annotations.NativeDefinitionAnnotation;
13 15
 import org.openzen.zenscript.codemodel.context.CompilingPackage;
14 16
 import org.openzen.zenscript.codemodel.context.FileResolutionContext;
15 17
 import org.openzen.zenscript.codemodel.context.ModuleTypeResolutionContext;
16
-import org.openzen.zenscript.codemodel.definition.ClassDefinition;
17
-import org.openzen.zenscript.codemodel.definition.EnumDefinition;
18
-import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
19
-import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
20
-import org.openzen.zenscript.codemodel.definition.StructDefinition;
21
-import org.openzen.zenscript.codemodel.definition.ZSPackage;
22
-import org.openzen.zenscript.codemodel.expression.ConstantByteExpression;
23
-import org.openzen.zenscript.codemodel.expression.ConstantDoubleExpression;
24
-import org.openzen.zenscript.codemodel.expression.ConstantFloatExpression;
25
-import org.openzen.zenscript.codemodel.expression.ConstantIntExpression;
26
-import org.openzen.zenscript.codemodel.expression.ConstantLongExpression;
27
-import org.openzen.zenscript.codemodel.expression.ConstantSByteExpression;
28
-import org.openzen.zenscript.codemodel.expression.ConstantShortExpression;
29
-import org.openzen.zenscript.codemodel.expression.ConstantStringExpression;
30
-import org.openzen.zenscript.codemodel.expression.ConstantUIntExpression;
31
-import org.openzen.zenscript.codemodel.expression.ConstantULongExpression;
32
-import org.openzen.zenscript.codemodel.expression.ConstantUShortExpression;
33
-import org.openzen.zenscript.codemodel.expression.Expression;
34
-import org.openzen.zenscript.codemodel.expression.ExpressionSymbol;
35
-import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
36
-import org.openzen.zenscript.codemodel.expression.StorageCastExpression;
18
+import org.openzen.zenscript.codemodel.definition.*;
19
+import org.openzen.zenscript.codemodel.expression.*;
37 20
 import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
38 21
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
39
-import org.openzen.zenscript.codemodel.member.CasterMember;
40
-import org.openzen.zenscript.codemodel.member.ConstructorMember;
41
-import org.openzen.zenscript.codemodel.member.FieldMember;
42
-import org.openzen.zenscript.codemodel.member.GetterMember;
43
-import org.openzen.zenscript.codemodel.member.ImplementationMember;
44
-import org.openzen.zenscript.codemodel.member.MethodMember;
45
-import org.openzen.zenscript.codemodel.member.OperatorMember;
46
-import org.openzen.zenscript.codemodel.member.SetterMember;
22
+import org.openzen.zenscript.codemodel.member.*;
47 23
 import org.openzen.zenscript.codemodel.member.ref.FunctionalMemberRef;
48 24
 import org.openzen.zenscript.codemodel.partial.PartialStaticMemberGroupExpression;
49 25
 import org.openzen.zenscript.codemodel.scope.ExpressionScope;
@@ -56,7 +32,8 @@ import org.openzen.zenscript.codemodel.type.storage.StaticStorageTag;
56 32
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
57 33
 import org.openzen.zenscript.codemodel.type.storage.StorageType;
58 34
 import org.openzen.zenscript.javashared.*;
59
-import org.openzen.zenscript.lexer.*;
35
+import org.openzen.zenscript.lexer.ParseException;
36
+import org.openzen.zenscript.lexer.ZSTokenParser;
60 37
 import org.openzen.zenscript.parser.BracketExpressionParser;
61 38
 import org.openzen.zenscript.parser.expression.ParsedExpression;
62 39
 import org.openzen.zenscript.parser.type.IParsedType;
@@ -77,11 +54,12 @@ public class JavaNativeModule {
77 54
 	private final GlobalTypeRegistry registry;
78 55
 	private final PackageDefinitions definitions = new PackageDefinitions();
79 56
 	private final JavaCompiledModule compiled;
80
-	
57
+
81 58
 	private final Map<Class<?>, HighLevelDefinition> definitionByClass = new HashMap<>();
82 59
 	private final Map<Class<?>, TypeID> typeByClass = new HashMap<>();
83 60
 	private final Map<Class<?>, TypeID> unsignedByClass = new HashMap<>();
84
-	
61
+	private final TypeVariableContext context = new TypeVariableContext();
62
+
85 63
 	public final Map<String, ISymbol> globals = new HashMap<>();
86 64
 	private BracketExpressionParser bep;
87 65
 
@@ -95,13 +73,13 @@ public class JavaNativeModule {
95 73
 		this.basePackage = basePackage;
96 74
 		module = new Module(name);
97 75
 		this.registry = registry;
98
-		
76
+
99 77
 		for (JavaNativeModule dependency : dependencies) {
100 78
 			definitionByClass.putAll(dependency.definitionByClass);
101 79
 		}
102
-		
80
+
103 81
 		compiled = new JavaCompiledModule(module, FunctionParameter.NONE);
104
-		
82
+
105 83
 		typeByClass.put(void.class, BasicTypeID.VOID);
106 84
 		typeByClass.put(boolean.class, BasicTypeID.BOOL);
107 85
 		typeByClass.put(byte.class, BasicTypeID.SBYTE);
@@ -118,7 +96,7 @@ public class JavaNativeModule {
118 96
 		typeByClass.put(Long.class, registry.getOptional(BasicTypeID.LONG));
119 97
 		typeByClass.put(Float.class, registry.getOptional(BasicTypeID.FLOAT));
120 98
 		typeByClass.put(Double.class, registry.getOptional(BasicTypeID.DOUBLE));
121
-		
99
+
122 100
 		unsignedByClass.put(byte.class, BasicTypeID.BYTE);
123 101
 		unsignedByClass.put(short.class, BasicTypeID.USHORT);
124 102
 		unsignedByClass.put(int.class, BasicTypeID.UINT);
@@ -128,7 +106,7 @@ public class JavaNativeModule {
128 106
 		unsignedByClass.put(Integer.class, registry.getOptional(BasicTypeID.INT));
129 107
 		unsignedByClass.put(Long.class, registry.getOptional(BasicTypeID.LONG));
130 108
 	}
131
-	
109
+
132 110
 	public SemanticModule toSemantic(ModuleSpace space) {
133 111
 		return new SemanticModule(
134 112
 				module,
@@ -144,31 +122,31 @@ public class JavaNativeModule {
144 122
 				space.getAnnotations(),
145 123
 				space.getStorageTypes());
146 124
 	}
147
-	
125
+
148 126
 	public JavaCompiledModule getCompiled() {
149 127
 		return compiled;
150 128
 	}
151
-	
129
+
152 130
 	public HighLevelDefinition addClass(Class<?> cls) {
153
-		if (definitionByClass.containsKey(cls))
131
+		if (definitionByClass.containsKey(cls)) {
154 132
 			return definitionByClass.get(cls);
155
-        
133
+		}
156 134
         return convertClass(cls);
157 135
 	}
158
-	
136
+
159 137
 	public void addGlobals(Class<?> cls) {
160 138
 		HighLevelDefinition definition = new ClassDefinition(CodePosition.NATIVE, module, pkg, "__globals__", Modifiers.PUBLIC);
161 139
 		JavaClass jcls = JavaClass.fromInternalName(getInternalName(cls), JavaClass.Kind.CLASS);
162 140
 		compiled.setClassInfo(definition, jcls);
163 141
 		StoredType thisType = registry.getForMyDefinition(definition).stored();
164
-		TypeVariableContext context = new TypeVariableContext();
165
-		
142
+		//TypeVariableContext context = new TypeVariableContext();
143
+
166 144
 		for (Field field : cls.getDeclaredFields()) {
167 145
 			if (!field.isAnnotationPresent(ZenCodeGlobals.Global.class))
168 146
 				continue;
169 147
 			if (!isStatic(field.getModifiers()))
170 148
 				continue;
171
-			
149
+
172 150
 			ZenCodeGlobals.Global global = field.getAnnotation(ZenCodeGlobals.Global.class);
173 151
 			StoredType type = loadStoredType(context, field.getAnnotatedType());
174 152
 			String name = global.value().isEmpty() ? field.getName() : global.value();
@@ -179,18 +157,18 @@ public class JavaNativeModule {
179 157
 			compiled.setFieldInfo(fieldMember.autoGetter, javaField);
180 158
 			globals.put(name, new ExpressionSymbol((position, scope) -> new StaticGetterExpression(CodePosition.BUILTIN, fieldMember.autoGetter.ref(thisType, GenericMapper.EMPTY))));
181 159
 		}
182
-		
160
+
183 161
 		for (Method method : cls.getDeclaredMethods()) {
184 162
 			if (!method.isAnnotationPresent(ZenCodeGlobals.Global.class))
185 163
 				continue;
186 164
 			if (!isStatic(method.getModifiers()))
187 165
 				continue;
188
-			
166
+
189 167
 			ZenCodeGlobals.Global global = method.getAnnotation(ZenCodeGlobals.Global.class);
190 168
 			String name = global.value().isEmpty() ? method.getName() : global.value();
191 169
 			MethodMember methodMember = new MethodMember(CodePosition.NATIVE, definition, Modifiers.PUBLIC | Modifiers.STATIC, name, getHeader(context, method), null);
192 170
 			definition.addMember(methodMember);
193
-			
171
+
194 172
 			boolean isGenericResult = methodMember.header.getReturnType().isGeneric();
195 173
 			compiled.setMethodInfo(methodMember, new JavaMethod(jcls, JavaMethod.Kind.STATIC, method.getName(), false, getMethodDescriptor(method), method.getModifiers(), isGenericResult));
196 174
 			globals.put(name, new ExpressionSymbol((position, scope) -> {
@@ -199,15 +177,15 @@ public class JavaNativeModule {
199 177
 			}));
200 178
 		}
201 179
 	}
202
-	
180
+
203 181
 	public FunctionalMemberRef loadStaticMethod(Method method) {
204 182
 		if (!isStatic(method.getModifiers()))
205 183
 			throw new IllegalArgumentException("Method \"" + method.toString() + "\" is not static");
206
-		
184
+
207 185
 		HighLevelDefinition definition = addClass(method.getDeclaringClass());
208 186
 		JavaClass jcls = JavaClass.fromInternalName(getInternalName(method.getDeclaringClass()), JavaClass.Kind.CLASS);
209
-		
210
-		TypeVariableContext context = new TypeVariableContext();
187
+
188
+		//TypeVariableContext context = new TypeVariableContext();
211 189
 		MethodMember methodMember = new MethodMember(CodePosition.NATIVE, definition, Modifiers.PUBLIC | Modifiers.STATIC, method.getName(), getHeader(context, method), null);
212 190
 		definition.addMember(methodMember);
213 191
 		boolean isGenericResult = methodMember.header.getReturnType().isGeneric();
@@ -218,7 +196,7 @@ public class JavaNativeModule {
218 196
 	private boolean isInBasePackage(String className) {
219 197
 		return className.startsWith(basePackage + ".");
220 198
 	}
221
-	
199
+
222 200
 	private ZSPackage getPackage(String className) {
223 201
 		//TODO validate
224 202
 		if(this.basePackage == null || this.basePackage.isEmpty())
@@ -226,22 +204,44 @@ public class JavaNativeModule {
226 204
 		//TODO make a lang package?
227 205
 		if (!className.contains(".") || className.startsWith("java.lang"))
228 206
 			return pkg;
229
-		
207
+
230 208
 		if (className.startsWith("."))
231 209
 			className = className.substring(1);
232 210
 		else if (className.startsWith(basePackage + "."))
233 211
 			className = className.substring(basePackage.length() + 1);
234 212
 		else
235 213
             throw new IllegalArgumentException("Invalid class name: \"" + className + "\" not in the given base package: \"" + basePackage + "\"");
236
-		
214
+
237 215
 		String[] classNameParts = Strings.split(className, '.');
238 216
 		ZSPackage classPkg = pkg;
239 217
 		for (int i = 0; i < classNameParts.length - 1; i++)
240 218
 			classPkg = classPkg.getOrCreatePackage(classNameParts[i]);
241
-		
219
+
242 220
 		return classPkg;
243 221
 	}
244
-	
222
+
223
+	private <T> HighLevelDefinition checkRegistry(Class<T> cls) {
224
+		String name = cls.getCanonicalName();
225
+		if (!name.startsWith("java.lang.") && !name.startsWith("java.util.")) {
226
+			return null;
227
+		}
228
+
229
+		name = name.substring("java.lang.".length());
230
+		for (DefinitionTypeID definition : registry.getDefinitions()) {
231
+			final HighLevelDefinition highLevelDefinition = definition.definition;
232
+			for (DefinitionAnnotation annotation : highLevelDefinition.annotations) {
233
+				if (annotation instanceof NativeDefinitionAnnotation) {
234
+					final String identifier = ((NativeDefinitionAnnotation) annotation).getIdentifier();
235
+					if (identifier.equals(name) || identifier.equals("stdlib::" + name)) {
236
+						return highLevelDefinition;
237
+					}
238
+				}
239
+			}
240
+		}
241
+
242
+		return null;
243
+	}
244
+
245 245
 	private <T> HighLevelDefinition convertClass(Class<T> cls) {
246 246
 		if ((cls.getModifiers() & Modifier.PUBLIC) == 0)
247 247
 			throw new IllegalArgumentException("Class \" " + cls.getName() + "\" must be public");
@@ -252,71 +252,86 @@ public class JavaNativeModule {
252 252
 
253 253
 		String className = cls.getName();
254 254
         boolean isStruct = cls.isAnnotationPresent(ZenCodeType.Struct.class);
255
-        
256
-        ZSPackage classPkg;
257
-        ZenCodeType.Name nameAnnotation = cls.getDeclaredAnnotation(ZenCodeType.Name.class);
258
-		className = className.contains(".") ? className.substring(className.lastIndexOf('.') + 1) : className;
259
-        if (nameAnnotation == null) {
260
-            classPkg = getPackage(className);
261
-        } else {
262
-            String specifiedName = nameAnnotation.value();
263
-			if (specifiedName.startsWith(".")) {
264
-				classPkg = getPackage(specifiedName);
265
-				className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);
266
-			} else if (specifiedName.indexOf('.') >= 0) {
267
-				if (!specifiedName.startsWith(pkg.fullName))
268
-					throw new IllegalArgumentException("Specified @Name as \"" + specifiedName + "\" for class: \"" + cls.toString() + "\" but it's not in the module root package: \"" + pkg.fullName + "\"");
269
-
270
-				classPkg = getPackage(basePackage + specifiedName.substring(pkg.fullName.length()));
271
-				className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);
272
-			} else {
273
-                classPkg = getPackage(specifiedName);
274
-                className = nameAnnotation.value();
275
-			}
276
-		}
277 255
 
278
-
279
-		
280
-		HighLevelDefinition definition;
256
+		HighLevelDefinition definition = checkRegistry(cls);
257
+		final boolean foundRegistry = definition != null;
281 258
 		String internalName = getInternalName(cls);
282 259
 		JavaClass javaClass;
283
-		if (cls.isInterface()) {
284
-			definition = new InterfaceDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
285
-			javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.INTERFACE);
286
-		} else if (cls.isEnum()) {
287
-			definition = new EnumDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
288
-			javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.ENUM);
289
-		} else if (isStruct) {
290
-			definition = new StructDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
291
-			javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.CLASS);
260
+
261
+		if (foundRegistry) {
262
+			javaClass = JavaClass.fromInternalName(internalName, definition.isInterface() ? JavaClass.Kind.INTERFACE : JavaClass.Kind.CLASS);
292 263
 		} else {
293
-			definition = new ClassDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC);
294
-			javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.CLASS);
264
+			ZSPackage classPkg;
265
+			ZenCodeType.Name nameAnnotation = cls.getDeclaredAnnotation(ZenCodeType.Name.class);
266
+			className = className.contains(".") ? className.substring(className.lastIndexOf('.') + 1) : className;
267
+			if (nameAnnotation == null) {
268
+				classPkg = getPackage(className);
269
+			} else {
270
+				String specifiedName = nameAnnotation.value();
271
+				if (specifiedName.startsWith(".")) {
272
+					classPkg = getPackage(specifiedName);
273
+					className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);
274
+				} else if (specifiedName.indexOf('.') >= 0) {
275
+					if (!specifiedName.startsWith(pkg.fullName))
276
+						throw new IllegalArgumentException("Specified @Name as \"" + specifiedName + "\" for class: \"" + cls
277
+								.toString() + "\" but it's not in the module root package");
278
+
279
+					classPkg = getPackage(basePackage + specifiedName.substring(pkg.fullName.length()));
280
+					className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);
281
+				} else {
282
+					classPkg = getPackage(specifiedName);
283
+					className = nameAnnotation.value();
284
+				}
285
+			}
295 286
 
287
+
288
+			if (cls.isInterface()) {
289
+				definition = new InterfaceDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
290
+				javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.INTERFACE);
291
+			} else if (cls.isEnum()) {
292
+				definition = new EnumDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
293
+				javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.ENUM);
294
+			} else if (isStruct) {
295
+				definition = new StructDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
296
+				javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.CLASS);
297
+			} else {
298
+				definition = new ClassDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC);
299
+				javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.CLASS);
300
+
301
+			}
296 302
 		}
297 303
 
298 304
 		//Moved up here so that circular dependencies are caught (hopefully)
299 305
 		definitionByClass.put(cls, definition);
300 306
 
301
-		TypeVariableContext context = new TypeVariableContext();
307
+		//TypeVariableContext context = new TypeVariableContext();
302 308
 		TypeVariable<Class<T>>[] javaTypeParameters = cls.getTypeParameters();
303
-        definition.typeParameters = new TypeParameter[cls.getTypeParameters().length];
309
+		TypeParameter[] typeParameters = new TypeParameter[cls.getTypeParameters().length];
310
+		definition.typeParameters = typeParameters;
311
+
304 312
 		for (int i = 0; i < javaTypeParameters.length; i++) {
313
+			//Put up here for nested parameters?
305 314
 			TypeVariable<Class<T>> typeVariable = javaTypeParameters[i];
306 315
 			TypeParameter parameter = new TypeParameter(CodePosition.NATIVE, typeVariable.getName());
316
+			typeParameters[i] = parameter;
317
+			context.put(typeVariable, parameter);
318
+		}
319
+
320
+		for (int i = 0; i < javaTypeParameters.length; i++) {
321
+			TypeVariable<Class<T>> typeVariable = javaTypeParameters[i];
322
+			TypeParameter parameter = typeParameters[i];
307 323
 			for (AnnotatedType bound : typeVariable.getAnnotatedBounds()) {
308 324
 				TypeID type = loadType(context, bound).type;
309 325
 				parameter.addBound(new ParameterTypeBound(CodePosition.NATIVE, type));
310 326
 			}
311
-            definition.typeParameters[i] = parameter;
312
-			context.put(typeVariable, parameter);
327
+			typeParameters[i] = parameter;
313 328
 		}
314
-  
315
-		if (definition instanceof ClassDefinition && cls.getAnnotatedSuperclass() != null && shouldLoadType(cls.getAnnotatedSuperclass().getType())) {
329
+
330
+		if (!foundRegistry && definition instanceof ClassDefinition && cls.getAnnotatedSuperclass() != null && shouldLoadType(cls.getAnnotatedSuperclass().getType())) {
316 331
 			definition.setSuperType(loadType(context, cls.getAnnotatedSuperclass()).type);
317 332
 		}
318 333
 
319
-		if (definition.getSuperType() == null && cls != Object.class) {
334
+		if (!foundRegistry && definition.getSuperType() == null && cls != Object.class) {
320 335
 			definition.setSuperType(loadType(context, Object.class, false, false).type);
321 336
 		}
322 337
 
@@ -328,10 +343,9 @@ public class JavaNativeModule {
328 343
 				compiled.setImplementationInfo(member, new JavaImplementation(true, javaClass));
329 344
 			}
330 345
 		}
331
-		
332
-		
346
+
333 347
 		compiled.setClassInfo(definition, javaClass);
334
-		
348
+
335 349
 		StoredType thisType = new StoredType(registry.getForMyDefinition(definition), AutoStorageTag.INSTANCE);
336 350
 		for (Field field : cls.getDeclaredFields()) {
337 351
 			ZenCodeType.Field annotation = field.getAnnotation(ZenCodeType.Field.class);
@@ -339,17 +353,17 @@ public class JavaNativeModule {
339 353
 				continue;
340 354
 			if (!isPublic(field.getModifiers()))
341 355
 				continue;
342
-			
356
+
343 357
 			final String fieldName = annotation.value().isEmpty() ? field.getName() : annotation.value();
344
-			
358
+
345 359
 			StoredType fieldType = loadStoredType(context, field.getAnnotatedType());
346 360
 			FieldMember member = new FieldMember(CodePosition.NATIVE, definition, getMethodModifiers(field), fieldName, thisType, fieldType, registry, 0, 0, null);
347 361
 			definition.addMember(member);
348 362
 			compiled.setFieldInfo(member, new JavaField(javaClass, field.getName(), getDescriptor(field.getType())));
349 363
 		}
350
-		
364
+
351 365
 		boolean hasConstructor = false;
352
-		for (java.lang.reflect.Constructor constructor : cls.getConstructors()) {
366
+		for (java.lang.reflect.Constructor<?> constructor : cls.getConstructors()) {
353 367
 			ZenCodeType.Constructor constructorAnnotation = (ZenCodeType.Constructor) constructor.getAnnotation(ZenCodeType.Constructor.class);
354 368
 			if (constructorAnnotation != null) {
355 369
 				ConstructorMember member = asConstructor(context, definition, constructor);
@@ -358,13 +372,13 @@ public class JavaNativeModule {
358 372
 				hasConstructor = true;
359 373
 			}
360 374
 		}
361
-		
362
-		if (!hasConstructor) {
375
+
376
+		if (!hasConstructor && !foundRegistry) {
363 377
 			// no constructor! make a private constructor so the compiler doesn't add one
364 378
 			ConstructorMember member = new ConstructorMember(CodePosition.BUILTIN, definition, Modifiers.PRIVATE, new FunctionHeader(BasicTypeID.VOID), BuiltinID.CLASS_DEFAULT_CONSTRUCTOR);
365 379
 			definition.addMember(member);
366 380
 		}
367
-		
381
+
368 382
 		for (Method method : cls.getDeclaredMethods()) {
369 383
 			ZenCodeType.Method methodAnnotation = method.getAnnotation(ZenCodeType.Method.class);
370 384
 			if (methodAnnotation != null) {
@@ -373,42 +387,42 @@ public class JavaNativeModule {
373 387
 				compiled.setMethodInfo(member, getMethod(javaClass, method, member.header.getReturnType()));
374 388
 				continue;
375 389
 			}
376
-			
390
+
377 391
 			ZenCodeType.Getter getter = method.getAnnotation(ZenCodeType.Getter.class);
378 392
 			if (getter != null) {
379 393
 				GetterMember member = asGetter(context, definition, method, getter);
380 394
 				definition.addMember(member);
381 395
 				compiled.setMethodInfo(member, getMethod(javaClass, method, member.getType()));
382 396
 			}
383
-			
397
+
384 398
 			ZenCodeType.Setter setter = method.getAnnotation(ZenCodeType.Setter.class);
385 399
 			if (setter != null) {
386 400
 				SetterMember member = asSetter(context, definition, method, setter);
387 401
 				definition.addMember(member);
388 402
 				compiled.setMethodInfo(member, getMethod(javaClass, method, BasicTypeID.VOID.stored));
389 403
 			}
390
-			
404
+
391 405
 			ZenCodeType.Operator operator = method.getAnnotation(ZenCodeType.Operator.class);
392 406
 			if (operator != null) {
393 407
 				OperatorMember member = asOperator(context, definition, method, operator);
394 408
 				definition.addMember(member);
395 409
 				compiled.setMethodInfo(member, getMethod(javaClass, method, member.header.getReturnType()));
396 410
 			}
397
-			
411
+
398 412
 			ZenCodeType.Caster caster = method.getAnnotation(ZenCodeType.Caster.class);
399 413
 			if (caster != null) {
400 414
 				CasterMember member = asCaster(context, definition, method, caster);
401 415
 				definition.addMember(member);
402 416
 				compiled.setMethodInfo(member, getMethod(javaClass, method, member.toType));
403 417
 			}
404
-			
418
+
405 419
 			/*if (!annotated) {
406 420
 				MethodMember member = asMethod(definition, method, null);
407 421
 				definition.addMember(member);
408 422
 				compiled.setMethodInfo(member, getMethod(javaClass, method, member.header.getReturnType()));
409 423
 			}*/
410 424
 		}
411
-		
425
+
412 426
 		return definition;
413 427
 	}
414 428
 
@@ -449,7 +463,7 @@ public class JavaNativeModule {
449 463
             if(methodAnnotation != null) {
450 464
                 String name = !methodAnnotation.value().isEmpty() ? methodAnnotation.value() : method.getName();
451 465
 
452
-                TypeVariableContext context = new TypeVariableContext();
466
+                //TypeVariableContext context = new TypeVariableContext();
453 467
 
454 468
                 final Parameter[] parameters = getExpansionParameters(method);
455 469
 
@@ -468,7 +482,7 @@ public class JavaNativeModule {
468 482
                 if (implicit) {
469 483
                     modifiers |= Modifiers.IMPLICIT;
470 484
                 }
471
-                TypeVariableContext context = new TypeVariableContext();
485
+                //TypeVariableContext context = new TypeVariableContext();
472 486
                 StoredType toType = loadStoredType(context, method.getAnnotatedReturnType());
473 487
                 final CasterMember member = new CasterMember(CodePosition.NATIVE, expansion, modifiers, toType,  null);
474 488
 
@@ -571,16 +585,16 @@ public class JavaNativeModule {
571 585
 		return null;
572 586
 	}
573 587
 
574
-	
588
+
575 589
 	private boolean shouldLoadType(Type type) {
576 590
 		if (type instanceof Class)
577 591
 			return definitionByClass.containsKey(type) || shouldLoadClass((Class<?>)type);
578 592
 		if (type instanceof ParameterizedType)
579 593
 			return shouldLoadType(((ParameterizedType)type).getRawType());
580
-		
594
+
581 595
 		return false;
582 596
 	}
583
-	
597
+
584 598
 	private String getClassName(Class<?> cls) {
585 599
 	    return cls.isAnnotationPresent(ZenCodeType.Name.class) ? cls.getAnnotation(ZenCodeType.Name.class).value() : cls.getName();
586 600
     }
@@ -588,25 +602,25 @@ public class JavaNativeModule {
588 602
 	private boolean shouldLoadClass(Class<?> cls) {
589 603
 	    return isInBasePackage(getClassName(cls));
590 604
     }
591
-	
605
+
592 606
 	private boolean isGetterName(String name) {
593 607
 		return name.startsWith("get") || name.startsWith("is") || name.startsWith("has");
594 608
 	}
595
-	
609
+
596 610
 	private String translateGetterName(String name) {
597 611
 		if (name.startsWith("get"))
598 612
 			return name.substring(3, 4).toLowerCase() + name.substring(4);
599
-		
613
+
600 614
 		return name;
601 615
 	}
602
-	
616
+
603 617
 	private String translateSetterName(String name) {
604 618
 		if (name.startsWith("set"))
605 619
 			return name.substring(3, 4).toLowerCase() + name.substring(4);
606
-		
620
+
607 621
 		return name;
608 622
 	}
609
-	
623
+
610 624
 	private ConstructorMember asConstructor(TypeVariableContext context, HighLevelDefinition definition, java.lang.reflect.Constructor method) {
611 625
 		FunctionHeader header = getHeader(context, method);
612 626
 		return new ConstructorMember(
@@ -616,7 +630,7 @@ public class JavaNativeModule {
616 630
 				header,
617 631
 				null);
618 632
 	}
619
-	
633
+
620 634
 	private MethodMember asMethod(TypeVariableContext context, HighLevelDefinition definition, Method method, ZenCodeType.Method annotation) {
621 635
 		String name = annotation != null && !annotation.value().isEmpty() ? annotation.value() : method.getName();
622 636
 		FunctionHeader header = getHeader(context, method);
@@ -628,15 +642,15 @@ public class JavaNativeModule {
628 642
 				header,
629 643
 				null);
630 644
 	}
631
-	
645
+
632 646
 	private OperatorMember asOperator(TypeVariableContext context, HighLevelDefinition definition, Method method, ZenCodeType.Operator annotation) {
633 647
 		FunctionHeader header = getHeader(context, method);
634 648
 		if (isStatic(method.getModifiers()))
635 649
 			throw new IllegalArgumentException("operator method \"" + method.toString() + "\"cannot be static");
636
-		
650
+
637 651
 		// TODO: check number of parameters
638 652
 		//if (header.parameters.length != annotation.value().parameters)
639
-		
653
+
640 654
 		return new OperatorMember(
641 655
 				CodePosition.NATIVE,
642 656
 				definition,
@@ -645,7 +659,7 @@ public class JavaNativeModule {
645 659
 				header,
646 660
 				null);
647 661
 	}
648
-	
662
+
649 663
 	private GetterMember asGetter(TypeVariableContext context, HighLevelDefinition definition, Method method, ZenCodeType.Getter annotation) {
650 664
 		StoredType type = loadStoredType(context, method.getAnnotatedReturnType());
651 665
 		String name = null;
@@ -653,34 +667,34 @@ public class JavaNativeModule {
653 667
 			name = annotation.value();
654 668
 		if (name == null)
655 669
 			name = translateGetterName(method.getName());
656
-		
670
+
657 671
 		return new GetterMember(CodePosition.NATIVE, definition, getMethodModifiers(method), name, type, null);
658 672
 	}
659
-	
673
+
660 674
 	private SetterMember asSetter(TypeVariableContext context, HighLevelDefinition definition, Method method, ZenCodeType.Setter annotation) {
661 675
 		if (method.getParameterCount() != 1)
662 676
 			throw new IllegalArgumentException("Illegal setter: \"" + method.toString() + "\"must have exactly 1 parameter");
663
-		
677
+
664 678
 		StoredType type = loadStoredType(context, method.getAnnotatedParameterTypes()[0]);
665 679
 		String name = null;
666 680
 		if (annotation != null && !annotation.value().isEmpty())
667 681
 			name = annotation.value();
668 682
 		if (name == null)
669 683
 			name = translateSetterName(method.getName());
670
-		
684
+
671 685
 		return new SetterMember(CodePosition.NATIVE, definition, getMethodModifiers(method), name, type, null);
672 686
 	}
673
-	
687
+
674 688
 	private CasterMember asCaster(TypeVariableContext context, HighLevelDefinition definition, Method method, ZenCodeType.Caster annotation) {
675 689
 		boolean implicit = annotation != null && annotation.implicit();
676 690
 		int modifiers = Modifiers.PUBLIC;
677 691
 		if (implicit)
678 692
 			modifiers |= Modifiers.IMPLICIT;
679
-		
693
+
680 694
 		StoredType toType = loadStoredType(context, method.getAnnotatedReturnType());
681 695
 		return new CasterMember(CodePosition.NATIVE, definition, modifiers, toType, null);
682 696
 	}
683
-	
697
+
684 698
 	private FunctionHeader getHeader(TypeVariableContext context, java.lang.reflect.Constructor constructor) {
685 699
 		return getHeader(
686 700
 				context,
@@ -689,7 +703,7 @@ public class JavaNativeModule {
689 703
 				constructor.getTypeParameters(),
690 704
 				constructor.getAnnotatedExceptionTypes());
691 705
 	}
692
-	
706
+
693 707
 	private FunctionHeader getHeader(TypeVariableContext context, Method method) {
694 708
 		return getHeader(
695 709
 				context,
@@ -698,7 +712,7 @@ public class JavaNativeModule {
698 712
 				method.getTypeParameters(),
699 713
 				method.getAnnotatedExceptionTypes());
700 714
 	}
701
-	
715
+
702 716
 	protected Expression getDefaultValue(Parameter parameter, StoredType type) {
703 717
 		if (parameter.isAnnotationPresent(ZenCodeType.Optional.class)) {
704 718
 				final String s = parameter.getAnnotation(ZenCodeType.Optional.class).value();
@@ -776,34 +790,41 @@ public class JavaNativeModule {
776 790
 			TypeVariable<Method>[] javaTypeParameters,
777 791
 			AnnotatedType[] exceptionTypes) {
778 792
 		StoredType returnType = javaReturnType == null ? BasicTypeID.VOID.stored : loadStoredType(context, javaReturnType);
779
-		
793
+
794
+
795
+		TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];
796
+		for (int i = 0; i < javaTypeParameters.length; i++) {
797
+			//Put up here for nested parameters?
798
+			TypeVariable<Method> typeVariable = javaTypeParameters[i];
799
+			TypeParameter parameter = new TypeParameter(CodePosition.NATIVE, typeVariable.getName());
800
+			typeParameters[i] = parameter;
801
+			context.put(typeVariable, parameter);
802
+		}
803
+
804
+		for (int i = 0; i < javaTypeParameters.length; i++) {
805
+			TypeVariable<Method> javaTypeParameter = javaTypeParameters[i];
806
+
807
+			for (AnnotatedType bound : javaTypeParameter.getAnnotatedBounds())
808
+				typeParameters[i].addBound(new ParameterTypeBound(CodePosition.NATIVE, loadType(context, bound).type));
809
+		}
810
+
780 811
 		FunctionParameter[] parameters = new FunctionParameter[javaParameters.length];
781 812
 		for (int i = 0; i < parameters.length; i++) {
782 813
 			Parameter parameter = javaParameters[i];
783 814
 
784 815
 			//AnnotatedType parameterType = parameter.getAnnotatedType();
785
-			StoredType type = loadStoredType(context, parameter);
816
+ 			StoredType type = loadStoredType(context, parameter);
786 817
 			Expression defaultValue = getDefaultValue(parameter, type);
787 818
 			parameters[i] = new FunctionParameter(type, parameter.getName(), defaultValue, parameter.isVarArgs());
788 819
 		}
789
-		
790
-		TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];
791
-		for (int i = 0; i < javaTypeParameters.length; i++) {
792
-			TypeVariable<Method> javaTypeParameter = javaTypeParameters[i];
793
-			typeParameters[i] = new TypeParameter(CodePosition.UNKNOWN, javaTypeParameter.getName());
794
-			context.put(javaTypeParameter, typeParameters[i]);
795
-			
796
-			for (AnnotatedType bound : javaTypeParameter.getAnnotatedBounds())
797
-				typeParameters[i].addBound(new ParameterTypeBound(CodePosition.NATIVE, loadType(context, bound).type));
798
-		}
799
-		
820
+
800 821
 		if (exceptionTypes.length > 1)
801 822
 			throw new IllegalArgumentException("A method can only throw a single exception type!");
802
-		
823
+
803 824
 		StoredType thrownType = exceptionTypes.length == 0 ? null : loadStoredType(context, exceptionTypes[0]);
804 825
 		return new FunctionHeader(typeParameters, returnType, thrownType, AutoStorageTag.INSTANCE, parameters);
805 826
 	}
806
-	
827
+
807 828
 	private StoredType loadStoredType(TypeVariableContext context, AnnotatedType annotatedType) {
808 829
 		return loadType(context, annotatedType);
809 830
 	}
@@ -815,27 +836,27 @@ public class JavaNativeModule {
815 836
 			return new StoredType(registry.getOptional(type.type), type.getSpecifiedStorage());
816 837
 		return type;
817 838
 	}
818
-	
839
+
819 840
 	private StoredType loadType(TypeVariableContext context, AnnotatedType annotatedType) {
820 841
 		if (annotatedType.isAnnotationPresent(ZenCodeType.USize.class))
821 842
 			return BasicTypeID.USIZE.stored;
822 843
 		else if (annotatedType.isAnnotationPresent(ZenCodeType.NullableUSize.class))
823 844
 			return registry.getOptional(BasicTypeID.USIZE).stored();
824
-		
845
+
825 846
 		boolean nullable = annotatedType.isAnnotationPresent(ZenCodeType.Nullable.class);
826 847
 		boolean unsigned = annotatedType.isAnnotationPresent(ZenCodeType.Unsigned.class);
827
-		
848
+
828 849
 		return loadType(context, annotatedType, nullable, unsigned);
829 850
 	}
830
-	
851
+
831 852
 	private StoredType loadType(TypeVariableContext context, AnnotatedElement type, boolean nullable, boolean unsigned) {
832 853
 		StoredType result = loadType(context, type, unsigned);
833 854
 		if (nullable)
834 855
 			result = new StoredType(registry.getOptional(result.type), result.getSpecifiedStorage());
835
-		
856
+
836 857
 		return result;
837 858
 	}
838
-	
859
+
839 860
 	@SuppressWarnings("ChainOfInstanceofChecks")
840 861
     private StoredType loadType(TypeVariableContext context, AnnotatedElement type, boolean unsigned) {
841 862
 		if (type instanceof Class) {
@@ -851,53 +872,71 @@ public class JavaNativeModule {
851 872
 			} else if (classType.isAnnotationPresent(FunctionalInterface.class)) {
852 873
 				return loadFunctionalInterface(context, classType, new AnnotatedElement[0]);
853 874
 			}
854
-			
875
+
855 876
 			if (typeByClass.containsKey(classType))
856 877
 				return typeByClass.get(classType).stored();
857
-			
878
+
858 879
 			HighLevelDefinition definition = addClass(classType);
859
-			return registry.getForDefinition(definition).stored();
880
+			final List<StoredType> s = new ArrayList<>();
881
+			for (TypeVariable<? extends Class<?>> typeParameter : classType.getTypeParameters()) {
882
+				s.add(registry.getGeneric(context.get(typeParameter)).stored());
883
+			}
884
+
885
+			return registry.getForDefinition(definition, s.toArray(StoredType.NONE)).stored();
860 886
 		} else if (type instanceof ParameterizedType) {
861 887
 			ParameterizedType parameterizedType = (ParameterizedType) type;
862
-			Class<?> rawType = (Class) parameterizedType.getRawType();
888
+			Class<?> rawType = (Class<?>) parameterizedType.getRawType();
863 889
 			if (rawType.isAnnotationPresent(FunctionalInterface.class))
864 890
 				return loadFunctionalInterface(context, rawType, (AnnotatedElement[]) parameterizedType.getActualTypeArguments());
865
-			
891
+
866 892
 			Type[] parameters = parameterizedType.getActualTypeArguments();
867 893
 			StoredType[] codeParameters = new StoredType[parameters.length];
868 894
 			for (int i = 0; i < parameters.length; i++)
869 895
 			    codeParameters[i] = loadType(context, (AnnotatedElement) parameters[i], false, false);
870
-       
896
+
871 897
 			if(rawType == Map.class) {
872 898
                 return registry.getAssociative(codeParameters[0], codeParameters[1]).stored();
873 899
             }
874
-            
900
+
875 901
             HighLevelDefinition definition = addClass(rawType);
876 902
             return registry.getForDefinition(definition, codeParameters).stored();
877
-		} else if (type instanceof TypeVariable) {
878
-            TypeVariable variable = (TypeVariable) type;
903
+		} else if (type instanceof TypeVariable<?>) {
904
+            TypeVariable<?> variable = (TypeVariable<?>) type;
879 905
             return registry.getGeneric(context.get(variable)).stored();
880 906
         }else if(type instanceof AnnotatedType){
881
-		    final StoredType storedType;
907
+		    StoredType storedType;
882 908
 		    if(type instanceof AnnotatedParameterizedType) {
883 909
                 AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) type;
910
+
884 911
                 final Type rawType = ((ParameterizedType) parameterizedType.getType()).getRawType();
885 912
                 final AnnotatedType[] actualTypeArguments = parameterizedType.getAnnotatedActualTypeArguments();
886 913
                 final StoredType[] codeParameters = new StoredType[actualTypeArguments.length];
887 914
                 for(int i = 0; i < actualTypeArguments.length; i++) {
888 915
                     codeParameters[i] = loadType(context, actualTypeArguments[i], false, false);
889 916
                 }
890
-            
917
+
891 918
                 if(rawType == Map.class) {
892 919
                     storedType = registry.getAssociative(codeParameters[0], codeParameters[1]).stored();
893
-                } else {
894
-                    HighLevelDefinition definition = addClass((Class<?>) rawType);
895
-                    storedType = registry.getForDefinition(definition, codeParameters).stored();
920
+                } else if(rawType instanceof Class<?>){
921
+                	final Map<TypeParameter, StoredType> map = new HashMap<>();
922
+					final TypeVariable<? extends Class<?>>[] typeParameters = ((Class<?>) rawType).getTypeParameters();
923
+					final StoredType loadType = loadType(context, (AnnotatedElement) rawType, unsigned);
924
+					for (int i = 0; i < typeParameters.length; i++) {
925
+						final TypeParameter typeParameter = context.get(typeParameters[i]);
926
+						map.put(typeParameter, codeParameters[i]);
927
+					}
928
+					storedType = loadType.instance(new GenericMapper(CodePosition.NATIVE, registry, map));
929
+				} else {
930
+                    storedType = loadType(context, (AnnotatedElement) rawType, unsigned);
896 931
                 }
897 932
             } else {
898
-		        storedType = loadType(context, (AnnotatedElement) ((AnnotatedType) type).getType(), unsigned);
933
+				if (((AnnotatedType) type).getType() instanceof WildcardType) {
934
+					storedType = BasicTypeID.UNDETERMINED.stored();
935
+				} else {
936
+					storedType = loadType(context, (AnnotatedElement) ((AnnotatedType) type).getType(), unsigned);
937
+				}
899 938
             }
900
-            
939
+
901 940
 		    if(type.isAnnotationPresent(ZenCodeStorageTag.class)) {
902 941
 		        //Replace with switch if more StorageTagTypes are added
903 942
                 if(type.getAnnotation(ZenCodeStorageTag.class).value() == StorageTagType.STATIC) {
@@ -905,22 +944,22 @@ public class JavaNativeModule {
905 944
                 }
906 945
             }
907 946
 		    return storedType;
908
-		    
947
+
909 948
         } else {
910 949
 			throw new IllegalArgumentException("Could not analyze type: " + type);
911 950
 		}
912 951
 	}
913
-	
952
+
914 953
 	private StoredType loadFunctionalInterface(TypeVariableContext loadContext, Class<?> cls, AnnotatedElement[] parameters) {
915 954
 		Method functionalInterfaceMethod = getFunctionalInterfaceMethod(cls);
916 955
 		TypeVariableContext context = convertTypeParameters(cls);
917 956
 		FunctionHeader header = getHeader(context, functionalInterfaceMethod);
918
-		
957
+
919 958
 		Map<TypeParameter, StoredType> mapping = new HashMap<>();
920 959
 		TypeVariable[] javaParameters = cls.getTypeParameters();
921
-		for (int i = 0; i < javaParameters.length; i++)
960
+		for (int i = 0; i < parameters.length; i++)
922 961
 			mapping.put(context.get(javaParameters[i]), loadType(loadContext, parameters[i], false, false));
923
-		
962
+
924 963
 		JavaMethod method = new JavaMethod(
925 964
 				JavaClass.fromInternalName(getInternalName(cls), JavaClass.Kind.INTERFACE),
926 965
 				JavaMethod.Kind.INTERFACE,
@@ -932,71 +971,77 @@ public class JavaNativeModule {
932 971
 		StorageTag tag = new JavaFunctionalInterfaceStorageTag(functionalInterfaceMethod, method);
933 972
 		return registry.getFunction(header).stored(tag);
934 973
 	}
935
-	
974
+
936 975
 	private <T> TypeVariableContext convertTypeParameters(Class<T> cls) {
937
-		TypeVariableContext context = new TypeVariableContext();
976
+		//TypeVariableContext context = new TypeVariableContext();
938 977
 		TypeVariable<Class<T>>[] javaTypeParameters = cls.getTypeParameters();
939 978
 		TypeParameter[] typeParameters = new TypeParameter[cls.getTypeParameters().length];
979
+
940 980
 		for (int i = 0; i < javaTypeParameters.length; i++) {
941 981
 			TypeVariable<Class<T>> typeVariable = javaTypeParameters[i];
942 982
 			TypeParameter parameter = new TypeParameter(CodePosition.NATIVE, typeVariable.getName());
943
-			for (AnnotatedType bound : typeVariable.getAnnotatedBounds()) {
944
-				TypeID type = loadType(context, bound).type;
945
-				parameter.addBound(new ParameterTypeBound(CodePosition.NATIVE, type));
946
-			}
947 983
 			typeParameters[i] = parameter;
984
+
985
+			//Put up here so that Nested Type parameters may work..?
948 986
 			context.put(typeVariable, parameter);
949 987
 		}
988
+
989
+		for (int i = 0; i < javaTypeParameters.length; i++) {
990
+			for (AnnotatedType bound : javaTypeParameters[i].getAnnotatedBounds()) {
991
+				TypeID type = loadType(context, bound).type;
992
+				typeParameters[i].addBound(new ParameterTypeBound(CodePosition.NATIVE, type));
993
+			}
994
+		}
950 995
 		return context;
951 996
 	}
952
-	
997
+
953 998
 	private Method getFunctionalInterfaceMethod(Class<?> functionalInterface) {
954 999
 		for (Method method : functionalInterface.getDeclaredMethods()) {
955 1000
 			if (!method.isDefault())
956 1001
 				return method;
957 1002
 		}
958
-		
1003
+
959 1004
 		return null;
960 1005
 	}
961
-	
1006
+
962 1007
 	private int getMethodModifiers(Member method) {
963 1008
 		int result = Modifiers.PUBLIC;
964 1009
 		if (isStatic(method.getModifiers()))
965 1010
 			result |= Modifiers.STATIC;
966 1011
 		if (isFinal(method.getModifiers()))
967 1012
 			result |= Modifiers.FINAL;
968
-		
1013
+
969 1014
 		return result;
970 1015
 	}
971
-	
1016
+
972 1017
 	private static boolean isPublic(int modifiers) {
973 1018
 		return (modifiers & Modifier.PUBLIC) > 0;
974 1019
 	}
975
-	
1020
+
976 1021
 	private static boolean isStatic(int modifiers) {
977 1022
 		return (modifiers & Modifier.STATIC) > 0;
978 1023
 	}
979
-	
1024
+
980 1025
 	private static boolean isFinal(int modifiers) {
981 1026
 		return (modifiers & Modifier.FINAL) > 0;
982 1027
 	}
983
-	
1028
+
984 1029
 	private static String getInternalName(Class<?> cls) {
985 1030
 		return org.objectweb.asm.Type.getInternalName(cls);
986 1031
 	}
987
-	
1032
+
988 1033
 	private static String getDescriptor(Class<?> cls) {
989 1034
 		return org.objectweb.asm.Type.getDescriptor(cls);
990 1035
 	}
991
-	
1036
+
992 1037
 	private static String getMethodDescriptor(java.lang.reflect.Constructor constructor) {
993 1038
 		return org.objectweb.asm.Type.getConstructorDescriptor(constructor);
994 1039
 	}
995
-	
1040
+
996 1041
 	private static String getMethodDescriptor(Method method) {
997 1042
 		return org.objectweb.asm.Type.getMethodDescriptor(method);
998 1043
 	}
999
-	
1044
+
1000 1045
 	private static JavaMethod getMethod(JavaClass cls, java.lang.reflect.Constructor constructor) {
1001 1046
 		return new JavaMethod(
1002 1047
 				cls,
@@ -1007,7 +1052,7 @@ public class JavaNativeModule {
1007 1052
 				constructor.getModifiers(),
1008 1053
 				false);
1009 1054
 	}
1010
-	
1055
+
1011 1056
 	private static JavaMethod getMethod(JavaClass cls, Method method, StoredType result) {
1012 1057
 		JavaMethod.Kind kind;
1013 1058
 		if (method.getName().equals("<init>"))
@@ -1018,7 +1063,7 @@ public class JavaNativeModule {
1018 1063
 			kind = JavaMethod.Kind.STATIC;
1019 1064
 		else
1020 1065
 			kind = JavaMethod.Kind.INSTANCE;
1021
-		
1066
+
1022 1067
 		return new JavaMethod(cls, kind, method.getName(), false, getMethodDescriptor(method), method.getModifiers(), result.isGeneric());
1023 1068
 	}
1024 1069
 
@@ -1028,15 +1073,15 @@ public class JavaNativeModule {
1028 1073
 
1029 1074
 	private static class TypeVariableContext {
1030 1075
 		private final Map<TypeVariable, TypeParameter> typeVariables = new HashMap<>();
1031
-		
1076
+
1032 1077
 		public void put(TypeVariable variable, TypeParameter parameter) {
1033 1078
 			typeVariables.put(variable, parameter);
1034 1079
 		}
1035
-		
1080
+
1036 1081
 		public TypeParameter get(TypeVariable variable) {
1037 1082
 			if (!typeVariables.containsKey(variable))
1038 1083
 				throw new IllegalStateException("Could not find type variable " + variable.getName());
1039
-			
1084
+
1040 1085
 			return typeVariables.get(variable);
1041 1086
 		}
1042 1087
 	}

+ 1
- 1
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaClass.java View File

@@ -52,7 +52,7 @@ public class JavaClass implements Comparable<JavaClass> {
52 52
 	
53 53
 	public static String getNameFromFile(String filename) {
54 54
 		if (filename.indexOf('.') > 0)
55
-			return filename.substring(0, filename.indexOf('.'));
55
+			return filename.substring(0, filename.lastIndexOf('.'));
56 56
 		else
57 57
 			return filename;
58 58
 	}

+ 10
- 4
JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareDefinitionVisitor.java View File

@@ -73,7 +73,7 @@ public class JavaPrepareDefinitionVisitor implements DefinitionVisitor<JavaClass
73 73
 			JavaNativeClass list = new JavaNativeClass(new JavaClass("java.util", "List", JavaClass.Kind.INTERFACE));
74 74
 			JavaClass arrayList = new JavaClass("java.util", "ArrayList", JavaClass.Kind.CLASS);
75 75
 			list.addMethod("constructor", JavaMethod.getNativeConstructor(arrayList, "()V"));
76
-			list.addInstanceMethod("add", "add", "(Ljava/lang/Object;)Z"); List<?> l;
76
+			list.addInstanceMethod("add", "add", "(Ljava/lang/Object;)Z");
77 77
 			list.addInstanceMethod("insert", "add", "(Ljava/lang/Object;I)V");
78 78
 			list.addInstanceMethod("remove", "remove", "(Ljava/lang/Object;)Z");
79 79
 			list.addInstanceMethod("indexOf", "indexOf", "(Ljava/lang/Object;)I");
@@ -236,14 +236,20 @@ public class JavaPrepareDefinitionVisitor implements DefinitionVisitor<JavaClass
236 236
 	
237 237
 	private final JavaContext context;
238 238
 	private final String filename;
239
+	private final String className;
239 240
 	private final JavaClass outerClass;
240 241
 	private final JavaCompiledModule module;
241
-	
242
+
242 243
 	public JavaPrepareDefinitionVisitor(JavaContext context, JavaCompiledModule module, String filename, JavaClass outerClass) {
244
+		this(context, module, filename, outerClass, JavaClass.getNameFromFile(filename));
245
+	}
246
+
247
+	public JavaPrepareDefinitionVisitor(JavaContext context, JavaCompiledModule module, String filename, JavaClass outerClass, String className) {
243 248
 		this.context = context;
244 249
 		this.filename = filename;
245 250
 		this.outerClass = outerClass;
246 251
 		this.module = module;
252
+		this.className = className;
247 253
 	}
248 254
 	
249 255
 	private boolean isPrepared(HighLevelDefinition definition) {
@@ -298,7 +304,7 @@ public class JavaPrepareDefinitionVisitor implements DefinitionVisitor<JavaClass
298 304
 		if (isPrepared(definition))
299 305
 			return context.getJavaClass(definition);
300 306
 		
301
-		JavaClass cls = new JavaClass(context.getPackageName(definition.pkg), JavaClass.getNameFromFile(filename), JavaClass.Kind.CLASS);
307
+		JavaClass cls = new JavaClass(context.getPackageName(definition.pkg), className, JavaClass.Kind.CLASS);
302 308
 		context.setJavaClass(definition, cls);
303 309
 		return cls;
304 310
 	}
@@ -313,7 +319,7 @@ public class JavaPrepareDefinitionVisitor implements DefinitionVisitor<JavaClass
313 319
 			context.setJavaNativeClass(definition, nativeClasses.get(nativeTag.value));
314 320
 		}
315 321
 		
316
-		JavaClass cls = new JavaClass(context.getPackageName(definition.pkg), JavaClass.getNameFromFile(filename), JavaClass.Kind.CLASS);
322
+		JavaClass cls = new JavaClass(context.getPackageName(definition.pkg), className, JavaClass.Kind.CLASS);
317 323
 		context.setJavaClass(definition, cls);
318 324
 		return cls;
319 325
 	}

+ 124
- 0
Parser/src/main/java/org/openzen/zenscript/parser/EscapableBracketParser.java View File

@@ -0,0 +1,124 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.parser;
7
+
8
+import org.openzen.zencode.shared.CodePosition;
9
+import org.openzen.zencode.shared.CompileException;
10
+import org.openzen.zenscript.codemodel.OperatorType;
11
+import org.openzen.zenscript.codemodel.expression.CallArguments;
12
+import org.openzen.zenscript.codemodel.expression.CallStaticExpression;
13
+import org.openzen.zenscript.codemodel.expression.Expression;
14
+import org.openzen.zenscript.codemodel.member.ref.FunctionalMemberRef;
15
+import org.openzen.zenscript.codemodel.partial.IPartialExpression;
16
+import org.openzen.zenscript.codemodel.scope.ExpressionScope;
17
+import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
18
+import org.openzen.zenscript.codemodel.type.StringTypeID;
19
+import org.openzen.zenscript.codemodel.type.TypeID;
20
+import org.openzen.zenscript.lexer.ParseException;
21
+import org.openzen.zenscript.lexer.ZSToken;
22
+import org.openzen.zenscript.lexer.ZSTokenParser;
23
+import org.openzen.zenscript.lexer.ZSTokenType;
24
+import org.openzen.zenscript.parser.expression.ParsedExpression;
25
+import org.openzen.zenscript.parser.expression.ParsedExpressionBinary;
26
+import org.openzen.zenscript.parser.expression.ParsedExpressionString;
27
+
28
+import java.util.ArrayList;
29
+import java.util.List;
30
+
31
+/**
32
+ * @author Hoofdgebruiker and kindlich
33
+ */
34
+public class EscapableBracketParser implements BracketExpressionParser {
35
+    private final FunctionalMemberRef method;
36
+    private final TypeID targetType;
37
+
38
+    public EscapableBracketParser(GlobalTypeRegistry registry, FunctionalMemberRef method) {
39
+        if (!method.isStatic())
40
+            throw new IllegalArgumentException("Method must be static");
41
+        if (method.getHeader().getNumberOfTypeParameters() > 0)
42
+            throw new IllegalArgumentException("Method cannot have type parameters");
43
+
44
+        this.method = method;
45
+        this.targetType = registry.getForDefinition(method.getTarget().definition);
46
+    }
47
+
48
+    @Override
49
+    public ParsedExpression parse(CodePosition position, ZSTokenParser tokens) throws ParseException {
50
+        StringBuilder string = new StringBuilder();
51
+
52
+        //This list will contain the BEP calls
53
+        //If this is only a normal BEP, then it will contain exactly one String ParsedExpression.
54
+        final List<ParsedExpression> expressionList = new ArrayList<>();
55
+
56
+        while (tokens.optional(ZSTokenType.T_GREATER) == null) {
57
+            ZSToken next = tokens.next();
58
+
59
+            if (next.type != ZSTokenType.T_DOLLAR) {
60
+                string.append(next.content);
61
+                string.append(tokens.getLastWhitespace());
62
+                continue;
63
+            }
64
+
65
+            //We found a $, now check that it has a { directly after it.
66
+            final String ws = tokens.getLastWhitespace();
67
+            if (!ws.isEmpty()) {
68
+                //$  {..} is not ${..} so we print it as literal
69
+                string.append(next.content).append(ws);
70
+                continue;
71
+            }
72
+
73
+            next = tokens.next();
74
+            //Now we check if it is a {
75
+            if (next.type == ZSTokenType.T_AOPEN) {
76
+                if (string.length() != 0) {
77
+                    expressionList.add(new ParsedExpressionString(position, string.toString(), false));
78
+                    string = new StringBuilder();
79
+                }
80
+                expressionList.add(ParsedExpression.parse(tokens));
81
+                tokens.required(ZSTokenType.T_ACLOSE, "} expected.");
82
+                string.append(tokens.getLastWhitespace());
83
+            } else {
84
+                //No { after the $, so we treat them both as literal
85
+                string.append("$").append(ws); //Technically, the whitespace here is empty, but let's be sure
86
+                string.append(next.content).append(tokens.getLastWhitespace());
87
+            }
88
+
89
+        }
90
+
91
+        if (string.length() != 0) {
92
+            expressionList.add(new ParsedExpressionString(position, string.toString(), false));
93
+        }
94
+
95
+        return new StaticMethodCallExpression(position, expressionList);
96
+    }
97
+
98
+    private class StaticMethodCallExpression extends ParsedExpression {
99
+
100
+        private final ParsedExpression call;
101
+
102
+        public StaticMethodCallExpression(CodePosition position, List<ParsedExpression> expressions) {
103
+            super(position);
104
+            ParsedExpression p = null;
105
+            for (ParsedExpression expression : expressions) {
106
+                p = p == null ? expression : new ParsedExpressionBinary(expression.position, p, expression, OperatorType.ADD);
107
+            }
108
+
109
+            this.call = p;
110
+        }
111
+
112
+        @Override
113
+        public IPartialExpression compile(ExpressionScope scope) throws CompileException {
114
+            final Expression methodCall = call.compile(scope.withHint(StringTypeID.AUTO)).eval();
115
+            final CallArguments arguments = new CallArguments(methodCall);
116
+            return new CallStaticExpression(position, targetType, method, method.getHeader(), arguments);
117
+        }
118
+
119
+        @Override
120
+        public boolean hasStrongType() {
121
+            return true;
122
+        }
123
+    }
124
+}

+ 19
- 9
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionFunction.java View File

@@ -81,19 +81,29 @@ public class ParsedExpressionFunction extends ParsedExpression {
81 81
 			
82 82
 			header.setReturnType(returnType);
83 83
 		}
84
-		
85
-		if (!scope.genericInferenceMap.isEmpty()) {
84
+
85
+
86
+		if (genericHeader.typeParameters.length > 0 && !scope.genericInferenceMap.isEmpty()) {
86 87
 			// perform type parameter inference
87 88
 			StoredType returnType = statements.getReturnType();
88
-			Map<TypeParameter, StoredType> inferredTypes = returnType.type.inferTypeParameters(scope.getMemberCache(), genericHeader.getReturnType());
89
-			if (inferredTypes == null)
90
-				throw new CompileException(position, CompileExceptionCode.TYPE_ARGUMENTS_NOT_INFERRABLE, "Could not infer generic type parameters");
91
-			
92
-			scope.genericInferenceMap.putAll(inferredTypes);
89
+			if (returnType != null) {
90
+				Map<TypeParameter, StoredType> inferredTypes = returnType.type.inferTypeParameters(scope.getMemberCache(), genericHeader
91
+						.getReturnType());
92
+				if (inferredTypes == null) {
93
+					throw new CompileException(position, CompileExceptionCode.TYPE_ARGUMENTS_NOT_INFERRABLE, "Could not infer generic type parameters");
94
+				}
95
+
96
+				scope.genericInferenceMap.putAll(inferredTypes);
97
+			}
98
+
99
+		}
100
+
101
+		final FunctionHeader thatOtherHeader = genericHeader.withGenericArguments(new GenericMapper(position, scope.getTypeRegistry(), scope.genericInferenceMap));
102
+		if(thatOtherHeader.getReturnType().isBasic(BasicTypeID.UNDETERMINED)) {
103
+			thatOtherHeader.setReturnType(header.getReturnType());
93 104
 		}
94
-		
95 105
 		StoredType functionType = scope.getTypeRegistry()
96
-				.getFunction(genericHeader.withGenericArguments(new GenericMapper(position, scope.getTypeRegistry(), scope.genericInferenceMap)))
106
+				.getFunction(thatOtherHeader)
97 107
 				.stored(storage);
98 108
 		return new FunctionExpression(position, functionType, closure, header, statements);
99 109
 	}

+ 0
- 4
ScriptingExample/scripts/arithmetic.zs View File

@@ -1,4 +0,0 @@
1
-var j = 0;
2
-println(j);
3
-j++;
4
-println(j);

+ 0
- 75
ScriptingExample/scripts/arrays.zs View File

@@ -1,75 +0,0 @@
1
-//val a  = new int[](3, 10);
2
-//
3
-//for i in a {
4
-//	println(i);
5
-//}
6
-//
7
-//println(a[1]);
8
-////
9
-////
10
-val multiDim = new int[,,](1,2,3, 130);
11
-println(multiDim[0,1,2]);
12
-
13
-val t = multiDim;
14
-//
15
-//
16
-val b = new int[](3);
17
-for i in b {
18
-	println(i);
19
-}
20
-
21
-
22
-val c = new int[,,](1,2,3);
23
-println(c[0,1,2]);
24
-//
25
-//
26
-//val d = new string[,,](5,5,5, "HelloWorld");
27
-//println(d[2,2,2]);
28
-//
29
-//
30
-////val e = new int[](a, value => value);
31
-//
32
-//
33
-//var projection = (value => value) as function(value as string`borrow) as string;
34
-//val e = new string[](5, "HelloWorld");
35
-//val f = new string[]<string>(e, projection);
36
-//var projection = (value => value) as function(value as string`borrow) as string;
37
-//val a = new string[](5, "HelloWorld");
38
-//val b = new string[]<string>(a, projection);
39
-
40
-val d = new string[,,](3,4,5, "HelloWorld");
41
-
42
-val someString = "someString";
43
-
44
-var projection = (value => "" + value) as function(value as string`borrow) as string;
45
-val e = new string[,,]<string>(
46
-    d, 
47
-    (value => "137" + value + someString) as function(value as string`borrow) as string
48
-);
49
-
50
-
51
-val aSomeArray = new string[](5, "HelloWorld");
52
-val bSomeArray = new string[]<string>(aSomeArray, projection);
53
-println("HelloWorldProjectedArray");
54
-println(bSomeArray[1]);
55
-
56
-println(e[2,3,4]);
57
-
58
-
59
-val constructorLambdaArray = new string[](5, i => "No" + i);
60
-println(constructorLambdaArray[1]);
61
-
62
-val constructorLambdaArrayMulti = new string[,](5, 5, (i1, i2) => "No" + i1 + i2);
63
-println(constructorLambdaArrayMulti[1, 2]);
64
-
65
-
66
-val testArray = new string[,](5, 5, "helloWorld");
67
-
68
-val indexedProjectionWithLambdaNonInlined = new string[,]<string>(testArray as string[,], (index1, index2, value) => {
69
-    return value + "" + index1 + index2;
70
-} as function(index1 as usize, index2 as usize, value as string`borrow) as string);
71
-
72
-val indexedProjectionWithLambdaInlined = new string[,]<string>(testArray, ((i as usize, j as usize, s as string`borrow) => (s + "" + i + j) as string) as function(i as usize, j as usize, s as string`borrow) as string);
73
-
74
-println(indexedProjectionWithLambdaNonInlined[1, 2]);
75
-println(indexedProjectionWithLambdaInlined[1, 2]);

+ 0
- 83
ScriptingExample/scripts/classes.zs View File

@@ -1,83 +0,0 @@
1
-public class myTestClass {
2
-
3
-	var nonFinalInt as int = 10;
4
-	val finalInt as int = 20;
5
-
6
-	static var staticNonFinalInt as int = 10;
7
-
8
-	public this() {
9
-
10
-	}
11
-
12
-	public this(nonfinalInt as int) {
13
-		this.nonFinalInt = nonfinalInt;
14
-		println(nonfinalInt);
15
-	}
16
-
17
-	public test() as string {
18
-		return "TEST";
19
-	}
20
-}
21
-
22
-
23
-
24
-val tt = new myTestClass(666);
25
-println(tt.test());
26
-
27
-public interface myTestInterface {
28
-	test() as string;
29
-}
30
-
31
-
32
-public enum myTestEnum {
33
-	ADD(6),
34
-    SUB(6),
35
-    MUL(7),
36
-    DIV(7),
37
-    MOD(7),
38
-    CAT(6),
39
-    OR(4),
40
-    AND(4),
41
-    XOR(4),
42
-    NEG(8),
43
-    NOT(8),
44
-    INVERT(8),
45
-    CONTAINS(5),
46
-    COMPARE(5),
47
-    ASSIGN(0),
48
-    ADDASSIGN(0),
49
-    SUBASSIGN(0),
50
-    MULASSIGN(0),
51
-    DIVASSIGN(0),
52
-    MODASSIGN(0),
53
-    CATASSIGN(0),
54
-    ORASSIGN(0),
55
-    ANDASSIGN(0),
56
-    XORASSIGN(0),
57
-    ANDAND(3),
58
-    OROR(2),
59
-    TERNARY(1),
60
-    COALESCE(2),
61
-    INCREMENT(8),
62
-    DECREMENT(8),
63
-    MEMBER(9),
64
-    RANGE(9),
65
-    INDEX(9),
66
-    CALL(9),
67
-    CAST(9),
68
-    PRIMARY(10);
69
-
70
-	private val priority as int;
71
-	private val isCommutative as bool;
72
-
73
-	public static val test as int = 10;
74
-
75
-	this(i as int) {
76
-		this(i, false);
77
-	}
78
-
79
-	this(i as int, isCommutative as bool) {
80
-		this.priority = i;
81
-		this.isCommutative = isCommutative;
82
-		}
83
-}

+ 0
- 58
ScriptingExample/scripts/conditionals.zs View File

@@ -1,58 +0,0 @@
1
-val ternaryOperation = true ? 100 : 222;
2
-
3
-println(ternaryOperation);
4
-
5
-
6
-
7
-var coalesce_one as string? = null;
8
-var coalesce_tow as string? = "test";
9
-
10
-
11
-var coalesce = coalesce_one ?? coalesce_tow;
12
-
13
-println(coalesce);
14
-
15
-
16
-//if(coalesce == "test123") {
17
-//	println("true");
18
-//}
19
-
20
-if 1 == 1 {
21
-	println("intCompareTrue");
22
-}
23
-
24
-//if(1 == "1") {
25
-//	println("well...");
26
-//}
27
-
28
-
29
-if "1" == 1 {
30
-	println("...");
31
-}
32
-
33
-
34
-//var coco as int? = 10;
35
-//var coal as int? = 1;
36
-//
37
-//println(coal ?? coco);
38
-
39
-
40
-println(".....");
41
-println(true ? "RR" : "TT");
42
-
43
-
44
-println((false && true && true) ? "true" : "false");
45
-println((true && true) ? "true" : "false");
46
-println((true && false) ? "true" : "false");
47
-println((false && false) ? "true" : "false");
48
-
49
-println("---");
50
-
51
-println((false || true) ? "true" : "false");
52
-println((true || true) ? "true" : "false");
53
-println((true || false) ? "true" : "false");
54
-println((false || false) ? "true" : "false");
55
-
56
-println(":::");
57
-
58
-println((1 <= 2) ? "true" : "false");

+ 0
- 17
ScriptingExample/scripts/functionalInterfaces.zs View File

@@ -1,17 +0,0 @@
1
-//val x = ((a as string) => "hello" + a) as function`auto(a as string) as string`auto;
2
-//invokeFunctional(x);
3
-
4
-val y = (a as int, b as int) => a + b;
5
-
6
-
7
-//invokeFunctionalInt((a, b) => a + b);
8
-invokeFunctionalInt(y);
9
-
10
-
11
-println(((x as int) => x)(10));
12
-
13
-//TODO: Globals can't be "captured"
14
-//invokeFunctionalInt((a, b) => {
15
-//	println("a");
16
-//	return a + b;
17
-//});

+ 0
- 20
ScriptingExample/scripts/functions.zs View File

@@ -1,20 +0,0 @@
1
-function test() as void {
2
-	println("functions.zs; test1");
3
-}
4
-
5
-
6
-
7
-function test2() as void {
8
-	println("functions.zs; test2");
9
-}
10
-
11
-
12
-test();
13
-test2();
14
-println(test3(1, 3));
15
-
16
-
17
-function test3(a as int, b as int) as int{
18
-	println(a+b);
19
-	return a + b;
20
-}

+ 0
- 19
ScriptingExample/scripts/functions2.zs View File

@@ -1,19 +0,0 @@
1
-val outOfScope = 10;
2
-
3
-val fun = (a as int, b as int) as int => a + b * outOfScope;
4
-println(fun(30, 20));
5
-
6
-function apply_fn2(fn as function(value as int, othervalue as int) as int, value as int) as int {
7
-    return fn(value, value);
8
-}
9
-
10
-//function scale(value as int, scale as int) as int {
11
-
12
-//	val fun as function(value as int, othervalue as int) as int = ((v as int) as int => v * scale);
13
-//    return apply(fun, scale);
14
-//}
15
-
16
-apply_fn2(fun, 10);
17
-
18
-//println("Value: " + scale(10, 5));
19
-

+ 0
- 1
ScriptingExample/scripts/functions3.zs View File

@@ -1 +0,0 @@
1
-function apply2(fn as function(value as int) as int, scale as int) as int {
    return fn(scale);
}

function scale2(value as int, scale as int) as int {
    return apply2(v => v * scale, value);
}

+ 0
- 22
ScriptingExample/scripts/functions_lambdas.zs View File

@@ -1,22 +0,0 @@
1
-val outOfScope = 10;
2
-val fun1 = (a as int, b as int) as int => a + b * outOfScope;
3
-val fun2 = (a as int, c as int) as int => 13;
4
-
5
-
6
-
7
-function apply(fn as function(value as int) as int, scale as int) as int {
8
-    return fn(scale);
9
-}
10
-
11
-
12
-println(fun1(30, 20));
13
-println(fun2(30, 20));
14
-
15
-
16
-
17
-
18
-function scale(value as int, scale as int) as int {
19
-    return apply(v => v * scale, value);
20
-}
21
-
22
-println("Value: " + scale(10, 5));

+ 0
- 12
ScriptingExample/scripts/helloworld.zs View File

@@ -1,12 +0,0 @@
1
-println("Hello \&copy; world!");
2
-println(5);
3
-println(2 + 5);
4
-println(1 - 2);
5
-println(1 + 3 as long);
6
-
7
-println(<hello world in bracket parser>);
8
-
9
-
10
-var a = null as string?;
11
-if (a == null)
12
-	println("A is null!");

+ 0
- 54
ScriptingExample/scripts/integration.zs View File

@@ -1,54 +0,0 @@
1
-import example.TestClass;
2
-import example.TestInterface;
3
-
4
-val instance = new TestClass("Instance");
5
-println("Name: " + instance.name);
6
-instance.dump();
7
-instance.withDefaultParameter("Hello");
8
-instance.withDefaultParameter("Hello", "moon");
9
-
10
-var generated = instance.generate();
11
-println(generated.getValue());
12
-
13
-class TestOperators {
14
-	public (name as string) as void {
15
-		println("MyTestClass: " + name);
16
-	}
17
-	
18
-	//.(key as string) as string
19
-	//	=> "key " + key;
20
-}
21
-
22
-/*class TestImplementation {
23
-	public implements TestInterface {
24
-		interfaceMethod() => "TestImplementation";
25
-	}
26
-}*/
27
-
28
-val testInstance = new TestOperators();
29
-//testInstance("something");
30
-
31
-something.dump();
32
-
33
-val objects = makeArray(5);
34
-printMany(objects);
35
-
36
-
37
-println(<test string>);
38
-println(<test string>.name);
39
-println(<test string>.interfaceMethod());
40
-//println(new TestImplementation().interfaceMethod());
41
-
42
-var diamond = <item:minecraft:diamond>;
43
-var dirt = <item:minecraft:dirt>;
44
-addShapedRecipe("TestRecipe", diamond, [[dirt, dirt, dirt],[dirt, dirt, dirt],[dirt, dirt, dirt]]);
45
-
46
-var count = 10;
47
-floatMethod(5f * count);
48
-
49
-invokeFunctional(name => "Hello " + name);
50
-
51
-//val x = (a as string) => "hello" + a;
52
-//invokeFunctional(x);
53
-
54
-testOptional(value => value == null ? 0 : value.length as int);

+ 0
- 17
ScriptingExample/scripts/interfaces.zs View File

@@ -1,17 +0,0 @@
1
-public interface MyInterface {
2
-	interfaceMethod() as string;
3
-}
4
-
5
-public class MyClass {
6
-	val name as string;
7
-	
8
-	public this(name as string) {
9
-		this.name = name;
10
-	}
11
-
12
-	public implements MyInterface {
13
-		interfaceMethod() => "InterfaceMethod " + name;
14
-	}
15
-}
16
-
17
-println(new MyClass("hello").interfaceMethod());

+ 0
- 94
ScriptingExample/scripts/moreHellos.zs View File

@@ -1,94 +0,0 @@
1
-println("Hello world!");
2
-println(1 as string);
3
-
4
-
5
-var test = "test";
6
-println(test);
7
-
8
-
9
-test = "testMore";
10
-println(test);
11
-
12
-test = 13;
13
-println(test);
14
-
15
-val test2 = 14;
16
-println(test2);
17
-
18
-
19
-if true 
20
-    println("ifTest");
21
-else
22
-    println("elseTest");
23
-
24
-
25
-if false 
26
-    println("testIf");
27
-else
28
-    println("testElse");
29
-
30
-
31
-if (true) {
32
-
33
-    while :testLable true {
34
-        println("trueee");
35
-        if true
36
-            break testLable;
37
-        else
38
-            println("nobreak");
39
-    }
40
-    
41
-    
42
-    do {
43
-        println("tru"); 
44
-        if(false){
45
-        	println("brea");
46
-            continue;
47
-        }
48
-        else{
49
-            println("");
50
-            break;
51
-        }
52
-    } while true;
53
-}
54
-
55
-var testArray = ["1", "2", "3"];
56
-
57
-for item in testArray {
58
-	println("test");
59
-	println(item);
60
-}
61
-
62
-println("");
63
-
64
-var test3 = [1, 2, 3];
65
-
66
-for item in test3 {
67
-	println(item);
68
-}
69
-
70
-for i, item in ["5", "ttt"] {
71
-	println(item + i);
72
-}
73
-
74
-for i, item in [1, 5, 7] {
75
-	println(item + (i as int));
76
-}
77
-
78
-for myI in 10 .. 20 {
79
-	println(myI);
80
-}
81
-
82
-var lateInit as string;
83
-lateInit = "initialized later";
84
-println(lateInit);
85
-
86
-
87
-
88
-
89
-
90
-/*
91
-catch e as string {
92
-	println("noLuv");
93
-}
94
-*/

+ 0
- 3
ScriptingExample/scripts/scriptparameters.zs View File

@@ -1,3 +0,0 @@
1
-println("Arguments:");
2
-for i, arg in args
3
-	println("- " + i + ": " + arg);

+ 0
- 2
ScriptingExample/scripts/statements.zs View File

@@ -1,2 +0,0 @@
1
-//while true
2
-//	println("Hello!");

+ 0
- 18
ScriptingExample/scripts/switch.zs View File

@@ -1,18 +0,0 @@
1
-val test as int = 10;
2
-
3
-
4
-switch test {
5
-	case 1 : println("yay"); break;
6
-	case 10 : println("yo");
7
-	case 100 : println("y");
8
-	default: println("yup");
9
-}
10
-
11
-
12
-val test2 as string = "hello";
13
-
14
-switch test2 {
15
-	case "bye" : println("hello");
16
-	case "hello" : println("goodbye");
17
-	default: println("hellogoodbye");
18
-}

+ 0
- 70
ScriptingExample/scripts/variants.zs View File

@@ -1,70 +0,0 @@
1
-public variant Result<T, E> {
2
-    Ok(T),
3
-    Error(E),
4
-    Other(T, E);
5
-    
6
-    /*public then<R>(fn as function(result as T) as Result<R, E>) as Result<R, E> {
7
-        return match this {
8
-            Ok(result) => fn(result),
9
-            Error(error) => Error(error),
10
-			Other(result, error) => fn(result)
11
-        };
12
-    }*/
13
-    
14
-    //public handle<X>(handler as function(error as E) as Result<T, X>) as Result<T, X> {
15
-    //    return match this {
16
-    //        Ok(result) => Ok(result),
17
-    //        Error(error) => handler(error)
18
-    //    };
19
-    //}
20
-    
21
-    public expect() as T {
22
-        return match this {
23
-            Ok(result) => result,
24
-            Error(error) => panic "expect() called on an error value",
25
-			Other(result, error) => result
26
-        };
27
-    }
28
-    
29
-    public orElse(other as T) as T {
30
-        return match this {
31
-            Ok(result) => result,
32
-            Error(error) => other,
33
-			Other(result, error) => result
34
-        };
35
-    }
36
-    
37
-    public orElse(other as function(error as E) as T) as T {
38
-        return match this {
39
-            Ok(result) => result,
40
-            Error(error) => other(error),
41
-			Other(result, error) => result
42
-        };
43
-    }
44
-}
45
-
46
-
47
-
48
-function makeResult() as Result<string, string>
49
-    => Ok("10");
50
-
51
-
52
-function makeErrResult() as Result<string, string>
53
-    => Error("10");
54
-
55
-
56
-println(makeResult().orElse("Ten"));
57
-println(makeResult().expect());
58
-println(makeErrResult().orElse("Ten"));
59
-
60
-
61
-//CompileException [TYPE_ARGUMENTS_NOT_INFERRABLE] Could not infer generic type parameters [ParsedExpressionFunction.compile, line 75]
62
-//println(makeResult().then(tValue => Result<string, string>.Ok(tValue)).expect());
63
-
64
-//IllegalArgumentException: Cannot retrieve members of undetermined type [TypeMembers.<init>, line 71]
65
-//println(makeResult().then(a => Ok(a)).expect());
66
-//println(makeResult().then(a as string => Ok(a)).expect());
67
-
68
-//CompileException [UNEXPECTED_TOKEN] ) expected [LLparserTokenStream.required, line 97]
69
-//Wants to compile a call to function() instead of creating a lambda
70
-//println(makeResult().then((function (t as string) as Result<string, string>)(t => Ok(t))).expect());

+ 4
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Globals.java View File

@@ -67,6 +67,10 @@ public class Globals implements ZenCodeGlobals {
67 67
 	public static TestClass bracket(String value) {
68 68
 		return new TestClass(value);
69 69
 	}
70
+
71
+	public static String staticToString(String value) {
72
+		return value;
73
+	}
70 74
 	
71 75
 	@FunctionalInterface
72 76
 	public static interface MyFunctionalInterface {

+ 19
- 7
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java View File

@@ -15,35 +15,47 @@ import org.openzen.zenscript.codemodel.SemanticModule;
15 15
 import org.openzen.zenscript.codemodel.type.StringTypeID;
16 16
 import org.openzen.zenscript.lexer.ParseException;
17 17
 import org.openzen.zenscript.parser.BracketExpressionParser;
18
+import org.openzen.zenscript.parser.EscapableBracketParser;
19
+import org.openzen.zenscript.parser.PrefixedBracketParser;
18 20
 import org.openzen.zenscript.parser.SimpleBracketParser;
21
+import org.openzen.zenscript.scriptingexample.events.EventManager;
22
+import org.openzen.zenscript.scriptingexample.events.IEvent;
23
+import org.openzen.zenscript.scriptingexample.events.impl.CTStringedEvent;
19 24
 
20 25
 public class Main {
21 26
 	public static void main(String[] args) throws CompileException, ParseException, IOException, NoSuchMethodException {
22 27
 		ScriptingEngine scriptingEngine = new ScriptingEngine();
23 28
 		scriptingEngine.debug = true;
24
-		
29
+
25 30
 		JavaNativeModule example = scriptingEngine.createNativeModule("example", "org.openzen.zenscript.scriptingexample");
26 31
 		example.addGlobals(Globals.class);
27 32
 		example.addClass(TestBaseInterface.class);
28 33
 		example.addClass(TestGenericInterface.class);
29 34
 		example.addClass(TestClass.class);
30 35
 		example.addClass(TestInterface.class);
36
+		example.addClass(IEvent.class);
37
+		example.addClass(EventManager.class);
38
+		example.addClass(CTStringedEvent.class);
31 39
 		scriptingEngine.registerNativeProvided(example);
32
-		
40
+
33 41
 		File inputDirectory = new File("scripts");
34 42
 		File[] inputFiles = Optional.ofNullable(inputDirectory.listFiles((dir, name) -> name.endsWith(".zs"))).orElseGet(() -> new File[0]);
35 43
 		SourceFile[] sourceFiles = new SourceFile[inputFiles.length];
36 44
 		for (int i = 0; i < inputFiles.length; i++)
37 45
 			sourceFiles[i] = new FileSourceFile(inputFiles[i].getName(), inputFiles[i]);
38
-		
39
-		BracketExpressionParser bracketParser = new SimpleBracketParser(scriptingEngine.registry, example.loadStaticMethod(Globals.class.getMethod("bracket", String.class)));
46
+
47
+		final PrefixedBracketParser parser = new PrefixedBracketParser(null);
48
+		BracketExpressionParser testParser = new SimpleBracketParser(scriptingEngine.registry, example.loadStaticMethod(Globals.class.getMethod("bracket", String.class)));
49
+		parser.register("test", testParser);
50
+		BracketExpressionParser toStringParser = new EscapableBracketParser(scriptingEngine.registry, example.loadStaticMethod(Globals.class.getMethod("staticToString", String.class)));
51
+		parser.register("toString", toStringParser);
40 52
 		FunctionParameter parameter = new FunctionParameter(scriptingEngine.registry.getArray(StringTypeID.AUTO, 1).stored(), "args");
41
-		SemanticModule scripts = scriptingEngine.createScriptedModule("script", sourceFiles, bracketParser, new FunctionParameter[] { parameter });
53
+		SemanticModule scripts = scriptingEngine.createScriptedModule("script", sourceFiles, parser, new FunctionParameter[] { parameter });
42 54
 		if (!scripts.isValid())
43 55
 			return;
44
-		
56
+
45 57
 		scriptingEngine.registerCompiled(scripts);
46
-		
58
+
47 59
 		Map<FunctionParameter, Object> scriptArgs = new HashMap<>();
48 60
 		scriptArgs.put(parameter, new String[] { "hello", "world", "example" });
49 61
 		scriptingEngine.run(scriptArgs);

+ 24
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/EventManager.java View File

@@ -0,0 +1,24 @@
1
+package org.openzen.zenscript.scriptingexample.events;
2
+
3
+import org.openzen.zencode.java.ZenCodeType;
4
+import org.openzen.zenscript.scriptingexample.events.impl.CTStringedEvent;
5
+import org.openzen.zenscript.scriptingexample.events.impl.StringedEvent;
6
+
7
+@ZenCodeType.Name("example.org.openzen.scripting_example.events.EventManager")
8
+public class EventManager {
9
+    @ZenCodeType.Method
10
+    public static void register(IEvent<?, ?> event) {
11
+    //public static <EVE extends IEvent<EVE, VA>, VA extends SomeMCEvent> void register(IEvent<EVE, VA> event) {
12
+        System.out.println("HIT!!!");
13
+
14
+        if(event instanceof CTStringedEvent) {
15
+            ((CTStringedEvent)event).getHandler().accept(new CTStringedEvent(null));
16
+        }
17
+    }
18
+
19
+    //@ZenCodeType.Method
20
+    //public static void register(CTStringedEvent event) {
21
+    //    System.out.println("HIT!!!");
22
+    //    event.getConsumer().accept(new StringedEvent("Abcdef"));
23
+    //}
24
+}

+ 41
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/IEvent.java View File

@@ -0,0 +1,41 @@
1
+package org.openzen.zenscript.scriptingexample.events;
2
+
3
+import org.openzen.zencode.java.ZenCodeType;
4
+
5
+import java.util.function.Consumer;
6
+
7
+@ZenCodeType.Name("example.org.openzen.scripting_example.events.IEvent")
8
+public abstract class IEvent<E extends IEvent<E, V>, V extends SomeMCEvent> {
9
+
10
+    private V internal;
11
+
12
+    private Consumer<E> handler;
13
+
14
+    public IEvent(V internal) {
15
+        this.internal = internal;
16
+    }
17
+
18
+    @ZenCodeType.Constructor
19
+    public IEvent(Consumer<E> handler) {
20
+        this.handler = handler;
21
+    }
22
+
23
+    public void setInternal(V internal) {
24
+        this.internal = internal;
25
+    }
26
+
27
+    public abstract Consumer<V> getConsumer();
28
+
29
+
30
+    public V getInternal() {
31
+        return internal;
32
+    }
33
+
34
+    public Consumer<E> getHandler() {
35
+        return handler;
36
+    }
37
+
38
+    public void setHandler(Consumer<E> handler) {
39
+        this.handler = handler;
40
+    }
41
+}

+ 4
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/SomeMCEvent.java View File

@@ -0,0 +1,4 @@
1
+package org.openzen.zenscript.scriptingexample.events;
2
+
3
+public interface SomeMCEvent {
4
+}

+ 25
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/impl/CTStringedEvent.java View File

@@ -0,0 +1,25 @@
1
+package org.openzen.zenscript.scriptingexample.events.impl;
2
+
3
+import org.openzen.zencode.java.ZenCodeType;
4
+import org.openzen.zenscript.scriptingexample.events.IEvent;
5
+
6
+import java.util.function.Consumer;
7
+
8
+@ZenCodeType.Name("example.org.openzen.scripting_example.events.CTStringedEvent")
9
+public class CTStringedEvent extends IEvent<CTStringedEvent, StringedEvent> {
10
+
11
+    @ZenCodeType.Getter("blub")
12
+    public String getBlub() {
13
+        return "ASDF";
14
+    }
15
+
16
+    @ZenCodeType.Constructor
17
+    public CTStringedEvent(Consumer<CTStringedEvent> handler) {
18
+        super(handler);
19
+    }
20
+
21
+    @Override
22
+    public Consumer<StringedEvent> getConsumer() {
23
+        return stringedEvent -> getHandler().accept(new CTStringedEvent(null));
24
+    }
25
+}

+ 12
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/events/impl/StringedEvent.java View File

@@ -0,0 +1,12 @@
1
+package org.openzen.zenscript.scriptingexample.events.impl;
2
+
3
+import org.openzen.zenscript.scriptingexample.events.SomeMCEvent;
4
+
5
+public class StringedEvent implements SomeMCEvent {
6
+    private final String text;
7
+
8
+    public StringedEvent(String text) {
9
+
10
+        this.text = text;
11
+    }
12
+}

Loading…
Cancel
Save