Browse Source

Utility method Expression.evaluateObjectAsInt.

accumulation-v4.7
Dave Parker 8 years ago
parent
commit
0bbeebb7da
  1. 24
      prism/src/parser/ast/Expression.java

24
prism/src/parser/ast/Expression.java

@ -251,14 +251,7 @@ public abstract class Expression extends ASTElement
*/
public int evaluateInt(EvaluateContext ec) throws PrismLangException
{
Object o = evaluate(ec);
if (o instanceof Integer) {
return ((Integer) o).intValue();
}
if (o instanceof Boolean) {
return ((Boolean) o).booleanValue() ? 1 : 0;
}
throw new PrismLangException("Cannot evaluate to an integer", this);
return evaluateObjectAsInt(evaluate(ec));
}
/**
@ -343,6 +336,21 @@ public abstract class Expression extends ASTElement
return evaluateInt(new EvaluateContextSubstate(constantValues, substate, varMap));
}
/**
* Evaluate this object as an integer.
* Any typing issues cause an exception (but: we do allow conversion of boolean to 0/1).
*/
public static int evaluateObjectAsInt(Object o) throws PrismLangException
{
if (o instanceof Integer) {
return ((Integer) o).intValue();
}
if (o instanceof Boolean) {
return ((Boolean) o).booleanValue() ? 1 : 0;
}
throw new PrismLangException("Cannot evaluate " + o + " to an integer");
}
/**
* Evaluate this expression as a double.
* Any typing issues cause an exception (but: we do allow conversion of boolean to 0.0/1.0).

Loading…
Cancel
Save