Package com.creativewidgetworks.goldparser.parser

Examples of com.creativewidgetworks.goldparser.parser.Variable


        // Write the data to the console
        ioDriver.write(dataToPrint.getValue().asObject().toString());
        if (variableName != null) {
            // Read console input from the user (assuming that EOLN is not echoed to console)
            StringBuilder sb = new StringBuilder(ioDriver.read());
            theParser.setProgramVariable(variableName, new Variable(sb.toString()));
        } else {
            ioDriver.write(EOLN);
        }
    }
View Full Code Here


        String str = parser.getCurrentReduction() == null ? "\"\"" : parser.getCurrentReduction().get(0).asString();
       
        StringBuilder sb = new StringBuilder(str);
        sb.deleteCharAt(sb.length() - 1);
        sb.deleteCharAt(0);
        setValue(new Variable(sb.toString()));
    }
View Full Code Here

public class NumberLiteral extends Reduction {

    public NumberLiteral(GOLDParser parser) throws ParserException  {
        String literal = parser.getCurrentReduction() == null ? "<null>" : parser.getCurrentReduction().get(0).asString();
        try {
            setValue(new Variable(new BigDecimal(literal)));
        } catch (NumberFormatException e) {
            parser.raiseParserException(Simple2.formatMessage("error.token_nan", literal));
        }
    }
View Full Code Here

        }       
    }

    @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(Simple2.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(Simple2.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(Simple2.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(Simple2.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(Simple2.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(Simple2.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(Simple2.formatMessage("error.type_mismatch"));
            }
        } else if (theOperator.equals("&")) {
            // String concatenation
            if (oneOrBothValuesAreStrings()) {
                result = new Variable(lValue.toString() + rValue.toString());
            } else {
                theParser.raiseParserException(Simple2.formatMessage("error.type_mismatch"));
            }
        } else if (theOperator.equals("*")) {
            if (bothValuesAreNumbers()) {
                result = new Variable(lValue.asNumber().multiply(rValue.asNumber()));
            } else {
                theParser.raiseParserException(Simple2.formatMessage("error.type_mismatch"));
            }
        } else if (theOperator.equals("/")) {
            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));
            }
        }

        return result;
    }
View Full Code Here

        BigDecimal bd = valueToNegate.getValue().asNumber();
        if (bd == null) {
            theParser.raiseParserException(Simple2.formatMessage("error.negation_number_expected"));
        }

        return new Variable(bd.negate());
    }
View Full Code Here

        // Write the data to the console
        ioDriver.write(dataToPrint.getValue().asObject().toString());
        if (variableName != null) {
            // Read console input from the user (assuming that EOLN is not echoed to console)
            StringBuilder sb = new StringBuilder(ioDriver.read());
            theParser.setProgramVariable(variableName, new Variable(sb.toString()));
        }
        ioDriver.write(EOLN);
    }
View Full Code Here

        return variableName;
    }
   
    @Override
    public Variable getValue() {
        Variable var = theParser.getProgramVariable(variableName);
        return var == null ? new Variable("") : var;
    }
View Full Code Here

TOP

Related Classes of com.creativewidgetworks.goldparser.parser.Variable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.