예제 #1
0
        protected object EvaluateFunction(Environment env, UserFunction function)
        {
            if (function == null)
            {
                throw new ArgumentNullException(nameof(function), "null function");
            }

            ParameterNode parameters = function.Parameters;

            Tuple arguments = Evaluate(env) as Tuple;

            if (arguments == null)
            {
                throw new LepException("bad expression argument", this);
            }

            if (arguments.Count != parameters.Count)
            {
                throw new LepException("bad number of argument", this);
            }

            Environment inner = function.CreateEnvironment();

            for (int i = 0; i < arguments.Count; i++)
            {
                parameters.Evaluate(inner, i, arguments[i]);
            }

            try { return(function.Body.Evaluate(inner)); }
            catch (JumpSignal signal)
            {
                if (signal.SignalType == JumpSignal.ReturnSignal)
                {
                    return(signal.ReturnValue);
                }
                else
                {
                    throw;
                }
            }
        }
예제 #2
0
        protected object EvaluateFunction(Environment env, UserFunction function)
        {
            if (function == null)
            {
                throw new ArgumentNullException(nameof(function), "null function");
            }

            ParameterNode parameters = function.Parameters;

            if (Count != parameters.Count)
            {
                throw new LepException("bad number of arguments", this);
            }

            Environment inner = function.CreateEnvironment();

            int count = 0;

            foreach (IAstNode node in this)
            {
                parameters.Evaluate(inner, count++, node.Evaluate(env));
            }

            try { return(function.Body.Evaluate(inner)); }
            catch (JumpSignal signal)
            {
                if (signal.SignalType == JumpSignal.ReturnSignal)
                {
                    return(signal.ReturnValue);
                }
                else
                {
                    throw;
                }
            }
        }