Exemplo n.º 1
0
        /// <summary>
        /// Level two function calling with Qs Values
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public QsValue Invoke(params QsValue[] args)
        {
            var parms = (from arg in args
                         select
                         QsParameter.MakeParameter(arg, arg.ToString())).ToArray();

            return((QsScalar)InvokeByQsParameters(parms));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Level one function calling with direct Quantities.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public AnyQuantity <double> Invoke(params AnyQuantity <double>[] args)
        {
            var parms = (from arg in args
                         select
                         QsParameter.MakeParameter(arg.ToScalarValue(), arg.ToShortString())).ToArray();

            return(((QsScalar)InvokeByQsParameters(parms)).NumericalQuantity);
        }
Exemplo n.º 3
0
        public static QsParameter MakeParameter(object value, string rawValue)
        {
            QsParameter qp = new QsParameter();

            qp.ParameterValue = value;

            qp.ParameterRawText = rawValue;

            return(qp);
        }
Exemplo n.º 4
0
        /// <summary>
        /// This function is used internally from the expression calls.
        ///  by this I was able to solve passing function handle more than once into another functions.
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public QsValue GetInvoke(params QsParameter[] parameters)
        {
            List <QsParameter> ProcessedParameters = new List <QsParameter>();

            for (int ip = 0; ip < parameters.Count(); ip++)
            {
                QsParameter nakedParameter;

                if (this.Parameters[ip].Type == QsParamType.Function)
                {
                    //Handle to function.
                    if (parameters[ip].ParameterValue != null)
                    {
                        nakedParameter = QsParameter.MakeParameter(parameters[ip].ParameterValue, parameters[ip].ParameterRawText);  // and I will postpone the evaluation untill we process the function.
                    }
                    else
                    {
                        //look for the raw value , this is the trick to keep the passed function name in the parameters if it wasn't evaluated

                        nakedParameter = QsParameter.MakeParameter(parameters[ip].ParameterRawText, parameters[ip].ParameterRawText);
                    }
                }
                else if (this.Parameters[ip].Type == QsParamType.Raw)
                {
                    nakedParameter = QsParameter.MakeParameter(parameters[ip].ParameterRawText, parameters[ip].ParameterRawText);
                }
                else
                {
                    //normal variable
                    nakedParameter = QsParameter.MakeParameter(parameters[ip].QsNativeValue, parameters[ip].ParameterRawText);
                }

                ProcessedParameters.Add(nakedParameter);
            }

            return(InvokeByQsParameters(ProcessedParameters.ToArray()));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Evaluate every argument and call the suitable function
        /// </summary>
        /// <param name="vario"></param>
        /// <returns></returns>
        internal Expression GetInvokeExpression(QsVar vario, string[] args)
        {
            List <Expression> parameters = new List <Expression>();

            for (int ip = 0; ip < args.Count(); ip++)
            {
                Expression nakedParameter;
                Expression rawParameter = Expression.Constant(args[ip].Trim());

                if (this.Parameters[ip].Type == QsParamType.Function) //is this parameter in declaration is pointing to function handle
                {
                    //yes: treat this parameter as function handle
                    // get the argument as a string value to be used after that as a function name.
                    nakedParameter = Expression.Constant(args[ip]);  // and I will postpone the evaluation until we process the function.

                    //expression to make parameter from the corresponding naked parameter
                    parameters.Add(Expression.Call(typeof(QsParameter).GetMethod("MakeParameter"),
                                                   nakedParameter,
                                                   rawParameter));
                }
                else if (this.Parameters[ip].Type == QsParamType.Raw)
                {
                    //don't evaluate the parameter
                    // just take the text and pass it to the function as it is.
                    nakedParameter = Expression.Constant(QsParameter.MakeParameter(args[ip], args[ip]));

                    parameters.Add(nakedParameter);
                }
                else
                {
                    //normal variable

                    nakedParameter = vario.ParseArithmatic(args[ip]);

                    // if this was another function name without like  v(c,g,8)  where g is a function name will be passed to c
                    //  then excpetion will occur in GetQuantity that variable was not found in the scope

                    Expression tryBody = Expression.Call(typeof(QsParameter).GetMethod("MakeParameter"),
                                                         nakedParameter,
                                                         rawParameter);


                    // -> Catch(QsVariableNotFoundException e) {QsParameter.MakeParameter(e.ExtraData, args[ip])}

                    var        e         = Expression.Parameter(typeof(QsVariableNotFoundException), "e");
                    Expression catchBody = Expression.Call(typeof(QsParameter).GetMethod("MakeParameter"),
                                                           Expression.Property(e, "ExtraData"),
                                                           rawParameter);

                    // The try catch block when catch exception will execute the call but by passing the parameter as text only

                    /*
                     * var tt = Utils.Try(tryBody);
                     *
                     * tt.Catch(e, catchBody);
                     * Expression tryc  = tt.ToExpression()
                     */
                    Expression tryc = Expression.TryCatch(tryBody, Expression.Catch(e, catchBody));

                    parameters.Add(tryc);
                }
            }


            var qsParamArray = Expression.NewArrayInit(typeof(QsParameter), parameters);

            return(Expression.Call(Expression.Constant(this), this.GetType().GetMethod("InvokeByQsParameters"), qsParamArray));
        }