Example #1
0
        public static void LoadXml(string xml, Action <string, InvocationDelegate> callback)
        {
            LoadXmlNode(xml, new Action <string, XmlNode>((name, node) =>
            {
                XmlExpressionReader reader = new XmlExpressionReader();
                XmlNode firstChild         = null;
                foreach (XmlNode child in node.ChildNodes)
                {
                    if (child.NodeType == XmlNodeType.Element)
                    {
                        firstChild = child;
                        break;
                    }
                }
                //convert xml node to expression
                Expression expr = reader.Read(firstChild);

                CompileContext compile = new CompileContext();

                //compile expression
                var eval = compile.Compile(expr);

                callback(name, eval);
            }));
        }
Example #2
0
        public void Call_StaticMethod()
        {
            var expr    = Call(typeof(CallClass), "StaticHello");
            var compile = new CompileContext();
            var ctx     = new ExpressionContext();


            var result = compile.Compile(expr)(ctx);

            Assert.AreEqual("Static: Hello World", result);
        }
        public void Call_Args0()
        {
            int a = 1, b = 2;
            var funcExpr = Function(null, Return(Add(Constant(a), Constant(b))), typeof(int));
            var compile  = new CompileContext();
            //using (compile.BeginScope())
            //{
            var result = compile.Compile(Call(funcExpr, typeof(int)))(null);

            Assert.AreEqual(a + b, result);
            //}
        }
Example #4
0
        public void TestInstanceFuncParams()
        {
            object            result;
            MyObj             obj = new MyObj(1);
            ExpressionContext ctx = new ExpressionContext();

            ctx.AddVariable <Func <long, long[], long> >("sum");
            ctx.SetVariable("sum", new Func <long, long[], long>(obj.Sum));
            CompileContext comple = new CompileContext(ctx);

            var expr = ScriptExpressionReader.Instance.Parse("sum(2)", comple);

            result = comple.Compile(expr)(ctx);
            Assert.AreEqual(result, 3L);
            expr   = ScriptExpressionReader.Instance.Parse("sum(2,3)", comple);
            result = comple.Compile(expr)(ctx);
            Assert.AreEqual(result, 6L);
            expr   = ScriptExpressionReader.Instance.Parse("sum(2,3,4)", comple);
            result = comple.Compile(expr)(ctx);
            Assert.AreEqual(result, 10L);
        }
Example #5
0
        public void Call_InstanceMethod()
        {
            var varObj  = Variable <CallClass>("obj");
            var expr    = Call(varObj, "Hello");
            var compile = new CompileContext();
            var ctx     = new ExpressionContext();

            ctx.AddVariable <CallClass>("obj", new CallClass());

            var result = compile.Compile(expr)(ctx);

            Assert.AreEqual("Instance: Hello World", result);
        }
        public void Call_NamedFunc()
        {
            int a    = 1;
            var expr = Block(Function("func1", Return(Constant(a)), typeof(int)),
                             Return(Call("func1", typeof(int))));
            var compile = new CompileContext();
            //using (compile.BeginScope())
            //{
            var result = compile.Compile(expr)(null);

            Assert.AreEqual(a, result);
            //}
        }
Example #7
0
        public void Call_DelegateArgs1()
        {
            var varObj  = Variable <string>("del");
            var expr    = Call(varObj, typeof(string), Constant("My"));
            var compile = new CompileContext();
            var ctx     = new ExpressionContext();

            ctx.AddVariable("del", new Func <string, string>((say) =>
            {
                return(say + ": Hello World");
            }));

            var result = compile.Compile(expr)(ctx);

            Assert.AreEqual("My: Hello World", result);
        }
