Browse Source

WIP Array initializers; moved size variable naming to multiArray function

kindlich 6 years ago
parent
commit
280df6c5c0
No known key found for this signature in database

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

5
 import org.objectweb.asm.Type;
5
 import org.objectweb.asm.Type;
6
 
6
 
7
 public class ArrayInitializerHelper {
7
 public class ArrayInitializerHelper {
8
-	public static void visitProjected(JavaWriter javaWriter, int[] sizeLocations, int[] counterLocations, int dim, int originArrayLocation, Type originArrayType, int functionLocation, Type functionType, Type currentArrayType) {
8
+	public static void visitProjected(JavaWriter javaWriter, int[] sizeLocations, int dim, int originArrayLocation, Type originArrayType, int functionLocation, Type functionType, Type currentArrayType) {
9
 
9
 
10
-		visitMultiDimArray(javaWriter, sizeLocations, counterLocations, dim, currentArrayType, (elementType1, counterLocations1, sizeLocations1) -> {
10
+		visitMultiDimArray(javaWriter, sizeLocations, dim, currentArrayType, (elementType, counterLocations) -> {
11
 			//Load origin array
11
 			//Load origin array
12
 			javaWriter.loadObject(originArrayLocation);
12
 			javaWriter.loadObject(originArrayLocation);
13
 
13
 
29
 
29
 
30
 
30
 
31
 	public static void visitMultiDimArrayWithDefaultValue(JavaWriter javaWriter, int dim, int defaultLocation, Type currentArrayType, int[] sizeLocations) {
31
 	public static void visitMultiDimArrayWithDefaultValue(JavaWriter javaWriter, int dim, int defaultLocation, Type currentArrayType, int[] sizeLocations) {
32
-		visitMultiDimArray(javaWriter, sizeLocations, new int[dim], dim, currentArrayType, (elementType, counterLocations, sizeLocations1) -> javaWriter.load(elementType, defaultLocation));
32
+		visitMultiDimArray(javaWriter, sizeLocations, new int[dim], dim, currentArrayType, (elementType, counterLocations) -> javaWriter.load(elementType, defaultLocation));
33
+	}
34
+
35
+	public static void visitMultiDimArray(JavaWriter javaWriter, int[] sizeLocations, int dim, Type currentArrayType, InnermostFunction innermostFunction){
36
+		visitMultiDimArray(javaWriter, sizeLocations, new int[dim], dim, currentArrayType, innermostFunction);
33
 	}
37
 	}
34
 
38
 
35
 
39
 
36
-	public static void visitMultiDimArray(JavaWriter javaWriter, int[] sizeLocations, int[] counterLocations, int dim, Type currentArrayType, InnermostFunction innermostFunction) {
40
+	private static void visitMultiDimArray(JavaWriter javaWriter, int[] sizeLocations, int[] counterLocations, int dim, Type currentArrayType, InnermostFunction innermostFunction) {
37
 		final Label begin = new Label();
41
 		final Label begin = new Label();
38
 		final Label end = new Label();
42
 		final Label end = new Label();
39
 		javaWriter.label(begin);
43
 		javaWriter.label(begin);
63
 		javaWriter.dup();
67
 		javaWriter.dup();
64
 		javaWriter.loadInt(forLoopCounter);
68
 		javaWriter.loadInt(forLoopCounter);
65
 		if (dim == 1) {
69
 		if (dim == 1) {
66
-			innermostFunction.apply(elementType, counterLocations, sizeLocations);
70
+			innermostFunction.apply(elementType, counterLocations);
67
 		} else {
71
 		} else {
68
 			visitMultiDimArray(javaWriter, sizeLocations, counterLocations, dim - 1, elementType, innermostFunction);
72
 			visitMultiDimArray(javaWriter, sizeLocations, counterLocations, dim - 1, elementType, innermostFunction);
69
 		}
73
 		}
79
 		javaWriter.nameVariable(forLoopCounter, "i" + dim, loopStart, loopEnd, Type.getType(int.class));
83
 		javaWriter.nameVariable(forLoopCounter, "i" + dim, loopStart, loopEnd, Type.getType(int.class));
80
 
84
 
81
 		javaWriter.label(end);
85
 		javaWriter.label(end);
86
+
87
+		for (int i = 0; i < sizeLocations.length; i++) {
88
+			javaWriter.nameVariable(sizeLocations[i], "size" + (sizeLocations.length - i), begin, end, Type.getType(int.class));
89
+		}
82
 	}
90
 	}
83
 
91
 
84
 	@FunctionalInterface
92
 	@FunctionalInterface
85
 	public interface InnermostFunction {
93
 	public interface InnermostFunction {
86
-		void apply(Type elementType, int[] counterLocations, int[] sizeLocations);
94
+		void apply(Type elementType, int[] counterLocations);
87
 	}
95
 	}
88
 }
96
 }
89
 
97
 

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

3062
 				//D2Array = new T[arguments[1]][];
3062
 				//D2Array = new T[arguments[1]][];
3063
 				//Arrays.fill(D2Array, D1Array);
3063
 				//Arrays.fill(D2Array, D1Array);
3064
 
3064
 
3065
-				final Type ASMType;
3066
-				{
3067
-					final char[] chars = new char[type.dimension];
3068
-					Arrays.fill(chars, '[');
3069
-					ASMType = Type.getType(new String(chars) + context.getDescriptor(type.elementType));
3070
-				}
3065
+				final Type ASMType = context.getType(expression.type);
3071
 				final Type ASMElementType = context.getType(type.elementType);
3066
 				final Type ASMElementType = context.getType(type.elementType);
3072
 
3067
 
3073
 				final Label begin = new Label();
3068
 				final Label begin = new Label();
3077
 				final int defaultValueLocation = javaWriter.local(ASMElementType);
3072
 				final int defaultValueLocation = javaWriter.local(ASMElementType);
3078
 
3073
 
3079
 				if (builtin == BuiltinID.ARRAY_CONSTRUCTOR_SIZED) {
3074
 				if (builtin == BuiltinID.ARRAY_CONSTRUCTOR_SIZED) {
3075
+					//TODO type.elementType.type.accept(JavaDefaultExpressionVisitor.Instance).accept(this);
3076
+					//TODO I still need to write that one, I guess
3080
 					if (CompilerUtils.isPrimitive(type.elementType.type))
3077
 					if (CompilerUtils.isPrimitive(type.elementType.type))
3081
 						if (type.elementType.type == BasicTypeID.FLOAT) javaWriter.constant(0f);
3078
 						if (type.elementType.type == BasicTypeID.FLOAT) javaWriter.constant(0f);
3082
 						else if (type.elementType.type == BasicTypeID.DOUBLE) javaWriter.constant(0D);
3079
 						else if (type.elementType.type == BasicTypeID.DOUBLE) javaWriter.constant(0D);
3112
 
3109
 
3113
 				//naming the variables
3110
 				//naming the variables
3114
 				javaWriter.nameVariable(defaultValueLocation, "defaultValue", begin, end, ASMElementType);
3111
 				javaWriter.nameVariable(defaultValueLocation, "defaultValue", begin, end, ASMElementType);
3115
-				for (int i = 0; i < arraySizes.length; i++) {
3116
-					javaWriter.nameVariable(arraySizes[i], "size" + (arraySizes.length - i), begin, end, Type.getType(int.class));
3117
-				}
3118
 				return;
3112
 				return;
3119
 			}
3113
 			}
3120
 			case ARRAY_CONSTRUCTOR_LAMBDA: {
3114
 			case ARRAY_CONSTRUCTOR_LAMBDA: {
3153
 				final int functionLocation = javaWriter.local(functionType);
3147
 				final int functionLocation = javaWriter.local(functionType);
3154
 				javaWriter.storeObject(functionLocation);
3148
 				javaWriter.storeObject(functionLocation);
3155
 
3149
 
3156
-				int[] variableCounterLocations = new int[type.dimension];
3157
 				Type destinationArrayType = context.getType(expression.type);
3150
 				Type destinationArrayType = context.getType(expression.type);
3158
 
3151
 
3159
 
3152
 
3183
 				}
3176
 				}
3184
 
3177
 
3185
 
3178
 
3186
-				ArrayInitializerHelper.visitProjected(javaWriter, arraySizes, variableCounterLocations, type.dimension, originArrayLocation, originArrayType, functionLocation, functionType, destinationArrayType);
3179
+				ArrayInitializerHelper.visitProjected(javaWriter, arraySizes, type.dimension, originArrayLocation, originArrayType, functionLocation, functionType, destinationArrayType);
3187
 
3180
 
3188
 				javaWriter.label(end);
3181
 				javaWriter.label(end);
3189
 
3182
 
3190
 				//naming the variables
3183
 				//naming the variables
3191
 				javaWriter.nameVariable(functionLocation, "projectionFunction", begin, end, functionType);
3184
 				javaWriter.nameVariable(functionLocation, "projectionFunction", begin, end, functionType);
3192
-				for (int i = 0; i < arraySizes.length; i++) {
3193
-					javaWriter.nameVariable(arraySizes[i], "size" + (arraySizes.length - i), begin, end, Type.getType(int.class));
3194
-				}
3195
 				return;
3185
 				return;
3196
 
3186
 
3197
 			}
3187
 			}

Loading…
Cancel
Save