Skip to content

Commit a55c745

Browse files
small adjustments and test case
1 parent c383ba2 commit a55c745

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

types/src/main/java/com/compilerprogramming/ezlang/types/EZType.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,18 @@ public String toString() {
5252
}
5353
public String name() { return name; }
5454

55+
/**
56+
* Can we assign a value of other type to a var of this type?
57+
*/
5558
public boolean isAssignable(EZType other) {
5659
if (other == null || other instanceof EZTypeVoid || other instanceof EZTypeUnknown)
5760
return false;
5861
if (this == other || equals(other)) return true;
5962
if (this instanceof EZTypeNullable nullable) {
63+
// if this is Nullable and other is null then okay
6064
if (other instanceof EZTypeNull)
6165
return true;
66+
// if this is Nullable and other is compatible with base type then okay
6267
return nullable.baseType.isAssignable(other);
6368
}
6469
else if (other instanceof EZTypeNullable nullable) {

types/src/main/java/com/compilerprogramming/ezlang/types/TypeDictionary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.compilerprogramming.ezlang.exceptions.CompilerException;
44

5-
public class TypeDictionary extends Scope {
5+
public final class TypeDictionary extends Scope {
66
public final EZType.EZTypeUnknown UNKNOWN;
77
public final EZType.EZTypeInteger INT;
88
public final EZType.EZTypeNull NULL;

types/src/test/java/com/compilerprogramming/ezlang/types/TestTypes.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.compilerprogramming.ezlang.types;
22

3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
36
public class TestTypes {
47

58
EZType buildStruct1(TypeDictionary typeDictionary) {
@@ -18,4 +21,34 @@ EZType buildStruct2(TypeDictionary typeDictionary) {
1821
return typeDictionary.intern(s);
1922
}
2023

24+
@Test
25+
public void testTypes() {
26+
var typeDict = new TypeDictionary();
27+
28+
var tint = typeDict.INT;
29+
Assert.assertTrue(tint.isPrimitive());
30+
31+
var s1 = buildStruct1(typeDict);
32+
var s2 = buildStruct2(typeDict);
33+
34+
var s1Type = typeDict.localLookup("S1").type;
35+
var s2Type = typeDict.localLookup("S2").type;
36+
37+
Assert.assertTrue(s1.isAssignable(s1Type));
38+
Assert.assertFalse(tint.isAssignable(s1Type));
39+
Assert.assertFalse(s1.isAssignable(s2Type));
40+
41+
Assert.assertTrue(s2.isAssignable(s2Type));
42+
Assert.assertFalse(s2.isAssignable(s1Type));
43+
44+
var s1TypeDup = buildStruct1(typeDict);
45+
Assert.assertSame(s1Type, s1TypeDup);
46+
47+
var nullType = typeDict.NULL;
48+
var nullableS1Type = typeDict.merge(s1Type,nullType);
49+
Assert.assertTrue(nullableS1Type.isAssignable(nullType));
50+
Assert.assertTrue(nullableS1Type.isAssignable(s1Type));
51+
Assert.assertFalse(nullableS1Type.isAssignable(s2Type));
52+
}
53+
2154
}

0 commit comments

Comments
 (0)