Browse Source

WIP fixing stdlibs and compilation on the new version

Added functional interface types (Java)
Stan Hebben 4 years ago
parent
commit
6945ba67be

+ 6
- 10
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/DefinitionTypeID.java View File

@@ -29,9 +29,7 @@ public class DefinitionTypeID implements TypeID {
29 29
 	public final TypeID[] typeArguments;
30 30
 	public final DefinitionTypeID outer;
31 31
 	private TypeID normalized;
32
-	
33
-	public TypeID superType;
34
-	
32
+
35 33
 	public DefinitionTypeID(GlobalTypeRegistry typeRegistry, HighLevelDefinition definition, TypeID[] typeArguments) {
36 34
 		this(typeRegistry, definition, typeArguments, null);
37 35
 	}
@@ -50,7 +48,7 @@ public class DefinitionTypeID implements TypeID {
50 48
 		this.definition = definition;
51 49
 		this.typeArguments = typeArguments;
52 50
 		this.outer = outer;
53
-		
51
+
54 52
 		normalized = isDenormalized() ? normalize(typeRegistry) : this;
55 53
 		if (normalized instanceof DefinitionTypeID && ((DefinitionTypeID)normalized).isDenormalized())
56 54
 			throw new AssertionError();
@@ -63,10 +61,8 @@ public class DefinitionTypeID implements TypeID {
63 61
 		for (TypeID typeArgument : typeArguments)
64 62
 			if (!typeArgument.getNormalized().equals(typeArgument))
65 63
 				return true;
66
-		if (outer != null && !outer.getNormalized().equals(outer))
67
-			return true;
68
-		
69
-		return false;
64
+
65
+		return outer != null && !outer.getNormalized().equals(outer);
70 66
 	}
71 67
 	
72 68
 	private TypeID normalize(GlobalTypeRegistry typeRegistry) {
@@ -114,7 +110,6 @@ public class DefinitionTypeID implements TypeID {
114 110
 	public DefinitionTypeID(HighLevelDefinition definition) {
115 111
 		this.definition = definition;
116 112
 		this.typeArguments = TypeID.NONE;
117
-		this.superType = definition.getSuperType();
118 113
 		this.outer = null;
119 114
 	}
120 115
 	
@@ -185,7 +180,8 @@ public class DefinitionTypeID implements TypeID {
185 180
 				if (typeArgument.hasInferenceBlockingTypeParameters(parameters))
186 181
 					return true;
187 182
 		}
188
-		
183
+
184
+		TypeID superType = definition.getSuperType();
189 185
 		return superType != null && superType.hasInferenceBlockingTypeParameters(parameters);
190 186
 	}
191 187
 

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

@@ -9,6 +9,8 @@ import java.util.HashMap;
9 9
 import java.util.List;
10 10
 import java.util.Map;
11 11
 import java.util.Set;
12
+
13
+import org.openzen.zencode.shared.CodePosition;
12 14
 import org.openzen.zenscript.codemodel.GenericMapper;
13 15
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
14 16
 import org.openzen.zenscript.codemodel.expression.Expression;
@@ -89,4 +91,12 @@ public interface TypeID {
89 91
 	default boolean isDefinition(HighLevelDefinition definition) {
90 92
 		return false;
91 93
 	}
94
+
95
+	default boolean canCastImplicit(TypeID other) { return false; }
96
+
97
+	default boolean canCastExplicit(TypeID other) { return false; }
98
+
99
+	default Expression castImplicit(CodePosition position, TypeID other, Expression value) { return null; }
100
+
101
+	default Expression castExplicit(CodePosition position, TypeID other, Expression value) { return null; }
92 102
 }

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

@@ -518,11 +518,8 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
518 518
 				cache.get(baseType.instance(mapper)).copyMembersTo(members, TypeMemberPriority.INHERITED);
519 519
 		}
520 520
 		
521
-		if (definitionType.superType != null) {
522
-			cache.get(definitionType.superType).copyMembersTo(members, TypeMemberPriority.INHERITED);
523
-		} else if(definition.getSuperType() != null) {
524
-			cache.get(definition.getSuperType())
525
-					.copyMembersTo(members, TypeMemberPriority.INHERITED);
521
+		if (definition.getSuperType() != null) {
522
+			cache.get(definition.getSuperType()).copyMembersTo(members, TypeMemberPriority.INHERITED);
526 523
 		} else {
527 524
 			getter(definition, OBJECT_HASHCODE, "objectHashCode", INT);
528 525
 		}

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

@@ -423,6 +423,8 @@ public final class TypeMembers {
423 423
 			throw new IllegalArgumentException("Cannot cast to undetermined type!");
424 424
 		if (type == BasicTypeID.NULL && toType.isOptional())
425 425
 			return true;
426
+		if (type.canCastImplicit(toType))
427
+			return true;
426 428
 		
427 429
 		if (toType.isOptional() && canCastImplicit(toType.withoutOptional()))
428 430
 			return true;
@@ -466,6 +468,8 @@ public final class TypeMembers {
466 468
 		toType = toType.getNormalized();
467 469
 		if (canCastImplicit(toType))
468 470
 			return true;
471
+		if (type.canCastExplicit(toType))
472
+			return true;
469 473
 		
470 474
 		for (TypeMember<CasterMemberRef> caster : casters) {
471 475
 			if (caster.member.toType == toType)
@@ -552,6 +556,8 @@ public final class TypeMembers {
552 556
 			return value;
553 557
 		if (type == toType)
554 558
 			return value;
559
+		if (type.canCastImplicit(toType))
560
+			return type.castImplicit(position, toType, value);
555 561
 
556 562
 		if (type == BasicTypeID.NULL && toType.isOptional())
557 563
 			return new NullExpression(position, toType);
@@ -578,6 +584,8 @@ public final class TypeMembers {
578 584
 		toType = toType.getNormalized();
579 585
 		if (this.canCastImplicit(toType))
580 586
 			return castImplicit(position, value, toType, false);
587
+		if (type.canCastExplicit(toType))
588
+			return type.castExplicit(position, toType, value);
581 589
 		
582 590
         final TypeMembers typeMembers = cache.get(type);
583 591
         if(this.type != typeMembers.type && typeMembers.canCast(toType)) {

+ 22
- 0
JavaShared/src/main/java/org/openzen/zenscript/javashared/types/JavaFunctionalInterfaceTypeID.java View File

@@ -1,9 +1,13 @@
1 1
 package org.openzen.zenscript.javashared.types;
2 2
 
3
+import org.openzen.zencode.shared.CodePosition;
3 4
 import org.openzen.zenscript.codemodel.FunctionHeader;
5
+import org.openzen.zenscript.codemodel.expression.Expression;
4 6
 import org.openzen.zenscript.codemodel.type.FunctionTypeID;
5 7
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
8
+import org.openzen.zenscript.codemodel.type.TypeID;
6 9
 import org.openzen.zenscript.javashared.JavaMethod;
10
+import org.openzen.zenscript.javashared.expressions.JavaFunctionInterfaceCastExpression;
7 11
 
8 12
 import java.lang.reflect.Method;
9 13
 
@@ -17,4 +21,22 @@ public class JavaFunctionalInterfaceTypeID extends FunctionTypeID {
17 21
         this.functionalInterfaceMethod = functionalInterfaceMethod;
18 22
         this.method = method;
19 23
     }
24
+
25
+    @Override
26
+    public Expression castImplicit(CodePosition position, TypeID other, Expression value) {
27
+        if (other instanceof JavaFunctionalInterfaceTypeID) {
28
+            JavaFunctionalInterfaceTypeID otherType = (JavaFunctionalInterfaceTypeID)other;
29
+            if (header.isEquivalentTo(otherType.header))
30
+                return new JavaFunctionInterfaceCastExpression(position, otherType, value);
31
+
32
+            return null;
33
+        } else {
34
+            return null;
35
+        }
36
+    }
37
+
38
+    @Override
39
+    public Expression castExplicit(CodePosition position, TypeID other, Expression value) {
40
+        return this.castImplicit(position, other, value);
41
+    }
20 42
 }

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/parser/logger/ParserLogger.java View File

@@ -5,6 +5,6 @@ import org.openzen.zenscript.lexer.*;
5 5
 
6 6
 public interface ParserLogger extends IZSLogger, CompileExceptionLogger {
7 7
     default void logParseException(ParseException exception) {
8
-        throwingErr("Parser Exeption", exception);
8
+        throwingErr("Parser Exception @ " + exception.position.toString() + " : " + exception.message, exception);
9 9
     }
10 10
 }

+ 9
- 9
Parser/src/main/java/org/openzen/zenscript/parser/type/IParsedType.java View File

@@ -25,7 +25,7 @@ import org.openzen.zenscript.codemodel.type.TypeID;
25 25
  * @author Hoofdgebruiker
26 26
  */
27 27
 public interface IParsedType {
28
-	public static IParsedType parse(ZSTokenParser tokens) throws ParseException {
28
+	static IParsedType parse(ZSTokenParser tokens) throws ParseException {
29 29
 		IParsedType result = tryParse(tokens);
30 30
 		if (result == null)
31 31
 			throw new ParseException(tokens.getPosition(), "Type expected (got " + tokens.peek().content + ")");
@@ -33,7 +33,7 @@ public interface IParsedType {
33 33
 		return result;
34 34
 	}
35 35
 	
36
-	public static List<IParsedType> parseTypeArguments(ZSTokenParser tokens) throws ParseException {
36
+	static List<IParsedType> parseTypeArguments(ZSTokenParser tokens) throws ParseException {
37 37
 		if (!tokens.isNext(T_LESS))
38 38
 			return null;
39 39
 		
@@ -67,7 +67,7 @@ public interface IParsedType {
67 67
 		return genericParameters;
68 68
 	}
69 69
 	
70
-	public static List<IParsedType> parseTypeArgumentsForCall(ZSTokenParser tokens) throws ParseException {
70
+	static List<IParsedType> parseTypeArgumentsForCall(ZSTokenParser tokens) throws ParseException {
71 71
 		List<IParsedType> typeArguments = null;
72 72
 		if (tokens.optional(ZSTokenType.T_LESS) != null) {
73 73
 			try {
@@ -85,7 +85,7 @@ public interface IParsedType {
85 85
 		return typeArguments;
86 86
 	}
87 87
 	
88
-	public static IParsedType tryParse(ZSTokenParser tokens) throws ParseException {
88
+	static IParsedType tryParse(ZSTokenParser tokens) throws ParseException {
89 89
 		CodePosition position = tokens.getPosition();
90 90
 		
91 91
 		IParsedType result;
@@ -211,7 +211,7 @@ public interface IParsedType {
211 211
 		return result;
212 212
 	}
213 213
 	
214
-	public static TypeID[] compileList(List<IParsedType> typeParameters, TypeResolutionContext context) {
214
+	static TypeID[] compileList(List<IParsedType> typeParameters, TypeResolutionContext context) {
215 215
 		TypeID[] result = TypeID.NONE;
216 216
 		if (typeParameters != null) {
217 217
 			result = new TypeID[typeParameters.size()];
@@ -222,7 +222,7 @@ public interface IParsedType {
222 222
 		return result;
223 223
 	}
224 224
 	
225
-	public static TypeID[] compileTypes(List<IParsedType> typeParameters, TypeResolutionContext context) {
225
+	static TypeID[] compileTypes(List<IParsedType> typeParameters, TypeResolutionContext context) {
226 226
 		TypeID[] result = TypeID.NONE;
227 227
 		if (typeParameters != null && typeParameters.size() > 0) {
228 228
 			result = new TypeID[typeParameters.size()];
@@ -233,13 +233,13 @@ public interface IParsedType {
233 233
 		return result;
234 234
 	}
235 235
 	
236
-	public TypeID compile(TypeResolutionContext context);
236
+	TypeID compile(TypeResolutionContext context);
237 237
 	
238
-	public default AnnotationDefinition compileAnnotation(BaseScope scope) {
238
+	default AnnotationDefinition compileAnnotation(BaseScope scope) {
239 239
 		return null;
240 240
 	}
241 241
 	
242
-	public default TypeID[] compileTypeArguments(BaseScope scope) {
242
+	default TypeID[] compileTypeArguments(BaseScope scope) {
243 243
 		return TypeID.NONE;
244 244
 	}
245 245
 }

BIN
ScriptingExample/src/main/resources/StdLibs.jar View File


Loading…
Cancel
Save