Beispiel #1
0
        public object Eval(string expression)
        {
            if (expression != null && expression.Length > 200)
            {
                throw new Exception("Live demo doesn't support expression with more than 200 characters.");
            }

            _commandsHistory.HandleCommandExecuted(new CommandEvent(expression));

            var result = _interpreter.Eval(expression);

            return(result);
        }
Beispiel #2
0
        public bool DynamicExpresso(Parameter parameter)
        {
            DynamicExpresso.Interpreter interpreter = new DynamicExpresso.Interpreter();

            List <int> results = new List <int>(parameter.Numbers.Length);

            foreach (int number in parameter.Numbers)
            {
                int result = interpreter.Eval <int>(EXPRESSION, new DynamicExpresso.Parameter("n", number));

                results.Add(result);
            }

            return(Assert(results, parameter.Sum));
        }
Beispiel #3
0
        public bool DynamicExpresso(Parameter parameter)
        {
            DynamicExpresso.Interpreter interpreter = new DynamicExpresso.Interpreter();

            List <int> results = new List <int>(parameter.Statements.Length);

            foreach (string statement in parameter.Statements)
            {
                int result = interpreter.Eval <int>(statement);

                results.Add(result);
            }

            return(Assert(results, parameter.Sum));
        }
Beispiel #4
0
        public static object Eval(string expression)
        {
            try
            {
                if (expression == "\'\'\'")
                {
                    return('\'');
                }

                return(interpreter.Eval(expression));
            }
            catch (Exception ex)
            {
                throw new BaseException("Couldn't evaluate expression '" + expression.Replace("\n", @"\n") + "': " + ex.Message);
            }
        }
Beispiel #5
0
 public static object JScriptEvaluate(string code, object obj)
 {
     //'${type}'=='file'?'file':'text' 修正单引号 "${type}"=="file"?"文件":("${type}"=="header"?"请求头":"${type}")
     if (code.Contains("'=='") || code.Contains("'?'") || code.Contains("':'"))
     {
         code = code.Replace("'", "\"");
     }
     if (code.Contains("True") || code.Contains("False"))
     {
         code = code.Replace("True?", "true?").Replace("False?", "false?").Replace("=True", "=true").Replace("=False", "=false");
     }
     return(express.Eval(code));
     //if (v8 == null)
     //{
     //    v8 = new Microsoft.ClearScript.V8.V8ScriptEngine();
     //}
     //return v8.Evaluate(code);
 }
Beispiel #6
0
        public static Func <object, object, TimeSpan, bool> ComposeTestInputOutput(string errorRuleLang, string errorRule, Action <DateTime, string> logError)
        {
            if (string.IsNullOrEmpty(errorRuleLang) || string.IsNullOrEmpty(errorRule))
            {
                return((input, output, TimeSpan) => false);
            }

            return((input, output, timeSpan) =>
            {
                var isAssert = false;
                try
                {
                    if (errorRuleLang == "DynamicExpresso")
                    {
                        var interpreter = new DynamicExpresso.Interpreter();
                        var result = interpreter.Eval(errorRule, new[] {
                            new DynamicExpresso.Parameter("input", input),
                            new DynamicExpresso.Parameter("output", output),
                            new DynamicExpresso.Parameter("timeSpan", timeSpan)
                        });
                        if (result is bool)
                        {
                            isAssert = (bool)result;
                        }
                        else
                        {
                            isAssert = true;
                            logError(DateTime.Now, $"Rule lang '{errorRuleLang}', rule '{errorRule}' has returned not boolean result");
                        }
                    }
                    if (!isAssert)
                    {
                        logError(DateTime.Now, $"[{errorRuleLang}] {errorRule}");
                    }
                }
                catch (Exception ex)
                {
                    isAssert = true;
                    logError(DateTime.Now, Markdown(ex));
                }
                return isAssert;
            });
        }
        string IActivityChooser.GetNextActivityKey(Data.Workflow wf, Data.UnliqudatedObjectsWorkflowQuestion question, string settings)
        {
            var s = JsonConvert.DeserializeObject <MySettings>(settings);
            //TODO: pass in questions object
            var parameters = new[] {
                new DynamicExpresso.Parameter(CommonParameterNames.Workflow, wf),
                new DynamicExpresso.Parameter(CommonParameterNames.Ulo, wf.UnliquidatedObligation),
                new DynamicExpresso.Parameter(CommonParameterNames.wfQuestion, question),
            };
            var i = new DynamicExpresso.Interpreter();

            foreach (var e in s.Expressions)
            {
                bool res = i.Eval <bool>(e.Code, parameters);
                if (res)
                {
                    return(e.WorkflowActivityKey);
                }
            }
            return(null);
        }
