public void RemoveInBlock()
        {
            // { if (a) EmptyNode; }
            // { if (a) EmptyNode; else EmptyNode; }
            var if1 = new IfElseNode(new IdNode("a"), new EmptyNode());
            var if2 = new IfElseNode(new IdNode("a"), new EmptyNode(), new EmptyNode());

            var block1 = new BlockNode(new StListNode(if1));
            var block2 = new BlockNode(new StListNode(if2));

            if1.Parent = block1;
            if2.Parent = block2;

            var root = new StListNode(block1);

            root.Add(block2);
            block1.Parent = block2.Parent = root;

            var opt = new IfNullElseNull();

            root.Visit(opt);

            Assert.IsNull(root.Parent);
            Assert.AreEqual(root.ExprChildren.Count, 0);
            Assert.AreEqual(root.StatChildren.Count, 2);

            foreach (var node in root.StatChildren)
            {
                Assert.IsTrue(node is BlockNode);
                Assert.AreEqual(node.ExprChildren.Count, 0);
                Assert.AreEqual(node.StatChildren.Count, 1);
                Assert.IsTrue(node.StatChildren[0] is EmptyNode);
            }
        }
Esempio n. 2
0
 public override void VisitStListNode(StListNode bl)
 {
     if (bl.Parent == null)
     {
         Changed = false;
     }
     base.VisitStListNode(bl);
 }
 public override void VisitStListNode(StListNode bl)
 {
     PreVisit(bl);
     for (int i = 0; i < bl.StatChildren.Count; ++i)
     {
         bl.StatChildren[i].Visit(this);
     }
     PostVisit(bl);
 }
Esempio n. 4
0
 public override void VisitStListNode(StListNode bl)
 {
     if (st.Count > 0)
     {
         bl.Parent = st.Peek();
     }
     st.Push(bl);
     base.VisitStListNode(bl);
     st.Pop();
 }
        public override void VisitStListNode(StListNode bl)
        {
            var Count = bl.StatChildren.Count;

            if (Count > 0)
            {
                bl.StatChildren[0].Visit(this);
            }
            for (var i = 1; i < Count; i++)
            {
                Text += Environment.NewLine;
                bl.StatChildren[i].Visit(this);
            }
        }
        public void RemoveInnerIf2()
        {
            // if (a)
            //   if (b) EmptyNode;
            var ifInner = new IfElseNode(new IdNode("b"), new EmptyNode());
            var ifOuter = new IfElseNode(new IdNode("a"), ifInner);

            ifInner.Parent = ifOuter;

            var root = new StListNode(ifOuter);

            ifOuter.Parent = root;

            var opt = new IfNullElseNull();

            root.Visit(opt);

            Assert.IsNull(root.Parent);
            Assert.AreEqual(root.ExprChildren.Count, 0);
            Assert.AreEqual(root.StatChildren.Count, 1);
            Assert.IsTrue(root.StatChildren[0] is EmptyNode);
        }
        public void RemoveSimple()
        {
            // if (a) EmptyNode;
            // if (a) EmptyNode; else EmptyNode;
            var if1 = new IfElseNode(new IdNode("a"), new EmptyNode());
            var if2 = new IfElseNode(new IdNode("a"), new EmptyNode(), new EmptyNode());

            var root = new StListNode(if1);

            root.Add(if2);
            if1.Parent = if2.Parent = root;

            var opt = new IfNullElseNull();

            root.Visit(opt);

            Assert.IsNull(root.Parent);
            Assert.AreEqual(root.ExprChildren.Count, 0);
            Assert.AreEqual(root.StatChildren.Count, 2);

            Assert.IsTrue(root.StatChildren[0] is EmptyNode);
            Assert.IsTrue(root.StatChildren[1] is EmptyNode);
        }
        public void WithoutRemoveSimple2()
        {
            // if (a) a = 0;
            // if (a) a = 0; else a = 1;
            var if1 = new IfElseNode(new IdNode("a"), new AssignNode(new IdNode("a"), new IntNumNode(0)));
            var if2 = new IfElseNode(new IdNode("a"), new AssignNode(new IdNode("a"), new IntNumNode(0)), new AssignNode(new IdNode("a"), new IntNumNode(1)));

            var root = new StListNode(if1);

            root.Add(if2);
            if1.Parent = if2.Parent = root;

            var opt = new IfNullElseNull();

            root.Visit(opt);

            Assert.IsNull(root.Parent);
            Assert.AreEqual(root.ExprChildren.Count, 0);
            Assert.AreEqual(root.StatChildren.Count, 2);

            Assert.IsTrue(root.StatChildren[0] is IfElseNode);
            Assert.IsTrue(root.StatChildren[1] is IfElseNode);
        }
