Ejemplo n.º 1
0
        public static bool TransformToIncDec(BlockNode block)
        {
            var children    = block.GetChildren().ToList();
            var changesmade = false;

            for (var i = 0; i < children.Count; i++)
            {
                var node = children[i];

                if (!(node is VariableAssignmentNode assignment))
                {
                    continue;
                }

                BinaryOperatorNode expression = null;
                if (assignment.Value is AdditionNode add)
                {
                    expression = add;
                }
                if (assignment.Value is SubtractionNode sub)
                {
                    expression = sub;
                }
                if (expression == null)
                {
                    continue;
                }

                ExpressionNode left, right;
                if (expression.Left is ShortValueNode)
                {
                    left  = expression.Left;
                    right = expression.Right;
                }
                else
                {
                    right = expression.Left;
                    left  = expression.Right;
                }
                if (left is ShortValueNode value && value.Value == 1 && right is VariableValueNode var && var.VariableName == assignment.VariableName)
                {
                    children.RemoveAt(i);
                    block.RemoveChild(i);
                    if (expression is AdditionNode)
                    {
                        block.AddChild(new IncrementNode(assignment.VariableName));
                    }
                    else
                    {
                        block.AddChild(new DecrementNode(assignment.VariableName));
                    }

                    changesmade = true;
                }
            }

            return(changesmade);
        }
Ejemplo n.º 2
0
        public void EmitAssembly_If_CreatesBlock_WithElse()
        {
            var ast = new ASTNode();

            ast.AddChild(new VariableDeclarationNode("byte", "x"));
            ast.AddChild(new VariableAssignmentNode("x", new ShortValueNode(1)));
            var iftrueblock = new BlockNode();

            iftrueblock.AddChild(new VariableAssignmentNode("x", new ShortValueNode(1)));
            var iffalseblock = new BlockNode();

            iffalseblock.AddChild(new VariableAssignmentNode("x", new ShortValueNode(2)));
            ast.AddChild(new IfNode(new VariableValueNode("x"), iftrueblock, iffalseblock));

            var asm = EmitAssembly(ast).ToArray();

            ListEqual(new[] {
                "LD C 1",
                "XOR A",
                "CP C",
                "JP NZ generatedLabel1",
                "LD C 1",
                "JP generatedLabel2",
                "generatedLabel1:",
                "LD C 2",
                "generatedLabel2:"
            }, asm);
        }
Ejemplo n.º 3
0
        public override Node Visit(BlockNode block)
        {
            var blockNode = new BlockNode(block.Token, block.Depth);

            foreach (var statement in block.ChildNodes)
            {
                blockNode.AddChild(statement.Accept(this));
            }
            return(blockNode);
        }
Ejemplo n.º 4
0
        public void PropagteConstants_If_ExtractsTrueBody()
        {
            var ast = new ASTNode();

            ast.AddChild(new VariableDeclarationNode("byte", "x"));
            ast.AddChild(new VariableAssignmentNode("x", new ShortValueNode(1)));
            var block = new BlockNode();

            block.AddChild(new VariableDeclarationNode("byte", "a"));
            block.AddChild(new VariableAssignmentNode("a", new ShortValueNode(1)));
            ast.AddChild(new IfNode(new VariableValueNode("x"), block));

            var changesmade = PropagateConstants(ast);

            Assert.IsTrue(changesmade);
            var children = ast.GetChildren().ToList();

            Assert.AreEqual(4, children.Count);
            Assert.IsInstanceOfType(children[2], typeof(VariableDeclarationNode));
            Assert.IsInstanceOfType(children[3], typeof(VariableAssignmentNode));
        }
Ejemplo n.º 5
0
        private void Block(BlockNode node)
        {
            RequiredToken(Tag.DL_LBRACE);

            while (!TagIs(Tag.DL_RBRACE) && !TagIs(Tag.NULL))
            {
                if (Utils.IsTypeTag(next.Tag) || TagIs(Tag.KW_CONST))
                {
                    DeclNode decl = new DeclNode();
                    node.AddChild(decl);
                    Decl(decl);
                }
                else
                {
                    StmtNode stmt = new StmtNode();
                    node.AddChild(stmt);
                    Stmt(stmt);
                }
            }

            RequiredToken(Tag.DL_RBRACE);
        }
Ejemplo n.º 6
0
        public override Node VisitBlock(BlockContext context)
        {
            _blockDepth++;

            var blockNode = new BlockNode(context.Start, _blockDepth);

            foreach (StatementContext x in context.statement())
            {
                blockNode.AddChild(Visit(x));
            }

            _blockDepth--;
            return(blockNode);
        }
Ejemplo n.º 7
0
        public void PropagteConstants_If_ExtractsFalseBody()
        {
            var ast = new ASTNode();

            ast.AddChild(new VariableDeclarationNode("byte", "x"));
            ast.AddChild(new VariableAssignmentNode("x", new ShortValueNode(0)));
            var iftrue = new BlockNode();

            iftrue.AddChild(new VariableAssignmentNode("x", new ShortValueNode(1)));
            var iffalse = new BlockNode();

            iffalse.AddChild(new AdditionAssignmentNode("x", new ShortValueNode(2)));
            ast.AddChild(new IfNode(new VariableValueNode("x"), iftrue, iffalse));

            var changesmade = PropagateConstants(ast);

            Assert.IsTrue(changesmade);
            var children = ast.GetChildren().ToList();

            Assert.AreEqual(3, children.Count);
            Assert.IsInstanceOfType(children[2], typeof(AdditionAssignmentNode));
        }