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); })); }
public object ReadExpr(FSMContext context, XmlNode exprNode, bool isBlock) { CompileContext ctx = new CompileContext(new FSMExpressionContextAdapter(context)); var expr = exprReader.Read(exprNode, isBlock, ctx); return(expr); }
protected void Run(string xml, CompileContext compile = null, ExpressionContext ctx = null) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); var root = doc.DocumentElement; if (ctx == null) { ctx = new ExpressionContext(); } if (compile == null) { compile = new CompileContext(); } var reader = new XmlExpressionReader(); foreach (XmlNode exprNode in root) { if (exprNode.NodeType != XmlNodeType.Element) { continue; } string testName = exprNode.Attributes["name"].Value; Console.WriteLine("run start:" + testName); var expr = reader.Read(exprNode, true, compile); var eval = compile.Compile(expr); object result = eval(ctx); object assertResult = null; var resultAttr = exprNode.Attributes["result"]; if (resultAttr != null) { Type assertResultType = exprNode.Attributes["resultType"] != null?Type.GetType(GetTypeName(exprNode.Attributes["resultType"].Value), true) : typeof(string); string text = exprNode.Attributes["result"].Value; if (assertResultType == typeof(Type)) { assertResult = Type.GetType(text); } else { assertResult = Convert.ChangeType(text, assertResultType); } } Assert.AreEqual(assertResult, result, string.Format("name: {0}", testName)); Console.WriteLine("run end:" + testName); } }
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)); }
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); }); }