예제 #1
0
        /// <summary>
        /// Parse the element text and make the element point to delegate which evaluate the text if necessary.
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static QsSequenceElement Parse(string element, QsEvaluator qse, QsSequence sequence)
        {
            if (string.IsNullOrEmpty(element))
            {
                throw new QsException("Can't create element from empty string.");
            }

            //try direct quantity
            AnyQuantity <double> v;

            if (Unit.TryParseQuantity(element, out v))
            {
                var el = QsSequenceElement.FromQuantity(new QsScalar {
                    NumericalQuantity = v
                });
                el.ElementDeclaration = element;

                el.IndexEvaluation     = false;
                el.ParameterEvaluation = false;
                return(el);
            }
            else
            {
                QsSequenceElement se = new QsSequenceElement();

                //try one index delegate without parameters
                //Create the lambda function that will pass the index and parameters to the expression.
                SimpleLambdaBuilder lb = SimpleLambdaBuilder.Create(typeof(QsValue), "ElementValue");

                //add the index parameter
                lb.Parameter(typeof(int), sequence.SequenceIndexName);


                //find the index parameter in line to know if it will be evaluated or not
                if (element.IndexOf(sequence.SequenceIndexName) > -1)
                {
                    se.IndexEvaluation = true;
                }


                //make the sequence parameters.
                foreach (var seqParam in sequence.Parameters)
                {
                    lb.Parameter(typeof(QsValue), seqParam.Name);
                    if (element.IndexOf(seqParam.Name) > -1)
                    {
                        se.ParameterEvaluation = true;
                    }
                }

                QsVar pvar = new QsVar(qse, element, sequence, lb);

                lb.Body = pvar.ResultExpression;

                LambdaExpression le = lb.MakeLambda();

                se.ElementDeclaration = element;
                se.ElementExpression  = pvar.ResultExpression;
                se.ElementValue       = le.Compile();

                return(se);
            }

            throw new QsException("Check me in sequence element :( :( :( ");
        }