internal LoxFunction bind(LoxInstance instance) { Envir envir = new Envir(Closure); envir.define("this", instance); return(new LoxFunction(Declaration, envir, IsInitializer)); }
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 object call(Interpreter interpreter, List <object> args) { Envir envir = new Envir(Closure); for (int i = 0; i < Declaration.Parameters.Count; i++) { envir.define(Declaration.Parameters[i].Lexeme, args[i]); } try { interpreter.executeBlock(Declaration.Body, envir); } catch (LoxReturn returnValue) { if (IsInitializer) { return(Closure.getAt(0, "this")); } return(returnValue.Value); } if (IsInitializer) { return(Closure.getAt(0, "this")); } return(null); }
public Interpreter() { env = globals; globals.define("clock", new Functions.ClockFunction()); }