Beispiel #8
0
        private void CalText(string text)
        {
            //MenuItemDebug.Header = "[Debug] " + (mDebugMode == 0?"Both":mDebugMode == 1? "3rd":"Native");
            double resultNative = double.NaN;
            double result3rd    = double.NaN;

            mTextEqualitySign.Text = "=";
            bool isErrorDetected = false;

            if (mDebugMode == 0 || mDebugMode >= 2)
            {
                try
                {
                    RegexFormulas.EvalWarningFlags flags = RegexFormulas.EvalWarningFlags.None;
                    resultNative = RegexFormulas.Eval(text, ref flags);
                    if ((flags & RegexFormulas.EvalWarningFlags.DoubleToInt) != 0)
                    {
                        Console.WriteLine(RegexFormulas.EvalWarningFlags.DoubleToInt);
                        mTextEqualitySign.Text = "≈";
                    }
                }
                catch (ArgumentOutOfRangeException ea)
                {
                    isErrorDetected  = true;
                    mTextResult.Text = "OutOfRange:" + ea.Message;
                    resultNative     = double.NaN;
                }
                catch (OverflowException eof)
                {
                    isErrorDetected  = true;
                    mTextResult.Text = "Overflow:" + eof.Message;
                    resultNative     = double.NaN;
                }
                catch (DivideByZeroException ediv)
                {
                    isErrorDetected  = true;
                    mTextResult.Text = "DivideByZero";
                    resultNative     = double.NaN;
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex.Message);
                    resultNative = double.NaN;
                }
            }

#if !ENABLE_DynamicExpresso
            if (mDebugMode == 0 || mDebugMode == 1)
            {
                try
                {
                    var result = mInterpreter.Eval(cb.Text);

                    if (result.GetType() == typeof(int))
                    {
                        result3rd = (int)result;
                    }
                    else if (result.GetType() == typeof(float))
                    {
                        result3rd = (double)((float)result);
                    }
                    else if (result.GetType() == typeof(double))
                    {
                        result3rd = (double)result;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    result3rd = double.NaN;
                }
            }
#else
            result3rd = resultNative;
#endif

            if (double.IsNaN(resultNative) && double.IsNaN(result3rd))
            {
                if (!isErrorDetected)
                {
                    mTextResult.Text = "----";
                }
                ;
                mTextResult.Opacity = 0.5;
                if (mCalcCur != null)
                {
                    mCalcCur.mCalcResult     = Double.NaN;
                    mCalcCur.mCalcTextResult = "";
                }
            }
            else
            {
                if (mCalcCur == null)
                {
                    mCalcCur = new CalcItem();
                }
                mCalcCur.mCalcText = text;

                if (!double.IsNaN(resultNative))
                {
                    mCalcCur.mCalcResult = resultNative;
                }
                else if (!double.IsNaN(result3rd))
                {
                    mCalcCur.mCalcResult = result3rd;
                }

                mCalcCur.mCalcTextResult = mCalcCur.mCalcResult.ToString();

                if (mDebugMode == 0)
                {
                    Console.WriteLine("Native:" + resultNative + " 3rd:" + result3rd);
                }


                {
                    UpdateResult();
                    if (mDebugMode == 0)
                    {
                        if ((!double.IsNaN(resultNative) && !double.IsNaN(result3rd) && Math.Abs(resultNative - result3rd) > 2) ||
                            (double.IsNaN(resultNative) && !double.IsNaN(result3rd)))
                        {
                            mTextResult.Text = "N:" + resultNative + " 3:" + result3rd;
                        }
                    }


                    mTextResult.Opacity = 1;
                }
            }
        }