Browse Source

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

Stan Hebben 5 years ago
parent
commit
c3b948d8fe
21 changed files with 192 additions and 24 deletions
  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 View File

243
 	}
243
 	}
244
 	
244
 	
245
 	public boolean matchesImplicitly(CodePosition position, CallArguments arguments, TypeScope scope) {
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
 			return false;
247
 			return false;
248
 		
248
 		
249
 		FunctionHeader header = fillGenericArguments(position, scope, arguments.typeArguments);
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
 			if (!scope.getTypeMembers(arguments.arguments[i].type).canCastImplicit(header.parameters[i].type))
251
 			if (!scope.getTypeMembers(arguments.arguments[i].type).canCastImplicit(header.parameters[i].type))
252
 				return false;
252
 				return false;
253
 		}
253
 		}

+ 2
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/Expression.java View File

23
 import org.openzen.zenscript.codemodel.scope.TypeScope;
23
 import org.openzen.zenscript.codemodel.scope.TypeScope;
24
 import org.openzen.zenscript.codemodel.statement.Statement;
24
 import org.openzen.zenscript.codemodel.statement.Statement;
25
 import org.openzen.zenscript.codemodel.statement.StatementTransformer;
25
 import org.openzen.zenscript.codemodel.statement.StatementTransformer;
26
-import org.openzen.zenscript.codemodel.type.BasicTypeID;
27
 import org.openzen.zenscript.codemodel.type.InvalidTypeID;
26
 import org.openzen.zenscript.codemodel.type.InvalidTypeID;
28
 import org.openzen.zenscript.codemodel.type.StoredType;
27
 import org.openzen.zenscript.codemodel.type.StoredType;
29
 import org.openzen.zenscript.codemodel.type.storage.UniqueStorageTag;
28
 import org.openzen.zenscript.codemodel.type.storage.UniqueStorageTag;
