File indexing completed on 2024-04-28 03:40:42

0001 /*************************************************************************************
0002  *  Copyright (C) 2010 by Aleix Pol <aleixpol@kde.org>                               *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #ifndef EXPRESSIONTYPE_H
0020 #define EXPRESSIONTYPE_H
0021 
0022 #include "analitzaexport.h"
0023 #include <QString>
0024 #include <QList>
0025 #include <QMap>
0026 
0027 namespace Analitza
0028 {
0029 
0030 class ANALITZA_EXPORT ExpressionType
0031 {
0032     public:
0033         ///Just use undefined type when returning from a recursion.
0034         ///Vector and MatrixRow are the same type.
0035         enum Type { Error=0, Value, Vector, List, Lambda, Any, Many, Object, Char, Bool, Matrix };
0036         QString toString() const;
0037         
0038         ExpressionType(Type t=Error, int any=-1);
0039         ExpressionType(Type t, const ExpressionType& contained, int s=0);
0040         ExpressionType(Type t, const QList<ExpressionType>& alternatives);
0041         
0042         /** Constructs a type that identifies a custom Object */
0043         ExpressionType(const QString& objectName);
0044         ExpressionType(const ExpressionType& t);
0045         
0046         ~ExpressionType() {/* delete contained; */}
0047         
0048         bool operator==(const ExpressionType& t) const;
0049         bool operator!=(const ExpressionType& t) const { return !operator==(t); }
0050         ExpressionType operator=(const ExpressionType& et);
0051         
0052         /** Depth search to check if it's defined */
0053         bool isError() const;
0054         
0055         Type type() const { return m_type; }
0056         bool hasContained() const;
0057         ExpressionType contained() const;
0058         QList<ExpressionType> alternatives() const { Q_ASSERT(m_type==Many); return m_contained; }
0059         
0060         /** In case it's a Many type, it adds @p t as an alternative. If @p t is a Many type too, they will be merged */
0061         void addAlternative(const ExpressionType& t);
0062         int size() const { return m_size; }
0063         int anyValue() const { return m_any; }
0064         
0065         ExpressionType& addParameter(const ExpressionType& t);
0066         QList<ExpressionType> parameters() const { Q_ASSERT(m_type==Lambda); return m_contained; }
0067         QList<ExpressionType>& parameters() { Q_ASSERT(m_type==Lambda); return m_contained; }
0068         ExpressionType returnValue() const;
0069         
0070         bool addAssumption(const QString& bvar, const ExpressionType& t);
0071         QMap<QString, ExpressionType> assumptions() const;
0072         QMap<QString, ExpressionType>& assumptions();
0073         ExpressionType assumptionFor(const QString& bvar) const { return m_assumptions.value(bvar); }
0074         void addAssumptions(const QMap< QString, ExpressionType >& a);
0075         void clearAssumptions();
0076         
0077         /** Returns a new type with the stars solved according t @p info */
0078         ExpressionType starsToType(const QMap<int, ExpressionType>& info) const;
0079         
0080         /** @returns true if the current type can be converted into @p type */
0081         bool canReduceTo(const ExpressionType& type) const;
0082         
0083         /**
0084          * @returns false in case that just by looking at the current type we see that it won't be able to be reduced to @p type.
0085          * Useful to improve error reporting
0086          */
0087         bool canCompareTo(const ExpressionType& type) const;
0088         
0089         int increaseStars(int stars);
0090         
0091         ExpressionType& simplifyStars();
0092         
0093         /** when it's a many type, reduce to the one(s) that can be reduced to */
0094         void reduce(const ExpressionType& type);
0095         
0096         /** @returns the name of a custom type */
0097         QString objectName() const;
0098         
0099         static ExpressionType minimumType(const ExpressionType& t1, const ExpressionType& t2);
0100         static bool assumptionsMerge(QMap<QString, ExpressionType>& data, const QMap<QString, ExpressionType>& newmap);
0101         static void assumptionsUnion(QMap<QString, ExpressionType>& data, const QMap<QString, ExpressionType>& newmap);
0102         static QMap<int, ExpressionType> computeStars(const QMap<int, ExpressionType>& initial, const ExpressionType& candidate, const ExpressionType& type);
0103         static bool matchAssumptions(QMap<int, ExpressionType>* stars, const QMap<QString, ExpressionType>& assum1, const QMap<QString, ExpressionType>& assum2);
0104         static QStringList wrongAssumptions(const QMap<QString, ExpressionType>& assum1, const QMap<QString, ExpressionType>& assum2);
0105         void removeAssumptions(const QStringList& bvarStrings);
0106         static QList<ExpressionType> lambdaFromArgs(const QList<ExpressionType>& args);
0107         static QList<ExpressionType> manyFromArgs(const QList<ExpressionType>& args);
0108     private:
0109         static void starsSimplification(ExpressionType& t, QMap<int, int>& reductions, int& next);
0110         static QMap<int, ExpressionType> processContained(const QMap<int, ExpressionType>& initial, const ExpressionType& candidate, const ExpressionType& type);
0111         
0112         Type m_type;
0113         ///In case of list and vector the inside type
0114         QList<ExpressionType> m_contained;
0115         QMap<QString, ExpressionType> m_assumptions;
0116         union { int m_size; int m_any; };
0117         QString m_objectName;
0118 };
0119 
0120 }
0121 
0122 Q_DECLARE_TYPEINFO ( Analitza::ExpressionType, Q_MOVABLE_TYPE);
0123 
0124 #endif // EXPRESSIONTYPE_H