Search in sources :

Example 1 with Negate

use of gov.sandia.n2a.language.operator.Negate in project n2a by frothga.

the class Scalar method convert.

/**
 *        General utility: given a string containing a number with units, convert to the scaled SI value.
 */
public static double convert(String expression) {
    try {
        Operator op = Operator.parse(expression);
        double sign = 1;
        if (op instanceof Negate) {
            op = ((Negate) op).operand;
            sign = -1;
        }
        if (!(op instanceof Constant))
            return 0;
        Type result = ((Constant) op).value;
        if (result instanceof Scalar)
            return ((Scalar) result).value * sign;
    } catch (ParseException e) {
    }
    return 0;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Type(gov.sandia.n2a.language.Type) Constant(gov.sandia.n2a.language.Constant) Negate(gov.sandia.n2a.language.operator.Negate) ParseException(gov.sandia.n2a.language.ParseException)

Aggregations

Constant (gov.sandia.n2a.language.Constant)1 Operator (gov.sandia.n2a.language.Operator)1 ParseException (gov.sandia.n2a.language.ParseException)1 Type (gov.sandia.n2a.language.Type)1 Negate (gov.sandia.n2a.language.operator.Negate)1