Example #8
0
        private InvocationDelegate GetOrCacheExprEval(IExpressionContext context, Expression expr)
        {
            InvocationDelegate eval;

            if (cachedExprs == null)
            {
                cachedExprs = new Dictionary <Expression, InvocationDelegate>();
            }
            if (!cachedExprs.TryGetValue(expr, out eval))
            {
                var compileContext = new CompileContext(context);
                eval = compileContext.Compile(expr);
                cachedExprs[expr] = eval;
            }
            return(eval);
        }
        public void Delegate_Args0()
        {
            int a = 1, b = 2;
            var funcExpr = Function(null,
                                    Return(Add(Constant(a), Constant(b))),
                                    typeof(int));
            var compile = new CompileContext();
            //using (compile.BeginScope())
            //{
            var tmp = compile.Compile(funcExpr);

            var func = (Delegate)tmp(null);

            Assert.AreEqual(a + b, func.DynamicInvoke());
            //}
        }
        public void Call_Args2()
        {
            int a = 1, b = 2;
            var varA     = Variable <int>("a");
            var varB     = Variable <int>("b");
            var funcExpr = Function(null, new ParameterExpression[] { varA, varB },
                                    Return(Add(varA, varB)),
                                    typeof(int));
            var compile = new CompileContext();
            //using (compile.BeginScope())
            //{
            var result = compile.Compile(Call(funcExpr, typeof(int), Constant(a), Constant(b)))(null);

            Assert.AreEqual(a + b, result);
            //}
        }
        public void ToFunc_Args1()
        {
            var funcExpr = Function(null, new ParameterExpression[] { Variable <string>("text") },
                                    Return(Variable <string>("text")),
                                    typeof(string));
            var compile = new CompileContext();

            var tmp = compile.Compile(funcExpr);

            var del = (Delegate)tmp(null);

            //Func<string> funcString = del as Func<string>;

            Func <object, object> func = del as Func <object, object>;

            Assert.AreEqual("Hello World", func("Hello World"));
        }
        public void ToFunc_Args0()
        {
            var funcExpr = Function(null,
                                    Return(Constant("Hello World")),
                                    typeof(string));
            var compile = new CompileContext();

            var tmp = compile.Compile(funcExpr);

            var del = (Delegate)tmp(null);

            //Func<string> funcString = del as Func<string>;

            Func <object> func = del as Func <object>;

            Assert.AreEqual("Hello World", func());
        }
        public void Delegate_Args2()
        {
            int a = 1, b = 2;
            var varA     = Variable <int>("a");
            var varB     = Variable <int>("b");
            var funcExpr = Function(null, new ParameterExpression[] { varA, varB },
                                    Return(Add(varA, varB)),
                                    typeof(int));
            var compile = new CompileContext();
            //using (compile.BeginScope())
            //{
            var tmp  = compile.Compile(funcExpr);
            var func = (Delegate)tmp(null);

            Assert.AreEqual(a + b, func.DynamicInvoke(a, b));
            //}
        }
Example #14
0
        void CheckExpr(string exprString, object expectedResult, IExpressionContext ctx = null)
        {
            Expression expr;
            var        cCtx = new CompileContext(ctx);

            try
            {
                expr = ScriptExpressionReader.Instance.Parse(exprString, cCtx);
            }
            catch (Exception ex)
            {
                throw new Exception(exprString, ex);
            }
            object result;

            result = cCtx.Compile(expr)(ctx);
            Assert.AreEqual(expectedResult, result, exprString);
        }
        public void Func_Var_Nest_Func()
        {
            var varI     = Variable <int>("i");
            var funcExpr = Function("func_i", Block(new ParameterExpression[] { varI },
                                                    Assign(varI, Constant(2)),
                                                    Return(Call(Function(null, Return(varI), typeof(int)), typeof(int)))), typeof(int));
            var compile = new CompileContext();

            //using (compile.BeginScope())
            //{
            compile.AddVariable <int>("i");
            var tmp = compile.Compile(Call(funcExpr, typeof(int)));
            var ctx = new ExpressionContext();

            ctx.AddVariable <int>("i", 1);
            var result = tmp(ctx);

            Assert.AreEqual(2, result);
            //}
        }
        public void Sum()
        {
            string xml = Resource1.sum;

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);
            var root = doc.DocumentElement;

            var compile = new CompileContext();
            var reader  = new XmlExpressionReader();
            var expr    = reader.Read(root.FirstChild);

            var eval = compile.Compile(expr);

            var result = (Delegate)eval(null);

            int n = 5;

            Assert.AreEqual(RecursiveSum(n), result.DynamicInvoke(n));
        }
Example #17
0
            public void Sum()
            {
                int a        = 3;
                var n        = Variable <int>("n");
                var funcExpr = Function("sum", new ParameterExpression[] { n },
                                        Return(
                                            If(LessThanOrEqual(n, Constant(1)),
                                               Constant(1),
                                               Add(n, Call("sum", n.ValueType, PostDecrement(n))))
                                            )
                                        , typeof(int));
                var compile = new CompileContext();
                //using (compile.BeginScope())
                //{
                var func    = compile.Compile(Call(funcExpr, n.ValueType, Constant(a)));
                var result2 = func(null);
                int result  = RecursiveSum(a);

                Assert.AreEqual(result, result2);
                //}
            }
