}
}
@Override
public Variable getValue() throws ParserException {
Variable result = null;
Variable lValue = leftExpression.getValue();
Variable rValue = rightExpression.getValue();
boolean b = false;
if (theOperator.equals("==")) {
if (bothValuesAreNumbers()) {
b = lValue.asNumber().compareTo(rValue.asNumber()) == 0;
result = new Variable(Boolean.valueOf(b));
} else if (bothValuesAreBooleans()) {
b = lValue.asBool() == rValue.asBool();
result = new Variable(Boolean.valueOf(b));
} else if (bothValuesAreTimestamps()) {
b = lValue.asTimestamp().compareTo(rValue.asTimestamp()) == 0;
result = new Variable(Boolean.valueOf(b));
} else if (oneOrBothValuesAreStrings()) {
b = lValue.toString().compareTo(rValue.toString()) == 0;
result = new Variable(Boolean.valueOf(b));
} else {
theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
}
} else if (theOperator.equals("<>")) {
if (bothValuesAreNumbers()) {
b = lValue.asNumber().compareTo(rValue.asNumber()) != 0;
result = new Variable(Boolean.valueOf(b));
} else if (bothValuesAreBooleans()) {
b = lValue.asBool() != rValue.asBool();
result = new Variable(Boolean.valueOf(b));
} else if (bothValuesAreTimestamps()) {
b = lValue.asTimestamp().compareTo(rValue.asTimestamp()) != 0;
result = new Variable(Boolean.valueOf(b));
} else if (oneOrBothValuesAreStrings()) {
b = lValue.toString().compareTo(rValue.toString()) != 0;
result = new Variable(Boolean.valueOf(b));
} else {
theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
}
} else if (theOperator.equals("<")) {
if (bothValuesAreNumbers()) {
b = lValue.asNumber().compareTo(rValue.asNumber()) < 0;
result = new Variable(Boolean.valueOf(b));
} else if (bothValuesAreTimestamps()) {
b = lValue.asTimestamp().compareTo(rValue.asTimestamp()) < 0;
result = new Variable(Boolean.valueOf(b));
} else if (oneOrBothValuesAreStrings()) {
b = lValue.toString().compareTo(rValue.toString()) < 0;
result = new Variable(Boolean.valueOf(b));
} else {
theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
}
} else if (theOperator.equals("<=")) {
if (bothValuesAreNumbers()) {
b = lValue.asNumber().compareTo(rValue.asNumber()) <= 0;
result = new Variable(Boolean.valueOf(b));
} else if (bothValuesAreTimestamps()) {
b = lValue.asTimestamp().compareTo(rValue.asTimestamp()) <= 0;
result = new Variable(Boolean.valueOf(b));
} else if (oneOrBothValuesAreStrings()) {
b = lValue.toString().compareTo(rValue.toString()) <= 0;
result = new Variable(Boolean.valueOf(b));
} else {
theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
}
} else if (theOperator.equals(">")) {
if (bothValuesAreNumbers()) {
b = lValue.asNumber().compareTo(rValue.asNumber()) > 0;
result = new Variable(Boolean.valueOf(b));
} else if (bothValuesAreTimestamps()) {
b = lValue.asTimestamp().compareTo(rValue.asTimestamp()) > 0;
result = new Variable(Boolean.valueOf(b));
} else if (oneOrBothValuesAreStrings()) {
b = lValue.toString().compareTo(rValue.toString()) > 0;
result = new Variable(Boolean.valueOf(b));
} else {
theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
}
} else if (theOperator.equals(">=")) {
if (bothValuesAreNumbers()) {
b = lValue.asNumber().compareTo(rValue.asNumber()) >= 0;
result = new Variable(Boolean.valueOf(b));
} else if (bothValuesAreTimestamps()) {
b = lValue.asTimestamp().compareTo(rValue.asTimestamp()) >= 0;
result = new Variable(Boolean.valueOf(b));
} else if (oneOrBothValuesAreStrings()) {
b = lValue.toString().compareTo(rValue.toString()) >= 0;
result = new Variable(Boolean.valueOf(b));
} else {
theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
}
} else if (theOperator.equals("+")) {
if (bothValuesAreNumbers()) {
result = new Variable(lValue.asNumber().add(rValue.asNumber()));
} else {
// I prefer to overload + depending upon type
result = new Variable(lValue.toString() + rValue.toString());
}
} else if (theOperator.equals("-")) {
if (bothValuesAreNumbers()) {
result = new Variable(lValue.asNumber().subtract(rValue.asNumber()));
} else {
theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
}
} else if (theOperator.equals("&")) {
// String concatenation
if (oneOrBothValuesAreStrings()) {
result = new Variable(lValue.toString() + rValue.toString());
} else {
theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
}
} else if (theOperator.equals("*")) {
if (bothValuesAreNumbers()) {
result = new Variable(lValue.asNumber().multiply(rValue.asNumber()));
} else {
theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
}
} else if (theOperator.equals("/")) {
if (bothValuesAreNumbers()) {
try {
// This path will strip off extraneous decimal places, but will fail
// with numbers that repeat (e.g., 1/3 = .33333333333)
result = new Variable(lValue.asNumber().divide(rValue.asNumber()));
} catch (Exception e) {
// This path will handle the repeating numbers
result = new Variable(lValue.asNumber().divide(rValue.asNumber(),PRECISION, ROUNDING_MODE));
}
} else {
theParser.raiseParserException(Simple3.formatMessage("error.type_mismatch"));
}
}