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
 	public final TypeID[] typeArguments;
29
 	public final TypeID[] typeArguments;
30
 	public final DefinitionTypeID outer;
30
 	public final DefinitionTypeID outer;
31
 	private TypeID normalized;
31
 	private TypeID normalized;
32
-	
33
-	public TypeID superType;
34
-	
32
+
35
 	public DefinitionTypeID(GlobalTypeRegistry typeRegistry, HighLevelDefinition definition, TypeID[] typeArguments) {
33
 	public DefinitionTypeID(GlobalTypeRegistry typeRegistry, HighLevelDefinition definition, TypeID[] typeArguments) {
36
 		this(typeRegistry, definition, typeArguments, null);
34
 		this(typeRegistry, definition, typeArguments, null);
37
 	}
35
 	}
50
 		this.definition = definition;
48
 		this.definition = definition;
51
 		this.typeArguments = typeArguments;
49
 		this.typeArguments = typeArguments;
52
 		this.outer = outer;
50
 		this.outer = outer;
53
-		
51
+
54
 		normalized = isDenormalized() ? normalize(typeRegistry) : this;
52
 		normalized = isDenormalized() ? normalize(typeRegistry) : this;
55
 		if (normalized instanceof DefinitionTypeID && ((DefinitionTypeID)normalized).isDenormalized())
53
 		if (normalized instanceof DefinitionTypeID && ((DefinitionTypeID)normalized).isDenormalized())
56
 			throw new AssertionError();
54
 			throw new AssertionError();
63
 		for (TypeID typeArgument : typeArguments)
61
 		for (TypeID typeArgument : typeArguments)
64
 			if (!typeArgument.getNormalized().equals(typeArgument))
62
 			if (!typeArgument.getNormalized().equals(typeArgument))
65
 				return true;
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
 	private TypeID normalize(GlobalTypeRegistry typeRegistry) {
68
 	private TypeID normalize(GlobalTypeRegistry typeRegistry) {
114
 	public DefinitionTypeID(HighLevelDefinition definition) {
110
 	public DefinitionTypeID(HighLevelDefinition definition) {
115
 		this.definition = definition;
111
 		this.definition = definition;
116
 		this.typeArguments = TypeID.NONE;
112
 		this.typeArguments = TypeID.NONE;
117
-		this.superType = definition.getSuperType();
118
 		this.outer = null;
113
 		this.outer = null;
119
 	}
114
 	}
120
 	
115
 	
185
 				if (typeArgument.hasInferenceBlockingTypeParameters(parameters))
180
 				if (typeArgument.hasInferenceBlockingTypeParameters(parameters))
186
 					return true;
181
 					return true;
187
 		}
182
 		}
188
-		
183
+
184
+		TypeID superType = definition.getSuperType();
189
 		return superType != null && superType.hasInferenceBlockingTypeParameters(parameters);
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
 import java.util.List;
9
 import java.util.List;
10
 import java.util.Map;
10
 import java.util.Map;
11
 import java.util.Set;
11
 import java.util.Set;
12
+
13
+import org.openzen.zencode.shared.CodePosition;
12
 import org.openzen.zenscript.codemodel.GenericMapper;
14
 import org.openzen.zenscript.codemodel.GenericMapper;
13
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
15
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
14
 import org.openzen.zenscript.codemodel.expression.Expression;
16
 import org.openzen.zenscript.codemodel.expression.Expression;
