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
  */
5
  */
6
 package org.openzen.zenscript.javabytecode;
6
 package org.openzen.zenscript.javabytecode;
7
 
7
 
8
-import java.util.HashMap;
8
+import java.util.LinkedHashMap;
9
 import java.util.Map;
9
 import java.util.Map;
10
 import org.objectweb.asm.ClassWriter;
10
 import org.objectweb.asm.ClassWriter;
11
 import org.objectweb.asm.Opcodes;
11
 import org.objectweb.asm.Opcodes;
40
 	public JavaCompiler() {}
40
 	public JavaCompiler() {}
41
 	
41
 	
42
 	public JavaBytecodeModule compile(String packageName, SemanticModule module, JavaCompileSpace space) {
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
 		JavaBytecodeModule target = new JavaBytecodeModule(module.module, module.parameters);
45
 		JavaBytecodeModule target = new JavaBytecodeModule(module.module, module.parameters);
46
 		JavaBytecodeContext context = new JavaBytecodeContext(target, space, module.modulePackage, packageName);
46
 		JavaBytecodeContext context = new JavaBytecodeContext(target, space, module.modulePackage, packageName);
60
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
60
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
61
 			String className = getClassName(definition.position.getFilename());
61
 			String className = getClassName(definition.position.getFilename());
62
 			JavaScriptFile scriptFile = getScriptFile(scriptBlocks, definition.pkg.fullName + "/" + className);
62
 			JavaScriptFile scriptFile = getScriptFile(scriptBlocks, definition.pkg.fullName + "/" + className);
63
+			scriptFile.classWriter.visitSource(definition.position.getFilename(), null);
63
 			
64
 			
64
 			JavaClass cls = definition instanceof ExpansionDefinition ? context.getJavaExpansionClass(definition) : context.getJavaClass(definition);
65
 			JavaClass cls = definition instanceof ExpansionDefinition ? context.getJavaExpansionClass(definition) : context.getJavaClass(definition);
65
 			target.addClass(cls.internalName, definition.accept(new JavaDefinitionVisitor(context, scriptFile.classWriter)));
66
 			target.addClass(cls.internalName, definition.accept(new JavaDefinitionVisitor(context, scriptFile.classWriter)));
79
 			final SourceFile sourceFile = script.file;
80
 			final SourceFile sourceFile = script.file;
80
 			final String className = getClassName(sourceFile == null ? null : sourceFile.getFilename());
81
 			final String className = getClassName(sourceFile == null ? null : sourceFile.getFilename());
81
 			JavaScriptFile scriptFile = getScriptFile(scriptBlocks, script.pkg.fullName + "/" + className);
82
 			JavaScriptFile scriptFile = getScriptFile(scriptBlocks, script.pkg.fullName + "/" + className);
83
+			scriptFile.classWriter.visitSource(script.file.getFilename(), null);
82
 
84
 
83
 			String methodName = scriptFile.scriptMethods.isEmpty() ? "run" : "run" + scriptFile.scriptMethods.size();
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
 	}
137
 	}
138
 
138
 
