implement null type

This commit is contained in:
2020-02-14 14:10:48 +01:00
parent 9d83e5425d
commit 8ebbd6ae54
3 changed files with 47 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
package de.hsrm.compiler.Klang.types;
public class NullType extends Type {
private static NullType instance = null;
public static NullType getType() {
if (instance != null) {
return instance;
}
instance = new NullType();
return instance;
}
@Override
public String getName() {
return "naught";
}
@Override
public Type combine(Type that) {
// You can not combine null with a primitive type
if (that.isPrimitiveType()) {
throw new RuntimeException("Type missmatch: cannot combine " + this.getName() + " and " + that.getName());
}
// Everything else combines with null to the type it was before
return that;
}
@Override
public boolean isPrimitiveType() {
return false;
}
}

View File

@@ -19,6 +19,12 @@ public class StructType extends Type {
return this;
}
// If you combine a null type with a struct type, you
// always get the struct type back.
if (that == NullType.getType()) {
return this;
}
throw new RuntimeException("Type missmatch: cannot combine " + this.getName() + " and " + that.getName());
}

View File

@@ -16,11 +16,16 @@ public abstract class Type {
return FloatType.getType();
}
public static NullType getNullType() {
return NullType.getType();
}
public static Type getByName(String name) {
switch (name) {
case "bool": return getBooleanType();
case "int": return getIntegerType();
case "float": return getFloatType();
case "null": return getNullType();
default: return new StructType(name);
}
}