Commit 61586e98 authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Implement parsing for all important binary operators.

Binary ops now includes:
-
/
>=
<
<=
Unary:
-
YAY!

git-svn-id: https://groke.mcmaster.ca/svn/grad/colin/branches/TableTool_javization@10704 57e6efec-57d4-0310-aeb1-a6c144bb1a8b
parent 15a8649f
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -11,23 +11,42 @@ package ca.mcmaster.cas.matlab2smt;
public enum MatlabBinaryOperation {

    Addition,
    Minus,
    Multiplication,
    Division,

    GreaterThen,
    GreaterThenEqual,
    LessThen,
    LessThenEqual,
    Equals,

    BooleanAnd;
    BooleanAnd,
    BooleanOr;

    static MatlabBinaryOperation 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 + ")!");
        }
+3 −0
Original line number Diff line number Diff line
@@ -147,6 +147,9 @@ final public class MatlabParser implements MatlabExpressionWithSubExpression {
        public void enterE1(E1Context ctx) {
            System.out.println("In E1");
            incrementLevel();
            if (ctx.LOG_OR().size() > 0) {
                enqueHiddenOp(MatlabBinaryOperation.BooleanOr, ctx.LOG_OR().size());
            }
        }

        @Override
+3 −0
Original line number Diff line number Diff line
@@ -10,11 +10,14 @@ package ca.mcmaster.cas.matlab2smt;
 */
public enum MatlabUnaryOperation {

    Minus,
    Negation;

    static MatlabUnaryOperation 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 + ")!");
        }
+15 −2
Original line number Diff line number Diff line
@@ -41,10 +41,18 @@ public class MatlabParserOperatorParseTest {
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {"+", MatlabBinaryOperation.Addition, null},
                {"-", MatlabBinaryOperation.Minus, null},
                {"*", MatlabBinaryOperation.Multiplication, null},
                {"/", MatlabBinaryOperation.Division, null},
                {">", MatlabBinaryOperation.GreaterThen, null},
                {">=", MatlabBinaryOperation.GreaterThenEqual, null},
                {"<", MatlabBinaryOperation.LessThen, null},
                {"<=", MatlabBinaryOperation.LessThenEqual, null},
                {"==", MatlabBinaryOperation.Equals, null},
                {"&&", MatlabBinaryOperation.BooleanAnd, null}
                {"&&", MatlabBinaryOperation.BooleanAnd, null},
                {"||", MatlabBinaryOperation.BooleanOr, null},
                {"~", null, MatlabUnaryOperation.Negation},
                {"-", null, MatlabUnaryOperation.Minus}
        });
    }
    @Parameter
@@ -67,7 +75,12 @@ public class MatlabParserOperatorParseTest {
            assertThat(((MatlabLiteral)exp.m_lhs).m_value, is("1"));
            assertThat(((MatlabLiteral)exp.m_rhs).m_value, is("2"));
        } else {
            assertThat(false, is(true));
            // Unary op to test ...
            MatlabParser parser = new MatlabParser(null, inputSymbol + "3\n");
            MatlabExpressionUnaryOperation exp = (MatlabExpressionUnaryOperation)parser.getRootExpression();

            assertThat(exp.m_op, is(uop));
            assertThat(((MatlabLiteral)exp.m_expr).m_value, is("3"));
        }
    }
}