139
 	/**
139
 	/**
140
-	 * Checks if an expression can be inlined
140
+	 * Checks if an expression can be inLined
141
 	 *
141
 	 *
142
 	 * @param expression Expression to check for.
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
 		while (expression instanceof StorageCastExpression)
146
 		while (expression instanceof StorageCastExpression)
147
 			expression = ((StorageCastExpression) expression).value;
147
 			expression = ((StorageCastExpression) expression).value;
148
 
148
 
162
 	 * @param currentArrayType  The current type of the array, reduced during the recursions of the functions
162
 	 * @param currentArrayType  The current type of the array, reduced during the recursions of the functions
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!
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
 		final Label begin = new Label();
166
 		final Label begin = new Label();
167
 		final Label end = new Label();
167
 		final Label end = new Label();
168
 		javaWriter.label(begin);
168
 		javaWriter.label(begin);

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

8
 import org.openzen.zenscript.codemodel.FunctionParameter;
8
 import org.openzen.zenscript.codemodel.FunctionParameter;
9
 import org.openzen.zenscript.codemodel.expression.*;
9
 import org.openzen.zenscript.codemodel.expression.*;
10
 import org.openzen.zenscript.codemodel.expression.switchvalue.VariantOptionSwitchValue;
10
 import org.openzen.zenscript.codemodel.expression.switchvalue.VariantOptionSwitchValue;
11
-import org.openzen.zenscript.codemodel.generic.TypeParameter;
12
 import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
11
 import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
13
 import org.openzen.zenscript.codemodel.member.ref.FieldMemberRef;
12
 import org.openzen.zenscript.codemodel.member.ref.FieldMemberRef;
14
 import org.openzen.zenscript.codemodel.statement.ReturnStatement;
13
 import org.openzen.zenscript.codemodel.statement.ReturnStatement;
1897
 		final JavaWriter functionWriter;
1896
 		final JavaWriter functionWriter;
1898
 		
1897
 		
1899
 		//Bridge method!!!
1898
 		//Bridge method!!!
1900
-		if(!Objects.equals(methodInfo.descriptor, signature)) {
1899
+		if (!Objects.equals(methodInfo.descriptor, signature)) {
1901
 			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);
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
 			final JavaWriter bridgeWriter = new JavaWriter(expression.position, lambdaCW, bridgeMethodInfo, null, methodInfo.descriptor, null, "java/lang/Override");
1901
 			final JavaWriter bridgeWriter = new JavaWriter(expression.position, lambdaCW, bridgeMethodInfo, null, methodInfo.descriptor, null, "java/lang/Override");
1903
 			bridgeWriter.start();
1902
 			bridgeWriter.start();
1909
 				final FunctionParameter functionParameter = expression.header.parameters[i];
1908
 				final FunctionParameter functionParameter = expression.header.parameters[i];
1910
 				final Type type = context.getType(functionParameter.type);
1909
 				final Type type = context.getType(functionParameter.type);
1911
 				bridgeWriter.load(type, i + 1);
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
 			if(expression.header.getReturnType().type != BasicTypeID.VOID) {
1917
 			if(expression.header.getReturnType().type != BasicTypeID.VOID) {
1917
 				bridgeWriter.returnType(context.getType(expression.header.getReturnType()));
1918
 				bridgeWriter.returnType(context.getType(expression.header.getReturnType()));
1918
 			}
1919
 			}
3324
 
3325
 
3325
 
3326
 
3326
 				if (builtin == BuiltinID.ARRAY_CONSTRUCTOR_SIZED) {
3327
 				if (builtin == BuiltinID.ARRAY_CONSTRUCTOR_SIZED) {
3327
-					type.elementType.type.accept(JavaDefaultExpressionTypeVisitor.INSTANCE).accept(this);
3328
+					type.elementType.type.getDefaultValue().accept(this);
3328
 				} else {
3329
 				} else {
3329
 					expression.arguments.arguments[expression.arguments.arguments.length - 1].accept(this);
3330
 					expression.arguments.arguments[expression.arguments.arguments.length - 1].accept(this);
3330
 				}
3331
 				}
3338
 				return;
3339
 				return;
3339
 			}
3340
 			}
3340
 			case ARRAY_CONSTRUCTOR_LAMBDA: {
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
 				ArrayTypeID type = (ArrayTypeID) expression.type.type;
3363
 				ArrayTypeID type = (ArrayTypeID) expression.type.type;
3360
-
3364
+				
3365
+				//Labels
3361
 				final Label begin = new Label();
3366
 				final Label begin = new Label();
3362
 				final Label end = new Label();
3367
 				final Label end = new Label();
3363
-
3364
 				javaWriter.label(begin);
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
 				final Type originArrayType = context.getType(expression.arguments.arguments[0].type);
3372
 				final Type originArrayType = context.getType(expression.arguments.arguments[0].type);
3367
 				final int originArrayLocation = javaWriter.local(originArrayType);
3373
 				final int originArrayLocation = javaWriter.local(originArrayType);
3368
 				javaWriter.storeObject(originArrayLocation);
3374
 				javaWriter.storeObject(originArrayLocation);
3369
 				Type destinationArrayType = context.getType(expression.type);
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
 								Label inlineBegin = new Label();
3385
 								Label inlineBegin = new Label();
3388
 								Label inlineEnd = new Label();
3386
 								Label inlineEnd = new Label();
3389
 								javaWriter.label(inlineBegin);
3387
 								javaWriter.label(inlineBegin);
3390
-								final Type projectedElementType = Type.getType(originArrayType.getDescriptor().substring(type.dimension));
3388
+								
3391
 								final int projectedElementLocal = javaWriter.local(projectedElementType);
3389
 								final int projectedElementLocal = javaWriter.local(projectedElementType);
3392
 								javaWriter.store(projectedElementType, projectedElementLocal);
3390
 								javaWriter.store(projectedElementType, projectedElementLocal);
3393
-
3394
-
3391
+								
3392
+								
3395
 								JavaExpressionVisitor visitor = new JavaExpressionVisitor(context, module, javaWriter) {
3393
 								JavaExpressionVisitor visitor = new JavaExpressionVisitor(context, module, javaWriter) {
3396
 									@Override
3394
 									@Override
3397
 									public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
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
 										javaWriter.load(projectedElementType, projectedElementLocal);
3404
 										javaWriter.load(projectedElementType, projectedElementLocal);
3399
 										return null;
3405
 										return null;
3400
 									}
3406
 									}
3401
 								};
3407
 								};
3402
-
3408
+								
3403
 								Expression funcExpression = expression.arguments.arguments[1];
3409
 								Expression funcExpression = expression.arguments.arguments[1];
3404
 								while (funcExpression instanceof StorageCastExpression) {
3410
 								while (funcExpression instanceof StorageCastExpression) {
3405
 									funcExpression = ((StorageCastExpression) funcExpression).value;
3411
 									funcExpression = ((StorageCastExpression) funcExpression).value;
3406
 								}
3412
 								}
3407
-
3413
+								
3408
 								if (funcExpression instanceof FunctionExpression && ((FunctionExpression) funcExpression).body instanceof ReturnStatement) {
3414
 								if (funcExpression instanceof FunctionExpression && ((FunctionExpression) funcExpression).body instanceof ReturnStatement) {
3415
+									CompilerUtils.tagMethodParameters(context, module, ((FunctionExpression) funcExpression).header, false);
3409
 									((ReturnStatement) ((FunctionExpression) funcExpression).body).value.accept(visitor);
3416
 									((ReturnStatement) ((FunctionExpression) funcExpression).body).value.accept(visitor);
3410
 									javaWriter.addVariableInfo(new JavaLocalVariableInfo(projectedElementType, projectedElementLocal, inlineBegin, ((FunctionExpression) funcExpression).header.parameters[0].name, inlineEnd));
3417
 									javaWriter.addVariableInfo(new JavaLocalVariableInfo(projectedElementType, projectedElementLocal, inlineBegin, ((FunctionExpression) funcExpression).header.parameters[0].name, inlineEnd));
3418
+									
3411
 								} else throw new IllegalStateException("Trying to inline a non-inlineable expression");
3419
 								} else throw new IllegalStateException("Trying to inline a non-inlineable expression");
3412
-
3413
-
3420
+								
3421
+								
3414
 								javaWriter.label(inlineEnd);
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
 								//Apply function here
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
 				javaWriter.label(end);
3450
 				javaWriter.label(end);
3430
 				return;
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
 			case ARRAY_INDEXGET:
3454
 			case ARRAY_INDEXGET:
3454
 				break;
3455
 				break;

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

36
 
36
 
37
 	@Override
37
 	@Override
38
 	public Boolean visitBlock(BlockStatement statement) {
38
 	public Boolean visitBlock(BlockStatement statement) {
39
+    	javaWriter.position(statement.position.fromLine);
39
 		Boolean returns = false;
40
 		Boolean returns = false;
40
 		for (Statement statement1 : statement.statements) {
41
 		for (Statement statement1 : statement.statements) {
41
 			returns = statement1.accept(this);
42
 			returns = statement1.accept(this);
45
 
46
 
46
 	@Override
47
 	@Override
47
 	public Boolean visitBreak(BreakStatement statement) {
48
 	public Boolean visitBreak(BreakStatement statement) {
49
+    	javaWriter.position(statement.position.fromLine);
48
 		javaWriter.goTo(javaWriter.getNamedLabel(statement.target.label + "_end"));
50
 		javaWriter.goTo(javaWriter.getNamedLabel(statement.target.label + "_end"));
49
 		return false;
51
 		return false;
50
 	}
52
 	}
51
 
53
 
52
 	@Override
54
 	@Override
53
 	public Boolean visitContinue(ContinueStatement statement) {
55
 	public Boolean visitContinue(ContinueStatement statement) {
56
+    	javaWriter.position(statement.position.fromLine);
54
 		javaWriter.goTo(javaWriter.getNamedLabel(statement.target.label + "_start"));
57
 		javaWriter.goTo(javaWriter.getNamedLabel(statement.target.label + "_start"));
55
 		return false;
58
 		return false;
56
 	}
59
 	}
57
 
60
 
58
 	@Override
61
 	@Override
59
 	public Boolean visitDoWhile(DoWhileStatement statement) {
62
 	public Boolean visitDoWhile(DoWhileStatement statement) {
63
+    	javaWriter.position(statement.position.fromLine);
60
 		Label start = new Label();
64
 		Label start = new Label();
61
 		Label end = new Label();
65
 		Label end = new Label();
62
 		if (statement.label == null)
66
 		if (statement.label == null)
82
 
86
 
83
 	@Override
87
 	@Override
84
 	public Boolean visitExpression(ExpressionStatement statement) {
88
 	public Boolean visitExpression(ExpressionStatement statement) {
89
+    	javaWriter.position(statement.position.fromLine);
85
 		statement.expression.accept(nonPushingExpressionVisitor);
90
 		statement.expression.accept(nonPushingExpressionVisitor);
86
 		return false;
91
 		return false;
87
 	}
92
 	}
88
 
93
 
89
 	@Override
94
 	@Override
90
 	public Boolean visitForeach(ForeachStatement statement) {
95
 	public Boolean visitForeach(ForeachStatement statement) {
96
+    	javaWriter.position(statement.position.fromLine);
91
 		//Create Labels
97
 		//Create Labels
92
 		Label start = new Label();
98
 		Label start = new Label();
93
 		Label end = new Label();
99
 		Label end = new Label();
147
 
153
 
148
 	@Override
154
 	@Override
149
 	public Boolean visitIf(IfStatement statement) {
155
 	public Boolean visitIf(IfStatement statement) {
156
+    	javaWriter.position(statement.position.fromLine);
150
 		statement.condition.accept(expressionVisitor);
157
 		statement.condition.accept(expressionVisitor);
151
 		Label onElse = null;
158
 		Label onElse = null;
152
 		Label end = new Label();
159
 		Label end = new Label();
171
 	public Boolean visitLock(LockStatement statement) {
178
 	public Boolean visitLock(LockStatement statement) {
172
 		return false;
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
 	@Override
187
 	@Override
176
 	public Boolean visitReturn(ReturnStatement statement) {
188
 	public Boolean visitReturn(ReturnStatement statement) {
189
+    	javaWriter.position(statement.position.fromLine);
177
 		statement.value.accept(expressionVisitor);
190
 		statement.value.accept(expressionVisitor);
178
 		javaWriter.returnType(context.getType(statement.value.type));
191
 		javaWriter.returnType(context.getType(statement.value.type));
179
 		return true;
192
 		return true;
181
 
194
 
182
 	@Override
195
 	@Override
183
 	public Boolean visitSwitch(SwitchStatement statement) {
196
 	public Boolean visitSwitch(SwitchStatement statement) {
197
+    	javaWriter.position(statement.position.fromLine);
184
 
198
 
185
 		final Label start = new Label();
199
 		final Label start = new Label();
186
 		final Label end = new Label();
200
 		final Label end = new Label();
246
 
260
 
247
 	@Override
261
 	@Override
248
 	public Boolean visitThrow(ThrowStatement statement) {
262
 	public Boolean visitThrow(ThrowStatement statement) {
263
+    	javaWriter.position(statement.position.fromLine);
249
 		statement.value.accept(expressionVisitor);
264
 		statement.value.accept(expressionVisitor);
250
 		javaWriter.aThrow();
265
 		javaWriter.aThrow();
251
 		return false;
266
 		return false;
253
 
268
 
254
 	@Override
269
 	@Override
255
 	public Boolean visitTryCatch(TryCatchStatement statement) {
270
 	public Boolean visitTryCatch(TryCatchStatement statement) {
271
+    	javaWriter.position(statement.position.fromLine);
256
 		final Label tryCatchStart = new Label();
272
 		final Label tryCatchStart = new Label();
257
 		final Label tryFinish = new Label();
273
 		final Label tryFinish = new Label();
258
 		final Label tryCatchFinish = new Label();
274
 		final Label tryCatchFinish = new Label();
304
 
320
 
305
 	@Override
321
 	@Override
306
 	public Boolean visitVar(VarStatement statement) {
322
 	public Boolean visitVar(VarStatement statement) {
323
+    	javaWriter.position(statement.position.fromLine);
307
 		if (statement.initializer != null) {
324
 		if (statement.initializer != null) {
308
 			statement.initializer.accept(expressionVisitor);
325
 			statement.initializer.accept(expressionVisitor);
309
 		}
326
 		}
322
 
339
 
323
 	@Override
340
 	@Override
324
 	public Boolean visitWhile(WhileStatement statement) {
341
 	public Boolean visitWhile(WhileStatement statement) {
342
+    	javaWriter.position(statement.position.fromLine);
325
 		Label start = new Label();
343
 		Label start = new Label();
326
 		Label end = new Label();
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
  */
