コード例 #1
0
ファイル: InterpretingVisitor.cs プロジェクト: mamidon/nlox
 public object Visit(LiteralExpr expr)
 {
     return(expr.Value);
 }
コード例 #2
0
ファイル: Parser.cs プロジェクト: mamidon/nlox
        Stmt ForStatement()
        {
            Consume(TokenType.LeftParen, "Expected opening '('.");

            Stmt initializer = null;

            if (MatchNext(TokenType.Var))
            {
                initializer = VarDeclaration();
            }
            else if (MatchNext(TokenType.SemiColon))
            {
                initializer = null;
            }
            else
            {
                initializer = ExpressionStatement();
            }

            Expr conditional = null;

            if (!MatchNext(TokenType.SemiColon))
            {
                conditional = Expression();
                Consume(TokenType.SemiColon, "Expecting ';' in for loop");
            }


            Expr incrementer = null;

            if (!MatchNext(TokenType.RightParen))
            {
                incrementer = Expression();
                Consume(TokenType.RightParen, "Expected closing ')' in for loop");
            }

            var body = Statement();

            if (incrementer != null)
            {
                body = new BlockStmt(new List <Stmt> {
                    body,
                    new ExpressionStmt(incrementer)
                });
            }

            if (conditional == null)
            {
                conditional = new LiteralExpr(true);
            }
            body = new WhileStmt(conditional, body);

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

            return(body);
        }