コード例 #1
0
ファイル: Parser.cs プロジェクト: harryr0se/Lox
        private Stmt forStatement()
        {
            consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'.");

            Stmt initializer;

            if (match(TokenType.SEMICOLON))
            {
                initializer = null;
            }
            else if (match(TokenType.VAR))
            {
                initializer = varDeclaration();
            }
            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.RIGHT_PAREN))
            {
                increment = expression();
            }
            consume(TokenType.RIGHT_PAREN, "Expect ')' after for clauses.");

            Stmt body = statement();

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

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

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

            return(body);
        }
コード例 #2
0
ファイル: Interpreter.cs プロジェクト: davidelettieri/Lox
 private object Execute(Stmt statement) => statement.Accept(this);
コード例 #3
0
 public IfStmt(Expr condition, Stmt thenBranch, Stmt elseBranch)
 {
     Condition  = condition;
     ThenBranch = thenBranch;
     ElseBranch = elseBranch;
 }
コード例 #4
0
 public WhileStmt(Expr condition, Stmt body)
 {
     Condition = condition;
     Body      = body;
 }
コード例 #5
0
        private Stmt ForStatement()
        {
            Consume(LEFT_PAREN, "Expect '(' after 'for'.");

            // For loop doesn't get its own statement.
            // Instead we desugar and reduce it to a while loop.

            Stmt initializer;

            if (Match(SEMICOLON))
            {
                initializer = null;
            }
            else if (Match(VAR))
            {
                initializer = VarDeclaration();
            }
            else
            {
                initializer = ExpressionStatement();
            }

            Expr condition = null;

            if (!IsCurrentTokenType(SEMICOLON))
            {
                condition = Expression();
            }
            Consume(SEMICOLON, "Expect ';' after loop condition.");

            Expr increment = null;

            if (!IsCurrentTokenType(RIGHT_PAREN))
            {
                increment = Expression();
            }
            Consume(RIGHT_PAREN, "Expect ')' after for clauses.");

            // Parse the statement body of the for loop.
            Stmt body = Statement();

            if (increment != null)
            {
                // Add increment to be executed after the main body.
                body = new Stmt.Block(new List <Stmt>
                {
                    body,
                    new Stmt.Expression(increment)
                });
            }

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

            body = new Stmt.While(condition, body);
            if (initializer != null)
            {
                // Run the initializer once before the while loop.
                body = new Stmt.Block(new List <Stmt> {
                    initializer, body
                });
            }

            return(body);
        }
コード例 #6
0
ファイル: AstPrinter.cs プロジェクト: mhorskaya/cslox
 public string Print(Stmt stmt)
 {
     return(stmt.Accept(this));
 }
コード例 #7
0
ファイル: Interpreter.cs プロジェクト: mutuware/Lox
 private void Execute(Stmt stmt)
 {
     stmt.Accept(this);
 }
コード例 #8
0
 private void Resolve(Stmt stmt)
 {
     stmt.Accept(this);
 }
コード例 #9
0
 public void Execute(Stmt stmt)
 {
     stmt.Accept(this);
 }
コード例 #10
0
ファイル: Resolver.cs プロジェクト: mutuware/Lox
 public void Resolve(Stmt stmt)
 {
     stmt.Accept(this);
 }
コード例 #11
0
ファイル: Resolver.cs プロジェクト: sula0/Lox
 public void Resolve(Stmt statement) => MatchStatement(statement);
コード例 #12
0
 public If(Expr condition, Stmt thenBranch, Stmt elseBranch)
 {
     this.condition  = condition;
     this.thenBranch = thenBranch;
     this.elseBranch = elseBranch;
 }
コード例 #13
0
 public While(Expr condition, Stmt body)
 {
     this.condition = condition;
     this.body      = body;
 }
コード例 #14
0
 private void execute(Stmt item)
 {
     item.accept(this);
 }
コード例 #15
0
 public string Print(Stmt stmt) => stmt.Accept(this);