示例#1
0
        private Stmt classDeclaration()
        {
            Token name = consume(TokenType.TOKEN_IDENTIFIER, "Expect class name.");

            Expr.Variable superclass = null;
            if (match(TokenType.TOKEN_LESS))
            {
                consume(TokenType.TOKEN_IDENTIFIER, "Expect superclass name.");
                superclass = new Expr.Variable(previous());
            }

            consume(TokenType.TOKEN_LEFT_BRACE, "Expect '{' before class body.");

            List <Stmt.Function> methods = new List <Stmt.Function>();

            while (!check(TokenType.TOKEN_RIGHT_BRACE) && !isAtEnd())
            {
                methods.Add(function("method"));
            }

            consume(TokenType.TOKEN_RIGHT_BRACE, "Expect '}' after class body.");

            return(new Stmt.Class(name, superclass, methods));
        }
示例#2
0
 public Class(Token name, Expr.Variable superclass, List <Stmt.Function> methods)
 {
     this.name       = name;
     this.superclass = superclass;
     this.methods    = methods;
 }