Esempio n. 9
0
 public virtual void VisitStListNode(StListNode bl)
 {
 }
Esempio n. 10
0
        protected override void DoAction(int action)
        {
            switch (action)
            {
            case 2: // progr -> stlist
            { root = ValueStack[ValueStack.Depth - 1].blVal; }
            break;

            case 3: // stlist -> statement
            { CurrentSemanticValue.blVal = new StListNode(ValueStack[ValueStack.Depth - 1].stVal); }
            break;

            case 4: // stlist -> stlist, statement
            {
                ValueStack[ValueStack.Depth - 2].blVal.Add(ValueStack[ValueStack.Depth - 1].stVal);
                CurrentSemanticValue.blVal = ValueStack[ValueStack.Depth - 2].blVal;
            }
            break;

            case 5: // statement -> assign, SEMICOLON
            { CurrentSemanticValue.stVal = ValueStack[ValueStack.Depth - 2].stVal; }
            break;

            case 6: // statement -> for
            { CurrentSemanticValue.stVal = ValueStack[ValueStack.Depth - 1].stVal; }
            break;

            case 7: // statement -> while
            { CurrentSemanticValue.stVal = ValueStack[ValueStack.Depth - 1].stVal; }
            break;

            case 8: // statement -> if
            { CurrentSemanticValue.stVal = ValueStack[ValueStack.Depth - 1].stVal; }
            break;

            case 9: // statement -> block
            { CurrentSemanticValue.stVal = ValueStack[ValueStack.Depth - 1].stVal; }
            break;

            case 10: // statement -> input, SEMICOLON
            { CurrentSemanticValue.stVal = ValueStack[ValueStack.Depth - 2].stVal; }
            break;

            case 11: // statement -> print, SEMICOLON
            { CurrentSemanticValue.stVal = ValueStack[ValueStack.Depth - 2].stVal; }
            break;

            case 12: // statement -> var, SEMICOLON
            { CurrentSemanticValue.stVal = ValueStack[ValueStack.Depth - 2].stVal; }
            break;

            case 13: // statement -> goto, SEMICOLON
            { CurrentSemanticValue.stVal = ValueStack[ValueStack.Depth - 2].stVal; }
            break;

            case 14: // statement -> labelstatement
            { CurrentSemanticValue.stVal = ValueStack[ValueStack.Depth - 1].stVal; }
            break;

            case 15: // ident -> ID
            {
                if (!InDefSect)
                {
                    if (!SymbolTable.vars.ContainsKey(ValueStack[ValueStack.Depth - 1].sVal))
                    {
                        throw new Exception("(" + LocationStack[LocationStack.Depth - 1].StartLine + "," + LocationStack[LocationStack.Depth - 1].StartColumn + "): Variable " + ValueStack[ValueStack.Depth - 1].sVal + " not described");
                    }
                }
                CurrentSemanticValue.eVal = new IdNode(ValueStack[ValueStack.Depth - 1].sVal);
            }
            break;

            case 16: // assign -> ident, ASSIGN, expr
            { CurrentSemanticValue.stVal = new AssignNode(ValueStack[ValueStack.Depth - 3].eVal as IdNode, ValueStack[ValueStack.Depth - 1].eVal); }
            break;

            case 17: // block -> BEGIN, stlist, END
            { CurrentSemanticValue.stVal = new BlockNode(ValueStack[ValueStack.Depth - 2].blVal); }
            break;

            case 18: // for -> FOR, ident, ASSIGN, expr, COMMA, expr, statement
            { CurrentSemanticValue.stVal = new ForNode(ValueStack[ValueStack.Depth - 6].eVal as IdNode, ValueStack[ValueStack.Depth - 4].eVal, ValueStack[ValueStack.Depth - 2].eVal, ValueStack[ValueStack.Depth - 1].stVal); }
            break;

            case 19: // while -> WHILE, expr, statement
            { CurrentSemanticValue.stVal = new WhileNode(ValueStack[ValueStack.Depth - 2].eVal, ValueStack[ValueStack.Depth - 1].stVal); }
            break;

            case 20: // if -> IF, expr, statement, ELSE, statement
            { CurrentSemanticValue.stVal = new IfElseNode(ValueStack[ValueStack.Depth - 4].eVal, ValueStack[ValueStack.Depth - 3].stVal, ValueStack[ValueStack.Depth - 1].stVal); }
            break;

            case 21: // if -> IF, expr, statement
            { CurrentSemanticValue.stVal = new IfElseNode(ValueStack[ValueStack.Depth - 2].eVal, ValueStack[ValueStack.Depth - 1].stVal); }
            break;

            case 22: // expr -> expr, OR, A
            { CurrentSemanticValue.eVal = new BinOpNode(ValueStack[ValueStack.Depth - 3].eVal, ValueStack[ValueStack.Depth - 1].eVal, OpType.OR); }
            break;

            case 23: // expr -> A
            { CurrentSemanticValue.eVal = ValueStack[ValueStack.Depth - 1].eVal; }
            break;

            case 24: // A -> A, AND, B
            { CurrentSemanticValue.eVal = new BinOpNode(ValueStack[ValueStack.Depth - 3].eVal, ValueStack[ValueStack.Depth - 1].eVal, OpType.AND); }
            break;

            case 25: // A -> B
            { CurrentSemanticValue.eVal = ValueStack[ValueStack.Depth - 1].eVal; }
            break;

            case 26: // B -> B, EQUAL, C
            { CurrentSemanticValue.eVal = new BinOpNode(ValueStack[ValueStack.Depth - 3].eVal, ValueStack[ValueStack.Depth - 1].eVal, OpType.EQUAL); }
            break;

            case 27: // B -> B, NOTEQUAL, C
            { CurrentSemanticValue.eVal = new BinOpNode(ValueStack[ValueStack.Depth - 3].eVal, ValueStack[ValueStack.Depth - 1].eVal, OpType.NOTEQUAL); }
            break;

            case 28: // B -> C
            { CurrentSemanticValue.eVal = ValueStack[ValueStack.Depth - 1].eVal; }
            break;

            case 29: // C -> C, GREATER, E
            { CurrentSemanticValue.eVal = new BinOpNode(ValueStack[ValueStack.Depth - 3].eVal, ValueStack[ValueStack.Depth - 1].eVal, OpType.GREATER); }
            break;

            case 30: // C -> C, LESS, E
            { CurrentSemanticValue.eVal = new BinOpNode(ValueStack[ValueStack.Depth - 3].eVal, ValueStack[ValueStack.Depth - 1].eVal, OpType.LESS); }
            break;

            case 31: // C -> C, EQGREATER, E
            { CurrentSemanticValue.eVal = new BinOpNode(ValueStack[ValueStack.Depth - 3].eVal, ValueStack[ValueStack.Depth - 1].eVal, OpType.EQGREATER); }
            break;

            case 32: // C -> C, EQLESS, E
            { CurrentSemanticValue.eVal = new BinOpNode(ValueStack[ValueStack.Depth - 3].eVal, ValueStack[ValueStack.Depth - 1].eVal, OpType.EQLESS); }
            break;

            case 33: // C -> E
            { CurrentSemanticValue.eVal = ValueStack[ValueStack.Depth - 1].eVal; }
            break;

            case 34: // E -> E, PLUS, T
            { CurrentSemanticValue.eVal = new BinOpNode(ValueStack[ValueStack.Depth - 3].eVal, ValueStack[ValueStack.Depth - 1].eVal, OpType.PLUS); }
            break;

            case 35: // E -> E, MINUS, T
            { CurrentSemanticValue.eVal = new BinOpNode(ValueStack[ValueStack.Depth - 3].eVal, ValueStack[ValueStack.Depth - 1].eVal, OpType.MINUS); }
            break;

            case 36: // E -> T
            { CurrentSemanticValue.eVal = ValueStack[ValueStack.Depth - 1].eVal; }
            break;

            case 37: // T -> T, MULT, F
            { CurrentSemanticValue.eVal = new BinOpNode(ValueStack[ValueStack.Depth - 3].eVal, ValueStack[ValueStack.Depth - 1].eVal, OpType.MULT); }
            break;

            case 38: // T -> T, DIV, F
            { CurrentSemanticValue.eVal = new BinOpNode(ValueStack[ValueStack.Depth - 3].eVal, ValueStack[ValueStack.Depth - 1].eVal, OpType.DIV); }
            break;

            case 39: // T -> F
            { CurrentSemanticValue.eVal = ValueStack[ValueStack.Depth - 1].eVal; }
            break;

            case 40: // F -> ident
            { CurrentSemanticValue.eVal = ValueStack[ValueStack.Depth - 1].eVal as IdNode; }
            break;

            case 41: // F -> INUM
            { CurrentSemanticValue.eVal = new IntNumNode(ValueStack[ValueStack.Depth - 1].iVal); }
            break;

            case 42: // F -> LPAR, expr, RPAR
            { CurrentSemanticValue.eVal = ValueStack[ValueStack.Depth - 2].eVal; }
            break;

            case 43: // F -> BOOL
            { CurrentSemanticValue.eVal = new BoolValNode(ValueStack[ValueStack.Depth - 1].bVal); }
            break;

            case 44: // F -> MINUS, F
            { CurrentSemanticValue.eVal = new UnOpNode(ValueStack[ValueStack.Depth - 1].eVal, OpType.UNMINUS); }
            break;

            case 45: // F -> NOT, F
            { CurrentSemanticValue.eVal = new UnOpNode(ValueStack[ValueStack.Depth - 1].eVal, OpType.NOT); }
            break;

            case 46: // input -> INPUT, LPAR, ident, RPAR
            { CurrentSemanticValue.stVal = new InputNode(ValueStack[ValueStack.Depth - 2].eVal as IdNode); }
            break;

            case 47: // exprlist -> expr
            { CurrentSemanticValue.eVal = new ExprListNode(ValueStack[ValueStack.Depth - 1].eVal); }
            break;

            case 48: // exprlist -> exprlist, COMMA, expr
            {
                (ValueStack[ValueStack.Depth - 3].eVal as ExprListNode).Add(ValueStack[ValueStack.Depth - 1].eVal);
                CurrentSemanticValue.eVal = ValueStack[ValueStack.Depth - 3].eVal;
            }
            break;

            case 49: // print -> PRINT, LPAR, exprlist, RPAR
            { CurrentSemanticValue.stVal = new PrintNode(ValueStack[ValueStack.Depth - 2].eVal as ExprListNode); }
            break;

            case 50: // varlist -> ident
            { CurrentSemanticValue.stVal = new VarListNode(ValueStack[ValueStack.Depth - 1].eVal as IdNode); }
            break;

            case 51: // varlist -> varlist, COMMA, ident
            {
                (ValueStack[ValueStack.Depth - 3].stVal as VarListNode).Add(ValueStack[ValueStack.Depth - 1].eVal as IdNode);
                CurrentSemanticValue.stVal = ValueStack[ValueStack.Depth - 3].stVal;
            }
            break;

            case 52: // Anon@1 -> /* empty */
            { InDefSect = true; }
            break;

            case 53: // var -> VAR, Anon@1, varlist
            {
                foreach (var v in (ValueStack[ValueStack.Depth - 1].stVal as VarListNode).vars)
                {
                    SymbolTable.NewVarDef(v.Name);
                }
                InDefSect = false;
            }
            break;

            case 54: // goto -> GOTO, INUM
            { CurrentSemanticValue.stVal = new GotoNode(ValueStack[ValueStack.Depth - 1].iVal); }
            break;

            case 55: // labelstatement -> INUM, COLON, statement
            { CurrentSemanticValue.stVal = new LabelStatementNode(ValueStack[ValueStack.Depth - 3].iVal, ValueStack[ValueStack.Depth - 1].stVal); }
            break;
            }
        }
Esempio n. 11
0
 public override void VisitStListNode(StListNode bl)
 {
     Changed = false;
     base.VisitStListNode(bl);
 }