Ejemplo n.º 1
0
        private void GroupByPriority(int priority)
        {
            if (components.First == null)
            {
                return;
            }
            var node = components.First.Next; // skipping to second component, which should be operator component

            if (node == null)
            {
                return;
            }
            while (true)
            {
                IMathComponent component = node.Value;
                if (component is OperatorComponent opComponent)
                {
                    var prevComponent = node.Previous;
                    var nextComponent = node.Next;
                    if (nextComponent == null)
                    {
                        throw new InvalidExpressionSyntaxException("Invalid expression");
                    }
                    var nextOperator = nextComponent.Next;
                    if ((int)opComponent.Operator.ExecutionPriority == priority)
                    {
                        OperationComponent operation = new OperationComponent(new OperatorInstance(prevComponent.Value, nextComponent.Value, opComponent.Operator));
                        components.Remove(prevComponent);
                        components.Remove(nextComponent);
                        components.AddBefore(node, operation);
                        components.Remove(node);
                    }
                    if (nextOperator == null)
                    {
                        break;
                    }
                    node = nextOperator;
                }
                else
                {
                    throw new InvalidExpressionSyntaxException("Operator expected");
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Converts Function and it's parameters to math component and inserts it into internal data structure.
        /// </summary>
        /// <param name="function">Function type</param>
        /// <param name="funcExpression">Function parameter expression. Can be empty string if function has no parameters. Parameters must be separated by <see cref="Expressions.Function.FUNCTION_PARAMETER_SEPARATOR"/></param>
        /// <param name="negative">Whether function result should be negative</param>
        /// <param name="instanceCreator">Parser instance creator for parsing sub-expressions</param>
        /// <returns>This builder</returns>
        public ExpressionBuilder Function(Function function, string funcExpression, bool negative, ConstructNewParser instanceCreator)
        {
            string[] parameters;
            if (funcExpression.Length == 0)
            {
                parameters = new string[0];
            }
            else
            {
                parameters = funcExpression.Split(Expressions.Function.FUNCTION_PARAMETER_SEPARATOR);
            }
            IMathComponent component = new OperationComponent(new FunctionInstance(function, parameters.Length == 0 ? new IMathComponent[0] : ConvertToComponents(parameters, instanceCreator)))
            {
                Negative = negative
            };

            components.AddLast(component);
            return(this);
        }