Browse Source

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

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

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

@@ -5,7 +5,7 @@
5 5
  */
6 6
 package org.openzen.zenscript.javabytecode;
7 7
 
8
-import java.util.HashMap;
8
+import java.util.LinkedHashMap;
9 9
 import java.util.Map;
10 10
 import org.objectweb.asm.ClassWriter;
11 11
 import org.objectweb.asm.Opcodes;
@@ -40,7 +40,7 @@ public class JavaCompiler {
40 40
 	public JavaCompiler() {}
41 41
 	
42 42
 	public JavaBytecodeModule compile(String packageName, SemanticModule module, JavaCompileSpace space) {
43
-		Map<String, JavaScriptFile> scriptBlocks = new HashMap<>();
43
+		Map<String, JavaScriptFile> scriptBlocks = new LinkedHashMap<>();
44 44
 		
45 45
 		JavaBytecodeModule target = new JavaBytecodeModule(module.module, module.parameters);
46 46
 		JavaBytecodeContext context = new JavaBytecodeContext(target, space, module.modulePackage, packageName);
@@ -60,6 +60,7 @@ public class JavaCompiler {
60 60
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
61 61
 			String className = getClassName(definition.position.getFilename());
62 62
 			JavaScriptFile scriptFile = getScriptFile(scriptBlocks, definition.pkg.fullName + "/" + className);
63
+			scriptFile.classWriter.visitSource(definition.position.getFilename(), null);
63 64
 			
64 65
 			JavaClass cls = definition instanceof ExpansionDefinition ? context.getJavaExpansionClass(definition) : context.getJavaClass(definition);
65 66
 			target.addClass(cls.internalName, definition.accept(new JavaDefinitionVisitor(context, scriptFile.classWriter)));
@@ -79,6 +80,7 @@ public class JavaCompiler {
79 80
 			final SourceFile sourceFile = script.file;
80 81
 			final String className = getClassName(sourceFile == null ? null : sourceFile.getFilename());
81 82
 			JavaScriptFile scriptFile = getScriptFile(scriptBlocks, script.pkg.fullName + "/" + className);
83
+			scriptFile.classWriter.visitSource(script.file.getFilename(), null);
82 84
 
83 85
 			String methodName = scriptFile.scriptMethods.isEmpty() ? "run" : "run" + scriptFile.scriptMethods.size();
84 86
 

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

@@ -137,12 +137,12 @@ class ArrayInitializerHelper {
137 137
 	}
138 138
 
139 139
 	/**
140
-	 * Checks if an expression can be inlined
140
+	 * Checks if an expression can be inLined
141 141
 	 *
142 142
 	 * @param expression Expression to check for.
143
-	 * @return can expression be inlined
143
+	 * @return can expression be inLined
144 144
 	 */
145
-	static boolean canBeInlined(Expression expression) {
145
+	static boolean canBeInLined(Expression expression) {
146 146
 		while (expression instanceof StorageCastExpression)
147 147
 			expression = ((StorageCastExpression) expression).value;
148 148
 
@@ -162,7 +162,7 @@ class ArrayInitializerHelper {
162 162
 	 * @param currentArrayType  The current type of the array, reduced during the recursions of the functions
163 163
 	 * @param innermostFunction The function that will decide what to add to the array, needs to increase the stack size by one and may not touch the other stacks!
164 164
 	 */
165
-	private static void visitMultiDimArray(JavaWriter javaWriter, int[] sizeLocations, int[] counterLocations, int dim, Type currentArrayType, InnermostFunction innermostFunction) {
165
+	static void visitMultiDimArray(JavaWriter javaWriter, int[] sizeLocations, int[] counterLocations, int dim, Type currentArrayType, InnermostFunction innermostFunction) {
166 166
 		final Label begin = new Label();
167 167
 		final Label end = new Label();
168 168
 		javaWriter.label(begin);

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

@@ -8,7 +8,6 @@ import org.openzen.zenscript.codemodel.CompareType;
8 8
 import org.openzen.zenscript.codemodel.FunctionParameter;
9 9
 import org.openzen.zenscript.codemodel.expression.*;
10 10
 import org.openzen.zenscript.codemodel.expression.switchvalue.VariantOptionSwitchValue;
11
-import org.openzen.zenscript.codemodel.generic.TypeParameter;
12 11
 import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
13 12
 import org.openzen.zenscript.codemodel.member.ref.FieldMemberRef;
14 13
 import org.openzen.zenscript.codemodel.statement.ReturnStatement;
@@ -1897,7 +1896,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
1897 1896
 		final JavaWriter functionWriter;
1898 1897
 		
1899 1898
 		//Bridge method!!!
1900
-		if(!Objects.equals(methodInfo.descriptor, signature)) {
1899
+		if (!Objects.equals(methodInfo.descriptor, signature)) {
1901 1900
 			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);
1902 1901
 			final JavaWriter bridgeWriter = new JavaWriter(expression.position, lambdaCW, bridgeMethodInfo, null, methodInfo.descriptor, null, "java/lang/Override");
1903 1902
 			bridgeWriter.start();
@@ -1909,10 +1908,12 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
1909 1908
 				final FunctionParameter functionParameter = expression.header.parameters[i];
1910 1909
 				final Type type = context.getType(functionParameter.type);
1911 1910
 				bridgeWriter.load(type, i + 1);
1912
-				bridgeWriter.checkCast(type);
1911
+				if (!CompilerUtils.isPrimitive(functionParameter.type.type)) {
1912
+					bridgeWriter.checkCast(type);
1913
+				}
1913 1914
 			}
1914 1915
 			
1915
-			bridgeWriter.invokeVirtual(methodInfo);
1916
+			bridgeWriter.invokeVirtual(new JavaMethod(JavaClass.fromInternalName(className, JavaClass.Kind.CLASS), methodInfo.kind, methodInfo.name, methodInfo.compile, signature, methodInfo.modifiers, methodInfo.genericResult));
1916 1917
 			if(expression.header.getReturnType().type != BasicTypeID.VOID) {
1917 1918
 				bridgeWriter.returnType(context.getType(expression.header.getReturnType()));
1918 1919
 			}
@@ -3324,7 +3325,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
3324 3325
 
3325 3326
 
3326 3327
 				if (builtin == BuiltinID.ARRAY_CONSTRUCTOR_SIZED) {
3327
-					type.elementType.type.accept(JavaDefaultExpressionTypeVisitor.INSTANCE).accept(this);
3328
+					type.elementType.type.getDefaultValue().accept(this);
3328 3329
 				} else {
3329 3330
 					expression.arguments.arguments[expression.arguments.arguments.length - 1].accept(this);
3330 3331
 				}
@@ -3338,117 +3339,117 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
3338 3339
 				return;
3339 3340
 			}
3340 3341
 			case ARRAY_CONSTRUCTOR_LAMBDA: {
3341
-				ArrayTypeID type = (ArrayTypeID) expression.type.type;
3342
-
3343
-				if (type.dimension == 1) {
3344
-					// array = new T[arguments[0]]
3345
-					// lambda = arguments[1]
3346
-					// for (int i = 0; i < array.length; i++)
3347
-					//    array[i] = lambda.invoke(i);
3348
-					//
3349
-					// NOTE: arguments[1] can be a FunctionExpression; this can be optimized by running it inline
3350
-					throw new UnsupportedOperationException("Not yet supported!");
3351
-				} else {
3352
-					// TODO: implement
3353
-					throw new UnsupportedOperationException("Not yet supported!");
3354
-				}
3342
+				
3343
+				//Labels
3344
+				final Label begin = new Label();
3345
+				final Label end = new Label();
3346
+				javaWriter.label(begin);
3347
+				
3348
+				final Type ASMElementType = context.getType(expression.type);
3349
+				final int dimension = ((ArrayTypeID) expression.type.type).dimension;
3350
+				final int[] arraySizes = ArrayInitializerHelper.getArraySizeLocationsFromConstructor(dimension, expression.arguments.arguments, this);
3351
+				ArrayInitializerHelper.visitMultiDimArray(javaWriter, arraySizes, new int[dimension], dimension, ASMElementType, (elementType, counterLocations) -> {
3352
+					expression.arguments.arguments[dimension].accept(this);
3353
+					for (int counterLocation : counterLocations) {
3354
+						javaWriter.loadInt(counterLocation);
3355
+					}
3356
+					javaWriter.invokeInterface(context.getFunctionalInterface(expression.arguments.arguments[dimension].type));
3357
+				});
3358
+				javaWriter.label(end);
3359
+				return;
3355 3360
 			}
3356
-			case ARRAY_CONSTRUCTOR_PROJECTED: {
3357
-
3358
-
3361
+			case ARRAY_CONSTRUCTOR_PROJECTED:
3362
+			case ARRAY_CONSTRUCTOR_PROJECTED_INDEXED: {
3359 3363
 				ArrayTypeID type = (ArrayTypeID) expression.type.type;
3360
-
3364
+				
3365
+				//Labels
3361 3366
 				final Label begin = new Label();
3362 3367
 				final Label end = new Label();
3363
-
3364 3368
 				javaWriter.label(begin);
3365
-				expression.arguments.arguments[0].accept(this); //Origin array
3369
+				
3370
+				//Origin Array
3371
+				expression.arguments.arguments[0].accept(this);
3366 3372
 				final Type originArrayType = context.getType(expression.arguments.arguments[0].type);
3367 3373
 				final int originArrayLocation = javaWriter.local(originArrayType);
3368 3374
 				javaWriter.storeObject(originArrayLocation);
3369 3375
 				Type destinationArrayType = context.getType(expression.type);
3370
-
3371
-
3372
-				final boolean canBeInlined = ArrayInitializerHelper.canBeInlined(expression.arguments.arguments[1]);
3373
-				final Type functionType;    //Only used if not inline able
3374
-				final int functionLocation; //Only used if not inline able
3375
-				if (!canBeInlined) {
3376
-					expression.arguments.arguments[1].accept(this); //Projection Function
3377
-					functionType = context.getType(expression.arguments.arguments[1].type);
3378
-					functionLocation = javaWriter.local(functionType);
3379
-					javaWriter.storeObject(functionLocation);
3380
-					javaWriter.addVariableInfo(new JavaLocalVariableInfo(functionType, functionLocation, begin, "projectionFunction", end));
3381
-				}
3382
-
3383
-				final int[] arraySizes = ArrayInitializerHelper.getArraySizeLocationsProjected(type.dimension, originArrayType, originArrayLocation, javaWriter);
3384
-				ArrayInitializerHelper.visitProjected(javaWriter, arraySizes, type.dimension, originArrayLocation, originArrayType, destinationArrayType,
3385
-						(elementType, counterLocations) -> {
3386
-							if (canBeInlined) {
3376
+				
3377
+				final boolean indexed = builtin == BuiltinID.ARRAY_CONSTRUCTOR_PROJECTED_INDEXED;
3378
+				final boolean canBeInLined = ArrayInitializerHelper.canBeInLined(expression.arguments.arguments[1]);
3379
+				if (canBeInLined) {
3380
+					//We can inline, so do it
3381
+					final int[] arraySizes = ArrayInitializerHelper.getArraySizeLocationsProjected(type.dimension, originArrayType, originArrayLocation, javaWriter);
3382
+					final Type projectedElementType = Type.getType(originArrayType.getDescriptor().substring(type.dimension));
3383
+					ArrayInitializerHelper.visitProjected(javaWriter, arraySizes, type.dimension, originArrayLocation, originArrayType, destinationArrayType,
3384
+							(elementType, counterLocations) -> {
3387 3385
 								Label inlineBegin = new Label();
3388 3386
 								Label inlineEnd = new Label();
3389 3387
 								javaWriter.label(inlineBegin);
3390
-								final Type projectedElementType = Type.getType(originArrayType.getDescriptor().substring(type.dimension));
3388
+								
3391 3389
 								final int projectedElementLocal = javaWriter.local(projectedElementType);
3392 3390
 								javaWriter.store(projectedElementType, projectedElementLocal);
3393
-
3394
-
3391
+								
3392
+								
3395 3393
 								JavaExpressionVisitor visitor = new JavaExpressionVisitor(context, module, javaWriter) {
3396 3394
 									@Override
3397 3395
 									public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
3396
+										if(indexed) {
3397
+											final JavaParameterInfo parameterInfo = module.getParameterInfo(expression.parameter);
3398
+											if (parameterInfo != null && parameterInfo.index <= type.dimension) {
3399
+												javaWriter.loadInt(counterLocations[parameterInfo.index - 1]);
3400
+												return null;
3401
+											}
3402
+										}
3403
+										
3398 3404
 										javaWriter.load(projectedElementType, projectedElementLocal);
3399 3405
 										return null;
3400 3406
 									}
3401 3407
 								};
3402
-
3408
+								
3403 3409
 								Expression funcExpression = expression.arguments.arguments[1];
3404 3410
 								while (funcExpression instanceof StorageCastExpression) {
3405 3411
 									funcExpression = ((StorageCastExpression) funcExpression).value;
3406 3412
 								}
3407
-
3413
+								
3408 3414
 								if (funcExpression instanceof FunctionExpression && ((FunctionExpression) funcExpression).body instanceof ReturnStatement) {
3415
+									CompilerUtils.tagMethodParameters(context, module, ((FunctionExpression) funcExpression).header, false);
3409 3416
 									((ReturnStatement) ((FunctionExpression) funcExpression).body).value.accept(visitor);
3410 3417
 									javaWriter.addVariableInfo(new JavaLocalVariableInfo(projectedElementType, projectedElementLocal, inlineBegin, ((FunctionExpression) funcExpression).header.parameters[0].name, inlineEnd));
3418
+									
3411 3419
 								} else throw new IllegalStateException("Trying to inline a non-inlineable expression");
3412
-
3413
-
3420
+								
3421
+								
3414 3422
 								javaWriter.label(inlineEnd);
3415
-							} else {
3423
+							});
3424
+				} else {
3425
+					//We cannot inline, so get a hold of the function expression and apply it to every
3426
+					expression.arguments.arguments[1].accept(this); //Projection Function
3427
+					final Type functionType = context.getType(expression.arguments.arguments[1].type);
3428
+					final int functionLocation = javaWriter.local(functionType);
3429
+					javaWriter.storeObject(functionLocation);
3430
+					javaWriter.addVariableInfo(new JavaLocalVariableInfo(functionType, functionLocation, begin, "projectionFunction", end));
3431
+					final int[] arraySizes = ArrayInitializerHelper.getArraySizeLocationsProjected(type.dimension, originArrayType, originArrayLocation, javaWriter);
3432
+					ArrayInitializerHelper.visitProjected(javaWriter, arraySizes, type.dimension, originArrayLocation, originArrayType, destinationArrayType,
3433
+							(elementType, counterLocations) -> {
3416 3434
 								//Apply function here
3417
-								//javaWriter.loadObject(functionLocation);
3418
-								//javaWriter.swap();
3419
-
3420
-								//TODO invoke?
3421
-								//javaWriter.invokeVirtual(new JavaMethod(JavaClass.fromInternalName("lambda1", JavaClass.Kind.CLASS), JavaMethod.Kind.INSTANCE, "accept", true, "(Ljava/lang/String;)Ljava/lang/String;", 0, false));
3422
-
3423
-								//FIXME Critical! Currently returning the same object!
3424
-								//throw new UnsupportedOperationException("Cannot use projection functions yet!");
3425
-							}
3426
-						});
3427
-
3428
-
3435
+								javaWriter.loadObject(functionLocation);
3436
+								javaWriter.swap();
3437
+								
3438
+								if(indexed) {
3439
+									for (int counterLocation : counterLocations) {
3440
+										javaWriter.loadInt(counterLocation);
3441
+										javaWriter.swap();
3442
+									}
3443
+								}
3444
+								
3445
+								javaWriter.invokeInterface(context.getFunctionalInterface(expression.arguments.arguments[1].type));
3446
+							});
3447
+				}
3448
+				
3449
+				
3429 3450
 				javaWriter.label(end);
3430 3451
 				return;
3431
-
3432
-			}
3433
-			case ARRAY_CONSTRUCTOR_PROJECTED_INDEXED: {
3434
-				ArrayTypeID type = (ArrayTypeID) expression.type.type;
3435
-
3436
-				if (type.dimension == 1) {
3437
-					// original = arguments[0] (this is an array)
3438
-					// projector = arguments[1] (this is a lambda with 2 parameters)
3439
-					// array = new T[original.length]
3440
-					// for (int i = 0; i < array.length; i++)
3441
-					//   array[i] = projector(i, original[i]);
3442
-					//
3443
-					// NOTE: arguments[1] can be a FunctionExpression; this can be optimized by running it inline
3444
-
3445
-					// TODO: implement in bytecode
3446
-
3447
-					return;
3448
-				} else {
3449
-					// TODO: implement
3450
-					throw new UnsupportedOperationException("Not yet supported!");
3451
-				}
3452
+				
3452 3453
 			}
3453 3454
 			case ARRAY_INDEXGET:
3454 3455
 				break;

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

@@ -36,6 +36,7 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
36 36
 
37 37
 	@Override
38 38
 	public Boolean visitBlock(BlockStatement statement) {
39
+    	javaWriter.position(statement.position.fromLine);
39 40
 		Boolean returns = false;
40 41
 		for (Statement statement1 : statement.statements) {
41 42
 			returns = statement1.accept(this);
@@ -45,18 +46,21 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
45 46
 
46 47
 	@Override
47 48
 	public Boolean visitBreak(BreakStatement statement) {
49
+    	javaWriter.position(statement.position.fromLine);
48 50
 		javaWriter.goTo(javaWriter.getNamedLabel(statement.target.label + "_end"));
49 51
 		return false;
50 52
 	}
51 53
 
52 54
 	@Override
53 55
 	public Boolean visitContinue(ContinueStatement statement) {
56
+    	javaWriter.position(statement.position.fromLine);
54 57
 		javaWriter.goTo(javaWriter.getNamedLabel(statement.target.label + "_start"));
55 58
 		return false;
56 59
 	}
57 60
 
58 61
 	@Override
59 62
 	public Boolean visitDoWhile(DoWhileStatement statement) {
63
+    	javaWriter.position(statement.position.fromLine);
60 64
 		Label start = new Label();
61 65
 		Label end = new Label();
62 66
 		if (statement.label == null)
@@ -82,12 +86,14 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
82 86
 
83 87
 	@Override
84 88
 	public Boolean visitExpression(ExpressionStatement statement) {
89
+    	javaWriter.position(statement.position.fromLine);
85 90
 		statement.expression.accept(nonPushingExpressionVisitor);
86 91
 		return false;
87 92
 	}
88 93
 
89 94
 	@Override
90 95
 	public Boolean visitForeach(ForeachStatement statement) {
96
+    	javaWriter.position(statement.position.fromLine);
91 97
 		//Create Labels
92 98
 		Label start = new Label();
93 99
 		Label end = new Label();
@@ -147,6 +153,7 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
147 153
 
148 154
 	@Override
149 155
 	public Boolean visitIf(IfStatement statement) {
156
+    	javaWriter.position(statement.position.fromLine);
150 157
 		statement.condition.accept(expressionVisitor);
151 158
 		Label onElse = null;
152 159
 		Label end = new Label();
@@ -171,9 +178,15 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
171 178
 	public Boolean visitLock(LockStatement statement) {
172 179
 		return false;
173 180
 	}
174
-
181
+	
182
+	@Override
183
+	public Boolean visitInvalid(InvalidStatement statement) {
184
+		throw new UnsupportedOperationException("Invalid Statement: " + statement.message);
185
+	}
186
+	
175 187
 	@Override
176 188
 	public Boolean visitReturn(ReturnStatement statement) {
189
+    	javaWriter.position(statement.position.fromLine);
177 190
 		statement.value.accept(expressionVisitor);
178 191
 		javaWriter.returnType(context.getType(statement.value.type));
179 192
 		return true;
@@ -181,6 +194,7 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
181 194
 
182 195
 	@Override
183 196
 	public Boolean visitSwitch(SwitchStatement statement) {
197
+    	javaWriter.position(statement.position.fromLine);
184 198
 
185 199
 		final Label start = new Label();
186 200
 		final Label end = new Label();
@@ -246,6 +260,7 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
246 260
 
247 261
 	@Override
248 262
 	public Boolean visitThrow(ThrowStatement statement) {
263
+    	javaWriter.position(statement.position.fromLine);
249 264
 		statement.value.accept(expressionVisitor);
250 265
 		javaWriter.aThrow();
251 266
 		return false;
@@ -253,6 +268,7 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
253 268
 
254 269
 	@Override
255 270
 	public Boolean visitTryCatch(TryCatchStatement statement) {
271
+    	javaWriter.position(statement.position.fromLine);
256 272
 		final Label tryCatchStart = new Label();
257 273
 		final Label tryFinish = new Label();
258 274
 		final Label tryCatchFinish = new Label();
@@ -304,6 +320,7 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
304 320
 
305 321
 	@Override
306 322
 	public Boolean visitVar(VarStatement statement) {
323
+    	javaWriter.position(statement.position.fromLine);
307 324
 		if (statement.initializer != null) {
308 325
 			statement.initializer.accept(expressionVisitor);
309 326
 		}
@@ -322,6 +339,7 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
322 339
 
323 340
 	@Override
324 341
 	public Boolean visitWhile(WhileStatement statement) {
342
+    	javaWriter.position(statement.position.fromLine);
325 343
 		Label start = new Label();
326 344
 		Label end = new Label();
327 345
 

+ 1108
- 1103
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaWriter.java
File diff suppressed because it is too large
View File


+ 2
- 7
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeLoader.java View File

@@ -5,10 +5,7 @@
5 5
  */
6 6
 package org.openzen.zencode.java;
7 7
 
8
-import java.util.ArrayList;
9
-import java.util.HashMap;
10
-import java.util.List;
11
-import java.util.Map;
8
+import java.util.*;
12 9
 import java.util.function.Consumer;
13 10
 import org.openzen.zencode.shared.CompileException;
14 11
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
@@ -20,7 +17,6 @@ import org.openzen.zenscript.codemodel.definition.ZSPackage;
20 17
 public class JavaNativeLoader {
21 18
 	private final Class<?>[] classes;
22 19
 	private final Class<?>[] globals;
23
-	private final Map<String, LoadingModule> modulesByBasePackage = new HashMap<>();
24 20
 	private final Map<String, LoadingModule> modulesByName = new HashMap<>();
25 21
 	private boolean loaded = false;
26 22
 	
@@ -40,7 +36,6 @@ public class JavaNativeLoader {
40 36
 			throw new IllegalArgumentException("Module already exists: " + name);
41 37
 		
42 38
 		LoadingModule module = new LoadingModule(pkg, name, basePackage, dependencies);
43
-		modulesByBasePackage.put(name, module);
44 39
 		modulesByName.put(name, module);
45 40
 		
46 41
 		return module;
@@ -52,7 +47,7 @@ public class JavaNativeLoader {
52 47
 		sortClasses();
53 48
 		
54 49
 		ScriptingEngine engine = new ScriptingEngine();
55
-		for (LoadingModule module : modulesByBasePackage.values()) {
50
+		for (LoadingModule module : modulesByName.values()) {
56 51
 			load(engine, module);
57 52
 		}
58 53
 		return engine;

+ 12
- 8
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java View File

@@ -8,6 +8,7 @@ package org.openzen.zencode.java;
8 8
 import java.lang.reflect.AnnotatedType;
9 9
 import java.lang.reflect.Field;
10 10
 import java.lang.reflect.Method;
11
+import java.lang.reflect.Member;
11 12
 import java.lang.reflect.Modifier;
12 13
 import java.lang.reflect.Parameter;
13 14
 import java.lang.reflect.ParameterizedType;
@@ -232,6 +233,9 @@ public class JavaNativeModule {
232 233
 	}
233 234
 	
234 235
 	private ZSPackage getPackage(String className) {
236
+		//TODO validate
237
+		if(this.basePackage == null || this.basePackage.isEmpty())
238
+			return pkg;
235 239
 		//TODO make a lang package?
236 240
 		if (!className.contains(".") || className.startsWith("java.lang"))
237 241
 			return pkg;
@@ -271,7 +275,7 @@ public class JavaNativeModule {
271 275
 			} else if (specifiedName.indexOf('.') >= 0) {
272 276
 				if (!specifiedName.startsWith(pkg.fullName))
273 277
 					throw new IllegalArgumentException("Specified @Name as " + specifiedName + " but it's not in the module root package");
274
-				
278
+
275 279
 				classPkg = getPackage(basePackage + specifiedName.substring(pkg.fullName.length()));
276 280
 				className = className.substring(className.lastIndexOf('.') + 1);
277 281
 			} else {
@@ -279,7 +283,7 @@ public class JavaNativeModule {
279 283
                 className = nameAnnotation.value();
280 284
 			}
281 285
 		}
282
-		
286
+
283 287
 		TypeVariableContext context = new TypeVariableContext();
284 288
 		TypeVariable<Class<T>>[] javaTypeParameters = cls.getTypeParameters();
285 289
 		TypeParameter[] typeParameters = new TypeParameter[cls.getTypeParameters().length];
@@ -301,10 +305,10 @@ public class JavaNativeModule {
301 305
 			definition = new InterfaceDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
302 306
 			javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.INTERFACE);
303 307
 		} else if (cls.isEnum()) {
304
-			definition = new EnumDefinition(CodePosition.NATIVE, module, pkg, className, Modifiers.PUBLIC, null);
308
+			definition = new EnumDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
305 309
 			javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.ENUM);
306 310
 		} else if (isStruct) {
307
-			definition = new StructDefinition(CodePosition.NATIVE, module, pkg, className, Modifiers.PUBLIC, null);
311
+			definition = new StructDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC, null);
308 312
 			javaClass = JavaClass.fromInternalName(internalName, JavaClass.Kind.CLASS);
309 313
 		} else {
310 314
 			definition = new ClassDefinition(CodePosition.NATIVE, module, classPkg, className, Modifiers.PUBLIC);
@@ -339,7 +343,7 @@ public class JavaNativeModule {
339 343
 			final String fieldName = annotation.value().isEmpty() ? field.getName() : annotation.value();
340 344
 			
341 345
 			StoredType fieldType = loadStoredType(context, field.getAnnotatedType());
342
-			FieldMember member = new FieldMember(CodePosition.NATIVE, definition, Modifiers.PUBLIC, fieldName, thisType, fieldType, registry, 0, 0, null);
346
+			FieldMember member = new FieldMember(CodePosition.NATIVE, definition, getMethodModifiers(field), fieldName, thisType, fieldType, registry, 0, 0, null);
343 347
 			definition.addMember(member);
344 348
 			compiled.setFieldInfo(member, new JavaField(javaClass, field.getName(), getDescriptor(field.getType())));
345 349
 		}
@@ -587,7 +591,7 @@ public class JavaNativeModule {
587 591
 			return null;
588 592
 		}
589 593
 	}
590
-	
594
+
591 595
 	private FunctionHeader getHeader(
592 596
 			TypeVariableContext context,
593 597
 			AnnotatedType javaReturnType,
@@ -599,7 +603,7 @@ public class JavaNativeModule {
599 603
 		FunctionParameter[] parameters = new FunctionParameter[javaParameters.length];
600 604
 		for (int i = 0; i < parameters.length; i++) {
601 605
 			Parameter parameter = javaParameters[i];
602
-			
606
+
603 607
 			AnnotatedType parameterType = parameter.getAnnotatedType();
604 608
 			StoredType type = loadStoredType(context, parameter.getAnnotatedType());
605 609
 			Expression defaultValue = getDefaultValue(parameter, type);
@@ -736,7 +740,7 @@ public class JavaNativeModule {
736 740
 		return null;
737 741
 	}
738 742
 	
739
-	private int getMethodModifiers(Method method) {
743
+	private int getMethodModifiers(Member method) {
740 744
 		int result = Modifiers.PUBLIC;
741 745
 		if (isStatic(method.getModifiers()))
742 746
 			result |= Modifiers.STATIC;

+ 25
- 3
JavaIntegration/src/main/java/org/openzen/zencode/java/ScriptingEngine.java View File

@@ -11,6 +11,8 @@ import java.util.ArrayList;
11 11
 import java.util.Collections;
12 12
 import java.util.List;
13 13
 import java.util.Map;
14
+import java.util.function.Consumer;
15
+
14 16
 import org.openzen.zencode.shared.CompileException;
15 17
 import org.openzen.zencode.shared.SourceFile;
16 18
 import org.openzen.zenscript.codemodel.FunctionParameter;
@@ -29,6 +31,7 @@ import org.openzen.zenscript.lexer.ParseException;
29 31
 import org.openzen.zenscript.parser.BracketExpressionParser;
30 32
 import org.openzen.zenscript.parser.ParsedFile;
31 33
 import org.openzen.zenscript.parser.ZippedPackage;
34
+import org.openzen.zenscript.validator.ValidationLogEntry;
32 35
 import org.openzen.zenscript.validator.Validator;
33 36
 
34 37
 /**
@@ -92,13 +95,28 @@ public class ScriptingEngine {
92 95
 			BracketExpressionParser bracketParser,
93 96
 			FunctionParameter[] scriptParameters,
94 97
 			String... dependencies) throws ParseException
98
+	{
99
+		return createScriptedModule(name, sources, bracketParser, scriptParameters, Throwable::printStackTrace, System.out::println, sourceFile -> System.out.println("Loading " + sourceFile.getFilename()), dependencies);
100
+	}
101
+	
102
+	public SemanticModule createScriptedModule(
103
+			String name,
104
+			SourceFile[] sources,
105
+			BracketExpressionParser bracketParser,
106
+			FunctionParameter[] scriptParameters,
107
+			Consumer<CompileException> compileExceptionConsumer,
108
+			Consumer<ValidationLogEntry> validatorErrorConsumer,
109
+			Consumer<SourceFile> sourceFileConsumer,
110
+			String... dependencies) throws ParseException
95 111
 	{
96 112
 		Module scriptModule = new Module(name);
97 113
 		CompilingPackage scriptPackage = new CompilingPackage(new ZSPackage(space.rootPackage, name), scriptModule);
98 114
 		
99 115
 		ParsedFile[] files = new ParsedFile[sources.length];
100
-		for (int i = 0; i < sources.length; i++)
116
+		for (int i = 0; i < sources.length; i++) {
117
+			sourceFileConsumer.accept(sources[i]);
101 118
 			files[i] = ParsedFile.parse(scriptPackage, bracketParser, sources[i]);
119
+		}
102 120
 		
103 121
 		SemanticModule[] dependencyModules = new SemanticModule[dependencies.length + 1];
104 122
 		dependencyModules[0] = space.getModule("stdlib");
@@ -112,13 +130,13 @@ public class ScriptingEngine {
112 130
 				files,
113 131
 				space,
114 132
 				scriptParameters,
115
-				ex -> ex.printStackTrace());
133
+				compileExceptionConsumer);
116 134
 		if (!scripts.isValid())
117 135
 			return scripts;
118 136
 		
119 137
 		return Validator.validate(
120 138
 				scripts.normalize(),
121
-				error -> System.out.println(error.toString()));
139
+				validatorErrorConsumer);
122 140
 	}
123 141
 	
124 142
 	public void registerCompiled(SemanticModule module) {
@@ -147,4 +165,8 @@ public class ScriptingEngine {
147 165
 			runUnit.dump(new File("classes"));
148 166
 		runUnit.run(arguments, parentClassLoader);
149 167
 	}
168
+	
169
+	public List<JavaNativeModule> getNativeModules() {
170
+		return Collections.unmodifiableList(this.nativeModules);
171
+	}
150 172
 }

+ 0
- 113
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaDefaultExpressionTypeVisitor.java View File

@@ -1,113 +0,0 @@
1
-package org.openzen.zenscript.javashared;
2
-
3
-import org.openzen.zencode.shared.CodePosition;
4
-import org.openzen.zenscript.codemodel.expression.*;
5
-import org.openzen.zenscript.codemodel.type.*;
6
-
7
-public class JavaDefaultExpressionTypeVisitor implements TypeVisitor<Expression> {
8
-
9
-	public static final JavaDefaultExpressionTypeVisitor INSTANCE = new JavaDefaultExpressionTypeVisitor();
10
-
11
-	private JavaDefaultExpressionTypeVisitor(){}
12
-
13
-	@Override
14
-	public Expression visitBasic(BasicTypeID basic) {
15
-
16
-
17
-		if (basic == null)
18
-			throw new IllegalStateException("Null basic type!");
19
-
20
-		switch (basic) {
21
-			case UNDETERMINED:
22
-			case VOID:
23
-				throw new IllegalStateException("Void and Undetermined have no default type");
24
-			case NULL:
25
-				return new NullExpression(CodePosition.UNKNOWN);
26
-			case BOOL:
27
-				return new ConstantBoolExpression(CodePosition.UNKNOWN, false);
28
-			case BYTE:
29
-				return new ConstantByteExpression(CodePosition.UNKNOWN, 0);
30
-			case SBYTE:
31
-				return new ConstantSByteExpression(CodePosition.UNKNOWN, (byte) 0);
32
-			case SHORT:
33
-				return new ConstantShortExpression(CodePosition.UNKNOWN, (short) 0);
34
-			case USHORT:
35
-				return new ConstantUShortExpression(CodePosition.UNKNOWN, 0);
36
-			case INT:
37
-				return new ConstantIntExpression(CodePosition.UNKNOWN, 0);
38
-			case UINT:
39
-				return new ConstantUIntExpression(CodePosition.UNKNOWN, 0);
40
-			case LONG:
41
-				return new ConstantLongExpression(CodePosition.UNKNOWN, 0L);
42
-			case ULONG:
43
-				return new ConstantULongExpression(CodePosition.UNKNOWN, 0L);
44
-			case USIZE:
45
-				return new ConstantUSizeExpression(CodePosition.UNKNOWN, 0L);
46
-			case FLOAT:
47
-				return new ConstantFloatExpression(CodePosition.UNKNOWN, 0.0f);
48
-			case DOUBLE:
49
-				return new ConstantDoubleExpression(CodePosition.UNKNOWN, 0.0d);
50
-			case CHAR:
51
-				return new ConstantCharExpression(CodePosition.UNKNOWN, '\0');
52
-			default:
53
-				throw new IllegalStateException("Unknown basic type!");
54
-		}
55
-
56
-
57
-	}
58
-
59
-	@Override
60
-	public Expression visitString(StringTypeID string) {
61
-		return new NullExpression(CodePosition.UNKNOWN);
62
-	}
63
-
64
-	@Override
65
-	public Expression visitArray(ArrayTypeID array) {
66
-		return new NullExpression(CodePosition.UNKNOWN);
67
-	}
68
-
69
-	@Override
70
-	public Expression visitAssoc(AssocTypeID assoc) {
71
-		return new NullExpression(CodePosition.UNKNOWN);
72
-	}
73
-
74
-	@Override
75
-	public Expression visitGenericMap(GenericMapTypeID map) {
76
-		return new NullExpression(CodePosition.UNKNOWN);
77
-	}
78
-
79
-	@Override
80
-	public Expression visitIterator(IteratorTypeID iterator) {
81
-		return new NullExpression(CodePosition.UNKNOWN);
82
-	}
83
-
84
-	@Override
85
-	public Expression visitFunction(FunctionTypeID function) {
86
-		return new NullExpression(CodePosition.UNKNOWN);
87
-	}
88
-
89
-	@Override
90
-	public Expression visitDefinition(DefinitionTypeID definition) {
91
-		return new NullExpression(CodePosition.UNKNOWN);
92
-	}
93
-
94
-	@Override
95
-	public Expression visitGeneric(GenericTypeID generic) {
96
-		return new NullExpression(CodePosition.UNKNOWN);
97
-	}
98
-
99
-	@Override
100
-	public Expression visitRange(RangeTypeID range) {
101
-		return new NullExpression(CodePosition.UNKNOWN);
102
-	}
103
-
104
-	@Override
105
-	public Expression visitOptional(OptionalTypeID type) {
106
-		return new NullExpression(CodePosition.UNKNOWN);
107
-	}
108
-
109
-	@Override
110
-	public Expression visitInvalid(InvalidTypeID type) {
111
-		return new NullExpression(CodePosition.UNKNOWN);
112
-	}
113
-}

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

@@ -44,7 +44,7 @@ public class ParsedExpressionMember extends ParsedExpression {
44 44
 				scope.hints,
45 45
 				new GenericName(this.member, typeArguments));
46 46
 		if (member == null) {
47
-			TypeMembers members = scope.getTypeMembers(cValue.eval().type);
47
+			//TypeMembers members = scope.getTypeMembers(cValue.eval().type);
48 48
 			throw new CompileException(position, CompileExceptionCode.NO_SUCH_MEMBER, "Member not found: " + this.member);
49 49
 		}
50 50
 		

+ 24
- 3
ScriptingExample/scripts/arrays.zs View File

@@ -48,7 +48,28 @@ val e = new string[,,]<string>(
48 48
 );
49 49
 
50 50
 
51
-//val a = new string[](5, "HelloWorld");
52
-//val b = new string[]<string>(a, projection);
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);
53 73
 
54
-println(e[2,3,4]);
74
+println(indexedProjectionWithLambdaNonInlined[1, 2]);
75
+println(indexedProjectionWithLambdaInlined[1, 2]);

+ 7
- 1
ScriptingExample/scripts/functionalInterfaces.zs View File

@@ -8,4 +8,10 @@ val y = (a as int, b as int) => a + b;
8 8
 invokeFunctionalInt(y);
9 9
 
10 10
 
11
-println(((x as int) => x)(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
+//});

+ 2
- 2
Validator/src/main/java/org/openzen/zenscript/validator/visitors/ExpressionValidator.java View File

@@ -739,8 +739,8 @@ public class ExpressionValidator implements ExpressionVisitor<Void> {
739 739
 	}
740 740
 
741 741
 	private void checkFieldAccess(CodePosition position, FieldMemberRef field) {
742
-		if (!scope.getAccessScope().hasAccessTo(field.getTarget().getAccessScope(), Modifiers.PRIVATE))
743
-			validator.logError(ValidationLogEntry.Code.NO_ACCESS, position, "fields are private");
742
+		if (!scope.getAccessScope().hasAccessTo(field.getTarget().getAccessScope(), field.getTarget().getEffectiveModifiers()))
743
+			validator.logError(ValidationLogEntry.Code.NO_ACCESS, position, "no field access to " + field.describe());
744 744
 	}
745 745
 	
746 746
 	private void checkStatic(CodePosition position, DefinitionMemberRef member) {

Loading…
Cancel
Save