Browse Source

WIP: Rudimentary work on VarAargs

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

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

@@ -11,6 +11,7 @@ import org.openzen.zencode.shared.CodePosition;
11 11
 import org.openzen.zenscript.codemodel.expression.CallArguments;
12 12
 import org.openzen.zenscript.codemodel.expression.Expression;
13 13
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
14
+import org.openzen.zenscript.codemodel.type.ArrayTypeID;
14 15
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
15 16
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
16 17
 import org.openzen.zenscript.codemodel.scope.TypeScope;
@@ -165,7 +166,11 @@ public class FunctionHeader {
165 166
 	
166 167
 	public FunctionParameter getParameter(boolean isVariadic, int index) {
167 168
 		if (isVariadic && index >= parameters.length - 1) {
168
-			return parameters[parameters.length - 1];
169
+			final FunctionParameter parameter = parameters[parameters.length - 1];
170
+			if(parameter.type.type instanceof ArrayTypeID) {
171
+				return new FunctionParameter(((ArrayTypeID) parameter.type.type).elementType, parameter.name);
172
+			}
173
+			return parameter;
169 174
 		} else {
170 175
 			return parameters[index];
171 176
 		}

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

@@ -387,10 +387,37 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
387 387
 
388 388
 				}
389 389
 			}
390
-			for (Expression argument : expression.arguments.arguments) {
391
-				argument.accept(this);
390
+
391
+			final Expression[] arguments = expression.arguments.arguments;
392
+			final FunctionParameter[] parameters = expression.instancedHeader.parameters;
393
+
394
+			final boolean variadic = expression.instancedHeader.isVariadicCall(expression.arguments) && ((arguments.length != parameters.length) || !parameters[parameters.length - 1].type.type
395
+					.equals(arguments[arguments.length - 1].type.type));
396
+
397
+			if(variadic) {
398
+				for (int i = 0; i < parameters.length - 1; i++) {
399
+					arguments[i].accept(this);
400
+				}
401
+
402
+				final int arrayCount = (arguments.length - parameters.length) + 1;
403
+				javaWriter.constant(arrayCount);
404
+				javaWriter.newArray(context.getType(parameters[parameters.length - 1].type).getElementType());
405
+				for (int i = 0; i < arrayCount; i++) {
406
+					javaWriter.dup();
407
+					javaWriter.constant(i);
408
+					arguments[i + parameters.length - 1].accept(this);
409
+					javaWriter.arrayStore(context.getType(arguments[i].type));
410
+				}
411
+
412
+
413
+			} else {
414
+ 				for (Expression argument : arguments) {
415
+					argument.accept(this);
416
+				}
392 417
 			}
393 418
 
419
+
420
+
394 421
 			if (!checkAndExecuteMethodInfo(expression.member, expression.type, expression))
395 422
 				throw new IllegalStateException("Call target has no method info!");
396 423
 

+ 9
- 5
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedCallArguments.java View File

@@ -140,11 +140,14 @@ public class ParsedCallArguments {
140 140
 		List<StoredType>[] predictedTypes = new List[arguments.size()];
141 141
 		for (int i = 0; i < predictedTypes.length; i++)
142 142
 			predictedTypes[i] = new ArrayList<>();
143
-		
143
+
144 144
 		for (FunctionHeader header : candidates) {
145
+			//TODO: this is wrong!
146
+			boolean variadic = header.isVariadic();
145 147
 			for (int i = 0; i < arguments.size(); i++) {
146
-				if (!predictedTypes[i].contains(header.parameters[i].type))
147
-					predictedTypes[i].add(header.parameters[i].type);
148
+				final StoredType parameterType = header.getParameterType(variadic, i);
149
+				if (!predictedTypes[i].contains(parameterType))
150
+					predictedTypes[i].add(parameterType);
148 151
 			}
149 152
 		}
150 153
 		
@@ -223,8 +226,9 @@ public class ParsedCallArguments {
223 226
 	private boolean isCompatibleWith(BaseScope scope, FunctionHeader header, StoredType[] typeArguments) {
224 227
 		if (!header.accepts(arguments.size()))
225 228
 			return false;
226
-		
227
-		boolean variadic = false;
229
+
230
+		//TODO: This is wrong
231
+		boolean variadic = header.isVariadic();
228 232
 		for (int i = 0; i < arguments.size(); i++) {
229 233
 			FunctionParameter parameter = header.getParameter(variadic, i);
230 234
 			if (typeArguments == null && header.typeParameters != null && parameter.type.hasInferenceBlockingTypeParameters(header.typeParameters))

Loading…
Cancel
Save