当前位置: 首页>>代码示例>>C#>>正文


C# Evaluator.Eval方法代码示例

本文整理汇总了C#中Evaluator.Eval方法的典型用法代码示例。如果您正苦于以下问题:C# Evaluator.Eval方法的具体用法?C# Evaluator.Eval怎么用?C# Evaluator.Eval使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Evaluator的用法示例。


在下文中一共展示了Evaluator.Eval方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: PrintTable

        public static void PrintTable(string expression)
        {
            Evaluator<bool> eval = new Evaluator<bool>(expression, new ExpressionLib.Contexts.SimpleLogic());

            foreach (string var in eval.Variables)
                Console.Write("| " + var + " ");

            Console.WriteLine("| " + expression);

            Console.WriteLine();
            Permutations(eval.Variables, (vals) => {
                foreach ( bool v in vals.Values )
                    Console.Write("| " + SimpleLogic.ToString(v) + " ");

                Console.WriteLine("| " + SimpleLogic.ToString(eval.Eval(vals)));
            });

            Console.WriteLine();
        }
开发者ID:stormbreakerbg,项目名称:C-Sharp-expression-parser,代码行数:19,代码来源:TruthTables.cs

示例2: TestCustomExterns

        static void TestCustomExterns()
        {
            // Create an evaluator with custom defined functions:
            var ev = new Evaluator()
            {
                { "str", (v, e) => v.Eval(e[0]).ToString() },
                { "qualify", (v, e) =>
                {
                    if (e.Count != 2) throw new ArgumentException("qualify requires 2 parameters");

                    // Evaluate parameters:
                    var prefix = v.EvalExpecting<string>(e[0]);
                    var list = v.EvalExpecting<object[]>(e[1]);

                    var sb = new StringBuilder();
                    for (int i = 0; i < list.Length; ++i)
                    {
                        if (list[i].GetType() != typeof(string)) throw new ArgumentException("list item {0} must evaluate to a string".F(i + 1));
                        sb.AppendFormat("[{0}].[{1}]", prefix, (string)list[i]);
                        if (i < list.Length - 1) sb.Append(", ");
                    }
                    return sb.ToString();
                } },
                { "prefix", (v, e) =>
                {
                    if (e.Count != 2) throw new ArgumentException("prefix requires 2 parameters");

                    // Evaluate parameters:
                    var prefix = v.EvalExpecting<string>(e[0]);
                    var list = v.EvalExpecting<object[]>(e[1]);

                    var sb = new StringBuilder();
                    for (int i = 0; i < list.Length; ++i)
                    {
                        if (list[i].GetType() != typeof(string)) throw new ArgumentException("list item {0} must evaluate to a string".F(i + 1));
                        sb.AppendFormat("[{0}].[{1}] AS [{0}_{1}]", prefix, (string)list[i]);
                        if (i < list.Length - 1) sb.Append(", ");
                    }
                    return sb.ToString();
                } }
            };

            // Run through some test cases:
            {
                const string code = @"{prefix st [StudentID FirstName LastName]}";
                var prs = new Parser(new Lexer(new StringReader(code)));
                var expr = prs.ParseExpr();
                // Evaluate and output:
                var result = ev.Eval(expr);
                Output(result);
                Console.WriteLine();
            }
        }
开发者ID:JamesDunne,项目名称:mini-lisp,代码行数:53,代码来源:Program.cs

