/// <summary>
        /// Prepara o leitor de expressões para lidar com o maior número de objectos possível.
        /// </summary>
        /// <returns>O leitor de expressões.</returns>
        private ExpressionReader <AMathematicsObject, string, string> PrepareExpressionReader()
        {
            var result = new ExpressionReader <AMathematicsObject, string, string>(new MathematicsObjectParser(this.mediator));

            result.RegisterBinaryOperator("equal", this.Assign, 0);
            result.RegisterBinaryOperator("plus", this.Add, 2);
            result.RegisterBinaryOperator("times", this.Multiply, 3);
            result.RegisterBinaryOperator("minus", this.Subtract, 2);
            result.RegisterBinaryOperator("over", this.Divide, 3);
            result.RegisterUnaryOperator("minus", this.Symmetric, 2);
            result.RegisterBinaryOperator("hat", this.Exponentiate, 4);

            result.RegisterBinaryOperator("double_equal", this.AreEqual, 1);
            result.RegisterBinaryOperator("great_than", this.GreaterThan, 1);
            result.RegisterBinaryOperator("great_equal", this.GreaterOrEqualThan, 1);
            result.RegisterBinaryOperator("less_than", this.LesserThan, 1);
            result.RegisterBinaryOperator("less_equal", this.LesserOrEqualThan, 1);

            result.RegisterBinaryOperator("double_or", this.LogicalDisjunction, 2);
            result.RegisterBinaryOperator("double_and", this.LogicalConjunction, 3);

            result.RegisterExpressionDelimiterTypes("left_parenthesis", "right_parenthesis");
            result.RegisterSequenceDelimiterTypes("left_parenthesis", "right_parenthesis");
            result.RegisterSequenceDelimiterTypes("left_bracket", "right_bracket");
            result.RegisterExternalDelimiterTypes("left_bracket", "right_bracket");
            result.RegisterExternalDelimiterTypes("left_brace", "right_brace");
            result.RegisterExternalDelimiterTypes("double_quote", "double_quote");

            result.AddVoid("blancks");

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Tenta fazer a leitura da expressão.
        /// </summary>
        /// <param name="reader">O leitor de símbolos.</param>
        /// <param name="errors">A lista que irá receber os erros.</param>
        /// <param name="element">O elemento lido.</param>
        /// <returns>Verdadeiro caso a leitura seja bem-sucedida e falso caso contrário.</returns>
        public bool TryParsePolynomial(
            MementoSymbolReader <InputReader, string, string> reader,
            ILogStatus <string, EParseErrorLevel> errors,
            out ObjectType element)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            else
            {
                var expressionReader = new ExpressionReader <ObjectType, string, string>(
                    this.objectParser);
                expressionReader.RegisterBinaryOperator("plus", Add, 0);
                expressionReader.RegisterBinaryOperator("times", Multiply, 1);
                expressionReader.RegisterBinaryOperator("minus", Subtract, 0);
                expressionReader.RegisterBinaryOperator("hat", Power, 2);
                expressionReader.RegisterUnaryOperator("minus", Symmetric, 0);
                expressionReader.RegisterBinaryOperator("over", Divide, 1);

                if (this.expressionDelimiterTypes.Any())
                {
                    foreach (var expressionDelimiterKvp in this.expressionDelimiterTypes)
                    {
                        foreach (var closeDelimiter in expressionDelimiterKvp.Value)
                        {
                            expressionReader.RegisterExpressionDelimiterTypes(
                                expressionDelimiterKvp.Key,
                                closeDelimiter);
                        }
                    }
                }
                else
                {
                    expressionReader.RegisterExpressionDelimiterTypes("left_parenthesis", "right_parenthesis");
                }

                expressionReader.RegisterSequenceDelimiterTypes("left_parenthesis", "right_parenthesis");
                foreach (var kvp in this.externalDelimitersTypes)
                {
                    foreach (var delimiter in kvp.Value)
                    {
                        expressionReader.RegisterExternalDelimiterTypes(kvp.Key, delimiter);
                    }
                }

                expressionReader.AddVoid("blancks");
                expressionReader.AddVoid("space");
                expressionReader.AddVoid("carriage_return");
                expressionReader.AddVoid("new_line");

                var innerErrors = new LogStatus <string, EParseErrorLevel>();
                element = expressionReader.Parse(reader, innerErrors);
                foreach (var kvp in innerErrors.GetLogs())
                {
                    errors.AddLog(kvp.Value, kvp.Key);
                }

                if (innerErrors.HasLogs(EParseErrorLevel.ERROR))
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Efectua a leitura de um polinómio.
        /// </summary>
        /// <param name="polynomialReader">A cadeia de carácteres que contém o polinómimo.</param>
        /// <param name="conversion">O conversor entre o tipo de coeficiente e um inteiro.</param>
        /// <param name="errors">A lista de errros encontrados.</param>
        /// <param name="resultPolynomial">O polinómio lido.</param>
        /// <returns>Verdadeiro caso a leitura seja bem sucedida e falso caso contrário.</returns>
        /// <exception cref="ArgumentNullException">Se o leitor de símbolos ou  conversor forem nulos.</exception>
        public bool TryParsePolynomial(
            MementoSymbolReader <InputReader, string, string> polynomialReader,
            IConversion <int, T> conversion,
            ILogStatus <string, EParseErrorLevel> errors,
            out Polynomial <T> resultPolynomial)
        {
            if (polynomialReader == null)
            {
                throw new ArgumentNullException("polynomialReader");
            }

            if (conversion == null)
            {
                throw new ArgumentNullException("conversion");
            }

            this.conversion  = conversion;
            resultPolynomial = default(Polynomial <T>);
            var expressionReader = new ExpressionReader <ParsePolynomialItem <T>, string, string>(
                new SimplePolynomialReader <T>(this.coeffParser, this.ring));

            expressionReader.RegisterBinaryOperator("plus", Add, 0);
            expressionReader.RegisterBinaryOperator("times", Multiply, 1);
            expressionReader.RegisterBinaryOperator("minus", Subtract, 0);
            expressionReader.RegisterBinaryOperator("hat", Power, 2);
            expressionReader.RegisterUnaryOperator("minus", Symmetric, 0);

            expressionReader.RegisterExpressionDelimiterTypes("left_parenthesis", "right_parenthesis");
            expressionReader.RegisterSequenceDelimiterTypes("left_parenthesis", "right_parenthesis");
            foreach (var kvp in this.externalDelimitersTypes)
            {
                foreach (var delimiter in kvp.Value)
                {
                    expressionReader.RegisterExternalDelimiterTypes(kvp.Key, delimiter);
                }
            }

            expressionReader.AddVoid("space");
            expressionReader.AddVoid("carriage_return");
            expressionReader.AddVoid("new_line");

            var innerErrors      = new LogStatus <string, EParseErrorLevel>();
            var expressionResult = expressionReader.Parse(polynomialReader, innerErrors);

            if (innerErrors.HasLogs(EParseErrorLevel.ERROR))
            {
                if (errors != null)
                {
                    foreach (var message in innerErrors.GetLogs(EParseErrorLevel.ERROR))
                    {
                        errors.AddLog(message, EParseErrorLevel.ERROR);
                    }
                }

                return(false);
            }
            else
            {
                if (expressionResult.ValueType == EParsePolynomialValueType.COEFFICIENT)
                {
                    resultPolynomial = new Polynomial <T>(expressionResult.Coeff, this.ring);
                    return(true);
                }
                else if (expressionResult.ValueType == EParsePolynomialValueType.INTEGER)
                {
                    if (typeof(T).IsAssignableFrom(typeof(int)))
                    {
                        resultPolynomial = new Polynomial <T>((T)(object)expressionResult.Degree, this.ring);
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else if (expressionResult.ValueType == EParsePolynomialValueType.POLYNOMIAL)
                {
                    resultPolynomial = expressionResult.Polynomial;
                    return(true);
                }
                else
                {
                    if (errors != null)
                    {
                        errors.AddLog("Severe error.", EParseErrorLevel.ERROR);
                    }

                    return(false);
                }
            }
        }