Exemple #1
0
        public object Call(Interpreter interpreter, List <object> arguments)
        {
            Environment environment = new Environment(_closure);

            for (int i = 0; i < _declaration.Parameter.Count; i++)
            {
                environment.Define(_declaration.Parameter[i].Lexeme, arguments[i]);
            }

            try
            {
                interpreter.ExecuteBlock(_declaration.Body, environment);
            }
            catch (Return returnValue)
            {
                if (_isInitializer)
                {
                    return(_closure.GetAt(0, "this"));
                }

                return(returnValue.Value);
            }

            if (_isInitializer)
            {
                return(_closure.GetAt(0, "this"));
            }

            return(null);
        }
Exemple #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));
     }
 }