Esempio n. 1
0
        public override void VisitDoStatement(IDoStatement stmt, IList <IStatement> body)
        {
            if (IsTargetMatch(stmt, CompletionCase.EmptyCompletionBefore))
            {
                body.Add(EmptyCompletionExpression);
            }
            var loop = new DoLoop
            {
                Condition = _exprVisitor.ToLoopHeaderExpression(stmt.Condition, body)
            };

            body.Add(loop);

            if (IsTargetMatch(stmt, CompletionCase.InBody))
            {
                loop.Body.Add(EmptyCompletionExpression);
            }

            stmt.Body.Accept(this, loop.Body);


            if (IsTargetMatch(stmt, CompletionCase.EmptyCompletionAfter))
            {
                body.Add(EmptyCompletionExpression);
            }
        }
Esempio n. 2
0
        public void DoLoop()
        {
            var sst = new DoLoop
            {
                Condition = new LoopHeaderBlockExpression
                {
                    Body =
                    {
                        new ReturnStatement {
                            Expression = new ConstantValueExpression{
                                Value = "true"
                            }
                        }
                    }
                },
                Body =
                {
                    new ContinueStatement(),
                    new BreakStatement()
                }
            };

            AssertPrint(
                sst,
                "do",
                "{",
                "    continue;",
                "    break;",
                "}",
                "while (",
                "    {",
                "        return true;",
                "    }",
                ")");
        }
        //-----------------------------------------------------------
        public Type Visit(DoLoop node)
        {
            labelStorage.Push(node[0].AnchorToken.Lexeme);
            if (Visit((Assignment)node[1]) != Type.INTEGER)
            {
                throw new SemanticError(
                          "Loop variables can only be integers.",
                          node[1].AnchorToken);
            }

            if (Visit((dynamic)node[2]) != Type.INTEGER)
            {
                throw new SemanticError(
                          "Loop variables can only be integers.",
                          node[2].AnchorToken);
            }

            var loopEval = 3;

            if (!(node[loopEval] is StatementList))
            {
                if (Visit((dynamic)node[loopEval]) != Type.INTEGER)
                {
                    throw new SemanticError(
                              "Loop variables can only be integers.",
                              node[loopEval].AnchorToken);
                }
                loopEval++;
            }

            VisitChildren(node[loopEval]);
            return(Type.VOID);
        }
Esempio n. 4
0
        public void Equality_Default()
        {
            var a = new DoLoop();
            var b = new DoLoop();

            Assert.AreEqual(a, b);
            Assert.AreEqual(a.GetHashCode(), b.GetHashCode());
        }
Esempio n. 5
0
 public void CompileDoLoop(DoLoop doLoop)
 {
     textWriter.WriteLine("do ");
     MayBeIndent(doLoop.Body);
     textWriter.Write("while (");
     doLoop.Guard.AcceptCompiler(this);
     textWriter.WriteLine(");");
 }
Esempio n. 6
0
        public void DefaultValues()
        {
            var sut = new DoLoop();

            Assert.AreEqual(new UnknownExpression(), sut.Condition);
            Assert.AreEqual(Lists.NewList <IStatement>(), sut.Body);
            Assert.AreNotEqual(0, sut.GetHashCode());
            Assert.AreNotEqual(1, sut.GetHashCode());
        }
Esempio n. 7
0
        public void Equality_DifferentCondition()
        {
            var a = new DoLoop {
                Condition = new ConstantValueExpression()
            };
            var b = new DoLoop();

            Assert.AreNotEqual(a, b);
            Assert.AreNotEqual(a.GetHashCode(), b.GetHashCode());
        }
Esempio n. 8
0
        public void Equality_DifferentBody()
        {
            var a = new DoLoop();

            a.Body.Add(new ReturnStatement());
            var b = new DoLoop();

            Assert.AreNotEqual(a, b);
            Assert.AreNotEqual(a.GetHashCode(), b.GetHashCode());
        }
Esempio n. 9
0
        public void ChildrenIdentity()
        {
            var sut = new DoLoop
            {
                Condition = new ConstantValueExpression(),
                Body      =
                {
                    new ReturnStatement()
                }
            };

            AssertChildren(sut, sut.Condition, sut.Body.First());
        }
Esempio n. 10
0
        public void SettingValues()
        {
            var sut = new DoLoop
            {
                Condition = new ConstantValueExpression(),
                Body      =
                {
                    new ReturnStatement()
                }
            };

            Assert.AreEqual(new ConstantValueExpression(), sut.Condition);
            Assert.AreEqual(Lists.NewList(new ReturnStatement()), sut.Body);
        }
Esempio n. 11
0
        public void Equality_ReallyTheSame()
        {
            var a = new DoLoop {
                Condition = new ConstantValueExpression()
            };

            a.Body.Add(new ReturnStatement());
            var b = new DoLoop {
                Condition = new ConstantValueExpression()
            };

            b.Body.Add(new ReturnStatement());
            Assert.AreEqual(a, b);
            Assert.AreEqual(a.GetHashCode(), b.GetHashCode());
        }
Esempio n. 12
0
        public void CompileDoLoop(DoLoop doLoop)
        {
            XmlElement previousElement = currentElement;
            XmlElement tmpElement      = document.CreateElement("DoLoop");

            currentElement = document.CreateElement("Guard");
            doLoop.Guard.AcceptCompiler(this);
            tmpElement.AppendChild(currentElement);

            currentElement = document.CreateElement("Body");
            doLoop.Body.AcceptCompiler(this);
            tmpElement.AppendChild(currentElement);

            previousElement.AppendChild(tmpElement);
            currentElement = previousElement;
        }
Esempio n. 13
0
    protected virtual ICfgNode Build(DoLoop doLoop)
    {
        doLoop.Body?.Accept(this);
        var body = Result;

        doLoop.Condition.Accept(this);
        var condition = Result;

        if (condition == null && body == null)
        {
            return(null);
        }

        return(Emit.Do(
                   condition ?? doLoop.Condition,
                   body ?? doLoop.Body));
    }
Esempio n. 14
0
        /****************************************************************
         *                         Do Loop Node
         ***************************************************************/

        public Node DoLoop()
        {
            var loopResult = new DoLoop()
            {
                AnchorToken = Expect(TokenCategory.DO)
            };

            loopResult.Add(new Label()
            {
                AnchorToken = Expect(TokenCategory.INT_LITERAL)
            });
            loopResult.Add(Assignment());

            while (CurrentToken == TokenCategory.COMMA)
            {
                Expect(TokenCategory.COMMA);
                loopResult.Add(Expression());
            }

            loopResult.Add(EvaluateLoopStatements());
            return(loopResult);
        }
Esempio n. 15
0
 public virtual void Visit(DoLoop doLoop)
 {
     doLoop.Body?.Accept(this);
     doLoop.Condition.Accept(this);
 }
Esempio n. 16
0
        public void VisitorWithReturnIsImplemented()
        {
            var sut = new DoLoop();

            sut.Accept(23).VerifyWithReturn(v => v.Visit(sut, 23));
        }
Esempio n. 17
0
 public void Visit(DoLoop doLoop) => Result                       = Build(doLoop);