コード例 #1
0
        public override ValueExpression Evaluate(IParserContext context)
        {
            ValueExpression value = Value.Evaluate(context);

            if (value.Type == typeof(int))
            {
                return(Exp.Value(TokenPosition, ~(int)value.Value));
            }

            if (value.Type == typeof(uint))
            {
                return(Exp.Value(TokenPosition, ~(uint)value.Value));
            }

            if (value.Type == typeof(long))
            {
                return(Exp.Value(TokenPosition, ~(long)value.Value));
            }

            if (value.Type == typeof(ulong))
            {
                return(Exp.Value(TokenPosition, ~(ulong)value.Value));
            }

            throw new IllegalOperandsException("Bitwise operator not supported on " + value.Value, this);
        }
コード例 #2
0
ファイル: IsExpression.cs プロジェクト: mdabbagh88/core
        public override ValueExpression Evaluate(IParserContext context)
        {
            ClassName       className   = _typeExpression.Evaluate(context).Value as ClassName;
            ValueExpression objectValue = _objectExpression.Evaluate(context);
            Type            objectType  = objectValue.Type;

            if (objectValue.Value == null)
            {
                return(Exp.Value(TokenPosition, false));
            }

            objectType = objectType.Inspector().RealType;

            if (className == null)
            {
                throw new ExpressionEvaluationException("is operator requires a type. " + _typeExpression + " is not a type", this);
            }

            Type checkType = className.Type;

            if (!objectType.Inspector().IsValueType)
            {
                return(Exp.Value(TokenPosition, checkType.Inspector().IsAssignableFrom(objectType)));
            }

            checkType = Nullable.GetUnderlyingType(checkType) ?? checkType;

            return(Exp.Value(TokenPosition, checkType == objectType));
        }
コード例 #3
0
        public override ValueExpression Evaluate(IParserContext context)
        {
            ClassName className = _typeExpression.Evaluate(context).Value as ClassName;

            if (className == null)
            {
                throw new IllegalOperandsException("as operator requires type. " + _typeExpression + " is not a type", this);
            }

            Type            checkType   = className.Type;
            ValueExpression objectValue = _objectExpression.Evaluate(context);
            Type            objectType  = objectValue.Type;

            if (objectValue.Value == null)
            {
                return(Exp.Value(TokenPosition, null, checkType));
            }

            objectType = objectType.Inspector().RealType;

            if (!objectType.Inspector().IsValueType)
            {
                return(Exp.Value(TokenPosition, objectValue.Value, checkType));
            }

            if ((Nullable.GetUnderlyingType(checkType) ?? checkType) == objectType)
            {
                return(Exp.Value(TokenPosition, objectValue.Value, checkType));
            }

            return(Exp.Value(TokenPosition, null, checkType));
        }
コード例 #4
0
ファイル: TypeCastExpression.cs プロジェクト: mdabbagh88/core
        public override ValueExpression Evaluate(IParserContext context)
        {
            ClassName className = _typeExpression.Evaluate(context).Value as ClassName;

            if (className == null)
            {
                throw new ExpressionEvaluationException("type cast requires a type. " + _typeExpression + " is not a type", this);
            }

            return(Exp.Value(TokenPosition, Convert.ChangeType(_targetExpression.Evaluate(context).Value, className.Type, null), className.Type));
        }
コード例 #5
0
        public override ValueExpression Evaluate(IParserContext context)
        {
            ClassName className = ClassName.Evaluate(context).Value as ClassName;

            if (className == null)
            {
                throw new TypeInitializationException(ClassName.VarName, null);
            }

            return(Exp.Value(TokenPosition, className.Type.Inspector().GetConstructors()));
        }
コード例 #6
0
ファイル: OrElseExpression.cs プロジェクト: viciproject/core
        public override ValueExpression Evaluate(IParserContext context)
        {
            object left = Left.Evaluate(context).Value;

            if (context.ToBoolean(left))
            {
                return(Exp.Value(TokenPosition, true));
            }

            object right = Right.Evaluate(context).Value;

            return(Exp.Value(TokenPosition, context.ToBoolean(right)));
        }
コード例 #7
0
        public override ValueExpression Evaluate(IParserContext context)
        {
            ValueExpression value = Value.Evaluate(context);

            if (context == null)
            {
                return(Exp.Value(TokenPosition, !((bool)value.Value)));
            }
            else
            {
                return(Exp.Value(TokenPosition, !context.ToBoolean(value.Value)));
            }
        }
