Esempio n. 1
0
        private FunctionFactory()
        {
            functions = new Dictionary <string, IFunctionCog>();

            var cogType = typeof(IFunctionCog);
            var cogs    = AppDomain.CurrentDomain.GetAssemblies()
                          .Where(p => !p.IsDynamic)
                          .SelectMany(s => s.GetExportedTypes())
                          .Where(p => cogType.IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract);

            foreach (Type t in cogs)
            {
                IFunctionCog cog = (IFunctionCog)Activator.CreateInstance(t);
                foreach (string name in cog.FunctionName)
                {
                    functions.Add(name.ToLower(), cog);
                }
            }
        }
Esempio n. 2
0
        public EvalReturn Execute(string function, ref ControllerState cs)
        {
            // verify cog exists
            if (!functions.ContainsKey(function))
            {
                throw new ArgumentOutOfRangeException(function,
                                                      String.Format(cs.Config.strings["FunNotFound"], function));
            }

            IFunctionCog cog = functions[function];

            if (cog.NumArgs > 0) //Don't bother checking count & type of args if you take none.
            {
                // check number of arguments
                if (cs.stack.Count < cog.NumArgs)
                {
                    return(new EvalReturn(Response.Error,
                                          String.Format(cs.Config.strings["FunArgNumErr"], cog.FunctionName[0], cog.NumArgs),
                                          typeof(FunctionInstance)));
                }
                // Check the types of arguments
                Type[] argTypes = new Type[cog.NumArgs];
                int    topStack = cs.stack.Count;

                // Get all the types
                for (int i = 0; i < cog.NumArgs; i++)
                {
                    argTypes[i] = cs.stack.ElementAt(topStack - (topStack - i)).GetType();
                }

                bool allowed = false;
                foreach (Type[] t in cog.AllowedTypes)
                {
                    if (t.SequenceEqual(argTypes) || t.Contains(typeof(AnyType)))
                    {
                        allowed = true;
                        break;
                    }
                }

                if (!allowed)
                {
                    string argListString = "(";
                    foreach (Type t in argTypes)
                    {
                        argListString += t.ToString() + " ";
                    }
                    argListString += ")";

                    return(new EvalReturn(Response.Error,
                                          String.Format(cs.Config.strings["UnsupportedTypes"], argListString),
                                          typeof(FunctionInstance)));
                }
            }
            // call cog Execute
            ICalculonType retVal = cog.Execute(ref cs);

            if (retVal.GetType() == typeof(ErrorType))
            {
                return(new EvalReturn(Response.Error,
                                      retVal.Display, typeof(ErrorType)));
            }
            if (retVal.GetType() != typeof(EmptyType))
            {
                cs.stack.Push(retVal);
            }
            return(new EvalReturn(Response.Ok, retVal.Display, retVal.GetType()));
        }