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,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 View File

@@ -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 View File

@@ -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 View File

@@ -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 View File

@@ -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 View File

@@ -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 View File

@@ -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 View File

@@ -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 View File

@@ -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 View File

@@ -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 View File

@@ -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 View File

@@ -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 View File

@@ -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 View File

@@ -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 View File

@@ -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
 	}

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

@@ -5,7 +5,15 @@
5 5
  */
6 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 17
 import java.util.Collections;
10 18
 import java.util.HashMap;
11 19
 import java.util.Map;
@@ -25,8 +33,21 @@ import org.openzen.zenscript.codemodel.definition.EnumDefinition;
25 33
 import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
26 34
 import org.openzen.zenscript.codemodel.definition.StructDefinition;
27 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 48
 import org.openzen.zenscript.codemodel.expression.ExpressionSymbol;
29 49
 import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
50
+import org.openzen.zenscript.codemodel.expression.StorageCastExpression;
30 51
 import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
31 52
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
32 53
 import org.openzen.zenscript.codemodel.member.CasterMember;
@@ -48,8 +69,8 @@ import org.openzen.zenscript.codemodel.type.TypeID;
48 69
 import org.openzen.zenscript.codemodel.type.member.BuiltinID;
49 70
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
50 71
 import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
72
+import org.openzen.zenscript.codemodel.type.storage.StaticStorageTag;
51 73
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
52
-import org.openzen.zenscript.javabytecode.compiler.CompilerUtils;
53 74
 import org.openzen.zenscript.javashared.JavaClass;
54 75
 import org.openzen.zenscript.javashared.JavaCompiledModule;
55 76
 import org.openzen.zenscript.javashared.JavaField;
@@ -319,7 +340,6 @@ public class JavaNativeModule {
319 340
 			final String fieldName = annotation.value().isEmpty() ? field.getName() : annotation.value();
320 341
 			
321 342
 			StoredType fieldType = loadStoredType(context, field.getAnnotatedType());
322
-			
323 343
 			FieldMember member = new FieldMember(CodePosition.NATIVE, definition, getMethodModifiers(field), fieldName, thisType, fieldType, registry, 0, 0, null);
324 344
 			definition.addMember(member);
325 345
 			compiled.setFieldInfo(member, new JavaField(javaClass, field.getName(), getDescriptor(field.getType())));
@@ -498,7 +518,7 @@ public class JavaNativeModule {
498 518
 		return getHeader(
499 519
 				context,
500 520
 				null,
501
-				constructor.getAnnotatedParameterTypes(),
521
+				constructor.getParameters(),
502 522
 				constructor.getTypeParameters(),
503 523
 				constructor.getAnnotatedExceptionTypes());
504 524
 	}
@@ -507,22 +527,84 @@ public class JavaNativeModule {
507 527
 		return getHeader(
508 528
 				context,
509 529
 				method.getAnnotatedReturnType(),
510
-				method.getAnnotatedParameterTypes(),
530
+				method.getParameters(),
511 531
 				method.getTypeParameters(),
512 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 592
 	private FunctionHeader getHeader(
516 593
 			TypeVariableContext context,
517 594
 			AnnotatedType javaReturnType,
518
-			AnnotatedType[] parameterTypes,
595
+			Parameter[] javaParameters,
519 596
 			TypeVariable<Method>[] javaTypeParameters,
520 597
 			AnnotatedType[] exceptionTypes) {
521 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 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 610
 		TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];

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

@@ -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 View File

@@ -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 View File


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

@@ -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 View File

@@ -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…
Cancel
Save