コード例 #8
0
        public override ValueExpression Evaluate(IParserContext context)
        {
            ValueExpression returnValue = null;

            foreach (var expression in Expressions)
            {
                returnValue = expression.Evaluate(context);

                if (returnValue is ReturnValueExpression)
                {
                    return(returnValue);
                }
            }

            if (returnValue != null)
            {
                return(returnValue);
            }

            return(Exp.Value(TokenPosition, null, typeof(object)));
        }
コード例 #9
0
        public override ValueExpression Evaluate(IParserContext context)
        {
            ValueExpression from = _from.Evaluate(context);
            ValueExpression to   = _to.Evaluate(context);

            if (from.Type != typeof(int) && from.Type != typeof(long))
            {
                throw new ExpressionEvaluationException("Expression " + from + " does not evaluate to int or long", from);
            }

            if (to.Type != typeof(int) && to.Type != typeof(long))
            {
                throw new ExpressionEvaluationException("Expression " + from + " does not evaluate to int or long", from);
            }

            if (from.Type == typeof(long) || to.Type == typeof(long))
            {
                return(Exp.Value(TokenPosition, Range((long)Convert.ChangeType(from.Value, typeof(long), null), (long)Convert.ChangeType(to.Value, typeof(long), null))));
            }
            else
            {
                return(Exp.Value(TokenPosition, Range((int)Convert.ChangeType(from.Value, typeof(int), null), (int)Convert.ChangeType(to.Value, typeof(int), null))));
            }
        }
コード例 #10
0
        public override ValueExpression Evaluate(IParserContext context)
        {
            ValueExpression value = _value.Evaluate(context);

            if (value.Type == typeof(decimal))
            {
                return(Exp.Value(TokenPosition, -(decimal)value.Value));
            }

            if (value.Type == typeof(double))
            {
                return(Exp.Value(TokenPosition, -(double)value.Value));
            }

            if (value.Type == typeof(float))
            {
                return(Exp.Value(TokenPosition, -(float)value.Value));
            }

            if (value.Type == typeof(uint))
            {
                return(Exp.Value(TokenPosition, -(uint)value.Value));
            }

            if (value.Type == typeof(int))
            {
                return(Exp.Value(TokenPosition, -(int)value.Value));
            }

            if (value.Type == typeof(long))
            {
                return(Exp.Value(TokenPosition, -(long)value.Value));
            }

            throw new IllegalOperandsException("Unary minus is not supported for " + _value, this);
        }
コード例 #11
0
        public ValueExpression Evaluate(IParserContext context, bool assign, object newValue)
        {
            ValueExpression targetValue = Target.Evaluate(context);

            Type   targetType   = targetValue.Type;
            object targetObject = targetValue.Value;

            ValueExpression[] parameters      = EvaluateExpressionArray(Parameters, context);
            Type[]            parameterTypes  = parameters.ConvertAll(expr => expr.Type);
            object[]          parameterValues = parameters.ConvertAll(expr => expr.Value);

            if (targetType.IsArray)
            {
                if (targetType.GetArrayRank() != parameters.Length)
                {
                    throw new Exception("Array has a different rank. Number of arguments is incorrect");
                }

                Type returnType = targetType.GetElementType();

                bool useLong = false;

                foreach (Type t in parameterTypes)
                {
                    if (t == typeof(long) || t == typeof(long?))
                    {
                        useLong = true;
                    }
                    else if (t != typeof(int) & t != typeof(int?) && t != typeof(short) && t != typeof(short?) && t != typeof(ushort) && t != typeof(ushort?))
                    {
                        throw new BadArgumentException(t.GetType().Name + " is not a valid type for array indexers", this);
                    }
                }

#if !PCL
                if (useLong)
                {
                    long[] indexes = new long[parameters.Length];

                    for (int i = 0; i < parameters.Length; i++)
                    {
                        indexes[i] = Convert.ToInt64(parameterValues[i]);
                    }

                    if (assign)
                    {
                        ((Array)targetObject).SetValue(newValue, indexes);
                    }

                    return(Exp.Value(TokenPosition, ((Array)targetObject).GetValue(indexes), returnType));
                }
                else
#endif
                {
                    int[] indexes = new int[parameters.Length];

                    for (int i = 0; i < parameters.Length; i++)
                    {
                        indexes[i] = Convert.ToInt32(parameterValues[i]);
                    }

                    if (assign)
                    {
                        ((Array)targetObject).SetValue(newValue, indexes);
                    }

                    return(Exp.Value(TokenPosition, ((Array)targetObject).GetValue(indexes), returnType));
                }
            }
            else
            {
                DefaultMemberAttribute[] att = targetType.Inspector().GetCustomAttributes <DefaultMemberAttribute>(true);

                MethodInfo methodInfo = targetType.Inspector().GetPropertyGetter(att[0].MemberName, parameterTypes);

                object value = methodInfo.Invoke(targetObject, parameterValues);

                return(new ValueExpression(TokenPosition, value, methodInfo.ReturnType));
            }
        }
