1
/*************************************************************************************
2
* Copyright (C) 2010 by Aleix Pol <aleixpol@kde.org> *
4
* This program is free software; you can redistribute it and/or *
5
* modify it under the terms of the GNU General Public License *
6
* as published by the Free Software Foundation; either version 2 *
7
* of the License, or (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the Free Software *
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
17
*************************************************************************************/
19
#ifndef EXPRESSIONTYPE_H
20
#define EXPRESSIONTYPE_H
22
#include "analitzaexport.h"
30
class ANALITZA_EXPORT ExpressionType
33
///Just use undefined type when returning from a recursion
34
enum Type { Error=0, Value, Vector, List, Lambda, Any, Many, Object, Char, Bool };
35
QString toString() const;
37
ExpressionType(Type t=Error, int any=-1);
38
ExpressionType(Type t, const ExpressionType& contained, int s=0);
39
ExpressionType(Type t, const QList<ExpressionType>& alternatives);
41
/** Constructs a type that identifies a custom Object */
42
ExpressionType(const QString& objectName);
43
ExpressionType(const ExpressionType& t);
45
~ExpressionType() {/* delete contained; */}
47
bool operator==(const ExpressionType& t) const;
48
bool operator!=(const ExpressionType& t) const { return !operator==(t); }
49
ExpressionType operator=(const ExpressionType& et);
51
/** Depth search to check if it's defined */
54
Type type() const { return m_type; }
55
ExpressionType contained() const { Q_ASSERT(m_type==Vector || m_type==List); return m_contained.first(); }
56
QList<ExpressionType> alternatives() const { Q_ASSERT(m_type==Many); return m_contained; }
58
/** 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 */
59
void addAlternative(const ExpressionType& t);
60
int size() const { return m_size; }
61
int anyValue() const { return m_any; }
63
ExpressionType& addParameter(const ExpressionType& t);
64
QList<ExpressionType> parameters() const { Q_ASSERT(m_type==Lambda); return m_contained; }
65
ExpressionType returnValue() const;
67
void addAssumption(const QString& bvar, const ExpressionType& t);
68
QMap<QString, ExpressionType> assumptions() const;
69
QMap<QString, ExpressionType>& assumptions();
70
ExpressionType assumptionFor(const QString& bvar) const { return m_assumptions.value(bvar); }
71
void addAssumptions(const QMap< QString, ExpressionType >& a);
72
void clearAssumptions();
74
/** Returns a new type with the stars solved according t @p info */
75
ExpressionType starsToType(const QMap<int, ExpressionType>& info) const;
77
bool canReduceTo(const ExpressionType& type) const;
78
int increaseStars(int stars);
80
ExpressionType& simplifyStars();
82
/** when it's a many type, reduce to the one(s) that can be reduced to */
83
void reduce(const ExpressionType& type);
85
static ExpressionType minimumType(const ExpressionType& t1, const ExpressionType& t2);
86
static bool assumptionsMerge(QMap<QString, ExpressionType>& data, const QMap<QString, ExpressionType>& newmap);
87
static void assumptionsUnion(QMap< QString, Analitza::ExpressionType >& data, const QMap< QString, Analitza::ExpressionType >& newmap);
88
static QMap<int, ExpressionType> computeStars(const QMap<int, ExpressionType>& initial, const ExpressionType& candidate, const ExpressionType& type);
89
static bool matchAssumptions(QMap<int, ExpressionType>* stars, const QMap<QString, ExpressionType>& assum1, const QMap<QString, ExpressionType>& assum2);
90
static QList<ExpressionType> lambdaFromArgs(const QList<ExpressionType>& args);
91
static QList<ExpressionType> manyFromArgs(const QList<ExpressionType>& args);
93
static void starsSimplification(ExpressionType& t, QMap<int, int>& reductions, int& next);
96
///In case of list and vector the inside type
97
QList<ExpressionType> m_contained;
98
QMap<QString, ExpressionType> m_assumptions;
99
union { int m_size; int m_any; };
100
QString m_objectName;
104
#endif // EXPRESSIONTYPE_H