Browse Source

Merge remote-tracking branch 'Stan/development' into development

kindlich 5 years ago
parent
commit
8f96b3b2a8
No known key found for this signature in database
21 changed files with 200 additions and 27 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. 90
    8
      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
 	}

+ 90
- 8
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java View File

5
  */
5
  */
6
 package org.openzen.zencode.java;
6
 package org.openzen.zencode.java;
7
 
7
 
8
-import java.lang.reflect.*;
8
+import java.lang.reflect.AnnotatedType;
9
+import java.lang.reflect.Field;
10
+import java.lang.reflect.Method;
11
+import java.lang.reflect.Member;
12
+import java.lang.reflect.Modifier;
13
+import java.lang.reflect.Parameter;
14
+import java.lang.reflect.ParameterizedType;
15
+import java.lang.reflect.Type;
16
+import java.lang.reflect.TypeVariable;
9
 import java.util.Collections;
17
 import java.util.Collections;
10
 import java.util.HashMap;
18
 import java.util.HashMap;
11
 import java.util.Map;
19
 import java.util.Map;
25
 import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
33
 import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
26
 import org.openzen.zenscript.codemodel.definition.StructDefinition;
34
 import org.openzen.zenscript.codemodel.definition.StructDefinition;
27
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
35
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
36
+import org.openzen.zenscript.codemodel.expression.ConstantByteExpression;
37
+import org.openzen.zenscript.codemodel.expression.ConstantDoubleExpression;
38
+import org.openzen.zenscript.codemodel.expression.ConstantFloatExpression;
39
+import org.openzen.zenscript.codemodel.expression.ConstantIntExpression;
40
+import org.openzen.zenscript.codemodel.expression.ConstantLongExpression;
41
+import org.openzen.zenscript.codemodel.expression.ConstantSByteExpression;
42
+import org.openzen.zenscript.codemodel.expression.ConstantShortExpression;
43
+import org.openzen.zenscript.codemodel.expression.ConstantStringExpression;
44
+import org.openzen.zenscript.codemodel.expression.ConstantUIntExpression;
45
+import org.openzen.zenscript.codemodel.expression.ConstantULongExpression;
46
+import org.openzen.zenscript.codemodel.expression.ConstantUShortExpression;
47
+import org.openzen.zenscript.codemodel.expression.Expression;
28
 import org.openzen.zenscript.codemodel.expression.ExpressionSymbol;
48
 import org.openzen.zenscript.codemodel.expression.ExpressionSymbol;
29
 import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
49
 import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
50
+import org.openzen.zenscript.codemodel.expression.StorageCastExpression;
30
 import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
51
 import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
31
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
52
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
32
 import org.openzen.zenscript.codemodel.member.CasterMember;
53
 import org.openzen.zenscript.codemodel.member.CasterMember;
48
 import org.openzen.zenscript.codemodel.type.member.BuiltinID;
69
 import org.openzen.zenscript.codemodel.type.member.BuiltinID;
49
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
70
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
50
 import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
71
 import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
72
+import org.openzen.zenscript.codemodel.type.storage.StaticStorageTag;
51
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
73
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
52
-import org.openzen.zenscript.javabytecode.compiler.CompilerUtils;
53
 import org.openzen.zenscript.javashared.JavaClass;
74
 import org.openzen.zenscript.javashared.JavaClass;
54
 import org.openzen.zenscript.javashared.JavaCompiledModule;
75
 import org.openzen.zenscript.javashared.JavaCompiledModule;
55
 import org.openzen.zenscript.javashared.JavaField;
76
 import org.openzen.zenscript.javashared.JavaField;
319
 			final String fieldName = annotation.value().isEmpty() ? field.getName() : annotation.value();
340
 			final String fieldName = annotation.value().isEmpty() ? field.getName() : annotation.value();
320
 			
341
 			
321
 			StoredType fieldType = loadStoredType(context, field.getAnnotatedType());
342
 			StoredType fieldType = loadStoredType(context, field.getAnnotatedType());
322
-			
323
 			FieldMember member = new FieldMember(CodePosition.NATIVE, definition, getMethodModifiers(field), fieldName, thisType, fieldType, registry, 0, 0, null);
343
 			FieldMember member = new FieldMember(CodePosition.NATIVE, definition, getMethodModifiers(field), fieldName, thisType, fieldType, registry, 0, 0, null);
324
 			definition.addMember(member);
344
 			definition.addMember(member);
325
 			compiled.setFieldInfo(member, new JavaField(javaClass, field.getName(), getDescriptor(field.getType())));
345
 			compiled.setFieldInfo(member, new JavaField(javaClass, field.getName(), getDescriptor(field.getType())));
498
 		return getHeader(
518
 		return getHeader(
499
 				context,
519
 				context,
500
 				null,
520
 				null,
501
-				constructor.getAnnotatedParameterTypes(),
521
+				constructor.getParameters(),
502
 				constructor.getTypeParameters(),
522
 				constructor.getTypeParameters(),
503
 				constructor.getAnnotatedExceptionTypes());
523
 				constructor.getAnnotatedExceptionTypes());
504
 	}
524
 	}
507
 		return getHeader(
527
 		return getHeader(
508
 				context,
528
 				context,
509
 				method.getAnnotatedReturnType(),
529
 				method.getAnnotatedReturnType(),
510
-				method.getAnnotatedParameterTypes(),
530
+				method.getParameters(),
511
 				method.getTypeParameters(),
531
 				method.getTypeParameters(),
512
 				method.getAnnotatedExceptionTypes());
532
 				method.getAnnotatedExceptionTypes());
