コード例 #1
0
        private MathTryParseResult TryParseExpression(string expression, ICollection <Variable> variables)
        {
            foreach (var expressionParser in _expressionParsers
                     .OrderByDescending(f => f is SumParser)
                     .ThenByDescending(f => f is ProductParser)
                     .ThenByDescending(f => f.Name.Length)
                     .ToList())
            {
                var parseResult = expressionParser.TryParse(expression, variables);
                if (parseResult.IsSuccessfulCreated)
                {
                    return(parseResult);
                }
            }

            var matchedConstant = _constants
                                  .Where(c => c.Name.ToLower() == expression)
                                  .FirstOrDefault();

            if (matchedConstant != null)
            {
                return new MathTryParseResult
                       {
                           IsSuccessfulCreated = true,
                           Expression          = _numberFactory.Create(matchedConstant.Value)
                       }
            }
            ;

            if (variables != null && variables.Any(p => p.Name.ToLower() == expression))
            {
                return new MathTryParseResult
                       {
                           IsSuccessfulCreated = true,
                           Expression          = ParseVariable(expression, variables)
                       }
            }
            ;

            return(new MathTryParseResult
            {
                IsSuccessfulCreated = false,
                ErrorMessage = "Unknown Expression in expression: " + expression
            });
        }