public override string RunSample(RunSampleArgs args) { if (_evaluator == null) { _evaluator = new ExpressionEvaluator(this); } _evaluator.ClearOutput(); //for (int i = 0; i < 1000; i++) //for perf measurements, to execute 1000 times _evaluator.Evaluate(args.ParsedSample); return(_evaluator.GetOutput()); }
public override string RunSample(RunSampleArgs args) { if (_evaluator == null) { _evaluator = new ExpressionEvaluator(this); _evaluator.Globals.Add("null", _evaluator.Runtime.NoneValue); _evaluator.Globals.Add("true", true); _evaluator.Globals.Add("false", false); } _evaluator.ClearOutput(); //for (int i = 0; i < 1000; i++) //for perf measurements, to execute 1000 times _evaluator.Evaluate(args.ParsedSample); return(_evaluator.GetOutput()); }
public void TestEvaluator_BuiltIns() { var eval = new ExpressionEvaluator(); string script; object result; //Using methods imported from System.Math class script = @"abs(-1.0) + Log10(100.0) + sqrt(9) + floor(4.5) + sin(PI/2)"; result = eval.Evaluate(script); Assert.IsTrue(result is double, "Result is not double."); Assert.AreEqual(11.0, (double) result, 0.001, "Unexpected computation result"); //Using methods imported from System.Environment script = @"report = '#{MachineName}-#{OSVersion}-#{UserName}'"; result = eval.Evaluate(script); var expected = string.Format("{0}-{1}-{2}", Environment.MachineName, Environment.OSVersion, Environment.UserName); Assert.AreEqual(expected, result, "Unexpected computation result"); //Using special built-in methods print and format eval.ClearOutput(); script = @"print(format('{0} * {1} = {2}', 3, 4, 3 * 4))"; eval.Evaluate(script); result = eval.GetOutput(); Assert.AreEqual("3 * 4 = 12\r\n", result, "Unexpected computation result"); //Add custom built-in method SayHello and test it eval.Runtime.BuiltIns.AddMethod(SayHello, "SayHello", 1, 1, "name"); script = @"SayHello('John')"; result = eval.Evaluate(script); Assert.AreEqual("Hello, John!", result, "Unexpected computation result"); }