コード例 #1
0
ファイル: EvalEnvironment.cs プロジェクト: iExpr/iExpr.Core
        public virtual EvalContext CreateContext(CancellationTokenSource cancel = null)
        {
            var res = EvalContext.Create(cancel ?? new System.Threading.CancellationTokenSource());

            res.Evaluator      = Evaluator;
            res.BasicVariables = Variables;
            return(res);
        }
コード例 #2
0
        public static EvalContext Create(CancellationTokenSource cancel)
        {
            var res = new EvalContext
            {
                CancelToken = cancel
            };

            return(res);
        }
コード例 #3
0
 public static IExpr EvaluateNodeAccess(ExprNodeAccess expr, EvalContext environment)
 {
     try
     {
         var func = environment.GetValue <IAccessibleValue>(environment.Evaluate(expr.HeadExpr));
         return(func.Access(expr.Variable.ID));
     }
     catch (ExprException ex)
     {
         throw ex;
     }
     catch (IgnoredException ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         ExceptionHelper.RaiseAccessFailed(expr, expr.Variable.ID, null, ex);
         return(default);
コード例 #4
0
        /// <summary>
        /// 计算,并创建新的集合
        /// </summary>
        /// <param name="expr"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static IExpr EvaluateColletionValue(CollectionValue expr, EvalContext environment)
        {
            if (expr.IsCertain == true)
            {
                return(expr);
            }
            List <IValue> vs = new List <IValue>();

            //var que = from x in ls.Contents select Calculate(x);
            try
            {
                foreach (var v in (IEnumerable <IValue>)expr)
                {
                    if (v.IsCertain)
                    {
                        vs.Add(v);
                    }
                    else
                    {
                        var val = environment.Evaluate(v);
                        if (val is IValue)
                        {
                            vs.Add((IValue)val);
                        }
                        else//值是一个表达式
                        {
                            ExceptionHelper.RaiseNotValue(expr, v);
                        }
                    }
                }
                var res = expr.CreateNew();
                res.Reset(vs);
                return(res);
            }
            catch (ExprException ex)
            {
                throw ex;
            }
        }
コード例 #5
0
        /// <summary>
        /// 计算
        /// </summary>
        /// <param name="expr"></param>
        /// <returns></returns>
        public virtual IExpr Evaluate(IExpr expr, EvalContext context, bool evalConstant = false)
        {
            try
            {
                context.AssertNotCancel();
                if (expr == null)
                {
                    return(null);             //TODO:Attention this
                }
                switch (expr)
                {
                case ExprNodeBinaryOperation o:
                    return(EvaluateNodeBinaryOperation(o, context));

                case ExprNodeAccess o:
                    return(EvaluateNodeAccess(o, context));

                case ExprNodeCall o:
                    return(EvaluateNodeCall(o, context));

                case ExprNodeIndex o:
                    return(EvaluateNodeIndex(o, context));

                case ExprNodeContent o:
                    return(EvaluateNodeContent(o, context));

                case ConstantToken o:
                    if (evalConstant)
                    {
                        return(o.Value);
                    }
                    else
                    {
                        return(o);
                    }

                case VariableToken o:
                    return(EvaluateVariable(o, context));

                case CollectionValue o:
                    return(EvaluateColletionValue(o, context));

                case FunctionValue o:
                    return(o);

                case ConcreteValue o:
                    if (o.Value is IExpr)
                    {
                        return(context.Evaluate((IExpr)o.Value));
                    }
                    return(o);

                case NativeExprValue o:
                    return(context.Evaluate(o.Expr));

                default:
                    ExceptionHelper.RaiseInvalidExpressionFailed(this, expr, "Can't evaluate the expr");
                    return(null);
                }
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
            catch (ExprException ex)
            {
                throw ex;
            }
            catch (IgnoredException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exceptions.EvaluateException(this, "Evaluate failed.", ex);
            }
        }