File indexing completed on 2024-05-12 03:41:58

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 "vectorcommands.h"
0020 
0021 #include <QCoreApplication>
0022 
0023 #include "analitzautils.h"
0024 #include "expression.h"
0025 #include "list.h"
0026 #include "vector.h"
0027 #include "value.h"
0028 
0029 using Analitza::Expression;
0030 using Analitza::ExpressionType;
0031 
0032 const QString VectorCommand::id = QStringLiteral("vector");
0033 const ExpressionType VectorCommand::type = ExpressionType(ExpressionType::Lambda)
0034 .addParameter(ExpressionType(ExpressionType::Any))
0035 .addParameter(ExpressionType(ExpressionType::Vector, ExpressionType(ExpressionType::Any), -1));
0036 
0037 Expression VectorCommand::operator()(const QList< Analitza::Expression >& args)
0038 {
0039     Expression ret;
0040     
0041     const int nargs = args.size();
0042     
0043     switch(nargs) {
0044         case 0: {
0045             ret.addError(QCoreApplication::tr("Invalid parameter count for '%1'").arg(VectorCommand::id));
0046         }    break;
0047         case 1: {
0048             const Analitza::Object *arg = args.first().tree();
0049             
0050             //TODO vector(5) := vector{0,0,0,0,0}
0051             if (arg->type() == Analitza::Object::list) {
0052                 const Analitza::List *list = static_cast<const Analitza::List*>(arg);
0053                 const int n = list->size();
0054                 
0055                 Analitza::Vector *vector = new Analitza::Vector(n);
0056                 
0057                 for (int i = 0; i < n; ++i)
0058                     vector->appendBranch(list->at(i)->copy());
0059                 
0060                 ret.setTree(vector);
0061             } else {
0062                 ret.addError(QCoreApplication::tr("Invalid parameter type for '%1', was expected a list").arg(VectorCommand::id));
0063             }
0064         }    break;
0065         case 2: {
0066             const Analitza::Object *arg = args.first().tree();
0067             
0068             if (arg->type() == Analitza::Object::value) {
0069                 const Analitza::Cn *lengthobj = static_cast<const Analitza::Cn*>(args.first().tree());
0070                 
0071                 if (lengthobj->isInteger() && lengthobj->value() > 0) {
0072                     ret.setTree(new Analitza::Vector(lengthobj->intValue(), static_cast<const Analitza::Cn*>(args.last().tree())));
0073                 }
0074                 else {
0075                     ret.addError(QCoreApplication::tr("Vector size must be some integer value greater to zero"));
0076                 }
0077             } else {
0078                 ret.addError(QCoreApplication::tr("Vector size must be some integer value greater to zero"));
0079             }
0080         }    break;
0081         default:
0082             ret.addError(QCoreApplication::tr("Invalid parameter count for '%1'").arg(VectorCommand::id));
0083             break;
0084     }
0085     
0086     return ret;
0087 }