Beispiel #1
0
        Result EvalProp(Evaluator evaluater, Result[] argArray)
        { 
            if (argArray.Length == 0)
            {
                throw new ArgumentException();
            }

            if (argArray.Length == 1)
            {
                return argArray[0];
            }

            string memberName = argArray[1].Value as string;
            if (memberName == null)
            {
                throw new ArgumentException();
            }

            if (argArray[0] == null)
            {
                throw new ArgumentNullException("argArray[0]");
            }

            object target = argArray[0].Value;
            if (target == null)
            {
                throw new ArgumentException();
            }

            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; 
            PropertyInfo pi = type.GetProperty(memberName, bindingFlags);
            if (pi != null)
            {
                return new Result(pi.PropertyType, pi.GetValue(target, null));
            }

            FieldInfo fi = type.GetField(memberName, bindingFlags);
            if (fi != null)
            {
                return new Result(fi.FieldType, fi.GetValue(target));
            }

            throw new ArgumentException();

        }
Beispiel #2
0
 internal override Result Eval(Evaluator evaluater, Result[] argArray)
 {
     if (_isFunction)
     {
         return EvalFunc(evaluater, argArray); 
     }
     else
     {
         return EvalProp(evaluater, argArray);
     }
 }
 internal override Result Eval(Evaluator evaluater, Result[] argArray)
 {
     if ((bool)argArray[0].Value)
     {
         return argArray[1];
     }
     else
     {
         return argArray[2];
     }
 }
Beispiel #4
0
        internal override Result Eval(Evaluator evaluater, Result[] argArray)
        {
            if (!evaluater.VariableHolder.Exists(this.Name))
            {
                throw EvalException.VariableNotExist(this.Name);
            }

            object o = evaluater.VariableHolder.GetVariable(this.Name);
            if (o == null)
            {
                return new Result(null, null);
            }
            return new Result(o.GetType(), o);
        }
Beispiel #5
0
        protected virtual void ProcessArg(Evaluator evaluater, Result[] argArray)
        {
            if (argArray.Length != 2)
            {
                throw new ArgumentException();
            }

            foreach (Result r in argArray)
            {
                if (!IsEvalableType(r.Type))
                {
                    throw new NotSupportedException();
                }
            }
        }
Beispiel #6
0
        Result EvalFunc(Evaluator evaluater, Result[] argArray)
        {
            if (argArray.Length < 2)
            {
                throw new ArgumentException();
            }

            if (argArray[0] == null || argArray[1] == null)
            {
                throw new ArgumentNullException("argArray[0]");
            }

            object target = argArray[0].Value;
            if (target == null)
            {
                throw new ArgumentException();
            }

            string functionName = ConvertHelper.ToString(argArray[1].Value);

            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.InvokeMethod | BindingFlags.Public | BindingFlags.OptionalParamBinding;

            int length = argArray.Length - 2;
            object[] argValueAry = new object[length];
            for (int i = 0; i < length; i++)
            {
                argValueAry[i] = argArray[i + 2].Value;
            }
            Binder defaultBinder = Type.DefaultBinder;
            object ret = type.InvokeMember(functionName,
                bindingFlags, defaultBinder, target, argValueAry);
            if (ret != null)
                return new Result(ret.GetType(), ret);
            return new Result(null, null);
        }
Beispiel #7
0
 protected override Result DoEval(Evaluator evaluater, Result[] argArray)
 {
     Type type0 = argArray[0].Type;
     Type type1 = argArray[1].Type;
     if (type0 == null && type1 == null)
     {
         return new Result(null, null);
     }
     else
     {
         if (type0 == typeof(string))
             return new Result( ConvertHelper.ToString(argArray[0].Value) +  ConvertHelper.ToString(argArray[1].Value));
         else
         {
             double ret = System.Convert.ToDouble(argArray[0].Value) + System.Convert.ToDouble(argArray[1].Value);
             return new Result(type0, ConvertHelper.ChangeType(ret, type0));
         }
     }
 }
Beispiel #8
0
 internal override Result Eval(Evaluator evaluater, Result[] argArray)
 {
     return new Result(typeof(bool), !(bool)argArray[0].Value);
 }
Beispiel #9
0
 internal override Result Eval(Evaluator evaluater, Result[] argArray)
 {
     return new Result(typeof(int), ~((uint)ConvertHelper.ChangeType(argArray[0].Value, typeof(uint))));
 }
Beispiel #10
0
 protected override void ProcessArg(Evaluator evaluater, Result[] argArray)
 {
     base.ProcessArg(evaluater, argArray);
 }
Beispiel #11
0
 internal abstract Result Eval(Evaluator evaluater, Result[] argArray);