5
  */
6
 package org.openzen.zencode.java;
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
 import java.util.function.Consumer;
9
 import java.util.function.Consumer;
13
 import org.openzen.zencode.shared.CompileException;
10
 import org.openzen.zencode.shared.CompileException;
14
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
11
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
20
 public class JavaNativeLoader {
17
 public class JavaNativeLoader {
21
 	private final Class<?>[] classes;
18
 	private final Class<?>[] classes;
22
 	private final Class<?>[] globals;
19
 	private final Class<?>[] globals;
23
-	private final Map<String, LoadingModule> modulesByBasePackage = new HashMap<>();
24
 	private final Map<String, LoadingModule> modulesByName = new HashMap<>();
20
 	private final Map<String, LoadingModule> modulesByName = new HashMap<>();
25
 	private boolean loaded = false;
21
 	private boolean loaded = false;
26
 	
22
 	
40
 			throw new IllegalArgumentException("Module already exists: " + name);
36
 			throw new IllegalArgumentException("Module already exists: " + name);
41
 		
37
 		
42
 		LoadingModule module = new LoadingModule(pkg, name, basePackage, dependencies);
38
 		LoadingModule module = new LoadingModule(pkg, name, basePackage, dependencies);
43
-		modulesByBasePackage.put(name, module);
44
 		modulesByName.put(name, module);
39
 		modulesByName.put(name, module);
45
 		
40
 		
46
 		return module;
41
 		return module;
52
 		sortClasses();
47
 		sortClasses();
53
 		
48
 		
54
 		ScriptingEngine engine = new ScriptingEngine();
49
 		ScriptingEngine engine = new ScriptingEngine();
55
-		for (LoadingModule module : modulesByBasePackage.values()) {
50
+		for (LoadingModule module : modulesByName.values()) {
56
 			load(engine, module);
51
 			load(engine, module);
57
 		}
52
 		}
58
 		return engine;
53
 		return engine;

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

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

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

11
 import java.util.Collections;
11
 import java.util.Collections;
12
 import java.util.List;
12
 import java.util.List;
13
 import java.util.Map;
13
 import java.util.Map;
14
+import java.util.function.Consumer;
15
+
14
 import org.openzen.zencode.shared.CompileException;
16
 import org.openzen.zencode.shared.CompileException;
15
 import org.openzen.zencode.shared.SourceFile;
17
 import org.openzen.zencode.shared.SourceFile;
16
 import org.openzen.zenscript.codemodel.FunctionParameter;
18
 import org.openzen.zenscript.codemodel.FunctionParameter;
29
 import org.openzen.zenscript.parser.BracketExpressionParser;
31
 import org.openzen.zenscript.parser.BracketExpressionParser;
30
 import org.openzen.zenscript.parser.ParsedFile;
32
 import org.openzen.zenscript.parser.ParsedFile;
31
 import org.openzen.zenscript.parser.ZippedPackage;
33
 import org.openzen.zenscript.parser.ZippedPackage;
34
+import org.openzen.zenscript.validator.ValidationLogEntry;
32
 import org.openzen.zenscript.validator.Validator;
35
 import org.openzen.zenscript.validator.Validator;
33
 
36
 
34
 /**
37
 /**
92
 			BracketExpressionParser bracketParser,
95
 			BracketExpressionParser bracketParser,
93
 			FunctionParameter[] scriptParameters,
96
 			FunctionParameter[] scriptParameters,
94
 			String... dependencies) throws ParseException
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
 		Module scriptModule = new Module(name);
112
 		Module scriptModule = new Module(name);
97
 		CompilingPackage scriptPackage = new CompilingPackage(new ZSPackage(space.rootPackage, name), scriptModule);
113
 		CompilingPackage scriptPackage = new CompilingPackage(new ZSPackage(space.rootPackage, name), scriptModule);
98
 		
114
 		
99
 		ParsedFile[] files = new ParsedFile[sources.length];
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
 			files[i] = ParsedFile.parse(scriptPackage, bracketParser, sources[i]);
118
 			files[i] = ParsedFile.parse(scriptPackage, bracketParser, sources[i]);
119
+		}
102
 		
120
 		
103
 		SemanticModule[] dependencyModules = new SemanticModule[dependencies.length + 1];
121
 		SemanticModule[] dependencyModules = new SemanticModule[dependencies.length + 1];
104
 		dependencyModules[0] = space.getModule("stdlib");
122
 		dependencyModules[0] = space.getModule("stdlib");
112
 				files,
130
 				files,
113
 				space,
131
 				space,
114
 				scriptParameters,
132
 				scriptParameters,
115
-				ex -> ex.printStackTrace());
133
+				compileExceptionConsumer);
116
 		if (!scripts.isValid())
134
 		if (!scripts.isValid())
117
 			return scripts;
135
 			return scripts;
118
 		
136
 		
119
 		return Validator.validate(
137
 		return Validator.validate(
120
 				scripts.normalize(),
138
 				scripts.normalize(),
121
-				error -> System.out.println(error.toString()));
139
+				validatorErrorConsumer);
122
 	}
140
 	}
123
 	
141
 	
124
 	public void registerCompiled(SemanticModule module) {
142
 	public void registerCompiled(SemanticModule module) {
147
 			runUnit.dump(new File("classes"));
165
 			runUnit.dump(new File("classes"));
148
 		runUnit.run(arguments, parentClassLoader);
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
-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
 				scope.hints,
44
 				scope.hints,
45
 				new GenericName(this.member, typeArguments));
45
 				new GenericName(this.member, typeArguments));
46
 		if (member == null) {
46
 		if (member == null) {
47
-			TypeMembers members = scope.getTypeMembers(cValue.eval().type);
47
+			//TypeMembers members = scope.getTypeMembers(cValue.eval().type);
48
 			throw new CompileException(position, CompileExceptionCode.NO_SUCH_MEMBER, "Member not found: " + this.member);
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
 );
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
 invokeFunctionalInt(y);
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
 	}
739
 	}
740
 
740
 
741
 	private void checkFieldAccess(CodePosition position, FieldMemberRef field) {
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
 	private void checkStatic(CodePosition position, DefinitionMemberRef member) {
746
 	private void checkStatic(CodePosition position, DefinitionMemberRef member) {

Loading…
Cancel
Save