示例3: Main

        static void Main(string[] args)
        {
            // With apologies to Philip Greenspun.

            string[] badCodes = new string[]
            {
                // All should error:
                @"",
                @"(",
                @")",
                @"[",
                @"]",
                @"'",
                @"'\'",
                @"'\b'",
                @"(true)",
                @"(null)",
                @"(())",
                @"([] ())",
                @"~",
                @"-",

                @"(.ToString null)",
            };

            string[] goodCodes = new string[]
            {
                // All should succeed:
                @"true",
                @"false",
                @"null",
                @"'test'",
                @"~(if true true false)",
                @"(eval ~(if true true false))",
                @"(if true 'hello' 'world')",
                @"(if false 'hello' 'world')",
                @"(if (eq true false) true false)",
                @"(if (eq false false) true false)",
                @"(if (eq false false) true null)",
                @"(if (eq null null) true null)",
                @"(if (ne true false) true false)",
                @"(if (ne false false) true false)",
                @"(if (ne false false) true false)",
                @"(if (ne null null) true false)",
                @"'hello
            world'",
                @"'hello\nworld'",
                @"'hello\t\rworld'",
                @"'hello \'world\''",
                @"'hello ""world""'",
                @"`multiline
            raw
            string literal with ""quotes"" inside
            it and 'quotes' too.
            <html> is possible here.`",
                @"~'test'",
                @"~1.34",
                @"~1.34d",
                @"~1.34f",
                @"1.333333333333333333333333333333333333",
                @"1.333333333333333333333333333333333333d",
                @"1.333333333333333333333333333333333333f",
                @"1",
                @"008",
                @"-1",
                @"[10 -3]",

                // NOTE(jsd): Currently, whitespace separators are ignored for parsing purposes. This is subject to change.
                @"(.ToString (System.DateTime/Now) 'yyyyMMdd')",
                @"(. ToString (System . DateTime/Now) 'yyyyMMdd')",
                @"(.ToString (System. DateTime / Now) 'yyyyMMdd')",

                // TODO: add, inc, dec functions and possibly other basics:
                //@"(add x y)",
                //@"(inc 1)",
                //@"(dec 1)",
                // TODO: let with scoping
                //@"(let [x 1, y 2] (add x y))",
            };

            {
                int pass = 0, fail = 0;

                // Test the bad codes which should always fail:
                for (int i = 0; i < badCodes.Length; ++i)
                {
                    string code = badCodes[i];

                    try
                    {
                        var prs = new Parser(new Lexer(new StringReader(code)));
                        var expr = prs.ParseExpr();

                        // Evaluate:
                        var ev = new Evaluator();
                        var result = ev.Eval(expr);

                        // Failed! Should've gotten some sort of exception.
                        ++fail;
                    }
//.........这里部分代码省略.........
开发者ID:JamesDunne,项目名称:mini-lisp,代码行数:101,代码来源:Program.cs

示例4: Main

        static void Main(string[] args)
        {
            //string expr = "pr/2+(-3*2)^2 * sqrt(2+2) * max(sqrt(2), sqrt(4))";
            string expr = "(1 + 2.5) * 3";

            Dictionary<string, double> vars = new Dictionary<string, double>();
            vars.Add("pr", -1);
            Evaluator<double> eval = new Evaluator<double>(expr, new SimpleMath());

            try
            {
                Console.WriteLine("Sample math expression: " + expr);
                Console.WriteLine("In postfix format: " + eval.Expression);
                Console.WriteLine("Result: " + eval.Eval(vars));
            }
            catch (ParsingException e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                Console.ForegroundColor = ConsoleColor.Gray;
            }

            Console.WriteLine("----------------------------------------");

            Evaluator<bool> evalLogic = new Evaluator<bool>(new SimpleLogic());

            string exprLogic = "(F | (!T > T)) = ((F & T) > (F > T))";
            try
            {
                Console.WriteLine("Sample logical expression: " + exprLogic);
                Console.WriteLine("Result: " + evalLogic.EvalInfix(exprLogic));
            }
            catch (ParsingException e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                Console.ForegroundColor = ConsoleColor.Gray;
            }

            Console.WriteLine("----------------------------------------");

            //string exprLogic2 = "(p > (q > r)) > ((s > t) > (u > v))";
            //string exprLogic2 = "(p -> (not q -> r)) <-> ((r -> !q) -> (p -> r))";
            string exprLogic2 = "(((a|b)>(c=d))&(f^g))=h";

            try
            {
                Console.WriteLine("Truth table of: " + exprLogic2);

                Console.WriteLine();

                TruthTables.PrintTable(exprLogic2);
            }
            catch (ParsingException e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                Console.ForegroundColor = ConsoleColor.Gray;
            }

            Console.WriteLine("\nPress any key to continue...");
            Console.ReadKey();
        }
开发者ID:stormbreakerbg,项目名称:C-Sharp-expression-parser,代码行数:63,代码来源:Program.cs


注:本文中的Evaluator.Eval方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。