Beispiel #1
0
        private StmtResult IfStatement() =>

        /*
         * ifStmt → "if" "(" expression ")" statement
         *          ( "else" statement )? ;
         */
        from lParen in Consume(TokenType.LeftParen, "Expected '(' after 'if'.")
        from condition in Expression()
        from rParent in Consume(TokenType.RightParen, "Expected ')' after condition.")
        from thenBranch in Statement()
        from elseBranch in Match(TokenType.Else) ? Statement() : StmtResult.Ok(null)
        select new IfStmt(condition, thenBranch, elseBranch) as Stmt;
Beispiel #2
0
        public override async Task <StmtResult> Eval(EvalContext context)
        {
            // Debug.Log("block begins");
            await base.Eval(context);

            foreach (Stmt stmt in Statements)
            {
                //Debug.Log("block stmt begins");
                StmtResult result = await stmt.Eval(context);

                //Debug.Log("block stmt ends");
                if (result == StmtResult.Break)
                {
                    return(StmtResult.Break);
                }
            }
            return(StmtResult.None);
        }
Beispiel #3
0
        public override async Task <StmtResult> Eval(EvalContext context)
        {
            //Debug.Log("repeat begins");
            await base.Eval(context);

            while (true)
            {
                //Debug.Log("repeatbody begins");
                StmtResult result = await Body.Eval(context);

                if (result == StmtResult.Break || context.Target.isActiveAndEnabled == false)
                {
                    return(StmtResult.None);
                }
                Block newbody = (Block)Body;
                if (newbody.Statements.Count == 1 && newbody.Statements[0] is If)
                {
                    //임시방편
                    await new WaitForSeconds(0.2f);
                }
            }
        }
Beispiel #4
0
        private StmtResult ForStatement()
        {
            /*
             * forStmt → "for" "(" ( varDecl | exprStmt | ";" )
             *           expression? ";"
             *           expression? ")" statement ;
             */
            StmtResult ForInitializer()
            {
                if (Match(TokenType.Semicolon))
                {
                    return(StmtResult.Ok(null));
                }
                if (Match(TokenType.Var))
                {
                    return(VarDeclaration());
                }
                return(ExpressionStatement());
            }

            ExprResult ForCondition()
            {
                if (Check(TokenType.Semicolon))
                {
                    return(ExprResult.Ok(null));
                }
                return(Expression());
            }

            ExprResult ForIncrementer()
            {
                if (Check(TokenType.RightParen))
                {
                    return(ExprResult.Ok(null));
                }
                return(Expression());
            }

            Stmt BuildForAst(Stmt initializer, Expr condition, Expr incrementer, Stmt statement)
            {
                // express `for(var i = 0; i < 10; i++) { statement }` as a while loop;
                // var i = 0;
                // while(i<10) {
                //   { statement; }
                //   i++;
                // }
                var body = incrementer is null ? statement : new BlockStmt(new List <Stmt>()
                {
                    statement, new ExpressionStmt(incrementer)
                });
                var cond  = condition ?? new LiteralExpr(true);
                var loop  = new WhileStmt(cond, body);
                var block = initializer is null ? loop : new BlockStmt(new List <Stmt>()
                {
                    initializer, loop
                }) as Stmt;

                return(block);
            }

            return
                (from lParen in Consume(TokenType.LeftParen, "Expected '(' after 'for'.")
                 from initializer in ForInitializer() //first semi is parsed as part of initializer
                 from condition in ForCondition()
                 from semi2 in Consume(TokenType.Semicolon, "Expected ';' after condition.")
                 from incrementer in ForIncrementer()
                 from rParen in Consume(TokenType.RightParen, "Expected ')' after increment expression.")
                 from statement in Statement()
                 select BuildForAst(initializer, condition, incrementer, statement));
        }