コード例 #12
0
        public static Expression StringLiteral(string token, TokenPosition position, Expression[] terms)
        {
            string s = token.Substring(1, token.Length - 2);

            if (s.IndexOf('\\') < 0)
            {
                return(Exp.Value(position, s));
            }

            StringBuilder output = new StringBuilder(token.Length);

            bool   inEscape  = false;
            string hexString = null;

            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];

                if (inEscape)
                {
                    if (c == 'x')
                    {
                        hexString = "";
                        continue;
                    }

                    if (hexString == null && (c != 'x' || c != 'X'))
                    {
                        output.Append(UnEscape("\\" + c, position));
                        inEscape = false;
                        continue;
                    }

                    if (hexString == null)
                    {
                        inEscape = false;
                    }
                    else
                    {
                        if (((char.ToLower(c) < 'a' || char.ToLower(c) > 'f') && (c < '0' || c > '9')) || hexString.Length == 4)
                        {
                            output.Append(UnEscape("\\x" + hexString, position));
                            inEscape  = false;
                            hexString = null;
                        }
                        else
                        {
                            hexString += c;
                            continue;
                        }
                    }
                }

                if (c != '\\')
                {
                    output.Append(c);

                    continue;
                }

                inEscape = true;
            }

            return(Exp.Value(position, output.ToString()));
        }
コード例 #13
0
        public static Expression Number(string token, TokenPosition position, Expression[] terms)
        {
            string s = token;

            Type type = null;

            if (!char.IsDigit(s[s.Length - 1]))
            {
                string suffix = "" + char.ToUpper(s[s.Length - 1]);

                s = s.Remove(s.Length - 1);

                if (!char.IsDigit(s[s.Length - 1]))
                {
                    suffix = char.ToUpper(s[s.Length - 1]) + suffix;

                    s = s.Remove(s.Length - 1);
                }

                switch (suffix)
                {
                case "M":
                    type = typeof(decimal);
                    break;

                case "D":
                    type = typeof(double);
                    break;

                case "F":
                    type = typeof(float);
                    break;

                case "L":
                    type = typeof(long);
                    break;

                case "U":
                    type = typeof(uint);
                    break;

                case "LU":
                case "UL":
                    type = typeof(ulong);
                    break;
                }
            }

            if (type != null)
            {
                return(new ValueExpression(position, Convert.ChangeType(s, type, _numberFormat), type));
            }

            if (s.LastIndexOf('.') >= 0)
            {
                return(Exp.Value(position, Convert.ToDouble(s, _numberFormat)));
            }
            else
            {
                long n = Convert.ToInt64(s);

                if (n > Int32.MaxValue || n < Int32.MinValue)
                {
                    return(Exp.Value(position, n));
                }
                else
                {
                    return(Exp.Value(position, (int)n));
                }
            }
        }
コード例 #14
0
 public static Expression CharLiteral(string token, TokenPosition position, Expression[] terms)
 {
     return(Exp.Value(position, UnEscape(token.Substring(1, token.Length - 2), position)));
 }
