implement null type
This commit is contained in:
36
src/main/java/de/hsrm/compiler/Klang/types/NullType.java
Normal file
36
src/main/java/de/hsrm/compiler/Klang/types/NullType.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user