Browse Source

Added castValueTo method to Type classes.

git-svn-id: https://www.prismmodelchecker.org/svn/prism/prism/trunk@3288 bbc10eb1-c90d-0410-af57-cb519fbb1720
master
Dave Parker 15 years ago
parent
commit
af6eef0c49
  1. 19
      prism/src/parser/type/Type.java
  2. 12
      prism/src/parser/type/TypeBool.java
  3. 14
      prism/src/parser/type/TypeClock.java
  4. 14
      prism/src/parser/type/TypeDouble.java
  5. 12
      prism/src/parser/type/TypeInt.java

19
prism/src/parser/type/Type.java

@ -26,15 +26,34 @@
package parser.type;
import prism.PrismLangException;
public abstract class Type
{
public abstract String getTypeString();
/**
* Returns true iff a variable of this type can be assigned a value that is of type {@code type}.
*/
public boolean canAssign(Type type)
{
// Play safe: assume not possible, unless explicitly overridden.
return false;
}
/**
* Make sure that a value, stored as an Object (Integer, Boolean, etc.) is of this type.
* Basically, implement some implicit casts, e.g. Integer -> Double.
* This should only only work for combinations of types that satisfy {@code #canAssign(Type)}.
* If not, an exception is thrown (but such problems should have been caught earlier by type checking)
*/
public Object castValueTo(Object value) throws PrismLangException
{
// Play safe: assume error unless explicitly overridden.
throw new PrismLangException("Cannot cast a value to type " + getTypeString());
}
@Override
public String toString()
{
return getTypeString();

12
prism/src/parser/type/TypeBool.java

@ -26,6 +26,8 @@
package parser.type;
import prism.PrismLangException;
public class TypeBool extends Type
{
private static TypeBool singleton;
@ -54,8 +56,18 @@ public class TypeBool extends Type
return singleton;
}
@Override
public boolean canAssign(Type type)
{
return (type instanceof TypeBool);
}
@Override
public Object castValueTo(Object value) throws PrismLangException
{
if (value instanceof Integer)
return value;
else
throw new PrismLangException("Can't convert " + value.getClass() + " to type " + getTypeString());
}
}

14
prism/src/parser/type/TypeClock.java

@ -26,6 +26,8 @@
package parser.type;
import prism.PrismLangException;
public class TypeClock extends Type
{
private static TypeClock singleton;
@ -54,8 +56,20 @@ public class TypeClock extends Type
return singleton;
}
@Override
public boolean canAssign(Type type)
{
return (type instanceof TypeClock);
}
@Override
public Object castValueTo(Object value) throws PrismLangException
{
if (value instanceof Double)
return value;
if (value instanceof Integer)
return new Double(((Double) value).doubleValue());
else
throw new PrismLangException("Can't convert " + value.getClass() + " to type " + getTypeString());
}
}

14
prism/src/parser/type/TypeDouble.java

@ -26,6 +26,8 @@
package parser.type;
import prism.PrismLangException;
public class TypeDouble extends Type
{
private static TypeDouble singleton;
@ -54,8 +56,20 @@ public class TypeDouble extends Type
return singleton;
}
@Override
public boolean canAssign(Type type)
{
return (type instanceof TypeDouble || type instanceof TypeInt);
}
@Override
public Object castValueTo(Object value) throws PrismLangException
{
if (value instanceof Double)
return value;
if (value instanceof Integer)
return new Double(((Double) value).doubleValue());
else
throw new PrismLangException("Can't convert " + value.getClass() + " to type " + getTypeString());
}
}

12
prism/src/parser/type/TypeInt.java

@ -26,6 +26,8 @@
package parser.type;
import prism.PrismLangException;
public class TypeInt extends Type
{
private static TypeInt singleton;
@ -54,8 +56,18 @@ public class TypeInt extends Type
return singleton;
}
@Override
public boolean canAssign(Type type)
{
return (type instanceof TypeInt);
}
@Override
public Object castValueTo(Object value) throws PrismLangException
{
if (value instanceof Integer)
return value;
else
throw new PrismLangException("Can't convert " + value.getClass() + " to type " + getTypeString());
}
}
Loading…
Cancel
Save