public object VisitClassStmt(Stmt.ClassStmt stmt) { var enclosingClass = _currentClass; _currentClass = ClassType.CLASS; Declare(stmt.Name); Define(stmt.Name); if (stmt.Superclass != null && stmt.Name.Lexeme.Equals(stmt.Superclass.Name.Lexeme)) { Lox.Error(stmt.Superclass.Name, "A class cannot inherit from itself."); } if (stmt.Superclass != null) { _currentClass = ClassType.SUBCLASS; Resolve(stmt.Superclass); } if (stmt.Superclass != null) { BeginScope(); Scopes.Peek()["super"] = true; } BeginScope(); Scopes.Peek()["this"] = true; foreach (var method in stmt.Methods) { var declaration = FunctionType.METHOD; if (method.Name.Lexeme.Equals("init")) { declaration = FunctionType.INITIALIZER; } ResolveFunction(method, declaration); } EndScope(); if (stmt.Superclass != null) { EndScope(); } _currentClass = enclosingClass; return(null); }
public string VisitClassStmt(Stmt.ClassStmt stmt) { var builder = new StringBuilder(); builder.Append($"(class {stmt.Name.Lexeme}"); if (stmt.Superclass != null) { builder.Append($" < {Print(stmt.Superclass)}"); } foreach (var method in stmt.Methods) { builder.Append($" {Print(method)}"); } builder.Append(")"); return(builder.ToString()); }