File indexing completed on 2024-04-28 03:40:46
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 #include "substituteexpression.h" 0020 #include <QString> 0021 #include "object.h" 0022 #include "variable.h" 0023 #include "apply.h" 0024 #include "pushvalue.h" 0025 0026 using namespace Analitza; 0027 0028 Object* SubstituteExpression::run(const Object* pattern, const QMap<QString, const Object*>& values) 0029 { 0030 m_values=values; 0031 0032 return walk(pattern); 0033 } 0034 0035 QString SubstituteExpression::solveRename(const QString& name) const 0036 { 0037 return m_renames.contains(name) ? m_renames[name] : name; 0038 } 0039 0040 Object* SubstituteExpression::walkApply(const Apply* pattern) 0041 { 0042 Apply* ret = new Apply; 0043 Apply::const_iterator it=pattern->firstValue(), itEnd=pattern->constEnd(); 0044 ret->ulimit()=walk(pattern->ulimit()); 0045 ret->dlimit()=walk(pattern->dlimit()); 0046 ret->domain()=walk(pattern->domain()); 0047 0048 PushValue<QStringList> v(m_bvars, m_bvars); 0049 QVector<Ci*> bvars = pattern->bvarCi(); 0050 foreach(Ci* bvar, bvars) { 0051 Ci* nbvar = bvar->copy(); 0052 const QString name = bvar->name(); 0053 0054 const Object* val = m_values.value(bvar->name()); 0055 if(val && !m_renames.contains(name)) { 0056 Q_ASSERT(val->type()==Object::variable); 0057 QString newname = static_cast<const Ci*>(val)->name(); 0058 m_renames.insert(name, newname); 0059 } 0060 0061 nbvar->setName(solveRename(name)); 0062 ret->addBVar(nbvar); 0063 m_bvars.append(nbvar->name()); 0064 } 0065 0066 if(pattern->firstOperator().isCorrect()) { 0067 Operator op = pattern->firstOperator(); 0068 ret->appendBranch(walk(&op)); 0069 } 0070 0071 for(; it!=itEnd; ++it) 0072 ret->appendBranch(walk(*it)); 0073 0074 return ret; 0075 } 0076 0077 Object* SubstituteExpression::walkVariable(const Ci* pattern) 0078 { 0079 QString name = solveRename(pattern->name()); 0080 QMap<QString, const Object*>::const_iterator it = m_values.constFind(name); 0081 0082 if(it!=m_values.constEnd() /*&& !m_bvars.contains(name)*/) 0083 return it.value()->copy(); 0084 else 0085 return pattern->copy(); //Error? 0086 }