소스 검색

Fixed support for optional parameters. Also implemented java integration for them.

Stan Hebben 5 년 전
부모
커밋
c3b948d8fe
21개의 변경된 파일192개의 추가작업 그리고 24개의 파일을 삭제
  1. 2
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionHeader.java
  2. 2
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/Expression.java
  3. 2
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionVisitor.java
  4. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/FunctionalMemberRef.java
  5. 2
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/PropertyRef.java
  6. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialMemberGroupExpression.java
  7. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialStaticMemberGroupExpression.java
  8. 8
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/ArrayTypeID.java
  9. 46
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java
  10. 8
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/OptionalTypeID.java
  11. 8
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/StringTypeID.java
  12. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/TypeID.java
  13. 2
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java
  14. 2
    2
      IDE/src/main/java/org/openzen/zenscript/ide/Main.java
  15. 1
    1
      JavaAnnotations/src/main/java/org/openzen/zencode/java/ZenCodeType.java
  16. 82
    5
      JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java
  17. 6
    3
      Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedCallArguments.java
  18. 7
    0
      ScriptingExample/scripts/integration.zs
  19. 0
    0
      ScriptingExample/scripts/nope/match.zs
  20. 5
    0
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/TestClass.java
  21. 1
    1
      common.gradle

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionHeader.java 파일 보기

@@ -243,11 +243,11 @@ public class FunctionHeader {
243 243
 	}
244 244
 	
