Ejemplo n.º 1
0
        // Get the value of a number or variable.
        void Atom(out ParserResult result)
        {
            double m_double;
            int    m_int;

            switch (tokType)
            {
            case Types.DOUBLE:
                stack.Add("double: " + token);
                if (Double.TryParse(token, out m_double))
                {
                    result = new ParserResult(m_double);
                }
                else
                {
                    result = new ParserResult(0.0);
                    SyntaxErr(Errors.SYNTAX);
                }
                GetToken();
                return;

            case Types.INTEGER:

                stack.Add("integer: " + token);
                if (Int32.TryParse(token, out m_int))
                {
                    result = new ParserResult(m_int);
                }
                else
                {
                    result = new ParserResult(0);
                    SyntaxErr(Errors.SYNTAX);
                }
                GetToken();
                return;

            case Types.STRING:
                stack.Add("string: " + token);
                result = new ParserResult(token);
                GetToken();
                return;

            case Types.VARIABLE:
                string alias      = "";
                int    timeOffset = 0;
                stack.Add("variable: " + token);
                m_VariableParser.ParseVariableToken(token, out alias, out timeOffset);

                if (Regex.IsMatch(alias, "\'.+\'"))
                {    // strip single quotes
                    alias = alias.Substring(1, alias.Length - 2);
                }
                result = VariableResolver.Lookup(alias, defaultTimeInterval);
                if (Debug)
                {
                    Logger.WriteLine("SeriesLookup, Series:'" + alias + "'  Time offset: " + timeOffset + ", IsString:" + result.IsString);
                }
                if (result.IsSeries)
                {
                    Series s = result.Series;
                    s.Clear();
                    if (RecursiveCalculations && s is CalculationSeries && s.Expression.Trim() != "" && s.Expression.IndexOf("this") < 0)
                    {
                        (s as CalculationSeries).Calculate(t1, t2);      // might depend on calculation
                    }
                    else
                    {     // read data
                        s = ReadAndShiftData(-timeOffset, s, t1, t2);

                        if (Debug)
                        {
                            s.WriteToConsole();
                        }

                        Logger.WriteLine("Read " + s.Count + " points.  Missing Count = " + s.CountMissing());
                        Logger.WriteLine(s.ToString());

                        result = new ParserResult(s);
                    }
                }
                GetToken();
                return;

            case Types.FUNCTION:
                ParserFunction function;
                string         subExpression = "";
                ParserUtility.TryGetFunctionCall(token, out subExpression, out function);
                stack.Add("function call: " + subExpression);
                SeriesExpressionParser parser = new SeriesExpressionParser(m_db);
                parser.VariableResolver = this.VariableResolver;
                List <ParserResult> args = new List <ParserResult>();
                bool anySeriesArgs       = false;
                for (int i = 0; i < function.Parameters.Length; i++)
                {
                    var a = parser.Evaluate(function.Parameters[i], this.t1, this.t2, defaultTimeInterval);
                    if (a.IsSeries)
                    {
                        anySeriesArgs = true;
                    }
                    args.Add(a);
                }
                if (Debug)
                {
                    Logger.WriteLine("Evaluating function '" + function.Name + "'");
                }
                result = function.Evaluate(args);
                if (result.IsSeries && !anySeriesArgs)
                {
                    result.Series.Read(this.t1, this.t2);
                }

                if (result.IsSeries && Debug)
                {
                    Logger.WriteLine("Result is a series with:");
                    Logger.WriteLine("Count = " + result.Series.Count);
                    Logger.WriteLine("Missing Count = " + result.Series.CountMissing());
                    Logger.WriteLine("AverageOfSeries = " + Math.AverageOfSeries(result.Series));
                }
                //result = new ParserResult(2);

                GetToken();
                return;

            default:
                result = new ParserResult(0.0);
                SyntaxErr(Errors.SYNTAX);
                break;
            }
        }
Ejemplo n.º 2
0
 public void Add(string name, ParserResult value)
 {
     vars.Add(name, value);
 }
Ejemplo n.º 3
0
 public ParserEventArgs(ParserResult e, string variableName)
 {
     this.ParserResult = e;
     this.VariableName = variableName;
 }