Exemplo n.º 1
0
 public bool TryEvaluate(ContextStack contextStack, out object result)
 {
     try
     {
         var evaluater = new Expr.Evaluator();
         evaluater.VariableHolder = contextStack;
         result = evaluater.Eval(ast);
     }
     catch (Exception e)
     {
         throw new Exception($"eval express `{express}` error: {e.Message}", e);
     }
     return(true);
 }
Exemplo n.º 2
0
 internal override Result Eval(Evaluator evaluater, Result[] argArray)
 {
     return(new Result(typeof(int), ~((uint)ConvertHelper.ChangeType(argArray[0].Value, typeof(uint)))));
 }
Exemplo n.º 3
0
        internal override Result Eval(Evaluator evaluater, Result[] argArray)
        {
            if (argArray.Length < 2)
            {
                throw new ArgumentException();
            }

            object target = argArray[0].Value;

            if (target == null)
            {
                throw new ArgumentException();
            }
            int length = argArray.Length - 1;

            Type         type         = null;
            BindingFlags bindingFlags = BindingFlags.Default;

            if (target is Type)
            {
                type         = (Type)target;
                target       = null;
                bindingFlags = BindingFlags.Static;
            }
            else
            {
                type         = target.GetType();
                bindingFlags = BindingFlags.Instance;
            }
            bindingFlags = BindingFlags.Public;

            Type[]   argTypeArray = new Type[length];
            object[] argValueAry  = new object[length];
            for (int i = 0; i < length; i++)
            {
                argTypeArray[i] = argArray[i + 1].Type;
                argValueAry[i]  = argArray[i + 1].Value;
            }

            Binder       defaultBinder = Type.DefaultBinder;
            PropertyInfo pi            = type.GetProperty("Item", bindingFlags, defaultBinder, null, argTypeArray, null);

            if (pi == null)
            {
                pi = type.GetProperty("Items", bindingFlags, defaultBinder, null, argTypeArray, null);
            }
            if (pi != null)
            {
                return(new Result(pi.PropertyType, pi.GetValue(target, argValueAry)));
            }

            if (target == null)
            {
                throw new ArgumentException();
            }

            object ret;

            if (target is string)
            {
                string s     = (string)target;
                int    index = (int)ConvertHelper.ChangeType(argArray[1].Value, typeof(int));
                ret = s[index];
                return(new Result(typeof(char), ret));
            }

            if (target is Array)
            {
                Array array = (Array)target;
                if (array.Rank != (argArray.Length - 1))
                {
                    throw new ArgumentException();
                }

                int[] indices = new int[length];
                for (int i = 0; i < length; i++)
                {
                    indices[i] = (int)ConvertHelper.ChangeType(argArray[i + 1].Value, typeof(int));
                }

                ret = array.GetValue(indices);
                if (ret != null)
                {
                    return(new Result(ret.GetType(), ret));
                }
                return(new Result(null, null));
            }

            if (target is IList)
            {
                IList list = (IList)target;
                if (argArray.Length != 2)
                {
                    throw new ArgumentException();
                }
                int index = (int)ConvertHelper.ChangeType(argArray[1].Value, typeof(int));
                ret = list[index];
                if (ret != null)
                {
                    return(new Result(ret.GetType(), ret));
                }
                return(new Result(null, null));
            }

            throw new ArgumentException();
        }
Exemplo n.º 4
0
 internal override Result Eval(Evaluator evaluater, Result[] argArray)
 {
     return(new Result(typeof(bool), !(bool)argArray[0].Value));
 }
Exemplo n.º 5
0
 internal override Result Eval(Evaluator evaluater, Result[] argArray)
 {
     return(new Result(_type, _value));
 }