245 245
 	public boolean matchesImplicitly(CodePosition position, CallArguments arguments, TypeScope scope) {
246
-		if (arguments.arguments.length < minParameters || arguments.arguments.length > maxParameters)
246
+		if (!accepts(arguments.arguments.length))
247 247
 			return false;
248 248
 		
249 249
 		FunctionHeader header = fillGenericArguments(position, scope, arguments.typeArguments);
250
-		for (int i = 0; i < header.parameters.length; i++) {
250
+		for (int i = 0; i < arguments.arguments.length; i++) {
251 251
 			if (!scope.getTypeMembers(arguments.arguments[i].type).canCastImplicit(header.parameters[i].type))
252 252
 				return false;
253 253
 		}

+ 2
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/Expression.java 파일 보기

@@ -23,7 +23,6 @@ import org.openzen.zenscript.codemodel.type.member.TypeMembers;
23 23
 import org.openzen.zenscript.codemodel.scope.TypeScope;
24 24
 import org.openzen.zenscript.codemodel.statement.Statement;
25 25
 import org.openzen.zenscript.codemodel.statement.StatementTransformer;
26
-import org.openzen.zenscript.codemodel.type.BasicTypeID;
27 26
 import org.openzen.zenscript.codemodel.type.InvalidTypeID;
28 27
 import org.openzen.zenscript.codemodel.type.StoredType;
29 28
 import org.openzen.zenscript.codemodel.type.storage.UniqueStorageTag;
@@ -42,8 +41,8 @@ public abstract class Expression implements IPartialExpression {
42 41
 	public Expression(CodePosition position, StoredType type, StoredType thrownType) {
43 42
 		if (type == null)
44 43
 			throw new NullPointerException();
45
-		if (type.type == BasicTypeID.UNDETERMINED)
46
-			throw new IllegalArgumentException(position + ": Cannot use undetermined type as expression type");
44
+		//if (type.type == BasicTypeID.UNDETERMINED)
45
+		//	throw new IllegalArgumentException(position + ": Cannot use undetermined type as expression type");
47 46
 		
48 47
 		this.position = position;
49 48
 		this.type = type.getNormalized();

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionVisitor.java 파일 보기

@@ -95,11 +95,11 @@ public interface ExpressionVisitor<T> {
95 95
 	T visitInterfaceCast(InterfaceCastExpression expression);
96 96
 	
97 97
 	default T visitInvalid(InvalidExpression expression) {
98
-		throw new RuntimeException("Invalid expression");
98
+		throw new RuntimeException("Invalid expression @ " + expression.position + ": " + expression.message);
99 99
 	}
100 100
 	
101 101
 	default T visitInvalidAssign(InvalidAssignExpression expression) {
102
-		throw new RuntimeException("Invalid expression");
102
+		throw new RuntimeException("Invalid expression @ " + expression.position + ": " + expression.target.message);
103 103
 	}
104 104
 	
105 105
 	T visitIs(IsExpression expression);

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/FunctionalMemberRef.java 파일 보기

@@ -51,7 +51,7 @@ public class FunctionalMemberRef implements DefinitionMemberRef {
51 51
 	}
52 52
 	
53 53
 	public boolean accepts(int arguments) {
54
-		return arguments >= target.header.minParameters && arguments <= target.header.maxParameters;
54
+		return target.header.accepts(arguments);
55 55
 	}
56 56
 	
57 57
 	@Override

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/PropertyRef.java 파일 보기

@@ -46,8 +46,8 @@ public abstract class PropertyRef implements DefinitionMemberRef {
46 46
 	
47 47
 	public final StoredType getType() {
48 48
 		if (type == null) {
49
-			if (member.getType().type == BasicTypeID.UNDETERMINED)
50
-				throw new IllegalStateException("Property is not yet resolved!");
49
+			//if (member.getType().type == BasicTypeID.UNDETERMINED)
50
+			//	throw new IllegalStateException("Property is not yet resolved!");
51 51
 			
52 52
 			type = mapper == null ? member.getType() : member.getType().instance(mapper);
53 53
 			mapper = null;

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialMemberGroupExpression.java 파일 보기

@@ -90,7 +90,7 @@ public class PartialMemberGroupExpression implements IPartialExpression {
90 90
 	public List<FunctionHeader> getPossibleFunctionHeaders(TypeScope scope, List<StoredType> hints, int arguments) {
91 91
 		List<FunctionHeader> results = new ArrayList<>();
92 92
 		for (TypeMember<FunctionalMemberRef> method : group.getMethodMembers()) {
93
-			if (!method.member.accepts(arguments)  || method.member.isStatic())
93
+			if (!method.member.accepts(arguments) || method.member.isStatic())
94 94
 				continue;
95 95
 			
96 96
 			try {

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialStaticMemberGroupExpression.java 파일 보기

@@ -53,7 +53,7 @@ public class PartialStaticMemberGroupExpression implements IPartialExpression {
53 53
 	@Override
54 54
 	public List<FunctionHeader> getPossibleFunctionHeaders(TypeScope scope, List<StoredType> hints, int arguments) {
55 55
 		return group.getMethodMembers().stream()
56
-				.filter(method -> method.member.getHeader().parameters.length == arguments && method.member.isStatic())
56
+				.filter(method -> method.member.getHeader().accepts(arguments) && method.member.isStatic())
57 57
 				.map(method -> method.member.getHeader())
58 58
 				.collect(Collectors.toList());
59 59
 	}

+ 8
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/ArrayTypeID.java 파일 보기

@@ -7,8 +7,11 @@ package org.openzen.zenscript.codemodel.type;
7 7
 
8 8
 import java.util.List;
9 9
 import java.util.Set;
10
+import org.openzen.zencode.shared.CodePosition;
10 11
 import org.openzen.zenscript.codemodel.GenericMapper;
11 12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
13
+import org.openzen.zenscript.codemodel.expression.ArrayExpression;
14
+import org.openzen.zenscript.codemodel.expression.Expression;
12 15
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
13 16
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
14 17
 
@@ -36,6 +39,11 @@ public class ArrayTypeID implements TypeID {
36 39
 		this.normalized = elementType.getNormalized() == elementType ? this : registry.getArray(elementType.getNormalized(), dimension);
37 40
 	}
38 41
 	
42
+	@Override
43
+	public Expression getDefaultValue() {
44
+		return new ArrayExpression(CodePosition.UNKNOWN, Expression.NONE, this.stored());
45
+	}
46
+	
39 47
 	@Override
40 48
 	public ArrayTypeID getNormalized() {
41 49
 		return normalized;

+ 46
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java 파일 보기

@@ -8,8 +8,24 @@ package org.openzen.zenscript.codemodel.type;
8 8
 import java.util.Collections;
9 9
 import java.util.List;
10 10
 import java.util.Set;
11
+import org.openzen.zencode.shared.CodePosition;
11 12
 import org.openzen.zenscript.codemodel.GenericMapper;
12 13
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
14
+import org.openzen.zenscript.codemodel.expression.ConstantBoolExpression;
15
+import org.openzen.zenscript.codemodel.expression.ConstantByteExpression;
16
+import org.openzen.zenscript.codemodel.expression.ConstantCharExpression;
17
+import org.openzen.zenscript.codemodel.expression.ConstantDoubleExpression;
18
+import org.openzen.zenscript.codemodel.expression.ConstantFloatExpression;
19
+import org.openzen.zenscript.codemodel.expression.ConstantIntExpression;
20
+import org.openzen.zenscript.codemodel.expression.ConstantLongExpression;
21
+import org.openzen.zenscript.codemodel.expression.ConstantSByteExpression;
22
+import org.openzen.zenscript.codemodel.expression.ConstantShortExpression;
23
+import org.openzen.zenscript.codemodel.expression.ConstantUIntExpression;
24
+import org.openzen.zenscript.codemodel.expression.ConstantULongExpression;
25
+import org.openzen.zenscript.codemodel.expression.ConstantUShortExpression;
26
+import org.openzen.zenscript.codemodel.expression.ConstantUSizeExpression;
27
+import org.openzen.zenscript.codemodel.expression.Expression;
28
+import org.openzen.zenscript.codemodel.expression.NullExpression;
13 29
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
14 30
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
15 31
 
@@ -41,9 +57,12 @@ public enum BasicTypeID implements TypeID {
41 57
 	public final String name;
42 58
 	public final StoredType stored;
43 59
 	
60
+	private Expression defaultValue = null;
61
+	
44 62
 	BasicTypeID(String name) {
45 63
 		this.name = name;
46 64
 		stored = new StoredType(this, null);
65
+		this.defaultValue = defaultValue;
47 66
 	}
48 67
 	
49 68
 	@Override
@@ -100,9 +119,36 @@ public enum BasicTypeID implements TypeID {
100 119
 	public boolean hasDefaultValue() {
101 120
 		return true;
102 121
 	}
122
+	
123
+	@Override
124
+	public Expression getDefaultValue() {
125
+		if (defaultValue == null) // lazy init due to circular initialization in the constant expressions
126
+			defaultValue = generateDefaultValue();
127
+		
128
+		return defaultValue;
129
+	}
103 130
 
104 131
 	@Override
105 132
 	public void extractTypeParameters(List<TypeParameter> typeParameters) {
106 133
 		
107 134
 	}
135
+	
136
+	private Expression generateDefaultValue() {
137
+		switch (this) {
138
+			case NULL: return new NullExpression(CodePosition.UNKNOWN);
139
+			case BOOL: return new ConstantBoolExpression(CodePosition.UNKNOWN, false);
140
+			case BYTE: return new ConstantByteExpression(CodePosition.UNKNOWN, 0);
141
+			case SBYTE: return new ConstantSByteExpression(CodePosition.UNKNOWN, (byte)0);
142
+			case SHORT: return new ConstantShortExpression(CodePosition.UNKNOWN, (short)0);
143
+			case USHORT: return new ConstantUShortExpression(CodePosition.UNKNOWN, 0);
144
+			case INT: return new ConstantIntExpression(CodePosition.UNKNOWN, 0);
145
+			case UINT: return new ConstantUIntExpression(CodePosition.UNKNOWN, 0);
146
+			case LONG: return new ConstantLongExpression(CodePosition.UNKNOWN, 0);
147
+			case ULONG: return new ConstantULongExpression(CodePosition.UNKNOWN, 0);
148
+			case USIZE: return new ConstantUSizeExpression(CodePosition.UNKNOWN, 0);
149
+			case FLOAT: return new ConstantFloatExpression(CodePosition.UNKNOWN, 0);
150
+			case DOUBLE: return new ConstantDoubleExpression(CodePosition.UNKNOWN, 0);
151
+			default: return null;
152
+		}
153
+	}
108 154
 }

+ 8
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/OptionalTypeID.java 파일 보기

@@ -8,8 +8,11 @@ package org.openzen.zenscript.codemodel.type;
8 8
 import java.util.List;
9 9
 import java.util.Objects;
10 10
 import java.util.Set;
11
+import org.openzen.zencode.shared.CodePosition;
11 12
 import org.openzen.zenscript.codemodel.GenericMapper;
12 13
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
14
+import org.openzen.zenscript.codemodel.expression.Expression;
15
+import org.openzen.zenscript.codemodel.expression.NullExpression;
13 16
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
14 17
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
15 18
 
@@ -27,6 +30,11 @@ public class OptionalTypeID implements TypeID {
27 30
 		normalized = baseType.getNormalized() == baseType ? this : registry.getOptional(baseType.getNormalized());
28 31
 	}
29 32
 	
33
+	@Override
34
+	public Expression getDefaultValue() {
35
+		return new NullExpression(CodePosition.UNKNOWN);
36
+	}
37
+	
30 38
 	@Override
31 39
 	public TypeID getNormalized() {
32 40
 		return normalized;

+ 8
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/StringTypeID.java 파일 보기

@@ -7,8 +7,11 @@ package org.openzen.zenscript.codemodel.type;
7 7
 
8 8
 import java.util.List;
9 9
 import java.util.Set;
10
+import org.openzen.zencode.shared.CodePosition;
10 11
 import org.openzen.zenscript.codemodel.GenericMapper;
11 12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
13
+import org.openzen.zenscript.codemodel.expression.ConstantStringExpression;
14
+import org.openzen.zenscript.codemodel.expression.Expression;
12 15
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
13 16
 import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
14 17
 import org.openzen.zenscript.codemodel.type.storage.BorrowStorageTag;
@@ -32,6 +35,11 @@ public class StringTypeID implements TypeID {
32 35
 	
33 36
 	private StringTypeID() {}
34 37
 	
38
+	@Override
39
+	public Expression getDefaultValue() {
40
+		return new ConstantStringExpression(CodePosition.UNKNOWN, "");
41
+	}
42
+	
35 43
 	@Override
36 44
 	public TypeID getNormalized() {
37 45
 		return INSTANCE;

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/TypeID.java 파일 보기

@@ -10,6 +10,7 @@ import java.util.Map;
10 10
 import java.util.Set;
11 11
 import org.openzen.zenscript.codemodel.GenericMapper;
12 12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
13
+import org.openzen.zenscript.codemodel.expression.Expression;
13 14
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
14 15
 import org.openzen.zenscript.codemodel.type.member.LocalMemberCache;
15 16
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
@@ -38,6 +39,10 @@ public interface TypeID {
38 39
 	
39 40
 	boolean hasInferenceBlockingTypeParameters(TypeParameter[] parameters);
40 41
 	
42
+	default Expression getDefaultValue() {
43
+		return null;
44
+	}
45
+	
41 46
 	// Infers type parameters for this type so it matches with targetType
42 47
 	// returns false if that isn't possible
43 48
 	default Map<TypeParameter, StoredType> inferTypeParameters(LocalMemberCache cache, StoredType targetType) {

+ 2
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java 파일 보기

@@ -584,6 +584,8 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
584 584
 		if (modified.isOptional()) {
585 585
 			operator(builtin, OperatorType.EQUALS, new FunctionHeader(BOOL.stored, NULL.stored), BuiltinID.OPTIONAL_IS_NULL);
586 586
 			operator(builtin, OperatorType.NOTEQUALS, new FunctionHeader(BOOL.stored, NULL.stored), BuiltinID.OPTIONAL_IS_NOT_NULL);
587
+			operator(builtin, OperatorType.SAME, new FunctionHeader(BOOL.stored, NULL.stored), BuiltinID.OPTIONAL_IS_NULL);
588
+			operator(builtin, OperatorType.NOTSAME, new FunctionHeader(BOOL.stored, NULL.stored), BuiltinID.OPTIONAL_IS_NOT_NULL);
587 589
 		}
588 590
 		
589 591
 		processType(builtin);

+ 2
- 2
IDE/src/main/java/org/openzen/zenscript/ide/Main.java 파일 보기

@@ -29,8 +29,8 @@ public class Main {
29 29
     public static void main(String... args) throws IOException {
30 30
 		if (args.length == 0) {
31 31
 			// testing environment - TODO project chooser/creator
32
-			//args = new String[] { "../../ZenCode", "SharedJavaSource" };
33
-			args = new String[] { "../../ZenCode", "CodeModelJavaSource" };
32
+			args = new String[] { "../../ZenCode", "SharedJavaSource" };
33
+			//args = new String[] { "../../ZenCode", "CodeModelJavaSource" };
34 34
 		}
35 35
 		
36 36
 		Arguments arguments = new Arguments(args);

+ 1
- 1
JavaAnnotations/src/main/java/org/openzen/zencode/java/ZenCodeType.java 파일 보기

@@ -124,7 +124,7 @@ public interface ZenCodeType {
124 124
 	}
125 125
 	
126 126
 	@Retention(RetentionPolicy.RUNTIME)
127
-	@Target(ElementType.PACKAGE)
127
+	@Target(ElementType.PARAMETER)
128 128
 	@interface OptionalString {
129 129
 		String value() default "";
130 130
 	}

+ 82
- 5
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java 파일 보기

@@ -9,6 +9,7 @@ import java.lang.reflect.AnnotatedType;
9 9
 import java.lang.reflect.Field;
10 10
 import java.lang.reflect.Method;
11 11
 import java.lang.reflect.Modifier;
12
+import java.lang.reflect.Parameter;
12 13
 import java.lang.reflect.ParameterizedType;
13 14
 import java.lang.reflect.Type;
14 15
 import java.lang.reflect.TypeVariable;
@@ -31,8 +32,21 @@ import org.openzen.zenscript.codemodel.definition.EnumDefinition;
31 32
 import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
32 33
 import org.openzen.zenscript.codemodel.definition.StructDefinition;
33 34
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
35
+import org.openzen.zenscript.codemodel.expression.ConstantByteExpression;
36
+import org.openzen.zenscript.codemodel.expression.ConstantDoubleExpression;
37
+import org.openzen.zenscript.codemodel.expression.ConstantFloatExpression;
38
+import org.openzen.zenscript.codemodel.expression.ConstantIntExpression;
39
+import org.openzen.zenscript.codemodel.expression.ConstantLongExpression;
40
+import org.openzen.zenscript.codemodel.expression.ConstantSByteExpression;
41
+import org.openzen.zenscript.codemodel.expression.ConstantShortExpression;
42
+import org.openzen.zenscript.codemodel.expression.ConstantStringExpression;
43
+import org.openzen.zenscript.codemodel.expression.ConstantUIntExpression;
44
+import org.openzen.zenscript.codemodel.expression.ConstantULongExpression;
45
+import org.openzen.zenscript.codemodel.expression.ConstantUShortExpression;
46
+import org.openzen.zenscript.codemodel.expression.Expression;
34 47
 import org.openzen.zenscript.codemodel.expression.ExpressionSymbol;
35 48
 import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
49
+import org.openzen.zenscript.codemodel.expression.StorageCastExpression;
36 50
 import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
37 51
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
38 52
 import org.openzen.zenscript.codemodel.member.CasterMember;
@@ -54,6 +68,7 @@ import org.openzen.zenscript.codemodel.type.TypeID;
54 68
 import org.openzen.zenscript.codemodel.type.member.BuiltinID;
55 69
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
56 70
 import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
71
+import org.openzen.zenscript.codemodel.type.storage.StaticStorageTag;
57 72
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
58 73
 import org.openzen.zenscript.javashared.JavaClass;
59 74
 import org.openzen.zenscript.javashared.JavaCompiledModule;
@@ -499,7 +514,7 @@ public class JavaNativeModule {
499 514
 		return getHeader(
500 515
 				context,
501 516
 				null,
502
-				constructor.getAnnotatedParameterTypes(),
517
+				constructor.getParameters(),
503 518
 				constructor.getTypeParameters(),
504 519
 				constructor.getAnnotatedExceptionTypes());
505 520
 	}
@@ -508,22 +523,84 @@ public class JavaNativeModule {
508 523
 		return getHeader(
509 524
 				context,
510 525
 				method.getAnnotatedReturnType(),
511
-				method.getAnnotatedParameterTypes(),
526
+				method.getParameters(),
512 527
 				method.getTypeParameters(),
513 528
 				method.getAnnotatedExceptionTypes());
514 529
 	}
515 530
 	
531
+	private Expression getDefaultValue(Parameter parameter, StoredType type) {
532
+		if (parameter.isAnnotationPresent(ZenCodeType.Optional.class)) {
533
+			Expression defaultValue = type.type.getDefaultValue();
534
+			if (defaultValue == null)
535
+				throw new IllegalArgumentException(type.toString() + " doesn't have a default value");
536
+			return defaultValue;
537
+		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalInt.class)) {
538
+			ZenCodeType.OptionalInt annotation = parameter.getAnnotation(ZenCodeType.OptionalInt.class);
539
+			if (type.type == BasicTypeID.BYTE)
540
+				return new ConstantByteExpression(CodePosition.NATIVE, annotation.value());
541
+			else if (type.type == BasicTypeID.SBYTE)
542
+				return new ConstantSByteExpression(CodePosition.NATIVE, (byte)annotation.value());
543
+			else if (type.type == BasicTypeID.SHORT)
544
+				return new ConstantShortExpression(CodePosition.NATIVE, (short)annotation.value());
545
+			else if (type.type == BasicTypeID.USHORT)
546
+				return new ConstantUShortExpression(CodePosition.NATIVE, annotation.value());
547
+			else if (type.type == BasicTypeID.INT)
548
+				return new ConstantIntExpression(CodePosition.NATIVE, annotation.value());
549
+			else if (type.type == BasicTypeID.UINT)
550
+				return new ConstantUIntExpression(CodePosition.NATIVE, annotation.value());
551
+			else
552
+				throw new IllegalArgumentException("Cannot use int default values for " + type.toString());
553
+		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalLong.class)) {
554
+			ZenCodeType.OptionalLong annotation = parameter.getAnnotation(ZenCodeType.OptionalLong.class);
555
+			if (type.type == BasicTypeID.LONG)
556
+				return new ConstantLongExpression(CodePosition.NATIVE, annotation.value());
557
+			else if (type.type == BasicTypeID.ULONG)
558
+				return new ConstantULongExpression(CodePosition.NATIVE, annotation.value());
559
+			else
560
+				throw new IllegalArgumentException("Cannot use long default values for " + type.toString());
561
+		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalFloat.class)) {
562
+			ZenCodeType.OptionalFloat annotation = parameter.getAnnotation(ZenCodeType.OptionalFloat.class);
563
+			if (type.type == BasicTypeID.FLOAT)
564
+				return new ConstantFloatExpression(CodePosition.NATIVE, annotation.value());
565
+			else
566
+				throw new IllegalArgumentException("Cannot use float default values for " + type.toString());
567
+		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalDouble.class)) {
568
+			ZenCodeType.OptionalDouble annotation = parameter.getAnnotation(ZenCodeType.OptionalDouble.class);
569
+			if (type.type == BasicTypeID.DOUBLE)
570
+				return new ConstantDoubleExpression(CodePosition.NATIVE, annotation.value());
571
+			else
572
+				throw new IllegalArgumentException("Cannot use double default values for " + type.toString());
573
+		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalString.class)) {
574
+			ZenCodeType.OptionalString annotation = parameter.getAnnotation(ZenCodeType.OptionalString.class);
575
+			if (type.type == StringTypeID.INSTANCE) {
576
+				Expression result = new ConstantStringExpression(CodePosition.NATIVE, annotation.value());
577
+				if (type.getActualStorage() != StaticStorageTag.INSTANCE)
578
+					result = new StorageCastExpression(CodePosition.NATIVE, result, type);
579
+				return result;
580
+			} else {
581
+				throw new IllegalArgumentException("Cannot use string default values for " + type.toString());
582
+			}
583
+		} else {
584
+			return null;
585
+		}
586
+	}
587
+	
516 588
 	private FunctionHeader getHeader(
517 589
 			TypeVariableContext context,
518 590
 			AnnotatedType javaReturnType,
519
-			AnnotatedType[] parameterTypes,
591
+			Parameter[] javaParameters,
520 592
 			TypeVariable<Method>[] javaTypeParameters,
521 593
 			AnnotatedType[] exceptionTypes) {
522 594
 		StoredType returnType = javaReturnType == null ? BasicTypeID.VOID.stored : loadStoredType(context, javaReturnType);
523 595
 		
524
-		FunctionParameter[] parameters = new FunctionParameter[parameterTypes.length];
596
+		FunctionParameter[] parameters = new FunctionParameter[javaParameters.length];
525 597
 		for (int i = 0; i < parameters.length; i++) {
526
-			parameters[i] = new FunctionParameter(loadStoredType(context, parameterTypes[i]));
598
+			Parameter parameter = javaParameters[i];
599
+			
600
+			AnnotatedType parameterType = parameter.getAnnotatedType();
601
+			StoredType type = loadStoredType(context, parameter.getAnnotatedType());
602
+			Expression defaultValue = getDefaultValue(parameter, type);
603
+			parameters[i] = new FunctionParameter(type, parameter.getName(), defaultValue, parameter.isVarArgs());
527 604
 		}
528 605
 		
529 606
 		TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];

+ 6
- 3
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedCallArguments.java 파일 보기

@@ -14,6 +14,7 @@ import org.openzen.zencode.shared.CompileException;
14 14
 import org.openzen.zencode.shared.CompileExceptionCode;
15 15
 import org.openzen.zenscript.lexer.ZSTokenType;
16 16
 import org.openzen.zenscript.codemodel.FunctionHeader;
17
+import org.openzen.zenscript.codemodel.FunctionParameter;
17 18
 import org.openzen.zenscript.codemodel.expression.CallArguments;
18 19
 import org.openzen.zenscript.codemodel.expression.Expression;
19 20
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
@@ -220,14 +221,16 @@ public class ParsedCallArguments {
220 221
 	}
221 222
 	
222 223
 	private boolean isCompatibleWith(BaseScope scope, FunctionHeader header, StoredType[] typeArguments) {
223
-		if (arguments.size() != header.parameters.length)
224
+		if (!header.accepts(arguments.size()))
224 225
 			return false;
225 226
 		
227
+		boolean variadic = false;
226 228
 		for (int i = 0; i < arguments.size(); i++) {
227
-			if (typeArguments == null && header.typeParameters != null && header.parameters[i].type.hasInferenceBlockingTypeParameters(header.typeParameters))
229
+			FunctionParameter parameter = header.getParameter(variadic, i);
230
+			if (typeArguments == null && header.typeParameters != null && parameter.type.hasInferenceBlockingTypeParameters(header.typeParameters))
228 231
 				return false;
229 232
 			
230
-			if (!arguments.get(i).isCompatibleWith(scope, header.parameters[i].type.getNormalized()))
233
+			if (!arguments.get(i).isCompatibleWith(scope, header.getParameterType(variadic, i).getNormalized()))
231 234
 				return false;
232 235
 		}
233 236
 		

+ 7
- 0
ScriptingExample/scripts/integration.zs 파일 보기

@@ -4,6 +4,8 @@ import example.TestInterface;
4 4
 val instance = new TestClass("Instance");
5 5
 println("Name: " + instance.name);
6 6
 instance.dump();
7
+instance.withDefaultParameter("Hello");
8
+instance.withDefaultParameter("Hello", "moon");
7 9
 
8 10
 var generated = instance.generate();
9 11
 println(generated.getValue());
@@ -45,3 +47,8 @@ var count = 10;
45 47
 floatMethod(5f * count);
46 48
 
47 49
 invokeFunctional(name => "Hello " + name);
50
+
51
+//val x = (a as string) => "hello" + a;
52
+//invokeFunctional(x);
53
+
54
+testOptional(value => value == null ? 0 : value.length as int);

ScriptingExample/scripts/match.zs → ScriptingExample/scripts/nope/match.zs 파일 보기


+ 5
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/TestClass.java 파일 보기

@@ -52,4 +52,9 @@ public class TestClass implements TestInterface {
52 52
 	public TestGenericInterface<String> generate() {
53 53
 		return () -> name;
54 54
 	}
55
+	
56
+	@Method
57
+	public void withDefaultParameter(String a, @OptionalString("world") String b) {
58
+		System.out.println(a + " " + b);
59
+	}
55 60
 }

+ 1
- 1
common.gradle 파일 보기

@@ -6,7 +6,7 @@ apply plugin: 'java'
6 6
 apply plugin: 'maven'
7 7
 
8 8
 String mavenGroupId = 'org.openzen.zencode'
9
-String mavenVersion = '0.3.7'
9
+String mavenVersion = '0.3.8'
10 10
 
11 11
 sourceCompatibility = '1.8'
12 12
 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

Loading…
취소
저장