513
 	}
533
 	}
514
 	
534
 	
535
+	private Expression getDefaultValue(Parameter parameter, StoredType type) {
536
+		if (parameter.isAnnotationPresent(ZenCodeType.Optional.class)) {
537
+			Expression defaultValue = type.type.getDefaultValue();
538
+			if (defaultValue == null)
539
+				throw new IllegalArgumentException(type.toString() + " doesn't have a default value");
540
+			return defaultValue;
541
+		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalInt.class)) {
542
+			ZenCodeType.OptionalInt annotation = parameter.getAnnotation(ZenCodeType.OptionalInt.class);
543
+			if (type.type == BasicTypeID.BYTE)
544
+				return new ConstantByteExpression(CodePosition.NATIVE, annotation.value());
545
+			else if (type.type == BasicTypeID.SBYTE)
546
+				return new ConstantSByteExpression(CodePosition.NATIVE, (byte)annotation.value());
547
+			else if (type.type == BasicTypeID.SHORT)
548
+				return new ConstantShortExpression(CodePosition.NATIVE, (short)annotation.value());
549
+			else if (type.type == BasicTypeID.USHORT)
550
+				return new ConstantUShortExpression(CodePosition.NATIVE, annotation.value());
551
+			else if (type.type == BasicTypeID.INT)
552
+				return new ConstantIntExpression(CodePosition.NATIVE, annotation.value());
553
+			else if (type.type == BasicTypeID.UINT)
554
+				return new ConstantUIntExpression(CodePosition.NATIVE, annotation.value());
555
+			else
556
+				throw new IllegalArgumentException("Cannot use int default values for " + type.toString());
557
+		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalLong.class)) {
558
+			ZenCodeType.OptionalLong annotation = parameter.getAnnotation(ZenCodeType.OptionalLong.class);
559
+			if (type.type == BasicTypeID.LONG)
560
+				return new ConstantLongExpression(CodePosition.NATIVE, annotation.value());
561
+			else if (type.type == BasicTypeID.ULONG)
562
+				return new ConstantULongExpression(CodePosition.NATIVE, annotation.value());
563
+			else
564
+				throw new IllegalArgumentException("Cannot use long default values for " + type.toString());
565
+		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalFloat.class)) {
566
+			ZenCodeType.OptionalFloat annotation = parameter.getAnnotation(ZenCodeType.OptionalFloat.class);
567
+			if (type.type == BasicTypeID.FLOAT)
568
+				return new ConstantFloatExpression(CodePosition.NATIVE, annotation.value());
569
+			else
570
+				throw new IllegalArgumentException("Cannot use float default values for " + type.toString());
571
+		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalDouble.class)) {
572
+			ZenCodeType.OptionalDouble annotation = parameter.getAnnotation(ZenCodeType.OptionalDouble.class);
573
+			if (type.type == BasicTypeID.DOUBLE)
574
+				return new ConstantDoubleExpression(CodePosition.NATIVE, annotation.value());
575
+			else
576
+				throw new IllegalArgumentException("Cannot use double default values for " + type.toString());
577
+		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalString.class)) {
578
+			ZenCodeType.OptionalString annotation = parameter.getAnnotation(ZenCodeType.OptionalString.class);
579
+			if (type.type == StringTypeID.INSTANCE) {
580
+				Expression result = new ConstantStringExpression(CodePosition.NATIVE, annotation.value());
581
+				if (type.getActualStorage() != StaticStorageTag.INSTANCE)
582
+					result = new StorageCastExpression(CodePosition.NATIVE, result, type);
583
+				return result;
584
+			} else {
585
+				throw new IllegalArgumentException("Cannot use string default values for " + type.toString());
586
+			}
587
+		} else {
588
+			return null;
589
+		}
590
+	}
591
+
515
 	private FunctionHeader getHeader(
592
 	private FunctionHeader getHeader(
516
 			TypeVariableContext context,
593
 			TypeVariableContext context,
517
 			AnnotatedType javaReturnType,
594
 			AnnotatedType javaReturnType,
518
-			AnnotatedType[] parameterTypes,
595
+			Parameter[] javaParameters,
519
 			TypeVariable<Method>[] javaTypeParameters,
596
 			TypeVariable<Method>[] javaTypeParameters,
520
 			AnnotatedType[] exceptionTypes) {
597
 			AnnotatedType[] exceptionTypes) {
521
 		StoredType returnType = javaReturnType == null ? BasicTypeID.VOID.stored : loadStoredType(context, javaReturnType);
598
 		StoredType returnType = javaReturnType == null ? BasicTypeID.VOID.stored : loadStoredType(context, javaReturnType);
522
 		
599
 		
523
-		FunctionParameter[] parameters = new FunctionParameter[parameterTypes.length];
600
+		FunctionParameter[] parameters = new FunctionParameter[javaParameters.length];
524
 		for (int i = 0; i < parameters.length; i++) {
601
 		for (int i = 0; i < parameters.length; i++) {
525
-			parameters[i] = new FunctionParameter(loadStoredType(context, parameterTypes[i]));
602
+			Parameter parameter = javaParameters[i];
603
+
604
+			AnnotatedType parameterType = parameter.getAnnotatedType();
605
+			StoredType type = loadStoredType(context, parameter.getAnnotatedType());
606
+			Expression defaultValue = getDefaultValue(parameter, type);
607
+			parameters[i] = new FunctionParameter(type, parameter.getName(), defaultValue, parameter.isVarArgs());
526
 		}
608
 		}
527
 		
609
 		
528
 		TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];
610
 		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