Ejemplo n.º 1
0
 public object Visit(Stmt.Expression _expression)
 {
     Resolve(_expression.expression);
     return(null);
 }
Ejemplo n.º 2
0
        private Stmt ForStatement()
        {
            Consume(TokenType.LeftParen, "Expect '(' after 'for'.");

            Stmt initializer;

            if (Match(TokenType.Semicolon))
            {
                // None is defined
                initializer = null;
            }
            else if (Match(TokenType.Var))
            {
                initializer = VarDeclartion();
            }
            else
            {
                initializer = ExpressionStatement();
            }

            Expr condition = null;

            if (!Check(TokenType.Semicolon))
            {
                condition = Expression();
            }
            Consume(TokenType.Semicolon, "Expect ';' after loop condition");

            Expr increment = null;

            if (!Check(TokenType.RightParen))
            {
                increment = Expression();
            }
            Consume(TokenType.RightParen, "Expect ')' after for clauses.");

            Exception exception = null;

            try
            {
                m_LoopDepth++;
                Stmt body = Statement();

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

                if (condition == null)
                {
                    condition = new Expr.Literal(true);
                }

                body = new Stmt.While(condition, body);

                if (initializer != null)
                {
                    List <Stmt> content = new List <Stmt>()
                    {
                        initializer, body
                    };
                    body = new Stmt.Block(content);
                }

                return(body);
            }
            catch (Exception e)
            {
                exception = e;
            }
            finally
            {
                m_LoopDepth--;
            }

            throw exception;
        }
Ejemplo n.º 3
0
 public object Visit(Stmt.Expression _expression)
 {
     Evaluate(_expression.expression);
     return(null);
 }