Esempio n. 1
0
        public override ValueExpression Evaluate(IParserContext context)
        {
            IEnumerable collection = Expression.Evaluate(context).Value as IEnumerable;

            if (collection != null)
                foreach (var item in collection)
                {
                    var localContext = context.CreateLocal();

                    localContext.Set(Iterator.VarName, item);

                    var returnValue = Body.Evaluate(localContext);

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

            return Exp.NullValue(TokenPosition);
        }
Esempio n. 2
0
        public override ValueExpression Evaluate(IParserContext context)
        {
            IEnumerable collection = Expression.Evaluate(context).Value as IEnumerable;

            if (collection != null)
            {
                foreach (var item in collection)
                {
                    var localContext = context.CreateLocal();

                    localContext.Set(Iterator.VarName, item);

                    var returnValue = Body.Evaluate(localContext);

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

            return(Exp.NullValue(TokenPosition));
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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);
        }