Beispiel #12
0
 protected abstract Result DoEval(Evaluator evaluater, Result[] argArray);
Beispiel #13
0
        protected override void ProcessArg(Evaluator evaluater, Result[] argArray)
        {
            base.ProcessArg(evaluater, argArray);
            Type type0 = argArray[0].Type;
            Type type1 = argArray[1].Type;
            if (type0 == null && type1 == null)
            {
                return;
            }

            if (type0 == null)
            {
                if (type1.IsValueType)
                {
                    throw new InvalidCastException(string.Format("from null to {0}", type1.Name));
                }
            }
            if (type1 == null)
            {
                if (type0.IsValueType)
                {
                    throw new InvalidCastException(string.Format("from null to {0}", type0.Name));
                }
            }

            int result;
            if (BinaryHelper.ComparePrimitiveType(type0, type1, out result))
            {
                if (result > 0)
                {
                    argArray[1].Value = ConvertHelper.ChangeType(argArray[1].Value, type0);
                }
                else if (result < 0)
                {
                    argArray[0].Value = ConvertHelper.ChangeType(argArray[0].Value, type1);
                }
            }
        }
Beispiel #14
0
 protected override Result DoEval(Evaluator evaluater, Result[] argArray)
 {
     Type type = typeof(int);
     int ret = System.Convert.ToInt32(argArray[0].Value) % System.Convert.ToInt32(argArray[1].Value);
     return new Result(type, ConvertHelper.ChangeType(ret, type));
 }
Beispiel #15
0
 protected override Result DoEval(Evaluator evaluater, Result[] argArray)
 {
     return new Result(typeof(bool), (bool)argArray[0].Value && (bool)argArray[1].Value);
 }
Beispiel #16
0
 protected override Result DoEval(Evaluator evaluater, Result[] argArray)
 {
     Type type = argArray[0].Type;
     double ret = System.Convert.ToDouble(argArray[0].Value) / System.Convert.ToDouble(argArray[1].Value);
     return new Result(type, ConvertHelper.ChangeType(ret, type));
 }
Beispiel #17
0
 protected override Result DoEval(Evaluator evaluater, Result[] argArray)
 {
     int ret = 0;
     if (_isleft)
         ret = System.Convert.ToInt32(argArray[0].Value) << System.Convert.ToInt32(argArray[1].Value);
     else
     {
         ret = System.Convert.ToInt32(argArray[0].Value) >> System.Convert.ToInt32(argArray[1].Value);
     }
     return new Result(typeof(int), ret);
 }
Beispiel #18
0
 protected override Result DoEval(Evaluator evaluater, Result[] argArray)
 {
     int ret = System.Convert.ToInt32(argArray[0].Value) & System.Convert.ToInt32(argArray[1].Value);
     return new Result(typeof(int), ret);
 }
Beispiel #19
0
 protected override Result DoEval(Evaluator evaluater, Result[] argArray)
 {
     int result = 0;
     if (argArray[0].IsNull() && argArray[1].IsNull())
     {
         return ProcessResult(0);
     }
     IComparable cb0 = argArray[0].Value as IComparable;
     IComparable cb1 = argArray[1].Value as IComparable;
     if (cb0 != null)
     {
         result = cb0.CompareTo(argArray[1].Value);
     }
     else if (cb1 != null)
     {
         result = -cb1.CompareTo(argArray[0].Value);
     }
     else
     {
         throw new NotSupportedException();
     }
     return ProcessResult(result);
 }
Beispiel #20
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();
        }
Beispiel #21
0
 internal override Result Eval(Evaluator evaluater, Result[] argArray)
 {
     ProcessArg(evaluater, argArray);
     return DoEval(evaluater, argArray );
 }
Beispiel #22
0
 internal override Result Eval(Evaluator evaluater, Result[] argArray)
 {
     return new Result(typeof(string), this.Name);
 }
