Commit 62f90c24 authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Add proper parsing for the Matlab && operator. Required some reworking though.

Add parsing for the && operator.  However, the grammar file doesn't have an
explicit node for it.  To avoid having to create a node per such operations,
instead add a method so that they are automatically created as necessary.  Since
each one is used (repeatedly), as the level decrements stick the appropriate
operator in place.   The system will then pick up on this and handle the situation
appropriately.

git-svn-id: https://groke.mcmaster.ca/svn/grad/colin/branches/TableTool_javization@10564 57e6efec-57d4-0310-aeb1-a6c144bb1a8b
parent db502f3d
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -71,11 +71,24 @@ final public class MatlabParser implements MatlabExpressionWithSubExpression {

    private void decrementLevel() {
        this.m_level--;
        if (!m_hiddenOpStack.isEmpty() && m_hiddenOpStack.peekFirst().level == m_level) {
            // We have a hidden op here.  The normal methods won't find it, so make use of it!
            HiddenOpStackContainer hiddenOp = m_hiddenOpStack.peekFirst();
            addExpressionBinaryOperation(new MatlabExpressionBinaryOperation(null, hiddenOp.op, null));
            if (--hiddenOp.count == 0) {
                m_hiddenOpStack.removeFirst();
            }
        }

        if (!m_opStack.isEmpty() && m_opStack.peekFirst().level > m_level) {
            m_opStack.removeFirst();
        }
    }

    private void enqueHiddenOp(MatlabBinaryOperation op, int count) {
        m_hiddenOpStack.addFirst(new HiddenOpStackContainer(m_level, op, count));
    }

    private void addExpressionValue(MatlabExpressionValue expressionValue) {
        System.out.println("" + m_level + " " + m_opStack.peekFirst().level);
        m_opStack.peekFirst().op.setSubExpression(expressionValue);
@@ -140,6 +153,9 @@ final public class MatlabParser implements MatlabExpressionWithSubExpression {
        public void enterE2(E2Context ctx) {
            System.out.println("In E2");
            incrementLevel();
            if (ctx.LOG_AND().size() > 0) {
                enqueHiddenOp(MatlabBinaryOperation.BooleanAnd, ctx.LOG_AND().size());
            }
        }

        @Override
@@ -348,4 +364,17 @@ final public class MatlabParser implements MatlabExpressionWithSubExpression {
        }
    }
    Deque<ExpressionStackContainer> m_opStack = new ArrayDeque<MatlabParser.ExpressionStackContainer>();

    private static class HiddenOpStackContainer {

        public int level, count;
        public MatlabBinaryOperation op;

        public HiddenOpStackContainer(int level, MatlabBinaryOperation op, int count) {
            this.level = level;
            this.op = op;
            this.count = count;
        }
    }
    Deque<HiddenOpStackContainer> m_hiddenOpStack = new ArrayDeque<HiddenOpStackContainer>();
}
+9 −0
Original line number Diff line number Diff line
@@ -122,5 +122,14 @@ public class MatlabParserTest {
        assertEquals(new BooleanVariableType(), expr.type());
        assertEquals("(5 > 5)", expr.getCheckerOutput(generator, expr.type()));
    }

    @Test
    public void testInsanseExpressionsWithLiteralsAndVariables() {
        MatlabParser parser = new MatlabParser(new VariableParser("x,z,y"), "z == 1 && x == 0 && y == 2");
        MatlabExpression expr = parser.getRootExpression();

        assertEquals(new BooleanVariableType(), expr.type());
        assertEquals("(((z = 1) AND (x = 0)) AND (y = 2))", expr.getCheckerOutput(generator, expr.type()));
    }
    CheckerGenerator generator = new CVC3Generator();
}