Beispiel #1
0
        private static ScriptingValue PrimitiveXor(ScriptingValue[] arguments)
        {
            bool a = (bool)arguments[0].Value;
            bool b = (bool)arguments[1].Value;

            return(ScriptingValue.CreateValue(a ^ b));
        }
Beispiel #2
0
        // find pattern string
        private static ScriptingValue Find(ScriptingValue[] arguments)
        {
            string p = (string)arguments[0].Value;
            string s = (string)arguments[1].Value;

            return(ScriptingValue.CreateValue(s.IndexOf(p, StringComparison.CurrentCulture)));
        }
Beispiel #3
0
        // split pattern string
        private static ScriptingValue Split(ScriptingValue[] arguments)
        {
            string p = (string)arguments[0].Value;
            string s = (string)arguments[1].Value;

            return(ScriptingValue.CreateValue(s.Split(new string[] { p }, StringSplitOptions.None).Cast <object>().ToArray()));
        }
Beispiel #4
0
 // empty string/array
 private static ScriptingValue Empty(ScriptingValue[] arguments)
 {
     if (arguments[0].IsArray)
     {
         return(ScriptingValue.CreateValue(arguments[0].Length == 0));
     }
     else
     {
         return(ScriptingValue.CreateValue(((string)arguments[0].Value).Length == 0));
     }
 }
Beispiel #5
0
        // item index string/array
        private static ScriptingValue Item(ScriptingValue[] arguments)
        {
            int index = (int)arguments[0].Value;

            if (arguments[1].IsArray)
            {
                return(ScriptingValue.CreateValue(arguments[1][index]));
            }
            else
            {
                return(ScriptingValue.CreateValue(((string)arguments[1].Value)[index].ToString()));
            }
        }
Beispiel #6
0
        private static ScriptingValue PrimitiveNeg(ScriptingValue[] arguments)
        {
            IConvertible a = (IConvertible)arguments[0].Value;

            if (a is int)
            {
                return(ScriptingValue.CreateValue(-a.ToInt32(CultureInfo.InvariantCulture)));
            }
            else
            {
                return(ScriptingValue.CreateValue(-a.ToDouble(CultureInfo.InvariantCulture)));
            }
        }
Beispiel #7
0
        private static ScriptingValue PrimitiveMod(ScriptingValue[] arguments)
        {
            IConvertible a = (IConvertible)arguments[0].Value;
            IConvertible b = (IConvertible)arguments[1].Value;

            if (a is int && b is int)
            {
                return(ScriptingValue.CreateValue(a.ToInt32(CultureInfo.InvariantCulture) % b.ToInt32(CultureInfo.InvariantCulture)));
            }
            else
            {
                return(ScriptingValue.CreateValue(Math.IEEERemainder(a.ToDouble(CultureInfo.InvariantCulture), b.ToDouble(CultureInfo.InvariantCulture))));
            }
        }
Beispiel #8
0
        private static ScriptingValue PrimitiveConcat(ScriptingValue[] arguments)
        {
            object a = arguments[0].Value;
            object b = arguments[1].Value;

            if (a is string && b is string)
            {
                return(ScriptingValue.CreateValue(((IConvertible)a).ToString(CultureInfo.InvariantCulture) + ((IConvertible)b).ToString(CultureInfo.InvariantCulture)));
            }
            else
            {
                return(ScriptingValue.CreateArray(arguments[0].Concat(arguments[1]).ToArray()));
            }
        }
Beispiel #9
0
        // read_file file_name
        private static ScriptingValue ReadFile(ScriptingValue[] arguments)
        {
            string filename = (string)arguments[0].Value;

            try
            {
                using (StreamReader reader = new StreamReader(filename, true))
                {
                    return(ScriptingValue.CreateValue(reader.ReadToEnd()));
                }
            }
            catch (Exception)
            {
                return(ScriptingValue.CreateArray(new object[] { }));
            }
        }
Beispiel #10
0
        // write_file file_name content
        private static ScriptingValue WriteFile(ScriptingValue[] arguments)
        {
            string filename = (string)arguments[0].Value;
            string content  = (string)arguments[0].Value;

            try
            {
                using (StreamWriter writer = new StreamWriter(filename))
                {
                    writer.Write(content);
                }
                return(ScriptingValue.CreateValue(true));
            }
            catch (Exception)
            {
                return(ScriptingValue.CreateValue(false));
            }
        }
