示例#1
0
        private IExpression GetExpression(SubExpression s)
        {
            var subs = s.GetSubExpressions();

            if (subs == null)
            {
                return(null);
            }

            if (subs.Length == 1)
            {
                if (!subs[0].ExpressionString.HasMathSymbols())
                {
                    if (subs[0].ExpressionString.All(char.IsLetter))
                    {
                        if (!Variables.ContainsKey(subs[0].ExpressionString))
                        {
                            Variables.Add(subs[0].ExpressionString, 0);
                        }
                        return(new VariableValueExpression(subs[0].ExpressionString, Variables));
                    }
                    return(new StaticValueExpression(subs[0].ExpressionString));
                }
                else
                {
                    var ct = GetCalcType(subs[0].ExpressionString[0]);
                    if (ct != null)
                    {
                        return(new StaticCalcExpression(ct.Value));
                    }
                    else
                    {
                        throw new InvalidExpressionException(subs[0].ExpressionString);
                    }
                }
            }

            var es = new List <IExpression>();

            for (var i = 0; i < subs.Length; i++)
            {
                var ex = GetExpression(subs[i]);
                if (ex != null)
                {
                    es.Add(ex);
                }
            }

            if (es.Count > 0)
            {
                if (es.Count < 3)
                {
                    throw new InvalidExpressionException(s.ExpressionString);
                }

                if (es.Any(e => e is StaticCalcExpression))
                {
                    do
                    {
                        var index = IndexOfCalc(es);
                        var calc  = (StaticCalcExpression)es[index];
                        var v1    = es[index - 1];
                        var v2    = es[index + 1];

                        var e = new CalcExpression(v1, v2, calc.Type);
                        es.RemoveRange(index - 1, 3);
                        es.Insert(index - 1, e);
                    } while (es.Count > 1);
                }

                return(es.First());
            }

            return(null);
        }