Example #18
0
        public void Custom_Read()
        {
            string xml = Resource1.custom;

            var reader = new XmlExpressionReader();

            reader.AddReader("hello", "urn:my", (r) =>
            {
                Assert.AreEqual(r, reader);
                var exprs   = r.ReadChildExpressions();
                var operand = exprs[0];
                return(new HelloExpression(operand));
            });

            LoadXmlNode(xml, (name, node) =>
            {
                var expr = reader.Read(node.FirstChild);
                CompileContext compile = new CompileContext();
                var eval      = compile.Compile(expr);
                object result = eval(null);
                Assert.AreEqual("Hello World", result);
            });
        }
Example #19
0
        public static object Eval(Dictionary <string, object> variables, Expression expression, ExpressionContext ctx = null)
        {
            if (ctx == null)
            {
                ctx = new ExpressionContext();
            }


            var compile = new CompileContext();
            InvocationDelegate eval;

            if (variables != null)
            {
                foreach (var item in variables)
                {
                    compile.AddVariable(item.Value.GetType(), item.Key);
                    ctx.AddVariable(item.Key, item.Value);
                }
            }

            eval = compile.Compile(expression);

            return(eval(ctx));
        }
        public void Func_Clamp()
        {
            var varA = Variable <int>("a");
            var varB = Variable <int>("b");
            var expr = Block(
                Function("min_", new ParameterExpression[] { varA, varB },
                         Return(If(LessThanOrEqual(varA, varB), varA, varB)), typeof(int)),

                Function("max_", new ParameterExpression[] { varA, varB },
                         Return(If(GreaterThanOrEqual(varA, varB), varA, varB)), typeof(int)),

                Function("clamp", new ParameterExpression[] { Variable <int>("value"), Variable <int>("min"), Variable <int>("max") },
                         Return(Call("min_", typeof(int), Call("max_", typeof(int), Variable <int>("value"), Variable <int>("min")), Variable <int>("max"))), typeof(int)),

                Return(Call("clamp", typeof(int), Constant(100), Constant(1), Constant(10))));
            var compile = new CompileContext();
            //using (compile.BeginScope())
            //{
            var tmp    = compile.Compile(expr);
            var result = tmp(null);

            Assert.AreEqual(10, result);
            //}
        }
Example #21
0
        public void Type_Convert()
        {
            MyValue my    = new MyValue("a");
            string  myStr = my;

            Assert.AreEqual("a", myStr);
            my = myStr;
            Assert.AreEqual("a", my.Value);

            MyValue2 my2 = new MyValue2("a2");

            myStr = my2;
            //my2 = myStr;//can't convert
            Assert.AreEqual("a2", myStr);
            MyValue3 my3 = myStr;

            myStr = my3;



            Func <object, Type, object> convert = (value, toType) =>
            {
                Type fromType = value.GetType();
                InvocationDelegate eval;
                var compile = new CompileContext();

                compile.OverrideOperator(typeof(MyValue));
                compile.AddVariable(fromType, "a");
                using (compile.BeginScope())
                {
                    var expr = Convert(Variable(fromType, "a"), toType);

                    eval = compile.Compile(expr);
                }

                ExpressionContext ctx = new ExpressionContext();
                ctx.AddVariable(fromType, "a");
                ctx.SetVariable("a", value);
                return(eval(ctx));
            };

            object result;

            result = convert(new MyValue("Hello World"), typeof(string));
            Assert.AreEqual("Hello World", result);

            result = convert("Hello World", typeof(MyValue));
            Assert.AreEqual("Hello World", ((MyValue)result).Value);

            result = convert(new MyValue2("Hello World"), typeof(string));
            Assert.AreEqual("Hello World", result);

            try
            {
                result = convert("Hello World", typeof(MyValue2));
                Assert.AreEqual("Hello World", ((MyValue2)result).Value);
                Assert.Fail();
            }
            catch (OperatorNotImplementedException ex) { }

            result = convert(new MyValue3("Hello World"), typeof(string));
            Assert.AreEqual("Hello World", result);

            result = convert("Hello World", typeof(MyValue3));
            Assert.AreEqual("Hello World", ((MyValue3)result).Value);
        }