Beispiel #23
0
        static void Main(string[] args)
        {
            Type type = typeof(string);

            Parser ep = new Parser();

            Jyc.Expr.Evaluator evaluater = new Jyc.Expr.Evaluator();

            ParameterVariableHolder pvh = new ParameterVariableHolder();

            pvh.Parameters["char"] = new Parameter(typeof(char));
            pvh.Parameters["sbyte"] = new Parameter(typeof(sbyte));
            pvh.Parameters["byte"] = new Parameter(typeof(byte));
            pvh.Parameters["short"] = new Parameter(typeof(short));
            pvh.Parameters["ushort"] = new Parameter(typeof(ushort));
            pvh.Parameters["int"] = new Parameter(typeof(int));
            pvh.Parameters["uint"] = new Parameter(typeof(uint));
            pvh.Parameters["long"] = new Parameter(typeof(string));
            pvh.Parameters["ulong"] = new Parameter(typeof(ulong));
            pvh.Parameters["float"] = new Parameter(typeof(float));
            pvh.Parameters["double"] = new Parameter(typeof(double));
            pvh.Parameters["decimal"] = new Parameter(typeof(decimal));
            pvh.Parameters["DateTime"] = new Parameter(typeof(DateTime));

            pvh.Parameters["string"] = new Parameter(typeof(string));
            pvh.Parameters["Guid"] = new Parameter(typeof(Guid));
            pvh.Parameters["HttpUtility"] = new Parameter(typeof(HttpUtility));
            pvh.Parameters["Convert"] = new Parameter(typeof(Convert));
            pvh.Parameters["Math"] = new Parameter(typeof(Math));
            pvh.Parameters["Array "] = new Parameter(typeof(Array));
            pvh.Parameters["Random"] = new Parameter(typeof(Random));
            pvh.Parameters["TimeZone"] = new Parameter(typeof(TimeZone));
            pvh.Parameters["AppDomain "] = new Parameter(typeof(AppDomain));
            pvh.Parameters["Console"] = new Parameter(typeof(Console));
            pvh.Parameters["evaluater"] = new Parameter(evaluater);

            evaluater.VariableHolder = pvh;

            while (true)
            {
                System.Console.WriteLine("Input line,press Return to Eval:");
                string line = System.Console.ReadLine().Trim();
                if (string.IsNullOrEmpty(line))
                    break;
                try
                {
                    Tree tree = ep.Parse(line);

                    tree.Print(System.Console.Out);

                    object result = evaluater.Eval(tree);

                    if( result != null )
                        System.Console.WriteLine("Resut:{0}", result);
                    else
                        System.Console.WriteLine("Resut is null");
                }
                catch (Exception e)
                {
                    System.Console.WriteLine("Exception:" + e.GetType().Name +"->"+ e.Message);
                }
            }
        }
Beispiel #24
0
 internal override Result Eval(Evaluator evaluater, Result[] argArray)
 {
     return new Result(_type,_value);
 }
    /// <summary>
    /// Evaluates a simple expression, given via the parameter <paramref name="parameter"/>.
    /// </summary>
    /// <remarks>
    /// This converter will often be used in XAML files. Note that in XAML, an attribute beginning with a <c>'{'</c> character
    /// is interpreted as an invocation of a markup extension. So the expression "{0} + 5" must be escaped like this:
    /// <c>"{}{0} + 5"</c>. Note also that the boolean AND operator (<c>"&&"</c>) must be escaped too like this: <c>"{}{0} &amp;&amp; true"</c>.
    /// </remarks>
    /// <param name="values">The values used for the variables {0} .. {n}.</param>
    /// <param name="targetType">Type to that the evaluated result should be converted.</param>
    /// <param name="parameter">String containing the expression. Variables can be accessed via numbers in
    /// curly braces, for example "!({0} || {2})". The variables are mapped to the values specified by
    /// the <paramref name="values"/> array.</param>
    /// <param name="result">Will return the evaluated result of the given <paramref name="targetType"/>.</param>
    public bool Convert(IDataDescriptor[] values, Type targetType, object parameter, out object result)
    {
      result = null;
      string expression = parameter as string;
      if (string.IsNullOrEmpty(expression))
        return false;
      try
      {
        // We're using an expression parser from "devilplusplus", "C# Eval function"
        // See http://www.codeproject.com/KB/dotnet/Expr.aspx
        // The parser was slightly adapted to our needs:
        // - To access a variable, the variable identifier has to be written in curly braces, for example:
        //   {0} + {1}

        Parser ep = new Parser();
        Evaluator evaluator = new Evaluator();
        ParameterVariableHolder pvh = new ParameterVariableHolder();

        // The used expression parser supports access to static functions for those of the parameters whose type is a class.
        // We could add classes here like the code commented out below. To access a static member on the string class,
        // the expression could be for example: {string}.{Empty}
        // For now, we don't need this functionality, so we don't add types (Albert, 2009-04-22).

        //pvh.Parameters["char"] = new Parameter(typeof(char));
        //pvh.Parameters["sbyte"] = new Parameter(typeof(sbyte));
        //pvh.Parameters["byte"] = new Parameter(typeof(byte));
        //pvh.Parameters["short"] = new Parameter(typeof(short)); 
        //pvh.Parameters["ushort"] = new Parameter(typeof(ushort)); 
        //pvh.Parameters["int"] = new Parameter(typeof(int));
        //pvh.Parameters["uint"] = new Parameter(typeof(uint));
        //pvh.Parameters["long"] = new Parameter(typeof(string));
        //pvh.Parameters["ulong"] = new Parameter(typeof(ulong));
        //pvh.Parameters["float"] = new Parameter(typeof(float));
        //pvh.Parameters["double"] = new Parameter(typeof(double));
        //pvh.Parameters["decimal"] = new Parameter(typeof(decimal));
        //pvh.Parameters["DateTime"] = new Parameter(typeof(DateTime));
        //pvh.Parameters["string"] = new Parameter(typeof(string));

        //pvh.Parameters["Guid"] = new Parameter(typeof(Guid));

        //pvh.Parameters["Convert"] = new Parameter(typeof(Convert));
        //pvh.Parameters["Math"] = new Parameter(typeof(Math));
        //pvh.Parameters["Array"] = new Parameter(typeof(Array));
        //pvh.Parameters["Random"] = new Parameter(typeof(Random));
        //pvh.Parameters["TimeZone"] = new Parameter(typeof(TimeZone));

        // Add child binding values
        for (int i = 0; i < values.Length; i++)
        {
          IDataDescriptor value = values[i];
          Type type = value.DataType;
          if (type != null && !pvh.Parameters.Contains(type.Name))
            pvh.Parameters[type.Name] = new Parameter(type);
          pvh.Parameters[i.ToString()] = new Parameter(value.Value, type);
        }
        evaluator.VariableHolder = pvh;
        Tree tree = ep.Parse(expression);
        result = evaluator.Eval(tree);
        return TypeConverter.Convert(result, targetType, out result);
      }
      catch (Exception)
      {
        return false;
      }
    }
