Exemplo n.º 1
0
        public ICalculonType Execute(ref ControllerState cs)
        {
            ICalculonType rhs = cs.stack.Pop();
            ICalculonType lhs = cs.stack.Pop();

            Type[] argTypes = new Type[] { lhs.GetType(), rhs.GetType() };
            if (argTypes.SequenceEqual(new Type[] { typeof(Real), typeof(Real) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(Real), typeof(RealConstant) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(RealConstant), typeof(Real) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(Real), typeof(Integer) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(RealConstant), typeof(Integer) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(Integer), typeof(Real) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(Integer), typeof(RealConstant) })
                )
            {
                Real left  = new Real(lhs);
                Real right = new Real(rhs);
                return(left.Pow(right));
            }
            if (argTypes.SequenceEqual(new Type[] { typeof(Integer), typeof(Integer) }))
            {
                return(((Integer)lhs).Pow((Integer)rhs));
            }
            throw new ArgumentException(
                      String.Format(Config.handle.strings["UnsupportedTypes"], argTypes));
        }
Exemplo n.º 2
0
        public override ICalculonType Execute(ref ControllerState cs)
        {
            ICalculonType rhs = cs.stack.Pop();
            ICalculonType lhs = cs.stack.Pop();

            Type[] argTypes = new Type[] { lhs.GetType(), rhs.GetType() };
            if (argTypes.SequenceEqual(new Type[] { typeof(Integer), typeof(Integer) }))
            {
                if (((Integer)lhs).Mod((Integer)rhs).data == 0)
                {
                    return(Op((Integer)lhs, (Integer)rhs));
                }
                else
                {
                    // maybe include config option to convert to rational?
                    return(Op(new Real(lhs), new Real(rhs)));
                }
            }
            else
            {
                //push back onto stack
                cs.stack.Push(lhs);
                cs.stack.Push(rhs);
                //run as normal
                return(base.Execute(ref cs));
            }
        }
Exemplo n.º 3
0
        public ICalculonType Execute(ref ControllerState cs)
        {
            ICalculonType input = cs.stack.Pop();

            if (input.GetType() == typeof(Real))
            {
                return(((Real)input).Ln());
            }
            if (input.GetType() == typeof(RealConstant))
            {
                return(((RealConstant)input).ToReal().Ln());
            }
            if (input.GetType() == typeof(Integer))
            {
                return(((Integer)input).Ln());
            }
            throw new NotImplementedException();
        }
Exemplo n.º 4
0
        public virtual ICalculonType Execute(ref ControllerState cs)
        {
            ICalculonType rhs = cs.stack.Pop();
            ICalculonType lhs = cs.stack.Pop();

            Type[] argTypes = new Type[] { lhs.GetType(), rhs.GetType() };

            if (argTypes.SequenceEqual(new Type[] { typeof(Integer), typeof(Integer) }))
            {
                return(Op((Integer)lhs, (Integer)rhs));
            }
            if (argTypes.SequenceEqual(new Type[] { typeof(Integer), typeof(Real) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(Rational), typeof(Real) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(RealConstant), typeof(Real) }))
            {
                return(Op(new Real(lhs), (Real)rhs));
            }
            if (argTypes.SequenceEqual(new Type[] { typeof(Integer), typeof(Rational) }))
            {
                return(Op(new Rational((Integer)lhs), (Rational)rhs));
            }
            if (argTypes.SequenceEqual(new Type[] { typeof(Real), typeof(Integer) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(Real), typeof(Rational) }))
            {
                return(Op((Real)lhs, new Real(rhs)));
            }
            if (argTypes.SequenceEqual(new Type[] { typeof(Real), typeof(Real) }))
            {
                return(Op((Real)lhs, (Real)rhs));
            }
            if (argTypes.SequenceEqual(new Type[] { typeof(Rational), typeof(Integer) }))
            {
                return(Op((Rational)lhs, new Rational((Integer)rhs)));
            }
            if (argTypes.SequenceEqual(new Type[] { typeof(Rational), typeof(Rational) }))
            {
                return(Op((Rational)lhs, (Rational)rhs));
            }
            if (argTypes.SequenceEqual(new Type[] { typeof(RealConstant), typeof(Integer) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(RealConstant), typeof(Rational) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(RealConstant), typeof(RealConstant) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(Integer), typeof(RealConstant) }) ||
                argTypes.SequenceEqual(new Type[] { typeof(Rational), typeof(RealConstant) }))
            {
                return(Op(new Real(lhs), new Real(rhs)));
            }
            if (argTypes.SequenceEqual(new Type[] { typeof(Real), typeof(RealConstant) }))
            {
                return(Op((Real)lhs, new Real(rhs)));
            }
            throw new ArgumentException(String.Format(
                                            Config.handle.strings["ArithArgUnhandled"], argTypes));
        }
Exemplo n.º 5
0
        public ICalculonType Execute(ref ControllerState cs)
        {
            ICalculonType input = cs.stack.Pop();

            if (input.GetType() == typeof(Rational))
            {
                return(((Rational)input).Inv());
            }
            else
            {
                Real realInput = new Real(input);
                return(realInput.Inv());
            }
        }
Exemplo n.º 6
0
        public bool WriteConfigToFile()
        {
            if (AllowFilesystemWrites)
            {
                ICalculonType val = this["\"UseFile\""];
                if ((val.GetType() == typeof(Literal)) &&
                    (val.Display.ToLower() == "\"true\""))
                {
                    SqliteConnectionStringBuilder source =
                        new SqliteConnectionStringBuilder();
                    source.DataSource = backupfile;
                    using (SqliteConnection file =
                               new SqliteConnection(source.ToString()))
                    {
                        file.Open();
                        memoryConnection.BackupDatabase(file);
                        file.Close();
                    }
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 7
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()));
        }