Esempio n. 1
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));
            }
        }
Esempio n. 2
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));
        }
Esempio n. 3
0
        public ICalculonType Execute(ref ControllerState cs)
        {
            ICalculonType input          = cs.stack.Pop();
            Real          ConvertedInput = new Real(input);

            return(ConvertedInput.Log2());
        }
Esempio n. 4
0
        public ICalculonType Execute(ref ControllerState cs)
        {
            Literal       l      = (Literal)cs.stack.Pop();
            string        key    = l.Display;
            ICalculonType output = cs.Config[key];

            return(output);
        }
Esempio n. 5
0
        public ICalculonType Execute(ref ControllerState cs)
        {
            Literal       l   = (Literal)cs.stack.Pop();
            string        key = l.Display;
            ICalculonType ict = cs.stack.Pop();

            cs.Config[key] = ict;
            return(new EmptyType());
        }
Esempio n. 6
0
        public ICalculonType Execute(ref ControllerState cs)
        {
            ICalculonType zero = cs.stack.Pop();
            ICalculonType one  = cs.stack.Pop();

            cs.stack.Push(zero);
            cs.stack.Push(one);
            return(new EmptyType());
        }
Esempio n. 7
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));
        }
Esempio n. 8
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());
            }
        }
Esempio n. 9
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();
        }
Esempio n. 10
0
        public ICalculonType this[string key]
        {
            get
            {
                ICalculonType output = null;
                SqliteCommand cmd    = memoryConnection.CreateCommand();
                cmd.CommandText = "SELECT type, value FROM storage WHERE key=@key;";
                cmd.Parameters.AddWithValue("@key", key);
                using (SqliteDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        Type     t     = Type.GetType(sdr["type"].ToString());
                        string   val   = sdr["value"].ToString();
                        object[] array = new[] { val };
                        output = (ICalculonType)Activator.CreateInstance(t, array);
                    }
                }

                //if we got here and didn't find anything
                if (output == null)
                {
                    output = new EmptyType();
                }

                return(output);
            }

            set
            {
                SqliteCommand cmd = memoryConnection.CreateCommand();
                cmd.CommandText = "REPLACE INTO storage(key, value, type) VALUES (@key, @value, @type)";
                cmd.Parameters.AddWithValue("@key", key);
                cmd.Parameters.AddWithValue("@value", value.Display);
                cmd.Parameters.AddWithValue("@type", value.GetType().ToString());
                int status = cmd.ExecuteNonQuery();
                WriteConfigToFile();
            }
        }
Esempio n. 11
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);
        }
Esempio n. 12
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()));
        }