Beispiel #1
0
        public MathParserTermResult Parse(string expression, int startPos)
        {
            var i        = startPos;
            var dotCount = 0;

            while (i < expression.Length && (Char.IsDigit(expression[i]) || expression[i] == '.'))
            {
                if (expression[i] == '.')
                {
                    dotCount++;

                    if (dotCount > 1)
                    {
                        throw new InvalidOperationException("Wrong number of dots in expression!");
                    }
                }

                i++;
            }

            var result = new MathParserTermResult
            {
                StartPos = startPos,
                EndPos   = i,
                Term     = expression.Substring(startPos, i - startPos),
                TermType = TermType.Number
            };

            return(result);
        }
Beispiel #2
0
        public MathParserTermResult Parse(string expression, int startPos)
        {
            if (!IsOperator(expression[startPos]))
            {
                throw new InvalidOperationException($"{expression[startPos]} is not valid operator!");
            }

            var result = new MathParserTermResult
            {
                StartPos = startPos,
                EndPos   = startPos + 1,
                Term     = expression[startPos].ToString(),
                TermType = TermType.Operator
            };

            return(result);
        }
Beispiel #3
0
        public MathParserTermResult Parse(string expression, int startPos)
        {
            var i              = startPos;
            var termType       = TermType.Const;
            int numberOfParams = 0;

            MathParserTermResult internalTermResult = null;

            while (i < expression.Length && Char.IsLetter(expression[i]))
            {
                i++;
            }

            if (i < expression.Length && expression[i] == '(')
            {
                var subParser = new SubExpressionTermParser(true);
                internalTermResult = subParser.Parse(expression, i);
                numberOfParams     = subParser.NumberOfParameters;

                termType = TermType.Function;
            }

            var term = expression.Substring(startPos, i - startPos);

            var result = new ComplexMathParserResult
            {
                StartPos       = startPos,
                EndPos         = internalTermResult != null ? internalTermResult.EndPos : i,
                Term           = term,
                TermType       = termType,
                InternalTerm   = internalTermResult,
                NumberOfParams = numberOfParams
            };

            return(result);
        }