Browse Source

- Fix various bugs in the new system

- Slightly improved the way modules are compiled
Stan Hebben 6 years ago
parent
commit
cf2a752723
21 changed files with 186 additions and 113 deletions
  1. 9
    11
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionHeader.java
  2. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionParameter.java
  3. 6
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/Module.java
  4. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionBuilder.java
  5. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/DefinitionMember.java
  6. 3
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberGroup.java
  7. 4
    3
      CompilerShared/src/main/java/org/openzen/zenscript/compiler/SemanticModule.java
  8. 1
    8
      CompilerShared/src/main/java/org/openzen/zenscript/compiler/ZenCodeCompiler.java
  9. 21
    0
      CompilerShared/src/main/java/org/openzen/zenscript/compiler/ZenCodeCompilingModule.java
  10. 20
    12
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java
  11. 13
    0
      JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaCompiledModule.java
  12. 8
    0
      JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaContext.java
  13. 2
    0
      JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareClassMethodVisitor.java
  14. 16
    4
      JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareDefinitionMemberVisitor.java
  15. 2
    1
      JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareDefinitionVisitor.java
  16. 7
    10
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaDefinitionVisitor.java
  17. 5
    6
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaMemberCompiler.java
  18. 49
    29
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceCompiler.java
  19. 6
    16
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceFile.java
  20. 1
    1
      Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedNewExpression.java
  21. 6
    4
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java

+ 9
- 11
CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionHeader.java View File

@@ -46,7 +46,7 @@ public class FunctionHeader {
46 46
 		
47 47
 		minParameters = 0;
48 48
 		maxParameters = 0;
49
-		hasUnknowns = returnType == BasicTypeID.UNDETERMINED.stored;
49
+		hasUnknowns = returnType.type == BasicTypeID.UNDETERMINED;
50 50
 	}
51 51
 	