89
 	default boolean isDefinition(HighLevelDefinition definition) {
91
 	default boolean isDefinition(HighLevelDefinition definition) {
90
 		return false;
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
 				cache.get(baseType.instance(mapper)).copyMembersTo(members, TypeMemberPriority.INHERITED);
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
 		} else {
523
 		} else {
527
 			getter(definition, OBJECT_HASHCODE, "objectHashCode", INT);
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
 			throw new IllegalArgumentException("Cannot cast to undetermined type!");
423
 			throw new IllegalArgumentException("Cannot cast to undetermined type!");
424
 		if (type == BasicTypeID.NULL && toType.isOptional())
424
 		if (type == BasicTypeID.NULL && toType.isOptional())
425
 			return true;
425
 			return true;
426
+		if (type.canCastImplicit(toType))
427
+			return true;
426
 		
428
 		
427
 		if (toType.isOptional() && canCastImplicit(toType.withoutOptional()))
429
 		if (toType.isOptional() && canCastImplicit(toType.withoutOptional()))
428
 			return true;
430
 			return true;
466
 		toType = toType.getNormalized();
468
 		toType = toType.getNormalized();
467
 		if (canCastImplicit(toType))
469
 		if (canCastImplicit(toType))
468
 			return true;
470
 			return true;
471
+		if (type.canCastExplicit(toType))
472
+			return true;
469
 		
473
 		
470
 		for (TypeMember<CasterMemberRef> caster : casters) {
474
 		for (TypeMember<CasterMemberRef> caster : casters) {
471
 			if (caster.member.toType == toType)
475
 			if (caster.member.toType == toType)
552
 			return value;
556
 			return value;
553
 		if (type == toType)
557
 		if (type == toType)
554
 			return value;
558
 			return value;
559
+		if (type.canCastImplicit(toType))
560
+			return type.castImplicit(position, toType, value);
555
 
561
 
556
 		if (type == BasicTypeID.NULL && toType.isOptional())
562
 		if (type == BasicTypeID.NULL && toType.isOptional())
557
 			return new NullExpression(position, toType);
563
 			return new NullExpression(position, toType);
578
 		toType = toType.getNormalized();
584
 		toType = toType.getNormalized();
579
 		if (this.canCastImplicit(toType))
585
 		if (this.canCastImplicit(toType))
580
 			return castImplicit(position, value, toType, false);
586
 			return castImplicit(position, value, toType, false);
587
+		if (type.canCastExplicit(toType))
588
+			return type.castExplicit(position, toType, value);
581
 		
589
 		
582
         final TypeMembers typeMembers = cache.get(type);
590
         final TypeMembers typeMembers = cache.get(type);
583
         if(this.type != typeMembers.type && typeMembers.canCast(toType)) {
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
 package org.openzen.zenscript.javashared.types;
1
 package org.openzen.zenscript.javashared.types;
2
 
2
 
3
+import org.openzen.zencode.shared.CodePosition;
3
 import org.openzen.zenscript.codemodel.FunctionHeader;
4
 import org.openzen.zenscript.codemodel.FunctionHeader;
5
+import org.openzen.zenscript.codemodel.expression.Expression;
4
 import org.openzen.zenscript.codemodel.type.FunctionTypeID;
6
 import org.openzen.zenscript.codemodel.type.FunctionTypeID;
5
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
7
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
8
+import org.openzen.zenscript.codemodel.type.TypeID;
6
 import org.openzen.zenscript.javashared.JavaMethod;
9
 import org.openzen.zenscript.javashared.JavaMethod;
10
+import org.openzen.zenscript.javashared.expressions.JavaFunctionInterfaceCastExpression;
7
 
11
 
8
 import java.lang.reflect.Method;
12
 import java.lang.reflect.Method;
9
 
13
 
17
         this.functionalInterfaceMethod = functionalInterfaceMethod;
21
         this.functionalInterfaceMethod = functionalInterfaceMethod;
18
         this.method = method;
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
 
5
 
6
 public interface ParserLogger extends IZSLogger, CompileExceptionLogger {
6
 public interface ParserLogger extends IZSLogger, CompileExceptionLogger {
7
     default void logParseException(ParseException exception) {
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
  * @author Hoofdgebruiker
25
  * @author Hoofdgebruiker
26
  */
26
  */
27
 public interface IParsedType {
27
 public interface IParsedType {
28
-	public static IParsedType parse(ZSTokenParser tokens) throws ParseException {
28
+	static IParsedType parse(ZSTokenParser tokens) throws ParseException {
29
 		IParsedType result = tryParse(tokens);
29
 		IParsedType result = tryParse(tokens);
30
 		if (result == null)
30
 		if (result == null)
31
 			throw new ParseException(tokens.getPosition(), "Type expected (got " + tokens.peek().content + ")");
31
 			throw new ParseException(tokens.getPosition(), "Type expected (got " + tokens.peek().content + ")");
33
 		return result;
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
 		if (!tokens.isNext(T_LESS))
37
 		if (!tokens.isNext(T_LESS))
38
 			return null;
38
 			return null;
39
 		
39
 		
67
 		return genericParameters;
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
 		List<IParsedType> typeArguments = null;
71
 		List<IParsedType> typeArguments = null;
72
 		if (tokens.optional(ZSTokenType.T_LESS) != null) {
72
 		if (tokens.optional(ZSTokenType.T_LESS) != null) {
73
 			try {
73
 			try {
85
 		return typeArguments;
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
 		CodePosition position = tokens.getPosition();
89
 		CodePosition position = tokens.getPosition();
90
 		
90
 		
91
 		IParsedType result;
91
 		IParsedType result;
211
 		return result;
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
 		TypeID[] result = TypeID.NONE;
215
 		TypeID[] result = TypeID.NONE;
216
 		if (typeParameters != null) {
216
 		if (typeParameters != null) {
217
 			result = new TypeID[typeParameters.size()];
217
 			result = new TypeID[typeParameters.size()];
222
 		return result;
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
 		TypeID[] result = TypeID.NONE;
226
 		TypeID[] result = TypeID.NONE;
227
 		if (typeParameters != null && typeParameters.size() > 0) {
227
 		if (typeParameters != null && typeParameters.size() > 0) {
228
 			result = new TypeID[typeParameters.size()];
228
 			result = new TypeID[typeParameters.size()];
233
 		return result;
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
 		return null;
239
 		return null;
240
 	}
240
 	}
241
 	
241
 	
242
-	public default TypeID[] compileTypeArguments(BaseScope scope) {
242
+	default TypeID[] compileTypeArguments(BaseScope scope) {
243
 		return TypeID.NONE;
243
 		return TypeID.NONE;
244
 	}
244
 	}
245
 }
245
 }

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


Loading…
Cancel
Save