コード例 #15
0
        private ValueExpression Evaluate(IParserContext context, bool assign, object newValue)
        {
            ValueExpression targetValue = Target.Evaluate(context);
            object          targetObject;
            Type            targetType;

            if (targetValue.Value is ClassName)
            {
                targetType   = ((ClassName)targetValue.Value).Type;
                targetObject = null;
            }
            else
            {
                targetType   = targetValue.Type;
                targetObject = targetValue.Value;

                if (targetObject == null)
                {
                    return(new ValueExpression(TokenPosition, null, targetType));
                }
            }

            if (targetObject is IDynamicObject)
            {
                object value;
                Type   type;

                if (((IDynamicObject)targetObject).TryGetValue(Member, out value, out type))
                {
                    return(new ValueExpression(TokenPosition, value, type));
                }
            }

            MemberInfo[] members = FindMemberInHierarchy(targetType, Member);        // targetType.GetMember(_member);

            if (members.Length == 0)
            {
                PropertyInfo indexerPropInfo = targetType.Inspector().GetIndexer(new[] { typeof(string) });

                if (indexerPropInfo != null)
                {
                    return(new ValueExpression(TokenPosition, indexerPropInfo.GetValue(targetObject, new object[] { Member }), indexerPropInfo.PropertyType));
                }

                throw new UnknownPropertyException("Unknown property " + Member + " for object " + Target + " (type " + targetType.Name + ")", this);
            }

            if (members.Length >= 1 && members[0] is MethodInfo)
            {
                if (targetObject == null)
                {
                    return(Exp.Value(TokenPosition, new StaticMethod(targetType, Member)));
                }
                else
                {
                    return(Exp.Value(TokenPosition, new InstanceMethod(targetType, Member, targetObject)));
                }
            }

            MemberInfo member = members[0];

            if (members.Length > 1 && targetObject != null)     // CoolStorage, ActiveRecord and Dynamic Proxy frameworks sometimes return > 1 member
            {
                foreach (MemberInfo mi in members)
                {
                    if (mi.DeclaringType == targetObject.GetType())
                    {
                        member = mi;
                    }
                }
            }

            if (assign)
            {
                if (member is FieldInfo)
                {
                    ((FieldInfo)member).SetValue(targetObject, newValue);
                }

                if (member is PropertyInfo)
                {
                    ((PropertyInfo)member).SetValue(targetObject, newValue, null);
                }

                // Fall through to get the new property/field value below
            }

            if (member is FieldInfo)
            {
                return(new ValueExpression(TokenPosition, ((FieldInfo)member).GetValue(targetObject), ((FieldInfo)member).FieldType));
            }

            if (member is PropertyInfo)
            {
                return(new ValueExpression(TokenPosition, ((PropertyInfo)member).GetValue(targetObject, null), ((PropertyInfo)member).PropertyType));
            }

            throw new ExpressionEvaluationException(Member + " is not a field or property", this);
        }
コード例 #16
0
ファイル: CallExpression.cs プロジェクト: mdabbagh88/core
        public override ValueExpression Evaluate(IParserContext context)
        {
            object methodObject = MethodExpression.Evaluate(context).Value;

            ValueExpression[] parameters      = EvaluateExpressionArray(Parameters, context);
            Type[]            parameterTypes  = parameters.ConvertAll(expr => expr.Type);
            object[]          parameterValues = parameters.ConvertAll(expr => expr.Value);

            if (methodObject is MethodDefinition)
            {
                Type returnType;

                return(Exp.Value(TokenPosition, ((MethodDefinition)methodObject).Invoke(parameterTypes, parameterValues, out returnType), returnType));
            }

            if (methodObject is ConstructorInfo[])
            {
                ConstructorInfo[] constructors = (ConstructorInfo[])methodObject;

                MethodBase method = LazyBinder.SelectBestMethod(constructors, parameterTypes, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);

                if (method == null)
                {
                    throw new ExpressionEvaluationException("No match found for constructor " + constructors[0].Name, this);
                }

                object value = LazyBinder.Invoke(method, parameterValues);

                //object value = ((ConstructorInfo)method).Invoke(parameterValues);

                return(Exp.Value(TokenPosition, value, method.DeclaringType));
            }

            if (methodObject is Delegate[])
            {
                Delegate[]   delegates = (Delegate[])methodObject;
                MethodBase[] methods   = delegates.ConvertAll <Delegate, MethodBase>(d => d.GetMethodInfo());

                MethodBase method = LazyBinder.SelectBestMethod(methods, parameterTypes, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);

                if (method == null)
                {
                    throw new ExpressionEvaluationException("No match found for delegate " + MethodExpression, this);
                }

                object value = LazyBinder.Invoke(method, delegates[Array.IndexOf(methods, method)].Target, parameterValues);

                return(Exp.Value(TokenPosition, value, ((MethodInfo)method).ReturnType));
            }

            if (methodObject is Delegate)
            {
                Delegate   method     = (Delegate)methodObject;
                MethodInfo methodInfo = method.GetMethodInfo();

                object value = methodInfo.Invoke(method.Target, parameterValues);

                return(new ValueExpression(TokenPosition, value, methodInfo.ReturnType));
            }

            if (methodObject is FunctionDefinitionExpression)
            {
                FunctionDefinitionExpression func = (FunctionDefinitionExpression)methodObject;

                var functionContext = context.CreateLocal();

                for (int i = 0; i < parameterValues.Length; i++)
                {
                    functionContext.Set(func.ParameterNames[i], parameterValues[i]);
                }

                return(func.Body.Evaluate(functionContext));
            }

            throw new ExpressionEvaluationException(MethodExpression + " is not a function", this);
        }
コード例 #17
0
 public override ValueExpression Evaluate(IParserContext context)
 {
     return(Exp.Value(TokenPosition, new Converter <ClassName, Type>(GetTypeOfClassName)));
 }