Commit dc1ac65b authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Move some forgotten Matlab specific code into its parser.

The code to find the operation for a given text string was hidding in the
*Operation enumerations.  Move this code into MatlabParser, as it isn't
reusable as many languages use different symbols.

git-svn-id: https://groke.mcmaster.ca/svn/grad/colin/branches/TableTool_javization@10849 57e6efec-57d4-0310-aeb1-a6c144bb1a8b
parent 4761cd4b
Loading
Loading
Loading
Loading
+0 −28
Original line number Diff line number Diff line
@@ -27,34 +27,6 @@ public enum BinaryOperation {
    BooleanAnd,
    BooleanOr;

    public static BinaryOperation getOpForSymbol(String text) {
        if (text.equals("+")) {
            return Addition;
        } else if (text.equals("-")) {
            return Minus;
        } else if (text.equals("*")) {
            return Multiplication;
        } else if (text.equals("/")) {
            return Division;
        } else if (text.equals(">")) {
            return GreaterThen;
        } else if (text.equals(">=")) {
            return GreaterThenEqual;
        } else if (text.equals("<")) {
            return LessThen;
        } else if (text.equals("<=")) {
            return LessThenEqual;
        } else if (text.equals("==")) {
            return Equals;
        } else if (text.equals("&&")) {
            return BooleanAnd;
        } else if (text.equals("||")) {
            return BooleanOr;
        } else {
            throw new IllegalArgumentException("No such binary matlab operation defined (" + text + ")!");
        }
    }

    Collection<VariableTypeMarker> getInputTypeForOutput(VariableTypeMarker outputType) {
        // Boolean inputs only work with a boolean input!
        if (this == BooleanAnd || this == BooleanOr || this == GreaterThen || this == GreaterThenEqual ||
+0 −10
Original line number Diff line number Diff line
@@ -12,14 +12,4 @@ public enum UnaryOperation {

    Minus,
    Negation;

    public static UnaryOperation getOpForSymbol(String text) {
        if (text.equals("~")) {
            return Negation;
        } else if (text.equals("-")) {
            return Minus;
        } else {
            throw new IllegalArgumentException("No such unary matlab operation defined (" + text + ")!");
        }
    }
}
+43 −5
Original line number Diff line number Diff line
@@ -135,6 +135,44 @@ final public class MatlabParser {
            m_opStack.addFirst(new ExpressionStackContainer(m_level, op));
        }

        private static BinaryOperation getBinaryOpForSymbol(String text) {
            if (text.equals("+")) {
                return BinaryOperation.Addition;
            } else if (text.equals("-")) {
                return BinaryOperation.Minus;
            } else if (text.equals("*")) {
                return BinaryOperation.Multiplication;
            } else if (text.equals("/")) {
                return BinaryOperation.Division;
            } else if (text.equals(">")) {
                return BinaryOperation.GreaterThen;
            } else if (text.equals(">=")) {
                return BinaryOperation.GreaterThenEqual;
            } else if (text.equals("<")) {
                return BinaryOperation.LessThen;
            } else if (text.equals("<=")) {
                return BinaryOperation.LessThenEqual;
            } else if (text.equals("==")) {
                return BinaryOperation.Equals;
            } else if (text.equals("&&")) {
                return BinaryOperation.BooleanAnd;
            } else if (text.equals("||")) {
                return BinaryOperation.BooleanOr;
            } else {
                throw new IllegalArgumentException("No such binary matlab operation defined (" + text + ")!");
            }
        }

        private static UnaryOperation getUnaryOpForSymbol(String text) {
            if (text.equals("~")) {
                return UnaryOperation.Negation;
            } else if (text.equals("-")) {
                return UnaryOperation.Minus;
            } else {
                throw new IllegalArgumentException("No such unary matlab operation defined (" + text + ")!");
            }
        }

        @Override
        public Expression getSubExpression() {
            return m_rootExpression;
@@ -274,26 +312,26 @@ final public class MatlabParser {

        @Override
        public void enterG1(G1Context ctx) {
            addExpressionBinaryOperation(new ExpressionBinaryOperation(null, BinaryOperation.getOpForSymbol(ctx.getText()), null));
            addExpressionBinaryOperation(new ExpressionBinaryOperation(null, getBinaryOpForSymbol(ctx.getText()), null));
        }

        @Override
        public void enterG2(G2Context ctx) {
            addExpressionBinaryOperation(new ExpressionBinaryOperation(null, BinaryOperation.getOpForSymbol(ctx.getText()), null));
            addExpressionBinaryOperation(new ExpressionBinaryOperation(null, getBinaryOpForSymbol(ctx.getText()), null));
        }

        @Override
        public void enterG3(G3Context ctx) {
            addExpressionBinaryOperation(new ExpressionBinaryOperation(null, BinaryOperation.getOpForSymbol(ctx.getText()), null));
            addExpressionBinaryOperation(new ExpressionBinaryOperation(null, getBinaryOpForSymbol(ctx.getText()), null));
        }

        @Override
        public void enterG4(G4Context ctx) {
            addExpressionBinaryOperation(new ExpressionBinaryOperation(null, BinaryOperation.getOpForSymbol(ctx.getText()), null));
            addExpressionBinaryOperation(new ExpressionBinaryOperation(null, getBinaryOpForSymbol(ctx.getText()), null));
        }
        @Override
        public void enterPrefix_operator(Prefix_operatorContext ctx) {
            addPrefixUnaryOperation(new ExpressionUnaryOperation(UnaryOperation.getOpForSymbol(ctx.getText()), null));
            addPrefixUnaryOperation(new ExpressionUnaryOperation(getUnaryOpForSymbol(ctx.getText()), null));
        }

        @Override