Example #1
0
 public While(Expr condition, Stmt body)
 {
     Condition = condition;
     Body      = body;
 }
Example #2
0
 public StmtIf(Expr _condition, Stmt _thenBranch, Stmt _elseBranch)
 {
     condition  = _condition;
     thenBranch = _thenBranch;
     elseBranch = _elseBranch;
 }
Example #3
0
 void execute(Stmt s)
 {
     s.accept(this);
 }
Example #4
0
 private void Execute(Stmt stmt)
 {
     stmt.Accept(this);
 }
Example #5
0
 public StmtWhile(Expr _condition, Stmt _body)
 {
     condition = _condition;
     body      = _body;
 }
Example #6
0
 public If(Expr condition, Stmt thenBranch, Stmt elseBranch)
 {
     this.condition  = condition;
     this.thenBranch = thenBranch;
     this.elseBranch = elseBranch;
 }
Example #7
0
        private Stmt ForStatement()
        {
            //forStmt         → "for" "(" ( varDecl | exprStmt | ";" ) expression? ";" expression? ")" statement ;
            if (!TryParse(LEFT_PAREN))
            {
                throw new ParserError(Current, "'(' expected after 'for'.");
            }

            //initializer is optional
            Stmt initializer = null;

            if (!TryParse(SEMICOLON)) //initializer skipped?
            {
                if (TryParse(VAR))
                {
                    initializer = VarDeclaration();
                }
                else
                {
                    initializer = ExpressionStatement();
                }
            }

            //condition is optional
            Expr condition = null;

            if (!TryParse(SEMICOLON)) //condition skipped?
            {
                condition = Expression();
            }
            if (!TryParse(SEMICOLON))
            {
                throw new ParserError(Current, "';' expected after 'for' loop condition.");
            }

            //increment is also optional
            Expr increment = null;

            if (!TryParse(RIGHT_PAREN)) //increment skipped?
            {
                increment = Expression();
            }
            if (!TryParse(RIGHT_PAREN))
            {
                throw new ParserError(Current, "')' expected after 'for' clauses.");
            }

            //For is represented as Syntax-Tree build from primitives
            //{
            //    var i = 0;
            //    while (i < 10)
            //    {
            //        print i;
            //        i = i + 1;
            //    }
            //}

            Stmt body = Statement();

            //combine body and increment in the inner block
            if (increment != null)
            {
                body = new Block(new List <Stmt>
                {
                    body,
                    new ExpressionStatement(increment)
                });
            }

            //wrap body in while loop with condition
            if (condition != null)
            {
                body = new WhileStatement(condition, body);
            }

            //if there's an initializer combine it with body to form the outer block
            if (initializer != null)
            {
                body = new Block(new List <Stmt>
                {
                    initializer,
                    body
                });
            }

            return(body);
        }
Example #8
0
 void Resolve(Stmt stmt)
 {
     stmt.Accept(this);
 }
Example #9
0
 public While(Expr condition, Stmt body)
 {
     this.condition = condition;
     this.body      = body;
 }
Example #10
0
 public If(Expr Condition, Stmt ThenBranch, Stmt ElseBranch)
 {
     this.Condition  = Condition;
     this.ThenBranch = ThenBranch;
     this.ElseBranch = ElseBranch;
 }
Example #11
0
 public While(Expr Condition, Stmt Body)
 {
     this.Condition = Condition;
     this.Body      = Body;
 }
Example #12
0
 public IfStatement(Expr condition, Stmt thenBranch, Stmt elseBranch)
 {
     Condition  = condition;
     ThenBranch = thenBranch;
     ElseBranch = elseBranch;
 }
Example #13
0
 public WhileStatement(Expr condition, Stmt body)
 {
     Condition = condition;
     Body      = body;
 }
Example #14
0
 private void Resolve(Stmt statement)
 {
     statement.Accept(this);
 }