Example #1
0
        private TreeNode EvaluateFunction(FunctionNode node)
        {
            object handler = null;

            TreeNode parent = node.Parent;

            TreeNode [] args = null;

            if (!functions.ContainsKey(node.Function))
            {
                handler = ResolveFunction(node);
                if (handler == null)
                {
                    throw new InvalidFunctionException(node.Function);
                }
            }
            else
            {
                handler = functions[node.Function];
            }

            if (parent.Children[0] == node)
            {
                args = new TreeNode[parent.ChildCount - 1];

                for (int i = 0; i < args.Length; i++)
                {
                    args[i] = parent.Children[i + 1];

                    if (handler is MethodInfoContainer && !(handler as MethodInfoContainer).EvaluateVariables)
                    {
                        continue;
                    }
                }
            }

            if (handler is FunctionNode)
            {
                return((handler as FunctionNode).Evaluate(this, args));
            }
            else if (handler is SExpFunctionHandler)
            {
                return(((SExpFunctionHandler)handler)(this, args));
            }
            else if (handler is MethodInfoContainer)
            {
                MethodInfoContainer container = (MethodInfoContainer)handler;
                return((TreeNode)container.MethodInfo.Invoke(container.Object, new object [] { args }));
            }
            else
            {
                throw new InvalidFunctionException(String.Format(
                                                       "Unknown runtime method handler type {1}", handler.GetType()));
            }
        }
Example #2
0
        public void RegisterFunction(object o, MethodInfo method, string [] names, bool evaluateVariables)
        {
            var container = new MethodInfoContainer {
                MethodInfo        = method,
                Object            = o,
                EvaluateVariables = evaluateVariables
            };

            foreach (string name in names)
            {
                if (functions.ContainsKey(name))
                {
                    functions.Remove(name);
                }

                functions.Add(name, container);
            }
        }
Example #3
0
 private void StaticallyCacheMethodInfosFor(Type[] wrappedTypes)
 {
     lock (MethodInfos)
     {
         foreach (var wrappedType in wrappedTypes)
         {
             if (MethodInfos.ContainsKey(wrappedType))
             {
                 return;
             }
             MethodInfos[wrappedType] = new MethodInfoContainer(
                 wrappedType
                 .GetMethods(BindingFlags.Instance | BindingFlags.Public)
                 // TODO: handle method overloads, which this won't
                 .Distinct(new MethodInfoComparer())
                 .ToArray()
                 );
         }
     }
 }
Example #4
0
        public void RegisterFunction(object o, MethodInfo method, string [] names, bool evaluateVariables)
        {
            MethodInfoContainer container = new MethodInfoContainer();
            container.MethodInfo = method;
            container.Object = o;
            container.EvaluateVariables = evaluateVariables;

            foreach(string name in names) {
                if(functions.ContainsKey(name)) {
                    functions.Remove(name);
                }

                functions.Add(name, container);
            }
        }