Пример #1
0
 Expr IStatementVisitor <Expr> .Visit(Statement.While statement)
 {
     return(Expr.Loop(
                Expr.IfThenElse(
                    Expr.Dynamic(
                        Context.DynamicCache.GetConvertBinder(typeof(bool)),
                        typeof(bool),
                        statement.Test.Visit(this)),
                    Visit(statement.Body),
                    Expr.Break(scope.BreakLabel())),
                scope.BreakLabel()));
 }
Пример #2
0
        public ByteCodeChunk VisitWhile(Statement.While statement)
        {
            var chunk = new ByteCodeChunk();

            chunk.AddRange(VisitExpression(statement.Condition));
            var body = VisitBlock(statement.Body);

            chunk.AddInstruction(
                statement.IsUntilLoop ? Instruction.JmpIfTrue : Instruction.JmpIfFalse,
                body.Count + 9);    // 9 is the size of the final JmpShort instruction.
            chunk.AddRange(body);
            chunk.AddInstruction(Instruction.JmpShort, -(chunk.Count + 9));
            return(chunk);
        }
Пример #3
0
 public object VisitWhileStatement(Statement.While stmt)
 {
     // if statement to skip loop if conditions is not met
     stmt.Condition.Accept(this);
     addInstruction(0x04);
     addInstruction(0x40);
     addInstruction(0x03); // loop
     addInstruction(0x40); // while returns void
     stmt.Body.Accept(this);
     stmt.Condition.Accept(this);
     addInstruction(0x0D); // break if negated condition
     addInstruction(ZERO); // TODO break only current
     addInstruction(0x0b); // end loop
     addInstruction(0x0b); // end if statement
     return(null);
 }
Пример #4
0
        public object VisitWhileStatement(Statement.While statement)
        {
            try
            {
                while (IsTruthy(Evaluate(statement.condition)))
                {
                    Execute(statement.body);
                }

                return(null);
            }
            catch (BreakException)
            {
                /* Don't do anything, this is used to break out of the loop when we
                 * encounter a break statement. */
            }

            return(null);
        }
Пример #5
0
 public object VisitWhileStatement(Statement.While stmt)
 {
     return(null);
 }
Пример #6
0
 public object VisitWhileStatement(Statement.While statement)
 {
     Resolve(statement.condition);
     Resolve(statement.body);
     return(null);
 }
Пример #7
0
        private Statement ForStatement()
        {
            try
            {
                loopDepth++;

                Consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'.");

                Statement initialiser = null;

                if (Match(TokenType.SEMICOLON))
                {
                }
                else if (Match(TokenType.VAR))
                {
                    initialiser = VarDeclaration();
                }
                else
                {
                    initialiser = ExpressionStatement();
                }

                Expression condition = Expression();

                if (!Check(TokenType.SEMICOLON))
                {
                    condition = Expression();
                }

                Consume(TokenType.SEMICOLON, "Expect ';' after loop condition.");

                Expression increment = null;

                if (!Check(TokenType.RIGHT_PAREN))
                {
                    increment = Expression();
                }

                Consume(TokenType.RIGHT_PAREN, "Expect ')' after for clauses.");

                Statement body = Statement();

                if (increment != null)
                {
                    body = new Statement.Block(new List <Statement> {
                        body, new Statement.Expression(increment)
                    });
                }

                if (condition == null)
                {
                    condition = new Expression.Literal(true);
                }
                body = new Statement.While(condition, body);

                if (initialiser != null)
                {
                    body = new Statement.Block(new List <Statement> {
                        initialiser, body
                    });
                }

                return(body);
            }
            finally
            {
                loopDepth--;
            }
        }