File indexing completed on 2024-05-12 15:19:50

0001 /*************************************************************************************
0002  *  Copyright (C) 2014 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com>      *
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 "listcommands.h"
0020 
0021 #include <QCoreApplication>
0022 
0023 #include "expression.h"
0024 #include "list.h"
0025 #include "value.h"
0026 
0027 using Analitza::Expression;
0028 using Analitza::ExpressionType;
0029 
0030 const QString RangeCommand::id = QStringLiteral("range");
0031 const ExpressionType RangeCommand::type =     ExpressionType(ExpressionType::Lambda)
0032 .addParameter(ExpressionType(ExpressionType::Any, ExpressionType(ExpressionType::Value)))
0033 .addParameter(ExpressionType(ExpressionType::List, ExpressionType(ExpressionType::Value)));
0034 
0035 Expression RangeCommand::operator()(const QList< Analitza::Expression >& args)
0036 {
0037     Expression ret;
0038     
0039     const int nargs = args.size();
0040     
0041     double a = 1;
0042     double b = 0;
0043     double h = 1;
0044     
0045     switch(nargs) {
0046         case 0: {
0047             ret.addError(QCoreApplication::tr("Invalid parameter count for '%1'").arg(RangeCommand::id));
0048             
0049             return ret;
0050         }    break;
0051         case 1: {
0052             b = static_cast<const Analitza::Cn*>(args.first().tree())->value();
0053         }    break;
0054         case 2: {
0055             a = static_cast<const Analitza::Cn*>(args.first().tree())->value();
0056             b = static_cast<const Analitza::Cn*>(args.last().tree())->value();
0057         }    break;
0058         case 3: {
0059             a = static_cast<const Analitza::Cn*>(args.at(0).tree())->value();
0060             b = static_cast<const Analitza::Cn*>(args.at(1).tree())->value();
0061             h = static_cast<const Analitza::Cn*>(args.at(2).tree())->value();
0062         }    break;
0063         default:
0064             ret.addError(QCoreApplication::tr("Invalid parameter count for '%1'").arg(RangeCommand::id));
0065             
0066             return ret;
0067             break;
0068     }
0069     
0070     Analitza::List *seq = new Analitza::List;
0071         
0072     for (double x = a; x < b || qFuzzyCompare(x, b); x += h) {
0073         seq->appendBranch(new Analitza::Cn(x));
0074     }
0075     
0076     ret.setTree(seq);
0077     
0078     return ret;
0079 }