52 52
 	public FunctionHeader(BasicTypeID returnType) {
@@ -123,7 +123,7 @@ public class FunctionHeader {
123 123
 	}
124 124
 	
125 125
 	public boolean isDenormalized() {
126
-		if (returnType.getNormalized() != returnType)
126
+		if (!returnType.getNormalized().equals(returnType))
127 127
 			return true;
128 128
 		for (FunctionParameter parameter : parameters)
129 129
 			if (parameter.type.getNormalized() != parameter.type)
@@ -138,7 +138,7 @@ public class FunctionHeader {
138 138
 		
139 139
 		FunctionParameter[] normalizedParameters = new FunctionParameter[parameters.length];
140 140
 		for (int i = 0; i < normalizedParameters.length; i++)
141
-			normalizedParameters[i] = parameters[i].normalized(registry);
141
+			normalizedParameters[i] = parameters[i].normalize(registry);
142 142
 		return new FunctionHeader(typeParameters, returnType.getNormalized(), thrownType == null ? null : thrownType.getNormalized(), storage, normalizedParameters);
143 143
 	}
144 144
 	
@@ -154,10 +154,6 @@ public class FunctionHeader {
154 154
 		return false;
155 155
 	}
156 156
 	
157
-	public FunctionHeader withReturnType(StoredType returnType) {
158
-		return new FunctionHeader(typeParameters, returnType, thrownType, storage, parameters);
159
-	}
160
-	
161 157
 	public FunctionHeader inferFromOverride(GlobalTypeRegistry registry, FunctionHeader overridden) {
162 158
 		TypeParameter[] resultTypeParameters = typeParameters;
163 159
 		StoredType resultReturnType = this.returnType;
@@ -188,7 +184,7 @@ public class FunctionHeader {
188 184
 		if (arguments.arguments.length < minParameters || arguments.arguments.length > maxParameters)
189 185
 			return false;
190 186
 		
191
-		FunctionHeader header = fillGenericArguments(scope.getTypeRegistry(), arguments.typeArguments, scope.getLocalTypeParameters());
187
+		FunctionHeader header = fillGenericArguments(scope, arguments.typeArguments);
192 188
 		for (int i = 0; i < header.parameters.length; i++) {
193 189
 			if (!arguments.arguments[i].type.equals(header.parameters[i].type))
194 190
 				return false;
@@ -201,7 +197,7 @@ public class FunctionHeader {
201 197
 		if (arguments.arguments.length < minParameters || arguments.arguments.length > maxParameters)
202 198
 			return false;
203 199
 		
204
-		FunctionHeader header = fillGenericArguments(scope.getTypeRegistry(), arguments.typeArguments, scope.getLocalTypeParameters());
200
+		FunctionHeader header = fillGenericArguments(scope, arguments.typeArguments);
205 201
 		for (int i = 0; i < header.parameters.length; i++) {
206 202
 			if (!scope.getTypeMembers(arguments.arguments[i].type).canCastImplicit(header.parameters[i].type))
207 203
 				return false;
@@ -221,6 +217,8 @@ public class FunctionHeader {
221 217
 			}
222 218
 			result.append('>');
223 219
 		}
220
+		if (storage != null)
221
+			result.append(storage);
224 222
 		result.append('(');
225 223
 		for (int i = 0; i < parameters.length; i++) {
226 224
 			if (i > 0)
@@ -388,12 +386,12 @@ public class FunctionHeader {
388 386
 		return new FunctionHeader(typeParameters, returnType, thrownType == null ? null : thrownType.instance(mapper), storage, parameters);
389 387
 	}
390 388
 	
391
-	public FunctionHeader fillGenericArguments(GlobalTypeRegistry registry, StoredType[] arguments, GenericMapper typeParameterMapping) {
389
+	public FunctionHeader fillGenericArguments(TypeScope scope, StoredType[] arguments) {
392 390
 		if (arguments == null || arguments.length == 0)
393 391
 			return this;
394 392
 		
395 393
 		Map<TypeParameter, StoredType> typeArguments = StoredType.getMapping(typeParameters, arguments);
396
-		GenericMapper mapper = typeParameterMapping.getInner(registry, typeArguments);
394
+		GenericMapper mapper = scope.getLocalTypeParameters().getInner(scope.getTypeRegistry(), typeArguments);
397 395
 		
398 396
 		StoredType returnType = this.returnType.instance(mapper);
399 397
 		FunctionParameter[] parameters = new FunctionParameter[this.parameters.length];

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionParameter.java View File

@@ -54,7 +54,7 @@ public class FunctionParameter extends Taggable {
54 54
 		this.variadic = variadic;
55 55
 	}
56 56
 	
57
-	public FunctionParameter normalized(GlobalTypeRegistry registry) {
57
+	public FunctionParameter normalize(GlobalTypeRegistry registry) {
58 58
 		FunctionParameter result = new FunctionParameter(type.getNormalized(), name, defaultValue, variadic);
59 59
 		result.annotations = this.annotations;
60 60
 		return result;

+ 6
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/Module.java View File

@@ -5,13 +5,11 @@
5 5
  */
6 6
 package org.openzen.zenscript.codemodel;
7 7
 
8
-import org.openzen.zencode.shared.Taggable;
9
-
10 8
 /**
11 9
  *
12 10
  * @author Hoofdgebruiker
13 11
  */
14
-public class Module extends Taggable {
12
+public class Module {
15 13
 	public static final Module BUILTIN = new Module("builtin");
16 14
 	
17 15
 	public final String name;
@@ -19,4 +17,9 @@ public class Module extends Taggable {
19 17
 	public Module(String name) {
20 18
 		this.name = name;
21 19
 	}
20
+	
21
+	@Override
22
+	public String toString() {
23
+		return name;
24
+	}
22 25
 }

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionBuilder.java View File

@@ -61,7 +61,7 @@ public class ExpressionBuilder {
61 61
 				type,
62 62
 				member,
63 63
 				compiledArguments,
64
-				compiledArguments.getNumberOfTypeArguments() == 0 ? member.getHeader() : member.getHeader().fillGenericArguments(scope.getTypeRegistry(), compiledArguments.typeArguments, scope.getLocalTypeParameters()));
64
+				compiledArguments.getNumberOfTypeArguments() == 0 ? member.getHeader() : member.getHeader().fillGenericArguments(scope, compiledArguments.typeArguments));
65 65
 	}
66 66
 	
67 67
 	public Expression not(Expression value) throws CompileException {

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

@@ -47,6 +47,11 @@ public abstract class DefinitionMember extends Taggable implements IDefinitionMe
47 47
 		return annotations;
48 48
 	}
49 49
 	
50
+	@Override
51
+	public String toString() {
52
+		return describe();
53
+	}
54
+	
50 55
 	public boolean isStatic() {
51 56
 		return Modifiers.isStatic(getEffectiveModifiers());
52 57
 	}

+ 3
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberGroup.java View File

@@ -287,7 +287,7 @@ public class TypeMemberGroup {
287 287
 	
288 288
 	public Expression call(CodePosition position, TypeScope scope, Expression target, CallArguments arguments, boolean allowStaticUsage) throws CompileException {
289 289
 		FunctionalMemberRef method = selectMethod(position, scope, arguments, true, allowStaticUsage);
290
-		FunctionHeader instancedHeader = method.getHeader().fillGenericArguments(scope.getTypeRegistry(), arguments.typeArguments, scope.getLocalTypeParameters());
290
+		FunctionHeader instancedHeader = method.getHeader().fillGenericArguments(scope, arguments.typeArguments);
291 291
 		for (int i = 0; i < arguments.arguments.length; i++) {
292 292
 			arguments.arguments[i] = arguments.arguments[i].castImplicit(position, scope, instancedHeader.parameters[i].type);
293 293
 		}
@@ -316,13 +316,13 @@ public class TypeMemberGroup {
316 316
 			CompareType compareType) throws CompileException
317 317
 	{
318 318
 		FunctionalMemberRef method = selectMethod(position, scope, arguments, true, false);
319
-		FunctionHeader instancedHeader = method.getHeader().fillGenericArguments(scope.getTypeRegistry(), arguments.typeArguments, scope.getLocalTypeParameters());
319
+		FunctionHeader instancedHeader = method.getHeader().fillGenericArguments(scope, arguments.typeArguments);
320 320
 		return method.callWithComparator(position, compareType, target, instancedHeader, arguments, scope);
321 321
 	}
322 322
 	
323 323
 	public Expression callStatic(CodePosition position, TypeID target, TypeScope scope, CallArguments arguments) throws CompileException {
324 324
 		FunctionalMemberRef method = selectMethod(position, scope, arguments, false, true);
325
-		FunctionHeader instancedHeader = method.getHeader().fillGenericArguments(scope.getTypeRegistry(), arguments.typeArguments, scope.getLocalTypeParameters());
325
+		FunctionHeader instancedHeader = method.getHeader().fillGenericArguments(scope, arguments.typeArguments);
326 326
 		return method.callStatic(position, target, instancedHeader, arguments, scope);
327 327
 	}
328 328
 	

+ 4
- 3
CompilerShared/src/main/java/org/openzen/zenscript/compiler/SemanticModule.java View File

@@ -136,13 +136,14 @@ public class SemanticModule {
136 136
 		if (state != State.VALIDATED)
137 137
 			throw new IllegalStateException("Module is not yet validated");
138 138
 		
139
-		compiler.addModule(this);
139
+		ZenCodeCompilingModule module = compiler.addModule(this);
140 140
 		for (HighLevelDefinition definition : definitions.getAll()) {
141
-			compiler.addDefinition(definition, this);
141
+			module.addDefinition(definition);
142 142
 		}
143 143
 		for (ScriptBlock script : scripts) {
144
-			compiler.addScriptBlock(script);
144
+			module.addScriptBlock(script);
145 145
 		}
146
+		module.finish();
146 147
 	}
147 148
 	
148 149
 	public ModuleContext getContext() {

+ 1
- 8
CompilerShared/src/main/java/org/openzen/zenscript/compiler/ZenCodeCompiler.java View File

@@ -5,19 +5,12 @@
5 5
  */
6 6
 package org.openzen.zenscript.compiler;
7 7
 
8
-import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
-import org.openzen.zenscript.codemodel.ScriptBlock;
10
-
11 8
 /**
12 9
  *
13 10
  * @author Hoofdgebruiker
14 11
  */
15 12
 public interface ZenCodeCompiler {
16
-	void addModule(SemanticModule module);
17
-	
18
-	void addDefinition(HighLevelDefinition definition, SemanticModule module);
19
-	
20
-	void addScriptBlock(ScriptBlock script);
13
+	ZenCodeCompilingModule addModule(SemanticModule module);
21 14
 	
22 15
 	void finish();
23 16
 	

+ 21
- 0
CompilerShared/src/main/java/org/openzen/zenscript/compiler/ZenCodeCompilingModule.java View File

@@ -0,0 +1,21 @@
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.compiler;
7
+
8
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
+import org.openzen.zenscript.codemodel.ScriptBlock;
10
+
11
+/**
12
+ *
13
+ * @author Hoofdgebruiker
14
+ */
15
+public interface ZenCodeCompilingModule {
16
+	void addDefinition(HighLevelDefinition definition);
17
+	
18
+	void addScriptBlock(ScriptBlock script);
19
+	
20
+	void finish();
21
+}

+ 20
- 12
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java View File

@@ -19,6 +19,7 @@ import org.openzen.zenscript.codemodel.statement.Statement;
19 19
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
20 20
 import org.openzen.zenscript.compiler.SemanticModule;
21 21
 import org.openzen.zenscript.compiler.ZenCodeCompiler;
22
+import org.openzen.zenscript.compiler.ZenCodeCompilingModule;
22 23
 import org.openzen.zenscript.javabytecode.compiler.JavaClassWriter;
23 24
 import org.openzen.zenscript.javabytecode.compiler.JavaScriptFile;
24 25
 import org.openzen.zenscript.javabytecode.compiler.JavaStatementVisitor;
@@ -64,21 +65,28 @@ public class JavaCompiler extends JavaBaseCompiler implements ZenCodeCompiler {
64 65
 	}
65 66
 	
66 67
 	@Override
67
-	public void addModule(SemanticModule module) {
68
+	public ZenCodeCompilingModule addModule(SemanticModule module) {
68 69
 		context.addModule(module.module);
69
-	}
70
-
71
-	@Override
72
-	public void addDefinition(HighLevelDefinition definition, SemanticModule module) {
73
-		JavaPrepareDefinitionVisitor preparer = new JavaPrepareDefinitionVisitor(context, context.getJavaModule(module.module), definition.position.getFilename(), null);
74
-		definition.accept(preparer);
75 70
 		
76
-		definitions.add(definition);
77
-	}
71
+		return new ZenCodeCompilingModule() {
72
+			@Override
73
+			public void addDefinition(HighLevelDefinition definition) {
74
+				JavaPrepareDefinitionVisitor preparer = new JavaPrepareDefinitionVisitor(context, context.getJavaModule(module.module), definition.position.getFilename(), null);
75
+				definition.accept(preparer);
78 76
 
79
-	@Override
80
-	public void addScriptBlock(ScriptBlock script) {
81
-		scripts.add(script);
77
+				definitions.add(definition);
78
+			}
79
+
80
+			@Override
81
+			public void addScriptBlock(ScriptBlock script) {
82
+				scripts.add(script);
83
+			}
84
+
85
+			@Override
86
+			public void finish() {
87
+				
88
+			}
89
+		};
82 90
 	}
83 91
 
84 92
 	private String getClassName(String filename) {

+ 13
- 0
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaCompiledModule.java View File

@@ -23,6 +23,7 @@ public class JavaCompiledModule {
23 23
 	public final Module module;
24 24
 	
25 25
 	private final Map<HighLevelDefinition, JavaClass> classes = new HashMap<>();
26
+	private final Map<HighLevelDefinition, JavaClass> expansionClasses = new HashMap<>();
26 27
 	private final Map<HighLevelDefinition, JavaNativeClass> nativeClasses = new HashMap<>();
27 28
 	private final Map<ImplementationMember, JavaImplementation> implementations = new HashMap<>();
28 29
 	private final Map<IDefinitionMember, JavaField> fields = new HashMap<>();
@@ -38,6 +39,10 @@ public class JavaCompiledModule {
38 39
 		classes.put(definition, cls);
39 40
 	}
40 41
 	
42
+	public void setExpansionClassInfo(HighLevelDefinition definition, JavaClass cls) {
43
+		expansionClasses.put(definition, cls);
44
+	}
45
+	
41 46
 	public JavaClass getClassInfo(HighLevelDefinition definition) {
42 47
 		JavaClass cls = classes.get(definition);
43 48
 		if (cls == null)
@@ -46,6 +51,14 @@ public class JavaCompiledModule {
46 51
 		return cls;
47 52
 	}
48 53
 	
54
+	public JavaClass getExpansionClassInfo(HighLevelDefinition definition) {
55
+		JavaClass cls = expansionClasses.get(definition);
56
+		if (cls == null)
57
+			return getClassInfo(definition);
58
+		
59
+		return cls;
60
+	}
61
+	
49 62
 	public JavaClass optClassInfo(HighLevelDefinition definition) {
50 63
 		return classes.get(definition);
51 64
 	}

+ 8
- 0
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaContext.java View File

@@ -102,6 +102,10 @@ public abstract class JavaContext {
102 102
 		return getJavaModule(definition.module).getClassInfo(definition);
103 103
 	}
104 104
 	
105
+	public JavaClass getJavaExpansionClass(HighLevelDefinition definition) {
106
+		return getJavaModule(definition.module).getExpansionClassInfo(definition);
107
+	}
108
+	
105 109
 	public JavaClass optJavaClass(HighLevelDefinition definition) {
106 110
 		return getJavaModule(definition.module).optClassInfo(definition);
107 111
 	}
@@ -118,6 +122,10 @@ public abstract class JavaContext {
118 122
 		getJavaModule(definition.module).setClassInfo(definition, cls);
119 123
 	}
120 124
 	
125
+	public void setJavaExpansionClass(HighLevelDefinition definition, JavaClass cls) {
126
+		getJavaModule(definition.module).setExpansionClassInfo(definition, cls);
127
+	}
128
+	
121 129
 	public void setJavaNativeClass(HighLevelDefinition definition, JavaNativeClass cls) {
122 130
 		getJavaModule(definition.module).setNativeClassInfo(definition, cls);
123 131
 	}

+ 2
- 0
JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareClassMethodVisitor.java View File

@@ -8,6 +8,7 @@ package org.openzen.zenscript.javashared.prepare;
8 8
 import org.openzen.zenscript.javashared.JavaNativeClass;
9 9
 import org.openzen.zencode.shared.StringExpansion;
10 10
 import org.openzen.zenscript.codemodel.FunctionHeader;
11
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
11 12
 import org.openzen.zenscript.codemodel.OperatorType;
12 13
 import org.openzen.zenscript.codemodel.annotations.NativeTag;
13 14
 import org.openzen.zenscript.codemodel.member.CallerMember;
@@ -298,6 +299,7 @@ public class JavaPrepareClassMethodVisitor implements MemberVisitor<Void> {
298 299
 		
299 300
 		if (member.getOverrides() != null) {
300 301
 			DefinitionMemberRef base = member.getOverrides();
302
+			
301 303
 			JavaMethod baseMethod = context.getJavaMethod(base.getTarget());
302 304
 			method = new JavaMethod(
303 305
 					cls,

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

@@ -45,9 +45,18 @@ public class JavaPrepareDefinitionMemberVisitor implements DefinitionVisitor<Jav
45 45
 	public void prepare(TypeID type) {
46 46
 		if (!(type instanceof DefinitionTypeID))
47 47
 			return;
48
-			
48
+		
49 49
 		HighLevelDefinition definition = ((DefinitionTypeID)type).definition;
50
-		System.out.println("Preparing " + definition.name);
50
+		prepare(definition);
51
+	}
52
+	
53
+	public void prepare(HighLevelDefinition definition) {
54
+		if (isPrepared(definition))
55
+			return;
56
+		if (definition.module != module.module)
57
+			throw new IllegalArgumentException("Definition is not in the same module as the current module!");
58
+		
59
+		System.out.println("~~ Preparing " + definition.name);
51 60
 		definition.accept(this);
52 61
 	}
53 62
 	
@@ -103,7 +112,7 @@ public class JavaPrepareDefinitionMemberVisitor implements DefinitionVisitor<Jav
103 112
 			return context.getJavaClass(definition);
104 113
 		
105 114
 		JavaNativeClass nativeClass = context.getJavaNativeClass(definition);
106
-		JavaClass cls = context.getJavaClass(definition);
115
+		JavaClass cls = context.getJavaExpansionClass(definition);
107 116
 		visitExpansionMembers(definition, cls, nativeClass);
108 117
 		return cls;
109 118
 	}
@@ -133,7 +142,10 @@ public class JavaPrepareDefinitionMemberVisitor implements DefinitionVisitor<Jav
133 142
 		if (nativeClass == null) {
134 143
 			visitClassMembers(definition, cls, null, startsEmpty);
135 144
 		} else {
136
-			visitExpansionMembers(definition, cls, nativeClass);
145
+			cls.membersPrepared = true;
146
+			JavaClass expansionCls = context.getJavaExpansionClass(definition);
147
+			visitExpansionMembers(definition, expansionCls, nativeClass);
148
+			cls.empty = expansionCls.empty;
137 149
 		}
138 150
 		return cls;
139 151
 	}

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

@@ -347,9 +347,10 @@ public class JavaPrepareDefinitionVisitor implements DefinitionVisitor<JavaClass
347 347
 			cls.destructible = definition.isDestructible();
348 348
 			context.setJavaClass(definition, cls);
349 349
 		} else {
350
-			cls = outerClass == null ? new JavaClass(definition.pkg.fullName, filename, kind) : new JavaClass(outerClass, filename, kind);
350
+			cls = outerClass == null ? new JavaClass(definition.pkg.fullName, definition.name + "Expansion", kind) : new JavaClass(outerClass, definition.name + "Expansion", kind);
351 351
 			
352 352
 			context.setJavaClass(definition, nativeClass.cls);
353
+			context.setJavaExpansionClass(definition, cls);
353 354
 			context.setJavaNativeClass(definition, nativeClass);
354 355
 			
355 356
 			if (nativeClass.nonDestructible)

+ 7
- 10
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaDefinitionVisitor.java View File

@@ -7,7 +7,6 @@ package org.openzen.zenscript.javasource;
7 7
 
8 8
 import java.util.ArrayList;
9 9
 import java.util.List;
10
-import java.util.Map;
11 10
 import org.openzen.zencode.shared.CodePosition;
12 11
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
13 12
 import org.openzen.zenscript.codemodel.Modifiers;
@@ -33,7 +32,6 @@ import org.openzen.zenscript.codemodel.type.StoredType;
33 32
 import org.openzen.zenscript.codemodel.type.TypeID;
34 33
 import org.openzen.zenscript.compiler.CompileScope;
35 34
 import org.openzen.zenscript.compiler.SemanticModule;
36
-import org.openzen.zenscript.javashared.JavaNativeClass;
37 35
 import org.openzen.zenscript.javasource.scope.JavaSourceFileScope;
38 36
 import org.openzen.zenscript.javasource.scope.JavaSourceStatementScope;
39 37
 import org.openzen.zenscript.javashared.JavaClass;
@@ -53,9 +51,9 @@ public class JavaDefinitionVisitor implements DefinitionVisitor<Void> {
53 51
 	private final JavaSourceFormattingSettings settings;
54 52
 	private final List<ExpansionDefinition> expansions;
55 53
 	private final StringBuilder output;
56
-	private final Map<HighLevelDefinition, SemanticModule> modules;
57 54
 	private final JavaContext context;
58 55
 	private final JavaCompiledModule module;
56
+	private final SemanticModule semanticModule;
59 57
 	
60 58
 	public JavaDefinitionVisitor(
61 59
 			String indent,
@@ -65,7 +63,7 @@ public class JavaDefinitionVisitor implements DefinitionVisitor<Void> {
65 63
 			JavaSourceFile file,
66 64
 			StringBuilder output,
67 65
 			List<ExpansionDefinition> expansions,
68
-			Map<HighLevelDefinition, SemanticModule> modules)
66
+			SemanticModule semanticModule)
69 67
 	{
70 68
 		this.indent = indent;
71 69
 		this.compiler = compiler;
@@ -74,17 +72,16 @@ public class JavaDefinitionVisitor implements DefinitionVisitor<Void> {
74 72
 		this.settings = compiler.settings;
75 73
 		this.output = output;
76 74
 		this.expansions = expansions;
77
-		this.modules = modules;
78 75
 		this.context = compiler.context;
79 76
 		this.module = module;
77
+		this.semanticModule = semanticModule;
80 78
 	}
81 79
 	
82 80
 	private JavaSourceFileScope createScope(HighLevelDefinition definition) {
83
-		SemanticModule module = modules.get(definition.getOutermost());
84
-		GlobalTypeRegistry typeRegistry = module.compilationUnit.globalTypeRegistry;
81
+		GlobalTypeRegistry typeRegistry = semanticModule.compilationUnit.globalTypeRegistry;
85 82
 		DefinitionTypeID thisType = typeRegistry.getForMyDefinition(definition);
86 83
 		
87
-		CompileScope scope = new CompileScope(module.compilationUnit.globalTypeRegistry, module.expansions, module.annotations, module.storageTypes);
84
+		CompileScope scope = new CompileScope(semanticModule.compilationUnit.globalTypeRegistry, semanticModule.expansions, semanticModule.annotations, semanticModule.storageTypes);
88 85
 		return new JavaSourceFileScope(file.importer, compiler.helperGenerator, cls, scope, definition instanceof InterfaceDefinition, thisType, compiler.context);
89 86
 	}
90 87
 	
@@ -103,7 +100,7 @@ public class JavaDefinitionVisitor implements DefinitionVisitor<Void> {
103 100
 	@Override
104 101
 	public Void visitClass(ClassDefinition definition) {
105 102
 		JavaSourceFileScope scope = createScope(definition);
106
-		JavaClass cls = context.getJavaClass(definition);
103
+		JavaClass cls = context.getJavaExpansionClass(definition);
107 104
 		
108 105
 		output.append(indent);
109 106
 		convertModifiers(definition.modifiers);
@@ -401,7 +398,7 @@ public class JavaDefinitionVisitor implements DefinitionVisitor<Void> {
401 398
 				member.accept(memberCompiler);
402 399
 			memberCompiler.finish();
403 400
 		} else {
404
-			JavaMemberCompiler memberCompiler = new JavaMemberCompiler(compiler, module, file, settings, indent + settings.indent, output, scope, scope.isInterface, definition, modules);
401
+			JavaMemberCompiler memberCompiler = new JavaMemberCompiler(compiler, module, file, settings, indent + settings.indent, output, scope, scope.isInterface, definition, semanticModule);
405 402
 			for (IDefinitionMember member : definition.members)
406 403
 				member.accept(memberCompiler);
407 404
 			

+ 5
- 6
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaMemberCompiler.java View File

@@ -9,7 +9,6 @@ import org.openzen.zenscript.javashared.JavaTypeNameVisitor;
9 9
 import java.util.ArrayList;
10 10
 import java.util.Collections;
11 11
 import java.util.List;
12
-import java.util.Map;
13 12
 import org.openzen.zenscript.codemodel.FunctionHeader;
14 13
 import org.openzen.zenscript.codemodel.FunctionParameter;
15 14
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
@@ -53,10 +52,10 @@ public class JavaMemberCompiler extends BaseMemberCompiler {
53 52
 	private final JavaSourceFile file;
54 53
 	private final List<FieldMember> fields = new ArrayList<>();
55 54
 	private final boolean isInterface;
56
-	private final Map<HighLevelDefinition, SemanticModule> modules;
57 55
 	private final JavaContext context;
58 56
 	public boolean hasDestructor = false;
59 57
 	private final JavaCompiledModule module;
58
+	private final SemanticModule semanticModule;
60 59
 	
61 60
 	public JavaMemberCompiler(
62 61
 			JavaSourceCompiler compiler,
@@ -68,16 +67,16 @@ public class JavaMemberCompiler extends BaseMemberCompiler {
68 67
 			JavaSourceFileScope scope,
69 68
 			boolean isInterface,
70 69
 			HighLevelDefinition definition,
71
-			Map<HighLevelDefinition, SemanticModule> modules)
70
+			SemanticModule semanticModule)
72 71
 	{
73 72
 		super(settings, indent, output, scope, null, definition);
74 73
 		
75 74
 		this.file = file;
76 75
 		this.isInterface = isInterface;
77 76
 		this.compiler = compiler;
78
-		this.modules = modules;
79 77
 		this.context = compiler.context;
80 78
 		this.module = module;
79
+		this.semanticModule = semanticModule;
81 80
 	}
82 81
 	
83 82
 	private void compileMethod(DefinitionMember member, FunctionHeader header, Statement body) {
@@ -243,7 +242,7 @@ public class JavaMemberCompiler extends BaseMemberCompiler {
243 242
 			
244 243
 			begin(ElementType.INNERCLASS);
245 244
 			output.append("private class ").append(implementationName).append(" implements ").append(scope.type(member.type)).append(" {\n");
246
-			JavaMemberCompiler memberCompiler = new JavaMemberCompiler(compiler, module, file, settings, indent + settings.indent, output, scope, isInterface, definition, modules);
245
+			JavaMemberCompiler memberCompiler = new JavaMemberCompiler(compiler, module, file, settings, indent + settings.indent, output, scope, isInterface, definition, semanticModule);
247 246
 			for (IDefinitionMember m : member.members) {
248 247
 				m.accept(memberCompiler);
249 248
 			}
@@ -266,7 +265,7 @@ public class JavaMemberCompiler extends BaseMemberCompiler {
266 265
 				file,
267 266
 				output,
268 267
 				Collections.emptyList(),
269
-				modules);
268
+				semanticModule);
270 269
 		member.innerDefinition.accept(visitor);
271 270
 		return null;
272 271
 	}

+ 49
- 29
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceCompiler.java View File

@@ -6,7 +6,9 @@
6 6
 package org.openzen.zenscript.javasource;
7 7
 
8 8
 import java.io.File;
9
+import java.util.ArrayList;
9 10
 import java.util.HashMap;
11
+import java.util.List;
10 12
 import java.util.Map;
11 13
 import org.openzen.zencode.shared.SourceFile;
12 14
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
@@ -15,6 +17,7 @@ import org.openzen.zenscript.codemodel.definition.ZSPackage;
15 17
 import org.openzen.zenscript.compiler.CompilationUnit;
16 18
 import org.openzen.zenscript.compiler.SemanticModule;
17 19
 import org.openzen.zenscript.compiler.ZenCodeCompiler;
20
+import org.openzen.zenscript.compiler.ZenCodeCompilingModule;
18 21
 import org.openzen.zenscript.javashared.JavaBaseCompiler;
19 22
 import org.openzen.zenscript.javashared.prepare.JavaPrepareDefinitionVisitor;
20 23
 import org.openzen.zenscript.javashared.JavaClass;
@@ -28,8 +31,8 @@ public class JavaSourceCompiler extends JavaBaseCompiler implements ZenCodeCompi
28 31
 	public final JavaSourceSyntheticHelperGenerator helperGenerator;
29 32
 	
30 33
 	private final File directory;
31
-	private final Map<File, JavaSourceFile> sourceFiles = new HashMap<>();
32 34
 	public final JavaSourceContext context;
35
+	private final List<SourceModule> modules = new ArrayList<>();
33 36
 	
34 37
 	public JavaSourceCompiler(File directory, CompilationUnit compilationUnit) {
35 38
 		if (!directory.exists())
@@ -43,39 +46,15 @@ public class JavaSourceCompiler extends JavaBaseCompiler implements ZenCodeCompi
43 46
 	}
44 47
 	
45 48
 	@Override
46
-	public void addModule(SemanticModule module) {
49
+	public ZenCodeCompilingModule addModule(SemanticModule module) {
47 50
 		context.addModule(module.module);
48
-	}
49
-	
50
-	@Override
51
-	public void addDefinition(HighLevelDefinition definition, SemanticModule module) {
52
-		String filename = getFilename(definition);
53
-		JavaPrepareDefinitionVisitor prepare = new JavaPrepareDefinitionVisitor(context, context.getJavaModule(module.module), filename, null);
54
-		JavaClass cls = definition.accept(prepare);
55
-		
56
-		File file = new File(getDirectory(definition.pkg), cls.getName() + ".java");
57
-		System.out.println("Compiling " + definition.name + " as " + cls.fullName);
58
-		JavaSourceFile sourceFile = sourceFiles.get(file);
59
-		if (sourceFile == null)
60
-			sourceFiles.put(file, sourceFile = new JavaSourceFile(this, file, cls, module.module, definition.pkg));
61
-		
62
-		sourceFile.add(definition, module);
63
-	}
64
-	
65
-	@Override
66
-	public void addScriptBlock(ScriptBlock script) {
67
-		
51
+		SourceModule result = new SourceModule(module);
52
+		modules.add(result);
53
+		return result;
68 54
 	}
69 55
 	
70 56
 	@Override
71 57
 	public void finish() {
72
-		for (JavaSourceFile sourceFile : sourceFiles.values()) {
73
-			sourceFile.prepare(context);
74
-		}
75
-		
76
-		for (JavaSourceFile sourceFile : sourceFiles.values())
77
-			sourceFile.write();
78
-		
79 58
 		helperGenerator.write();
80 59
 	}
81 60
 	
@@ -107,4 +86,45 @@ public class JavaSourceCompiler extends JavaBaseCompiler implements ZenCodeCompi
107 86
 			return definition.name == null ? "Expansion" : definition.name;
108 87
 		}
109 88
 	}
89
+	
90
+	private class SourceModule implements ZenCodeCompilingModule {
91
+		private final Map<File, JavaSourceFile> sourceFiles = new HashMap<>();
92
+		private final SemanticModule module;
93
+		
94
+		public SourceModule(SemanticModule module) {
95
+			this.module = module;
96
+		}
97
+
98
+		@Override
99
+		public void addDefinition(HighLevelDefinition definition) {
100
+			String filename = getFilename(definition);
101
+			JavaPrepareDefinitionVisitor prepare = new JavaPrepareDefinitionVisitor(context, context.getJavaModule(module.module), filename, null);
102
+			JavaClass cls = definition.accept(prepare);
103
+
104
+			File file = new File(getDirectory(definition.pkg), cls.getName() + ".java");
105
+			System.out.println("Compiling " + definition.name + " as " + cls.fullName);
106
+			JavaSourceFile sourceFile = sourceFiles.get(file);
107
+			if (sourceFile == null)
108
+				sourceFiles.put(file, sourceFile = new JavaSourceFile(JavaSourceCompiler.this, file, cls, module, definition.pkg));
109
+
110
+			sourceFile.add(definition);
111
+		}
112
+
113
+		@Override
114
+		public void addScriptBlock(ScriptBlock script) {
115
+			// TODO: implement this too
116
+		}
117
+
118
+		@Override
119
+		public void finish() {
120
+			for (JavaSourceFile sourceFile : sourceFiles.values()) {
121
+				sourceFile.prepare(context);
122
+			}
123
+
124
+			for (JavaSourceFile sourceFile : sourceFiles.values())
125
+				sourceFile.write();
126
+
127
+		}
128
+		
129
+	}
110 130
 }

+ 6
- 16
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceFile.java View File

@@ -15,13 +15,8 @@ import java.io.OutputStreamWriter;
15 15
 import java.io.Writer;
16 16
 import java.nio.charset.StandardCharsets;
17 17
 import java.util.ArrayList;
18
-import java.util.HashMap;
19
-import java.util.HashSet;
20 18
 import java.util.List;
21
-import java.util.Map;
22
-import java.util.Set;
23 19
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
24
-import org.openzen.zenscript.codemodel.Module;
25 20
 import org.openzen.zenscript.codemodel.annotations.NativeTag;
26 21
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
27 22
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
@@ -40,15 +35,12 @@ public class JavaSourceFile {
40 35
 	private final JavaClass cls;
41 36
 	private final StringBuilder contents = new StringBuilder();
42 37
 	private final ZSPackage pkg;
43
-	private final Module module;
38
+	private final SemanticModule module;
44 39
 	
45 40
 	private HighLevelDefinition mainDefinition;
46 41
 	private final List<ExpansionDefinition> expansions = new ArrayList<>();
47 42
 	
48
-	private final Map<HighLevelDefinition, SemanticModule> modules = new HashMap<>();
49
-	private final Set<String> existing = new HashSet<>();
50
-	
51
-	public JavaSourceFile(JavaSourceCompiler compiler, File file, JavaClass cls, Module module, ZSPackage pkg) {
43
+	public JavaSourceFile(JavaSourceCompiler compiler, File file, JavaClass cls, SemanticModule module, ZSPackage pkg) {
52 44
 		this.compiler = compiler;
53 45
 		this.pkg = pkg;
54 46
 		this.cls = cls;
@@ -62,7 +54,7 @@ public class JavaSourceFile {
62 54
 		return cls.getName();
63 55
 	}
64 56
 	
65
-	public void add(HighLevelDefinition definition, SemanticModule module) {
57
+	public void add(HighLevelDefinition definition) {
66 58
 		if (definition instanceof ExpansionDefinition) {
67 59
 			expansions.add((ExpansionDefinition)definition);
68 60
 		} else if (mainDefinition != null) {
@@ -70,12 +62,10 @@ public class JavaSourceFile {
70 62
 		} else {
71 63
 			mainDefinition = definition;
72 64
 		}
73
-		
74
-		modules.put(definition, module);
75 65
 	}
76 66
 	
77 67
 	public void prepare(JavaContext context) {
78
-		JavaPrepareDefinitionMemberVisitor visitor = new JavaPrepareDefinitionMemberVisitor(context, context.getJavaModule(module));
68
+		JavaPrepareDefinitionMemberVisitor visitor = new JavaPrepareDefinitionMemberVisitor(context, context.getJavaModule(module.module));
79 69
 		
80 70
 		if (mainDefinition != null)
81 71
 			mainDefinition.accept(visitor);
@@ -109,12 +99,12 @@ public class JavaSourceFile {
109 99
 		JavaDefinitionVisitor visitor = new JavaDefinitionVisitor(
110 100
 				"",
111 101
 				compiler,
112
-				compiler.context.getJavaModule(module),
102
+				compiler.context.getJavaModule(module.module),
113 103
 				cls,
114 104
 				this,
115 105
 				contents,
116 106
 				expansions,
117
-				modules);
107
+				module);
118 108
 		definition.accept(visitor);
119 109
 		
120 110
 		if (!file.getParentFile().exists())

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedNewExpression.java View File

@@ -72,7 +72,7 @@ public class ParsedNewExpression extends ParsedExpression{
72 72
 					type,
73 73
 					member,
74 74
 					compiledArguments,
75
-					member.getHeader().fillGenericArguments(scope.getTypeRegistry(), compiledArguments.typeArguments, scope.getLocalTypeParameters()));
75
+					member.getHeader().fillGenericArguments(scope, compiledArguments.typeArguments));
76 76
 		} catch (CompileException ex) {
77 77
 			return new InvalidExpression(type, ex);
78 78
 		}

+ 6
- 4
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java View File

@@ -25,6 +25,7 @@ import org.openzen.zenscript.formatter.ScriptFormattingSettings;
25 25
 import org.openzen.zenscript.javabytecode.JavaCompiler;
26 26
 import org.openzen.zenscript.javabytecode.JavaModule;
27 27
 import org.openzen.zenscript.compiler.ModuleSpace;
28
+import org.openzen.zenscript.compiler.ZenCodeCompilingModule;
28 29
 import org.openzen.zenscript.lexer.ParseException;
29 30
 import org.openzen.zenscript.lexer.ZSToken;
30 31
 import org.openzen.zenscript.lexer.ZSTokenParser;
@@ -131,14 +132,15 @@ public class Main {
131 132
 		JavaCompiler compiler = new JavaCompiler(module.compilationUnit.globalTypeRegistry, false, null);
132 133
 		registry.register(compiler.getContext());
133 134
 		
134
-		compiler.getContext().addModule(module.module);
135
-		
135
+		ZenCodeCompilingModule compiling = compiler.addModule(module);
136 136
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
137
-			compiler.addDefinition(definition, module);
137
+			compiling.addDefinition(definition);
138 138
 		}
139 139
 		for (ScriptBlock script : module.scripts) {
140
-			compiler.addScriptBlock(script);
140
+			compiling.addScriptBlock(script);
141 141
 		}
142
+		compiling.finish();
143
+		
142 144
 		return compiler.finishAndGetModule();
143 145
 	}
144 146
 	

Loading…
Cancel
Save