public object Call(Interpreter interpreter, List <object> arguments)
        {
            VariableEnvironment environment = new VariableEnvironment(closure);

            for (int i = 0; i < declaration.parameters.Count; i++)
            {
                environment.Define(declaration.parameters[i].lexeme, arguments[i]);
            }
            try
            {
                interpreter.ExecuteBlock(declaration.body, environment);
            }
            catch (Return returnValue)
            {
                // Allow an empty return ("return;") in a constructor. When this happens, return "this".
                if (isInitializer)
                {
                    return(closure.GetAt(0, "this"));
                }

                return(returnValue.value);
            }

            return(null);
        }
Exemplo n.º 2
0
 private object LookUpVariable(Token name, Expr expr)
 {
     if (locals.TryGetValue(expr, out int distance))
     {
         return(environment.GetAt(distance, name.lexeme));
     }
     else
     {
         return(globals.Get(name));
     }
 }