Browse Source

Added Typemember check without storage tags.

Only WIP, tries to check the same type without storage tags and see if it can cast then.
kindlich 5 years ago
parent
commit
696aca1cfd
No known key found for this signature in database

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

@@ -0,0 +1,70 @@
1
+package org.openzen.zenscript.codemodel.type.member;
2
+
3
+import org.openzen.zenscript.codemodel.type.*;
4
+
5
+import java.util.Arrays;
6
+
7
+public class TagRemovingTypeVisitor implements TypeVisitor<StoredType> {
8
+    
9
+    private final LocalMemberCache cache;
10
+    
11
+    public TagRemovingTypeVisitor(LocalMemberCache cache) {
12
+        
13
+        this.cache = cache;
14
+    }
15
+    
16
+    @Override
17
+    public StoredType visitBasic(BasicTypeID basic) {
18
+        return basic.stored;
19
+    }
20
+    
21
+    @Override
22
+    public StoredType visitString(StringTypeID string) {
23
+        return string.stored();
24
+    }
25
+    
26
+    @Override
27
+    public StoredType visitArray(ArrayTypeID array) {
28
+        return new ArrayTypeID(cache.getRegistry(), array.elementType.type.accept(this), array.dimension).stored();
29
+    }
30
+    
31
+    @Override
32
+    public StoredType visitAssoc(AssocTypeID assoc) {
33
+        return new AssocTypeID(cache.getRegistry(), assoc.keyType.type.accept(this), assoc.valueType.type.accept(this)).stored();
34
+    }
35
+    
36
+    @Override
37
+    public StoredType visitGenericMap(GenericMapTypeID map) {
38
+        return new GenericMapTypeID(cache.getRegistry(), map.value.type.accept(this), map.key).stored();
39
+    }
40
+    
41
+    @Override
42
+    public StoredType visitIterator(IteratorTypeID iterator) {
43
+        return new IteratorTypeID(cache.getRegistry(), Arrays.stream(iterator.iteratorTypes).map(storedType -> storedType.type).map(typeID -> typeID.accept(this)).toArray(StoredType[]::new)).stored();
44
+    }
45
+    
46
+    @Override
47
+    public StoredType visitFunction(FunctionTypeID function) {
48
+        return function.stored();
49
+    }
50
+    
51
+    @Override
52
+    public StoredType visitDefinition(DefinitionTypeID definition) {
53
+        return definition.stored();
54
+    }
55
+    
56
+    @Override
57
+    public StoredType visitGeneric(GenericTypeID generic) {
58
+        return generic.stored();
59
+    }
60
+    
61
+    @Override
62
+    public StoredType visitRange(RangeTypeID range) {
63
+        return new RangeTypeID(cache.getRegistry(), range.baseType.type.accept(this)).stored;
64
+    }
65
+    
66
+    @Override
67
+    public StoredType visitOptional(OptionalTypeID type) {
68
+        return new OptionalTypeID(cache.getRegistry(), type.baseType.accept(this).type).stored();
69
+    }
70
+}

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

@@ -411,8 +411,15 @@ public final class TypeMembers {
411 411
 			return true;
412 412
 		if (type.isOptional() && areEquivalent(type.withoutOptional(), toType))
413 413
 			return true;
414
+   
415
+		if( getImplicitCaster(toType) != null || extendsOrImplements(toType.type))
416
+		    return true;
414 417
 		
415
-		return getImplicitCaster(toType) != null || extendsOrImplements(toType.type);
418
+        final StoredType accept = type.type.accept(new TagRemovingTypeVisitor(cache));
419
+        if(!this.type.type.equals(accept.type) && cache.get(accept).canCastImplicit(toType)){
420
+            return true;
421
+        }
422
+        return false;
416 423
 	}
417 424
 	
418 425
 	private boolean areEquivalent(StoredType fromType, StoredType toType) {
@@ -571,7 +578,12 @@ public final class TypeMembers {
571 578
 		if (this.canCastImplicit(toType))
572 579
 			return castImplicit(position, value, toType, false);
573 580
 		
574
-		for (TypeMember<CasterMemberRef> caster : casters)
581
+        final TypeMembers typeMembers = cache.get(type.type.accept(new TagRemovingTypeVisitor(cache)));
582
+        if(this.type.type != typeMembers.type.type && typeMembers.canCast(toType)) {
583
+            return typeMembers.castExplicit(position, value, toType, optional);
584
+        }
585
+        
586
+        for (TypeMember<CasterMemberRef> caster : casters)
575 587
 			if (areEquivalent(caster.member.toType, toType))
576 588
 				return castEquivalent(position, caster.member.cast(position, value, false), toType);
577 589
 		

+ 10
- 10
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionMap.java View File

@@ -81,16 +81,16 @@ public class ParsedExpressionMap extends ParsedExpression {
81 81
 			return new NewExpression(position, usedHint, constructor, CallArguments.EMPTY);
82 82
 		}
83 83
 		
84
-		if (!hasAssocHint && scope.hints.size() == 1) {
85
-			// compile as constructor call
86
-			StoredType hint = scope.hints.get(0);
87
-			for (int i = 0; i < keys.size(); i++) {
88
-				if (keys.get(i) != null)
89
-					throw new CompileException(position, CompileExceptionCode.UNSUPPORTED_NAMED_ARGUMENTS, "Named constructor arguments not yet supported");
90
-			}
91
-			ParsedCallArguments arguments = new ParsedCallArguments(null, values);
92
-			return ParsedNewExpression.compile(position, hint, arguments, scope);
93
-		}
84
+		//if (!hasAssocHint && scope.hints.size() == 1) {
85
+		//	// compile as constructor call
86
+		//	StoredType hint = scope.hints.get(0);
87
+		//	for (int i = 0; i < keys.size(); i++) {
88
+		//		if (keys.get(i) != null)
89
+		//			throw new CompileException(position, CompileExceptionCode.UNSUPPORTED_NAMED_ARGUMENTS, "Named constructor arguments not yet supported");
90
+		//	}
91
+		//	ParsedCallArguments arguments = new ParsedCallArguments(null, values);
92
+		//	return ParsedNewExpression.compile(position, hint, arguments, scope);
93
+		//}
94 94
 		
95 95
 		Expression[] cKeys = new Expression[keys.size()];
96 96
 		Expression[] cValues = new Expression[values.size()];

Loading…
Cancel
Save