42
 	public Expression(CodePosition position, StoredType type, StoredType thrownType) {
41
 	public Expression(CodePosition position, StoredType type, StoredType thrownType) {
43
 		if (type == null)
42
 		if (type == null)
44
 			throw new NullPointerException();
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
 		this.position = position;
47
 		this.position = position;
49
 		this.type = type.getNormalized();
48
 		this.type = type.getNormalized();

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionVisitor.java View File

95
 	T visitInterfaceCast(InterfaceCastExpression expression);
95
 	T visitInterfaceCast(InterfaceCastExpression expression);
96
 	
96
 	
97
 	default T visitInvalid(InvalidExpression expression) {
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
 	default T visitInvalidAssign(InvalidAssignExpression expression) {
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
 	T visitIs(IsExpression expression);
105
 	T visitIs(IsExpression expression);

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/FunctionalMemberRef.java View File

51
 	}
51
 	}
52
 	
52
 	
53
 	public boolean accepts(int arguments) {
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
 	@Override
57
 	@Override

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/PropertyRef.java View File

46
 	
46
 	
47
 	public final StoredType getType() {
47
 	public final StoredType getType() {
48
 		if (type == null) {
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
 			type = mapper == null ? member.getType() : member.getType().instance(mapper);
52
 			type = mapper == null ? member.getType() : member.getType().instance(mapper);
53
 			mapper = null;
53
 			mapper = null;

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialMemberGroupExpression.java View File

90
 	public List<FunctionHeader> getPossibleFunctionHeaders(TypeScope scope, List<StoredType> hints, int arguments) {
90
 	public List<FunctionHeader> getPossibleFunctionHeaders(TypeScope scope, List<StoredType> hints, int arguments) {
91
 		List<FunctionHeader> results = new ArrayList<>();
91
 		List<FunctionHeader> results = new ArrayList<>();
92
 		for (TypeMember<FunctionalMemberRef> method : group.getMethodMembers()) {
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
 				continue;
94
 				continue;
95
 			
95
 			
96
 			try {
96
 			try {

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialStaticMemberGroupExpression.java View File

53
 	@Override
53
 	@Override
54
 	public List<FunctionHeader> getPossibleFunctionHeaders(TypeScope scope, List<StoredType> hints, int arguments) {
54
 	public List<FunctionHeader> getPossibleFunctionHeaders(TypeScope scope, List<StoredType> hints, int arguments) {
55
 		return group.getMethodMembers().stream()
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
 				.map(method -> method.member.getHeader())
57
 				.map(method -> method.member.getHeader())
58
 				.collect(Collectors.toList());
58
 				.collect(Collectors.toList());
59
 	}
59
 	}

+ 8
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/ArrayTypeID.java View File

7
 
7
 
8
 import java.util.List;
8
 import java.util.List;
9
 import java.util.Set;
9
 import java.util.Set;
10
+import org.openzen.zencode.shared.CodePosition;
10
 import org.openzen.zenscript.codemodel.GenericMapper;
11
 import org.openzen.zenscript.codemodel.GenericMapper;
11
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
13
+import org.openzen.zenscript.codemodel.expression.ArrayExpression;
14
+import org.openzen.zenscript.codemodel.expression.Expression;
12
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
15
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
13
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
16
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
14
 
17
 
36
 		this.normalized = elementType.getNormalized() == elementType ? this : registry.getArray(elementType.getNormalized(), dimension);
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
 	@Override
47
 	@Override
40
 	public ArrayTypeID getNormalized() {
48
 	public ArrayTypeID getNormalized() {
41
 		return normalized;
49
 		return normalized;

+ 46
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java View File

8
 import java.util.Collections;
8
 import java.util.Collections;
9
 import java.util.List;
9
 import java.util.List;
10
 import java.util.Set;
10
 import java.util.Set;
11
+import org.openzen.zencode.shared.CodePosition;
11
 import org.openzen.zenscript.codemodel.GenericMapper;
12
 import org.openzen.zenscript.codemodel.GenericMapper;
12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
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
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
29
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
14
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
30
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
15
 
31
 
41
 	public final String name;
57
 	public final String name;
42
 	public final StoredType stored;
58
 	public final StoredType stored;
43
 	
59
 	
60
+	private Expression defaultValue = null;
61
+	
44
 	BasicTypeID(String name) {
62
 	BasicTypeID(String name) {
45
 		this.name = name;
63
 		this.name = name;
46
 		stored = new StoredType(this, null);
64
 		stored = new StoredType(this, null);
65
+		this.defaultValue = defaultValue;
47
 	}
66
 	}
48
 	
67
 	
49
 	@Override
68
 	@Override
100
 	public boolean hasDefaultValue() {
119
 	public boolean hasDefaultValue() {
101
 		return true;
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
 	@Override
131
 	@Override
105
 	public void extractTypeParameters(List<TypeParameter> typeParameters) {
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 View File

8
 import java.util.List;
8
 import java.util.List;
9
 import java.util.Objects;
9
 import java.util.Objects;
10
 import java.util.Set;
10
 import java.util.Set;
11
+import org.openzen.zencode.shared.CodePosition;
11
 import org.openzen.zenscript.codemodel.GenericMapper;
12
 import org.openzen.zenscript.codemodel.GenericMapper;
12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
13
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
14
+import org.openzen.zenscript.codemodel.expression.Expression;
15
+import org.openzen.zenscript.codemodel.expression.NullExpression;
13
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
16
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
14
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
17
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
15
 
18
 
27
 		normalized = baseType.getNormalized() == baseType ? this : registry.getOptional(baseType.getNormalized());
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
 	@Override
38
 	@Override
31
 	public TypeID getNormalized() {
39
 	public TypeID getNormalized() {
32
 		return normalized;
40
 		return normalized;

+ 8
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/StringTypeID.java View File

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

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/TypeID.java View File

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

+ 2
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java View File

584
 		if (modified.isOptional()) {
584
 		if (modified.isOptional()) {
585
 			operator(builtin, OperatorType.EQUALS, new FunctionHeader(BOOL.stored, NULL.stored), BuiltinID.OPTIONAL_IS_NULL);
585
 			operator(builtin, OperatorType.EQUALS, new FunctionHeader(BOOL.stored, NULL.stored), BuiltinID.OPTIONAL_IS_NULL);
586
 			operator(builtin, OperatorType.NOTEQUALS, new FunctionHeader(BOOL.stored, NULL.stored), BuiltinID.OPTIONAL_IS_NOT_NULL);
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
 		processType(builtin);
591
 		processType(builtin);

+ 2
- 2
IDE/src/main/java/org/openzen/zenscript/ide/Main.java View File

29
     public static void main(String... args) throws IOException {
29
     public static void main(String... args) throws IOException {
30
 		if (args.length == 0) {
30
 		if (args.length == 0) {
31
 			// testing environment - TODO project chooser/creator
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
 		Arguments arguments = new Arguments(args);
36
 		Arguments arguments = new Arguments(args);

+ 1
- 1
JavaAnnotations/src/main/java/org/openzen/zencode/java/ZenCodeType.java View File

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

+ 82
- 5
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java View File

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.Modifier;
11
 import java.lang.reflect.Modifier;
12
+import java.lang.reflect.Parameter;
12
 import java.lang.reflect.ParameterizedType;
13
 import java.lang.reflect.ParameterizedType;
13
 import java.lang.reflect.Type;
14
 import java.lang.reflect.Type;
14
 import java.lang.reflect.TypeVariable;
15
 import java.lang.reflect.TypeVariable;
31
 import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
32
 import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
32
 import org.openzen.zenscript.codemodel.definition.StructDefinition;
33
 import org.openzen.zenscript.codemodel.definition.StructDefinition;
33
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
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
 import org.openzen.zenscript.codemodel.expression.ExpressionSymbol;
47
 import org.openzen.zenscript.codemodel.expression.ExpressionSymbol;
35
 import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
48
 import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
49
+import org.openzen.zenscript.codemodel.expression.StorageCastExpression;
36
 import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
50
 import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
37
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
51
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
38
 import org.openzen.zenscript.codemodel.member.CasterMember;
52
 import org.openzen.zenscript.codemodel.member.CasterMember;
54
 import org.openzen.zenscript.codemodel.type.member.BuiltinID;
68
 import org.openzen.zenscript.codemodel.type.member.BuiltinID;
55
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
69
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
56
 import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
70
 import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
71
+import org.openzen.zenscript.codemodel.type.storage.StaticStorageTag;
57
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
72
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
58
 import org.openzen.zenscript.javashared.JavaClass;
73
 import org.openzen.zenscript.javashared.JavaClass;
59
 import org.openzen.zenscript.javashared.JavaCompiledModule;
74
 import org.openzen.zenscript.javashared.JavaCompiledModule;
499
 		return getHeader(
514
 		return getHeader(
500
 				context,
515
 				context,
501
 				null,
516
 				null,
502
-				constructor.getAnnotatedParameterTypes(),
517
+				constructor.getParameters(),
503
 				constructor.getTypeParameters(),
518
 				constructor.getTypeParameters(),
504
 				constructor.getAnnotatedExceptionTypes());
519
 				constructor.getAnnotatedExceptionTypes());
505
 	}
520
 	}
508
 		return getHeader(
523
 		return getHeader(
509
 				context,
524
 				context,
510
 				method.getAnnotatedReturnType(),
525
 				method.getAnnotatedReturnType(),
511
-				method.getAnnotatedParameterTypes(),
526
+				method.getParameters(),
512
 				method.getTypeParameters(),
527
 				method.getTypeParameters(),
513
 				method.getAnnotatedExceptionTypes());
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
 	private FunctionHeader getHeader(
588
 	private FunctionHeader getHeader(
517
 			TypeVariableContext context,
589
 			TypeVariableContext context,
518
 			AnnotatedType javaReturnType,
590
 			AnnotatedType javaReturnType,
519
-			AnnotatedType[] parameterTypes,
591
+			Parameter[] javaParameters,
520
 			TypeVariable<Method>[] javaTypeParameters,
592
 			TypeVariable<Method>[] javaTypeParameters,
521
 			AnnotatedType[] exceptionTypes) {
593
 			AnnotatedType[] exceptionTypes) {
522
 		StoredType returnType = javaReturnType == null ? BasicTypeID.VOID.stored : loadStoredType(context, javaReturnType);
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
 		for (int i = 0; i < parameters.length; i++) {
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
 		TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];
606
 		TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];

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

14
 import org.openzen.zencode.shared.CompileExceptionCode;
14
 import org.openzen.zencode.shared.CompileExceptionCode;
15
 import org.openzen.zenscript.lexer.ZSTokenType;
15
 import org.openzen.zenscript.lexer.ZSTokenType;
16
 import org.openzen.zenscript.codemodel.FunctionHeader;
16
 import org.openzen.zenscript.codemodel.FunctionHeader;
17
+import org.openzen.zenscript.codemodel.FunctionParameter;
17
 import org.openzen.zenscript.codemodel.expression.CallArguments;
18
 import org.openzen.zenscript.codemodel.expression.CallArguments;
18
 import org.openzen.zenscript.codemodel.expression.Expression;
19
 import org.openzen.zenscript.codemodel.expression.Expression;
19
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
20
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
220
 	}
221
 	}
221
 	
222
 	
222
 	private boolean isCompatibleWith(BaseScope scope, FunctionHeader header, StoredType[] typeArguments) {
223
 	private boolean isCompatibleWith(BaseScope scope, FunctionHeader header, StoredType[] typeArguments) {
223
-		if (arguments.size() != header.parameters.length)
224
+		if (!header.accepts(arguments.size()))
224
 			return false;
225
 			return false;
225
 		
226
 		
227
+		boolean variadic = false;
226
 		for (int i = 0; i < arguments.size(); i++) {
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
 				return false;
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
 				return false;
234
 				return false;
232
 		}
235
 		}
233
 		
236
 		

+ 7
- 0
ScriptingExample/scripts/integration.zs View File

4
 val instance = new TestClass("Instance");
4
 val instance = new TestClass("Instance");
5
 println("Name: " + instance.name);
5
 println("Name: " + instance.name);
6
 instance.dump();
6
 instance.dump();
7
+instance.withDefaultParameter("Hello");
8
+instance.withDefaultParameter("Hello", "moon");
7
 
9
 
8
 var generated = instance.generate();
10
 var generated = instance.generate();
9
 println(generated.getValue());
11
 println(generated.getValue());
45
 floatMethod(5f * count);
47
 floatMethod(5f * count);
46
 
48
 
47
 invokeFunctional(name => "Hello " + name);
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 View File


+ 5
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/TestClass.java View File

52
 	public TestGenericInterface<String> generate() {
52
 	public TestGenericInterface<String> generate() {
53
 		return () -> name;
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 View File

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

Loading…
Cancel
Save