Ejemplo n.º 1
0
        public static Symbol Execute(FunctionSymbol symbol, Stack <Symbol> arguments)
        {
            if (cached_functions == null)
            {
                LoadFunctionCache();
            }

            if (!cached_functions.ContainsKey(symbol.Name))
            {
                throw new InvalidFunctionException(symbol.Name);
            }

            FunctionCache function = cached_functions[symbol.Name];

            if (arguments.Count != function.ArgumentCount && function.ArgumentCount >= 0)
            {
                throw new ArgumentException(symbol.Name);
            }

            if (function.RawSymbols)
            {
                if (function.ArgumentCount == -1)
                {
                    return((Symbol)function.Method.Invoke(null, new object [] { arguments.ToArray() }));
                }
                else
                {
                    return((Symbol)function.Method.Invoke(null, arguments.ToArray()));
                }
            }

            ArrayList resolved_arguments = new ArrayList();

            while (arguments.Count > 0)
            {
                Symbol argument = arguments.Pop();
                if (!(argument is ValueSymbol))
                {
                    throw new ArgumentException("argument must be ValueSymbol");
                }
                resolved_arguments.Add((argument as ValueSymbol).Value);
            }

            double retval;

            if (function.ArgumentCount == -1)
            {
                retval = (double)function.Method.Invoke(null, new object [] { resolved_arguments.ToArray(typeof(double)) });
            }
            else
            {
                retval = (double)function.Method.Invoke(null, resolved_arguments.ToArray());
            }

            return(new NumberSymbol(retval));
        }
Ejemplo n.º 2
0
        public static Symbol Execute(FunctionSymbol symbol, Stack<Symbol> arguments)
        {
            if(cached_functions == null) {
                LoadFunctionCache();
            }

            if(!cached_functions.ContainsKey(symbol.Name)) {
                throw new InvalidFunctionException(symbol.Name);
            }

            FunctionCache function = cached_functions[symbol.Name];
            if(arguments.Count != function.ArgumentCount && function.ArgumentCount >= 0) {
                throw new ArgumentException(symbol.Name);
            }

            if(function.RawSymbols) {
                if(function.ArgumentCount == -1) {
                    return (Symbol)function.Method.Invoke(null, new object [] { arguments.ToArray() });
                } else {
                    return (Symbol)function.Method.Invoke(null, arguments.ToArray());
                }
            }

            ArrayList resolved_arguments = new ArrayList();

            while(arguments.Count > 0) {
                Symbol argument = arguments.Pop();
                if(!(argument is ValueSymbol)) {
                    throw new ArgumentException("argument must be ValueSymbol");
                }
                resolved_arguments.Add((argument as ValueSymbol).Value);
            }

            double retval;

            if(function.ArgumentCount == -1) {
                retval = (double)function.Method.Invoke(null, new object [] { resolved_arguments.ToArray(typeof(double)) });
            } else {
                retval = (double)function.Method.Invoke(null, resolved_arguments.ToArray());
            }

            return new NumberSymbol(retval);
        }