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 object VisitAssignExpr(Expr.Assign expr) { var value = Evaluate(expr.Value); if (_locals.TryGetValue(expr, out var distance)) { _environment.AssignAt(distance, expr.Name, value); } else { _globals.Assign(expr.Name, value); } return(value); }