public object visitClassStmt(Stmt.Class stmt) { object superClass = null; if (stmt.SuperClass != null) { superClass = evaluate(stmt.SuperClass); if (!(superClass is LoxClass)) { throw new RuntimeError(stmt.SuperClass.Name, "Superclass must be a class."); } } env.define(stmt.Name.Lexeme, null); if (stmt.SuperClass != null) { env = new Envir(env); env.define("super", superClass); } Dictionary <string, LoxFunction> methods = new Dictionary <string, LoxFunction>(); foreach (Stmt.Function method in stmt.Methods) { LoxFunction function = new LoxFunction(method, env, method.Name.Lexeme == "init"); methods[method.Name.Lexeme] = function; } LoxClass klass = new LoxClass(stmt.Name.Lexeme, (LoxClass)superClass, methods); if (stmt.SuperClass != null) { env = env.Enclosing; } env.assign(stmt.Name, klass); return(null); }
public void assign(Token name, object value) { if (Values.ContainsKey(name.Lexeme)) { Values[name.Lexeme] = value; return; } if (Enclosing != null) { Enclosing.assign(name, value); return; } throw new RuntimeError(name, $"Undefined variable '{name.Lexeme}'."); }