Browse Source

ExpressionLiteral: support for BigRational literals, use string for evaluateExact

master
Joachim Klein 8 years ago
committed by Dave Parker
parent
commit
b94c42ae4a
  1. 13
      prism/src/parser/ast/ExpressionLiteral.java

13
prism/src/parser/ast/ExpressionLiteral.java

@ -34,7 +34,7 @@ import parser.type.*;
public class ExpressionLiteral extends Expression public class ExpressionLiteral extends Expression
{ {
Object value; // Value Object value; // Value
String string; // Optionally, keep original string to preserve user formatting
String string; // Optionally, keep original string to preserve user formatting and allow exact evaluation
// Constructor // Constructor
@ -92,14 +92,25 @@ public class ExpressionLiteral extends Expression
@Override @Override
public Object evaluate(EvaluateContext ec) throws PrismLangException public Object evaluate(EvaluateContext ec) throws PrismLangException
{ {
if (value instanceof BigRational) {
// cast from BigRational to appropriate Java data type
return getType().castFromBigRational((BigRational) value);
}
return value; return value;
} }
@Override @Override
public BigRational evaluateExact(EvaluateContext ec) throws PrismLangException public BigRational evaluateExact(EvaluateContext ec) throws PrismLangException
{ {
if (value instanceof BigRational) {
// if already a BigRational, return that
return (BigRational) value;
} else if (value instanceof Boolean) {
return BigRational.from(value); return BigRational.from(value);
} }
// otherwise, convert from string representation (potentially provides more precision for double literals)
return BigRational.from(string);
}
@Override @Override
public boolean returnsSingleValue() public boolean returnsSingleValue()

Loading…
Cancel
Save