/// <summary> /// Bind 'this' /// </summary> /// <param name="instance">The instance</param> /// <returns></returns> public LoxFunction Bind(LoxInstance instance) { LoxEnvironment environment = new LoxEnvironment(_closure); environment.Define("this", instance); return(new LoxFunction(_declaration, environment, _is_initializer)); }
public object Call(Interpreter interpreter, IList <object> arguments) { // Environment LoxEnvironment environment = new LoxEnvironment(_closure); // Arguments for (int i = 0; i < _declaration.Parameters.Count(); i++) { environment.Define(_declaration.Parameters[i].Lexeme, arguments[i]); } // Execute try { interpreter.ExecuteBlock(_declaration.Body, environment); } catch (ReturnException return_value) { // We hit a return statement return(return_value.Value); } if (_is_initializer) { return(_closure.GetAt(0, "this")); } return(null); }
public Interpreter(IErrorHandler error_handler) { _error_handler = error_handler; // We need to set this here in C# _environment = _globals; // Add some native functions _globals.Define("clock", new NativeFunctions.Clock()); }
private LoxEnvironment Ancestor(int distance) { LoxEnvironment environment = this; for (int i = 0; i < distance; i++) { environment = environment.Enclosing; } return(environment); }
/// <summary> /// Ececute a block of statements /// </summary> /// <param name="statements">The statements</param> /// <param name="environment">The environment</param> public void ExecuteBlock(IEnumerable <Stmt> statements, LoxEnvironment environment) { // Save the current environment LoxEnvironment previous = this._environment; try { // Set the new environment this._environment = environment; foreach (Stmt statement in statements) { Execute(statement); } } finally { // Restore the old environment this._environment = previous; } }
public object Visit(Stmt.Class stmt) { _environment.Define(stmt.Name.Lexeme, null); // Superclass object superclass = null; if (stmt.Superclass != null) { superclass = Evaluate(stmt.Superclass); if (!(superclass is LoxClass)) { throw new RuntimeErrorException(stmt.Superclass.Name, "Superclass must be a class."); } _environment = new LoxEnvironment(_environment); _environment.Define("super", superclass); } // Methods HashMap <string, LoxFunction> methods = new HashMap <string, LoxFunction>(); foreach (Stmt.Function method in stmt.Methods) { LoxFunction function = new LoxFunction(method, _environment, method.Name.Lexeme.Equals("init")); methods.Put(method.Name.Lexeme, function); } LoxClass @class = new LoxClass(stmt.Name.Lexeme, (LoxClass)superclass, methods); if (superclass != null) { _environment = _environment.Enclosing; } _environment.Assign(stmt.Name, @class); return(null); }
public LoxEnvironment(LoxEnvironment enclosing) { this.Enclosing = enclosing; }
public LoxEnvironment() { this.Enclosing = null; }
public LoxFunction(Stmt.Function declaration, LoxEnvironment closure, bool is_initializer) { _declaration = declaration; _closure = closure; _is_initializer = is_initializer; }