/** * Performs unit analysis of a given infix operation. The first unit (non-scalar token) * encountered is returned, except with division. In that case, scalar is returned if both units * are the same and points is returned when dividing inches and points. * * @param u1 left-hand unit * @param u2 right-hand unit * @param op operator * @return Token containing resultant unit. */ public static ValueType evaluateUnits(ValueType u1, ValueType u2, Token op) { String unit = "SCALAR"; // default ValueType final String left = u1.toString(); final String right = u2.toString(); switch (op.text) { case "/": // In the case of division, we need only to be concerned in situations where // the final unit is not scalar if (!u1.toString().equals(right)) { if (left.equals("SCALAR")) { unit = right; break; } if (right.equals("SCALAR")) { unit = left; break; } } break; default: if (left.equals("SCALAR")) { unit = right; break; } if (right.equals("SCALAR")) { unit = left; break; } unit = left; break; } return ValueType.valueOf(unit); }
@Override public String getText() { return value.toString(); }
/** * Initialises the value tuple. * * @param type * @param value */ public ValueTuple(ValueType type, Object value) { this(type.toString(), value); }