File indexing completed on 2024-04-28 03:40:43
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 EXPRESSIONTYPECHECKER_H 0020 #define EXPRESSIONTYPECHECKER_H 0021 0022 #include "abstractexpressionvisitor.h" 0023 #include "analitzaexport.h" 0024 #include "expressiontype.h" 0025 #include <QStack> 0026 #include <QStringList> 0027 #include <QSet> 0028 0029 namespace Analitza 0030 { 0031 class Variables; 0032 class Expression; 0033 0034 class ANALITZA_EXPORT ExpressionTypeChecker : public AbstractExpressionVisitor 0035 { 0036 public: 0037 explicit ExpressionTypeChecker(Variables* v); 0038 0039 ExpressionType check(const Expression& exp); 0040 0041 virtual QVariant visit(const None* var) override; 0042 virtual QVariant visit(const Operator* var) override; 0043 virtual QVariant visit(const Ci* var) override; 0044 virtual QVariant visit(const Cn* var) override; 0045 virtual QVariant visit(const Container* var) override; 0046 virtual QVariant visit(const Vector* var) override; 0047 virtual QVariant visit(const List* l) override; 0048 virtual QVariant visit(const Matrix* c) override; 0049 virtual QVariant visit(const Analitza::MatrixRow* m) override; 0050 virtual QVariant visit(const Apply* a) override; 0051 virtual QVariant visit(const CustomObject* c) override; 0052 0053 virtual QVariant result() const override { return QVariant(); } 0054 0055 QStringList dependencies() const { return m_deps; } 0056 bool hasDependencies() const { return !m_deps.isEmpty(); } 0057 bool isCorrect() const { return m_err.isEmpty() && !current.isError(); } 0058 QStringList errors() const; 0059 0060 void initializeVars(const QMap<QString, ExpressionType>& types); 0061 QMap<QString, ExpressionType> variablesTypes() const; 0062 0063 private: 0064 ExpressionType tellTypeIdentity(const QString& name, const ExpressionType& type); 0065 ExpressionType solve(const Operator* o, const QVector<Object*>& parameters); 0066 bool inferType(const ExpressionType& c, const ExpressionType& targetType, QMap<QString, ExpressionType>* assumptions); 0067 QList<ExpressionType> computePairs(const QList<ExpressionType>& options, const ExpressionType& param); 0068 0069 QMap<QString, ExpressionType> typeIs(const Object* o, const ExpressionType& type); 0070 template <class T> 0071 QMap<QString, ExpressionType> typeIs(T it, const T& itEnd, const ExpressionType& type); 0072 0073 template <class T> 0074 QVariant visitListOrVector(const T* v, ExpressionType::Type t, int size); 0075 0076 ExpressionType typeForVar(const QString& var); 0077 0078 void addError(const QString& err); 0079 ExpressionType commonType(const QList<Object*>& values); 0080 bool isVariableDefined(const QString& id) const; 0081 0082 uint m_stars; 0083 QList<QStringList> m_err; 0084 QStringList m_calculating; 0085 ExpressionType current; 0086 Variables* m_v; 0087 QMap<QString, ExpressionType> m_typeForBVar; 0088 QMap<QString, ExpressionType> m_vars; 0089 QSet<QString> m_lambdascope; 0090 QStack<const Object*> m_calls; 0091 QStringList m_deps; 0092 }; 0093 0094 } 0095 0096 #endif // EXPRESSIONTYPECHECKER_H