Beispiel #1
0
 public ForStatement(DeclareNode initialDeclaration, ExpressionNode condition, ExpressionNode iteration, StatementNode statement)
 {
     InitialDeclaration = initialDeclaration;
     Condition          = condition;
     Iteration          = iteration;
     Statement          = statement;
 }
Beispiel #2
0
        private StatementNode ParseStatement()
        {
            // Return statement
            if (TryTakeFixedToken(FixedToken.Kind.Return))
            {
                // Expression to return
                var expression = ParseExpression();

                // Semi colon
                TakeFixedToken(FixedToken.Kind.SemiColon);

                return(new ReturnNode(expression));
            }
            // If statement
            else if (TryTakeFixedToken(FixedToken.Kind.If))
            {
                // ( expr )
                TakeFixedToken(FixedToken.Kind.ParOpen);
                var conditional = ParseExpression();
                TakeFixedToken(FixedToken.Kind.ParClose);

                // True statement
                var trueStatement = ParseStatement();

                // Optional else + statement
                StatementNode falseStatement = null;
                if (TryTakeFixedToken(FixedToken.Kind.Else))
                {
                    falseStatement = ParseStatement();
                }

                return(new IfStatmentNode(conditional, trueStatement, falseStatement));
            }
            // Block statement
            else if (TryTakeFixedToken(FixedToken.Kind.BracketOpen))
            {
                // Parse statements until we find (and take!) a closing bracket after one
                var blockItems = new List <BlockItemNode>();
                while (!TryTakeFixedToken(FixedToken.Kind.BracketClose))
                {
                    var statement = ParseBlockItem();
                    blockItems.Add(statement);
                }
                ;

                return(new BlockStatementNode(blockItems));
            }
            // Break and continue
            else if (TryTakeFixedToken(FixedToken.Kind.Break))
            {
                TakeFixedToken(FixedToken.Kind.SemiColon);
                return(new BreakStatement());
            }
            else if (TryTakeFixedToken(FixedToken.Kind.Continue))
            {
                TakeFixedToken(FixedToken.Kind.SemiColon);
                return(new ContinueStatement());
            }
            // While statement
            else if (TryTakeFixedToken(FixedToken.Kind.While))
            {
                // while ( expr ) statement
                TakeFixedToken(FixedToken.Kind.ParOpen);
                var expr = ParseExpression();
                TakeFixedToken(FixedToken.Kind.ParClose);
                var statement = ParseStatement();
                return(new WhileStatement(expr, statement));
            }
            // Do while statement
            else if (TryTakeFixedToken(FixedToken.Kind.Do))
            {
                // do statement while expr ;
                var statement = ParseStatement();
                TakeFixedToken(FixedToken.Kind.While);
                var expr = ParseExpression();
                TakeFixedToken(FixedToken.Kind.SemiColon);
                return(new DoWhileStatement(expr, statement));
            }
            // For statement
            else if (TryTakeFixedToken(FixedToken.Kind.For))
            {
                // We have two options
                // for ( decl opt-exp ; opt-exp ) statement
                // for ( opt-exp ; opt-exp ; opt-exp ) statement
                DeclareNode    initialDeclare = null;
                ExpressionNode initialExpr    = null;
                TakeFixedToken(FixedToken.Kind.ParOpen);
                // TODO: Support for other types
                if (PeekFixedToken(FixedToken.Kind.Int))
                {
                    // decl
                    initialDeclare = ParseDeclaration();
                }
                else
                {
                    // opt-exp ;
                    initialExpr = ParseOptionalExpression();
                    TakeFixedToken(FixedToken.Kind.SemiColon);
                }

                var conditionExpr = ParseOptionalExpression();
                TakeFixedToken(FixedToken.Kind.SemiColon);
                var iterationExpr = ParseOptionalExpression();
                TakeFixedToken(FixedToken.Kind.ParClose);
                var statement = ParseStatement();

                // Condition expression must be replaced with a non-zero constant if it's missing
                if (conditionExpr == null)
                {
                    conditionExpr = s_constOneNode;
                }

                if (initialDeclare != null)
                {
                    return(new ForStatement(initialDeclare, conditionExpr, iterationExpr, statement));
                }
                return(new ForStatement(initialExpr, conditionExpr, iterationExpr, statement));
            }

            // Treat it as an expression statement
            return(ParseExpressionStatement());
        }