slcom/slangc/model/BuiltinTypeBehaviour.sauce

67 lines
2.0 KiB
Plaintext

package slangc.model;
public class BuiltinTypeBehaviour {
public boolean isAssignableFrom(TypeModel otherType) {
// TODO Auto-generated method stub
return false;
}
/** If it waddles like a duck and quacks like a duck then it's a duck. */
public static class DuckBehaviour extends BuiltinTypeBehaviour {
@Override
public boolean isAssignableFrom(TypeModel otherType) {
return true;
}
}
public static class NumberBehaviour extends BuiltinTypeBehaviour {
private boolean isFloat;
private boolean isSigned;
private int numberOfBits;
public NumberBehaviour(boolean isFloat, boolean isSigned, int numberOfBits) {
super();
this.isFloat = isFloat;
this.isSigned = isSigned;
this.numberOfBits = numberOfBits;
}
@Override
public boolean isAssignableFrom(TypeModel otherType) {
if (otherType instanceof BuiltinTypeModel) {
BuiltinTypeModel otherBuiltin = (BuiltinTypeModel) otherType;
BuiltinTypeBehaviour otherBehaviour = otherBuiltin.getBuiltinTypeBehaviour();
if (otherBehaviour instanceof NumberBehaviour) {
return isAssignableFrom((NumberBehaviour)otherBehaviour);
} else if (otherBehaviour instanceof DuckBehaviour) {
return true;
} else {
return false;
}
} else {
return false;
}
}
private boolean isAssignableFrom(NumberBehaviour otherBehaviour) {
// Handle floats first
if (this.isFloat && !otherBehaviour.isFloat) {
return true;
} else if (!this.isFloat && otherBehaviour.isFloat) {
return false;
} else if (this.isFloat && otherBehaviour.isFloat) {
return this.numberOfBits >= otherBehaviour.numberOfBits;
}
// Otherwise we know it's an integer
if (this.numberOfBits == otherBehaviour.numberOfBits && this.isSigned == otherBehaviour.isSigned) {
return true;
} else if (this.numberOfBits > otherBehaviour.numberOfBits) {
return true;
} else {
return false;
}
}
}
}