Beispiel #11
0
 private static ScriptingValue ToString(ScriptingValue[] arguments)
 {
     return(ScriptingValue.CreateValue(((IConvertible)arguments[0].Value).ToString(CultureInfo.CurrentCulture)));
 }
Beispiel #12
0
        private static ScriptingValue PrimitiveNot(ScriptingValue[] arguments)
        {
            bool a = (bool)arguments[0].Value;

            return(ScriptingValue.CreateValue(!a));
        }
Beispiel #13
0
 private static ScriptingValue IOContinue(ScriptingValue[] arguments)
 {
     return(ScriptingValue.CreateValue(arguments[0].Value is Exception));
 }
Beispiel #14
0
        private static ScriptingValue PrimitiveGt(ScriptingValue[] arguments)
        {
            CompareResult result = Compare(arguments[0], arguments[1]);

            return(ScriptingValue.CreateValue(result == CompareResult.GreaterThan));
        }
Beispiel #15
0
        public static void LoadLibrary(ScriptingEnvironment e)
        {
            e.DefineValue("pure", ScriptingValue.CreateValue(new PureMonad()));
            e.DefineValue("ordered", ScriptingValue.CreateValue(new OrderedMonad()));
            e.DefineValue("state", ScriptingValue.CreateFunction(State, 1));
            e.DefineValue("continue", ScriptingValue.CreateFunction(Continue, 1));
            e.DefineValue("io", ScriptingValue.CreateFunction(IOContinue, 1));
            e.DefineValue("create_state", new ScriptingValue(RuntimeValueWrapper.CreateFunction(StateMonad.ReturnStateMonadValue, 2)));

            e.DefineValue("(+)", ScriptingValue.CreateFunction(PrimitiveAdd, 2));
            e.DefineValue("(-)", ScriptingValue.CreateFunction(PrimitiveSub, 2));
            e.DefineValue("(*)", ScriptingValue.CreateFunction(PrimitiveMul, 2));
            e.DefineValue("(/)", ScriptingValue.CreateFunction(PrimitiveDiv, 2));
            e.DefineValue("(%)", ScriptingValue.CreateFunction(PrimitiveMod, 2));
            e.DefineValue("(++)", ScriptingValue.CreateFunction(PrimitiveConcat, 2));
            e.DefineValue("(<)", ScriptingValue.CreateFunction(PrimitiveLt, 2));
            e.DefineValue("(<=)", ScriptingValue.CreateFunction(PrimitiveLe, 2));
            e.DefineValue("(>)", ScriptingValue.CreateFunction(PrimitiveGt, 2));
            e.DefineValue("(>=)", ScriptingValue.CreateFunction(PrimitiveGe, 2));
            e.DefineValue("(==)", ScriptingValue.CreateFunction(PrimitiveEq, 2));
            e.DefineValue("(!=)", ScriptingValue.CreateFunction(PrimitiveNe, 2));
            e.DefineValue("(&&)", ScriptingValue.CreateFunction(PrimitiveAnd, 2));
            e.DefineValue("(||)", ScriptingValue.CreateFunction(PrimitiveOr, 2));
            e.DefineValue("(^)", ScriptingValue.CreateFunction(PrimitiveXor, 2));
            e.DefineValue("not", ScriptingValue.CreateFunction(PrimitiveNot, 1));
            e.DefineValue("neg", ScriptingValue.CreateFunction(PrimitiveNeg, 1));
            e.DefineValue("unit", ScriptingValue.CreateFunction(PrimitiveUnit, 1));

            foreach (MethodInfo method in typeof(Math).GetMethods(BindingFlags.Public | BindingFlags.Static))
            {
                if (method.ReturnType == typeof(double) && method.GetParameters().Length > 0 && method.GetParameters().All(p => p.ParameterType == typeof(double)))
                {
                    string methodName = method.Name.ToLower();
                    if (!e.IsDefined(methodName))
                    {
                        e.DefineValue(methodName, ScriptingValue.CreateFunction(MakeDoubleFunction(method), method.GetParameters().Length));
                    }
                }
            }

            e.DefineValue("pi", ScriptingValue.CreateValue(Math.PI));
            e.DefineValue("e", ScriptingValue.CreateValue(Math.E));

            e.DefineValue("aggregate", ScriptingValue.CreateFunction(Aggregate, 3));
            e.DefineValue("distinct", ScriptingValue.CreateFunction(Distinct, 1));
            e.DefineValue("except", ScriptingValue.CreateFunction(Except, 2));
            e.DefineValue("first", ScriptingValue.CreateFunction(First, 2));
            e.DefineValue("intersect", ScriptingValue.CreateFunction(Intersect, 2));
            e.DefineValue("last", ScriptingValue.CreateFunction(Last, 2));
            e.DefineValue("order_by", ScriptingValue.CreateFunction(OrderBy, 2));
            e.DefineValue("reverse", ScriptingValue.CreateFunction(Reverse, 1));
            e.DefineValue("select", ScriptingValue.CreateFunction(Select, 2));
            e.DefineValue("select_many", ScriptingValue.CreateFunction(SelectMany, 2));
            e.DefineValue("skip", ScriptingValue.CreateFunction(Skip, 2));
            e.DefineValue("skip_while", ScriptingValue.CreateFunction(SkipWhile, 2));
            e.DefineValue("take", ScriptingValue.CreateFunction(Take, 2));
            e.DefineValue("take_while", ScriptingValue.CreateFunction(TakeWhile, 2));
            e.DefineValue("union", ScriptingValue.CreateFunction(Union, 2));
            e.DefineValue("where", ScriptingValue.CreateFunction(Where, 2));
            e.DefineValue("zip", ScriptingValue.CreateFunction(Zip, 2));

            e.DefineValue("to_lower", ScriptingValue.CreateFunction(ToLower, 1));
            e.DefineValue("to_upper", ScriptingValue.CreateFunction(ToUpper, 1));
            e.DefineValue("find", ScriptingValue.CreateFunction(Find, 2));
            e.DefineValue("find_all", ScriptingValue.CreateFunction(FindAll, 2));
            e.DefineValue("reg_find", ScriptingValue.CreateFunction(RegFind, 2));
            e.DefineValue("reg_find_all", ScriptingValue.CreateFunction(RegFindAll, 2));
            e.DefineValue("length", ScriptingValue.CreateFunction(Length, 1));
            e.DefineValue("item", ScriptingValue.CreateFunction(Item, 2));
            e.DefineValue("empty", ScriptingValue.CreateFunction(Empty, 1));
            e.DefineValue("split", ScriptingValue.CreateFunction(Split, 2));

            e.DefineValue("to_int", ScriptingValue.CreateFunction(ToInt, 1));
            e.DefineValue("to_double", ScriptingValue.CreateFunction(ToDouble, 1));
            e.DefineValue("to_string", ScriptingValue.CreateFunction(ToString, 1));

            e.DefineValue("read_file", ScriptingValue.CreateFunction(ReadFile, 1));
            e.DefineValue("write_file", ScriptingValue.CreateFunction(WriteFile, 2));
        }
Beispiel #16
0
        private static ScriptingValue PrimitiveNe(ScriptingValue[] arguments)
        {
            CompareResult result = Compare(arguments[0], arguments[1]);

            return(ScriptingValue.CreateValue(result != CompareResult.EqualTo));
        }
Beispiel #17
0
        // to_upper string
        private static ScriptingValue ToUpper(ScriptingValue[] arguments)
        {
            string s = (string)arguments[0].Value;

            return(ScriptingValue.CreateValue(s.ToUpper(CultureInfo.CurrentCulture)));
        }
Beispiel #18
0
 private static ScriptingValue Continue(ScriptingValue[] arguments)
 {
     return(ScriptingValue.CreateValue(true));
 }
Beispiel #19
0
 private static ScriptingValue DoubleFunction(MethodInfo method, ScriptingValue[] argument)
 {
     return(ScriptingValue.CreateValue((double)method.Invoke(null, argument.Select(v => (object)((IConvertible)v.Value).ToDouble(CultureInfo.InvariantCulture)).ToArray())));
 }
Beispiel #20
0
 private static ScriptingValue State(ScriptingValue[] arguments)
 {
     return(ScriptingValue.CreateValue(new StateMonad(arguments[0].ValueWrapper)));
 }