/// <summary> /// Consumes an if statement. /// </summary> private IfStatement IfStatement() { Consume(TokenType.LeftParen, "Expected '(' after 'if'."); var condition = Expression(); Consume(TokenType.RightParen, "Expected ')' after 'if' condition."); var thenBranch = Statement(); StatementBase elseBranch = null; if (NextTokenMatches(TokenType.Else)) { elseBranch = Statement(); } return(new IfStatement(condition, thenBranch, elseBranch)); }
public IfStatement(ExpressionBase condition, StatementBase thenBranch, StatementBase elseBranch) { Condition = condition; ThenBranch = thenBranch; ElseBranch = elseBranch; }
public WhileStatement(ExpressionBase condition, StatementBase body) { Condition = condition; Body = body; }
/// <summary> /// Runs the statement's <see cref="StatementBase.Accept(IStatementVisitor)"/> method. /// </summary> /// <param name="statement">Statement to execute.</param> private void Execute(StatementBase statement) { statement.Accept(this); }
/// <summary> /// Consumes a for statement (desugars into a block-enclosed while loop). /// </summary> private StatementBase ForStatement() { Consume(TokenType.LeftParen, "Expected '(' after 'for'."); StatementBase initializer = null; if (!NextTokenMatches(TokenType.SemiColon)) { initializer = NextTokenMatches(TokenType.Var) ? VarDeclaration() : ExpressionStatement(); } ExpressionBase condition = null; if (!PeekMatches(TokenType.SemiColon)) { condition = Expression(); } Consume(TokenType.SemiColon, "Expected ';' after for loop condition."); ExpressionBase increment = null; if (!PeekMatches(TokenType.RightParen)) { increment = Expression(); } Consume(TokenType.RightParen, "Expected ')' after for loop clauses."); try { _loopDepth++; var body = Statement(); if (increment != null) { // wrap the body in a block with the increment executing at the end body = new BlockStatement(new[] { body, new ExpressionStatement(increment) }); } // wire up a while loop using the condition and body condition ??= new LiteralExpression(true); body = new WhileStatement(condition, body); if (initializer != null) { // wrap the body in another block with the increment executing first body = new BlockStatement(new[] { initializer, body }); } return(body); } finally { _loopDepth--; } }