예제 #1
0
 public ScriptingEnvironment(string code, ScriptingEnvironment env = null)
 {
     this.context = parser.Parse(code).BuildContext();
     if (env != null)
     {
         this.context.PreviousContext = env.context;
     }
 }
예제 #2
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));
        }