コード例 #1
0
 public SharpLoxCallable(IEnumerable <Token> parameters, Block body, LoxEnvironment closure)
 {
     _parameters = parameters.ToList();
     _body       = body;
     _closure    = closure;
     Arity       = _parameters.Count();
 }
コード例 #2
0
        public object Call(Interpreter interpreter, IEnumerable <object> arguments)
        {
            var funcEnv = new LoxEnvironment {
                Parent = _closure
            };

            foreach (var(token, arg) in _parameters.Zip(arguments, ValueTuple.Create))
            {
                funcEnv.Define(token.Lexeme, arg);
            }

            try
            {
                interpreter.ExecuteBlock(_body.Statements, funcEnv);
            }
            catch (ReturnValue returnValue)
            {
                return(returnValue.Value);
            }

            return(null !);
        }