Stmt ParseClassDeclaration() { Token name = consume(TokenType.IDENTIFIER, "Expect class name."); Expr.Variable superclass = null; if (match(TokenType.LESS)) { Token superclassName = consume(TokenType.IDENTIFIER, "Expect superclass name after '<'."); superclass = new Expr.Variable(superclassName); } consume(TokenType.LEFT_BRACE, "Expect '{' before class body."); List <Stmt.Function> methods = new List <Stmt.Function>(); List <Stmt.Function> staticMethods = new List <Stmt.Function>(); while (peek().type != TokenType.RIGHT_BRACE && !isAtEnd()) { if (match(TokenType.CLASS)) { staticMethods.Add(ParseFuncDeclaration("static method")); } else { methods.Add(ParseFuncDeclaration("method")); } } consume(TokenType.RIGHT_BRACE, "Expect '}' after class body."); return(new Stmt.Class(name, superclass, staticMethods, methods)); }
public Class(Token name, Expr.Variable superclass, List <Stmt.Function> staticMethods, List <Stmt.Function> methods) { this.name = name; this.superclass = superclass; this.staticMethods = staticMethods; this.methods = methods; }
public object visitVariableExpr(Expr.Variable expr) { string name = expr.name.lexeme; if (scopes.Count > 0 && scopes[scopes.Count - 1].TryGetValue(name, out bool resolved) && !resolved) { Lox.ReportError(expr.name, "Can't read local variable in its own initializer."); } ResolveLocal(expr, name, AccessType.RHS); return(null); }
public string visitVariableExpr(Expr.Variable expr) { return(parenthesize(expr.name.lexeme)); }
public object visitVariableExpr(Expr.Variable expr) { // Variables and functions exist in the same environment, so unlike some other languages, // Lox does not allow a function to have the same name as a variable in the same environment. return(LookUpVariable(expr)); }