コード例 #1
0
        protected AExpression ExtractValue(ref string formula)
        {
            AExpression result;
            string      f = formula.TrimStart();

            // handle -sign
            bool wrapInNeg = f[0] == '-';

            if (wrapInNeg)
            {
                f = f.Substring(1).TrimStart();
            }
            if (f[0] == '(')
            {
                int closingPos = SearchClosingBracket(f);
                result = new Expression(f.Substring(1, closingPos - 1));
                f      = f.Substring(closingPos + 1);
            }
            else if ((f[0] >= '0' && f[0] <= '9') || f[0] == '.')
            {
                var m = numberReg.Match(f);
                if (!m.Success)
                {
                    throw new ArgumentException("Invalid number format");
                }
                float val = float.Parse(m.Value.Replace(",", "."), CultureInfo.InvariantCulture.NumberFormat);
                f      = f.Substring(m.Length);
                result = new FixedValue {
                    Value = val
                };
            }
            else
            {
                throw new NotSupportedException("Unknown value format");
            }

            if (wrapInNeg)
            {
                result = new NegWrapper()
                {
                    Expr = result
                };
            }
            formula = f.TrimStart();
            return(result);
        }