Beispiel #26
0
        public static object Evaluate(string Expression)
        {
            #region setup

            Type type = typeof(string);

            Parser ep = new Parser();

            Jyc.Expr.Evaluator evaluater = new Jyc.Expr.Evaluator();

            ParameterVariableHolder pvh = new ParameterVariableHolder();

            pvh.Parameters["char"] = new Parameter(typeof(char));
            pvh.Parameters["sbyte"] = new Parameter(typeof(sbyte));
            pvh.Parameters["byte"] = new Parameter(typeof(byte));
            pvh.Parameters["short"] = new Parameter(typeof(short));
            pvh.Parameters["ushort"] = new Parameter(typeof(ushort));
            pvh.Parameters["int"] = new Parameter(typeof(int));
            pvh.Parameters["uint"] = new Parameter(typeof(uint));
            pvh.Parameters["long"] = new Parameter(typeof(string));
            pvh.Parameters["ulong"] = new Parameter(typeof(ulong));
            pvh.Parameters["float"] = new Parameter(typeof(float));
            pvh.Parameters["double"] = new Parameter(typeof(double));
            pvh.Parameters["decimal"] = new Parameter(typeof(decimal));
            pvh.Parameters["DateTime"] = new Parameter(typeof(DateTime));
            pvh.Parameters["Int32"] = new Parameter(typeof(Int32));

            pvh.Parameters["string"] = new Parameter(typeof(string));
            pvh.Parameters["Guid"] = new Parameter(typeof(Guid));
            pvh.Parameters["HttpUtility"] = new Parameter(typeof(HttpUtility));
            pvh.Parameters["Convert"] = new Parameter(typeof(Convert));
            pvh.Parameters["Math"] = new Parameter(typeof(Math));
            pvh.Parameters["Array "] = new Parameter(typeof(Array));
            pvh.Parameters["Random"] = new Parameter(typeof(Random));
            pvh.Parameters["TimeZone"] = new Parameter(typeof(TimeZone));
            pvh.Parameters["AppDomain "] = new Parameter(typeof(AppDomain));
            pvh.Parameters["Console"] = new Parameter(typeof(Console));

            evaluater.VariableHolder = pvh;
            #endregion

            Tree tree = ep.Parse(Expression);

            object result = evaluater.Eval(tree);

            return result;
        }
Beispiel #27
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();
        }
Beispiel #28
0
        Result EvalProp(Evaluator evaluater, Result[] argArray)
        {
            if (argArray.Length == 0)
            {
                throw new ArgumentException();
            }

            if (argArray.Length == 1)
            {
                return(argArray[0]);
            }

            string memberName = argArray[1].Value as string;

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

            if (argArray[0] == null)
            {
                throw new ArgumentNullException("argArray[0]");
            }

            object target = argArray[0].Value;

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

            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;
            PropertyInfo pi = type.GetProperty(memberName, bindingFlags);

            if (pi != null)
            {
                return(new Result(pi.PropertyType, pi.GetValue(target, null)));
            }

            FieldInfo fi = type.GetField(memberName, bindingFlags);

            if (fi != null)
            {
                return(new Result(fi.FieldType, fi.GetValue(target)));
            }

            throw new ArgumentException();
        }