public object VisitClassStmt(Stmt.Class stmt) { _environment.Define(stmt.Name.Lexeme, null); object superclass = null; if (stmt.Superclass != null) { superclass = Evaluate(stmt.Superclass); if (!(superclass is LoxClass)) { throw new RuntimeError(stmt.Name, "Superclass must be a class."); } _environment = new EnvironmentScope(_environment); _environment.Define("super", superclass); } var methods = new Dictionary <string, LoxFunction>(); foreach (var method in stmt.Methods) { var function = new LoxFunction(method, _environment, method.Name.Lexeme == "init"); methods[method.Name.Lexeme] = function; } var klass = new LoxClass(stmt.Name.Lexeme, (LoxClass)superclass, methods); if (superclass != null) { _environment = _environment.Enclosing; } _environment.Assign(stmt.Name, klass); return(null); }
public string VisitClassStmt(Stmt.Class stmt) { var builder = new StringBuilder(); builder.Append("(class " + stmt.Name.Lexeme); //> Inheritance omit if (stmt.Superclass != null) { builder.Append(" < " + Print(stmt.Superclass)); } //< Inheritance omit foreach (var method in stmt.Methods) { builder.Append(" " + Print(method)); } builder.Append(")"); return(builder.ToString()); }
public object VisitClassStmt(Stmt.Class stmt) { Declare(stmt.Name); Define(stmt.Name); var enclosingClass = _currentClass; _currentClass = ClassType.CLASS; if (stmt.Superclass != null) { _currentClass = ClassType.SUBCLASS; Resolve(stmt.Superclass); BeginScope(); _scopes.Peek()["super"] = true; } BeginScope(); _scopes.Peek()["this"] = true; foreach (var method in stmt.Methods) { var declaration = FunctionType.METHOD; if (method.Name.Lexeme == "init") { declaration = FunctionType.INITIALIZER; } ResolveFunction(method, declaration); } EndScope(); if (stmt.Superclass != null) { EndScope(); } _currentClass = enclosingClass; return(null); }