Browse Source

Java Bytecode Array Constructor: Implemented projection with indices

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

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

3358
 				javaWriter.label(end);
3358
 				javaWriter.label(end);
3359
 				return;
3359
 				return;
3360
 			}
3360
 			}
3361
-			case ARRAY_CONSTRUCTOR_PROJECTED: {
3361
+			case ARRAY_CONSTRUCTOR_PROJECTED:
3362
+			case ARRAY_CONSTRUCTOR_PROJECTED_INDEXED: {
3362
 				ArrayTypeID type = (ArrayTypeID) expression.type.type;
3363
 				ArrayTypeID type = (ArrayTypeID) expression.type.type;
3363
 				
3364
 				
3364
 				//Labels
3365
 				//Labels
3373
 				javaWriter.storeObject(originArrayLocation);
3374
 				javaWriter.storeObject(originArrayLocation);
3374
 				Type destinationArrayType = context.getType(expression.type);
3375
 				Type destinationArrayType = context.getType(expression.type);
3375
 				
3376
 				
3376
-				
3377
+				final boolean indexed = builtin == BuiltinID.ARRAY_CONSTRUCTOR_PROJECTED_INDEXED;
3377
 				final boolean canBeInLined = ArrayInitializerHelper.canBeInLined(expression.arguments.arguments[1]);
3378
 				final boolean canBeInLined = ArrayInitializerHelper.canBeInLined(expression.arguments.arguments[1]);
3378
 				if (canBeInLined) {
3379
 				if (canBeInLined) {
3379
 					//We can inline, so do it
3380
 					//We can inline, so do it
3392
 								JavaExpressionVisitor visitor = new JavaExpressionVisitor(context, module, javaWriter) {
3393
 								JavaExpressionVisitor visitor = new JavaExpressionVisitor(context, module, javaWriter) {
3393
 									@Override
3394
 									@Override
3394
 									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
+										
3395
 										javaWriter.load(projectedElementType, projectedElementLocal);
3404
 										javaWriter.load(projectedElementType, projectedElementLocal);
3396
 										return null;
3405
 										return null;
3397
 									}
3406
 									}
3403
 								}
3412
 								}
3404
 								
3413
 								
3405
 								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);
3406
 									((ReturnStatement) ((FunctionExpression) funcExpression).body).value.accept(visitor);
3416
 									((ReturnStatement) ((FunctionExpression) funcExpression).body).value.accept(visitor);
3407
 									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
+									
3408
 								} else throw new IllegalStateException("Trying to inline a non-inlineable expression");
3419
 								} else throw new IllegalStateException("Trying to inline a non-inlineable expression");
3409
 								
3420
 								
3410
 								
3421
 								
3424
 								javaWriter.loadObject(functionLocation);
3435
 								javaWriter.loadObject(functionLocation);
3425
 								javaWriter.swap();
3436
 								javaWriter.swap();
3426
 								
3437
 								
3438
+								if(indexed) {
3439
+									for (int counterLocation : counterLocations) {
3440
+										javaWriter.loadInt(counterLocation);
3441
+										javaWriter.swap();
3442
+									}
3443
+								}
3444
+								
3427
 								javaWriter.invokeInterface(context.getFunctionalInterface(expression.arguments.arguments[1].type));
3445
 								javaWriter.invokeInterface(context.getFunctionalInterface(expression.arguments.arguments[1].type));
3428
 							});
3446
 							});
3429
 				}
3447
 				}
3433
 				return;
3451
 				return;
3434
 				
3452
 				
3435
 			}
3453
 			}
3436
-			case ARRAY_CONSTRUCTOR_PROJECTED_INDEXED: {
3437
-				ArrayTypeID type = (ArrayTypeID) expression.type.type;
3438
-				
3439
-				if (type.dimension == 1) {
3440
-					// original = arguments[0] (this is an array)
3441
-					// projector = arguments[1] (this is a lambda with 2 parameters)
3442
-					// array = new T[original.length]
3443
-					// for (int i = 0; i < array.length; i++)
3444
-					//   array[i] = projector(i, original[i]);
3445
-					//
3446
-					// NOTE: arguments[1] can be a FunctionExpression; this can be optimized by running it inline
3447
-
3448
-					// TODO: implement in bytecode
3449
-
3450
-					return;
3451
-				} else {
3452
-					// TODO: implement
3453
-					throw new UnsupportedOperationException("Not yet supported!");
3454
-				}
3455
-			}
3456
 			case ARRAY_INDEXGET:
3454
 			case ARRAY_INDEXGET:
3457
 				break;
3455
 				break;
3458
 			case ARRAY_INDEXSET:
3456
 			case ARRAY_INDEXSET:

Loading…
Cancel
Save