more work on var handling. Inject collection variables.

This commit is contained in:
suter_a 2020-04-29 17:10:24 +02:00
parent 61952744be
commit 2f48a30d19
4 changed files with 186 additions and 90 deletions

View File

@ -36,6 +36,8 @@
#include <QDateTime> #include <QDateTime>
#include <QProcess> #include <QProcess>
#include <QtGlobal> // Q_ASSERT
#include "PmuppScript.h" #include "PmuppScript.h"
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
@ -85,7 +87,7 @@ int PmuppScript::executeScript()
} }
QString cmd; QString cmd;
int status=0; int status;
for (int i=0; i<fScript.size(); i++) { for (int i=0; i<fScript.size(); i++) {
cmd = fScript.at(i); cmd = fScript.at(i);
if (cmd.startsWith("loadPath")) { if (cmd.startsWith("loadPath")) {
@ -109,7 +111,7 @@ int PmuppScript::executeScript()
} else if (cmd.startsWith("macro")) { } else if (cmd.startsWith("macro")) {
status = macro(cmd); status = macro(cmd);
} else if (cmd.startsWith("var")) { } else if (cmd.startsWith("var")) {
std::cout << "debug> will eventually handle variable definition here ..." << std::endl; status = var_cmd(cmd);
} else if (cmd.startsWith("col")) { } else if (cmd.startsWith("col")) {
std::cout << "debug> will eventually handle linking of a variable to a collection ..." << std::endl; std::cout << "debug> will eventually handle linking of a variable to a collection ..." << std::endl;
} else { } else {
@ -663,6 +665,47 @@ int PmuppScript::macro(const QString str, const QString plotFln)
return 0; return 0;
} }
//--------------------------------------------------------------------------
/**
* @brief PmuppScript::var_cmd
* @param str
* @return
*/
int PmuppScript::var_cmd(const QString str)
{
QStringList tok;
int idx=0;
// get linked collection index for further use
tok = str.split(' ', QString::SkipEmptyParts);
if (tok[1].endsWith("Err")) // error variable no need to do something
return 0;
idx = getCollectionIndex(tok[1]);
if (idx == -1) // var not linked to collection, ignore it
return 0;
// check for the related error variable if present
QString varErr = QString("%1%2").arg(tok[1]).arg("Err");
QString varErrCmd("");
for (int i=0; i<fScript.size(); i++) {
if (fScript.at(i).contains(varErr, Qt::CaseSensitive)) {
varErrCmd = fScript.at(i);
break;
}
}
std::string parse_str = str.toLatin1().data();
if (!varErrCmd.isEmpty()) {
parse_str += "\n";
parse_str += varErrCmd.toLatin1().data();
}
PVarHandler varHandler(fParamDataHandler->GetCollection(idx), parse_str);
fVarHandler.push_back(varHandler);
return 0;
}
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/** /**
* @brief PmuppScript::foundLabel * @brief PmuppScript::foundLabel
@ -749,3 +792,34 @@ QString PmuppScript::getNicerLabel(const QString label)
return nice; return nice;
} }
//--------------------------------------------------------------------------
/**
* @brief PmuppScript::getCollectionIndex
* @param var_name
* @return
*/
int PmuppScript::getCollectionIndex(const QString var_name)
{
int idx = -1;
QString cmd;
QStringList tok;
bool ok;
for (int i=0; i<fScript.size(); i++) {
cmd = fScript.at(i);
if (cmd.startsWith("col")) {
tok.clear();
tok = cmd.split(' ', QString::SkipEmptyParts);
if (tok[3] == var_name) {
idx = tok[1].toInt(&ok);
if (!ok) {
Q_ASSERT(0);
}
break;
}
}
}
return idx;
}

View File

@ -64,6 +64,7 @@ class PmuppScript : public QObject
int addY(const QString str); int addY(const QString str);
int plot(const QString str); int plot(const QString str);
int macro(const QString str, const QString plotFln=""); int macro(const QString str, const QString plotFln="");
int var_cmd(const QString str);
public slots: public slots:
int executeScript(); int executeScript();
@ -90,6 +91,8 @@ class PmuppScript : public QObject
bool foundLabel(PmuppCollection *coll, const QString label); bool foundLabel(PmuppCollection *coll, const QString label);
void minMax(QVector<double> dvec, double &min, double &max); void minMax(QVector<double> dvec, double &min, double &max);
QString getNicerLabel(const QString label); QString getNicerLabel(const QString label);
int getCollectionIndex(const QString var_name);
}; };
#endif // _PMUPPSCRIPT_H_ #endif // _PMUPPSCRIPT_H_

View File

@ -35,21 +35,25 @@
#include <QString> #include <QString>
#include "Pmupp.h"
#include <PAst.hpp> #include <PAst.hpp>
class PVarHandler { class PVarHandler {
public: public:
PVarHandler(); PVarHandler(PmuppCollection *coll, std::string parse_str);
void setInput(QString &str) { fInput = str.toLatin1().data(); } bool isValid() { return fIsValid; }
bool parse();
bool semcheck();
std::vector<double> getValues(); std::vector<double> getValues();
std::vector<double> getErrors(); std::vector<double> getErrors();
private: private:
std::string fInput; ///< the variable input to be parsed PmuppCollection *fColl; ///< collection need for parsing and evaluation
std::string fParseStr; ///< the variable input to be parsed
bool fIsValid;
mupp::ast::statement_list fAst; ///< the AST mupp::ast::statement_list fAst; ///< the AST
void injectPredefVariables();
}; };
#endif //_PVARHANDLER_H_ #endif //_PVARHANDLER_H_

View File

@ -1,83 +1,98 @@
/*************************************************************************** /***************************************************************************
PVarHandler.cpp PVarHandler.cpp
Author: Andreas Suter Author: Andreas Suter
e-mail: andreas.suter@psi.ch e-mail: andreas.suter@psi.ch
***************************************************************************/ ***************************************************************************/
/*************************************************************************** /***************************************************************************
* Copyright (C) 2007-2020 by Andreas Suter * * Copyright (C) 2007-2020 by Andreas Suter *
* andreas.suter@psi.ch * * andreas.suter@psi.ch *
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * * it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or * * the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
* * * *
* This program is distributed in the hope that it will be useful, * * This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. * * GNU General Public License for more details. *
* * * *
* You should have received a copy of the GNU General Public License * * You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the * * along with this program; if not, write to the *
* Free Software Foundation, Inc., * * Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
#include "PVarHandler.h" #include <iostream>
//-------------------------------------------------------------------------- #include "PVarHandler.h"
/**
* @brief PVarHandler::PVarHandler /*
*/ #include "PSkipper.hpp"
PVarHandler::PVarHandler() #include "PErrorHandler.hpp"
{ #include "PStatement.hpp"
fInput = ""; #include "PAstDump.hpp"
} #include "PProgram.hpp"
*/
//--------------------------------------------------------------------------
/** //--------------------------------------------------------------------------
* @brief PVarHandler::parse /**
* @return * @brief PVarHandler::PVarHandler
*/ */
bool PVarHandler::parse() PVarHandler::PVarHandler(PmuppCollection *coll, std::string parse_str) :
{ fColl(coll), fParseStr(parse_str), fIsValid(false)
return true; {
} injectPredefVariables();
}
//--------------------------------------------------------------------------
/** //--------------------------------------------------------------------------
* @brief PVarHandler::semcheck /**
* @return * @brief PVarHandler::getValues
*/ * @return
bool PVarHandler::semcheck() */
{ std::vector<double> PVarHandler::getValues()
return true; {
} std::vector<double> result;
//-------------------------------------------------------------------------- return result;
/** }
* @brief PVarHandler::getValues
* @return //--------------------------------------------------------------------------
*/ /**
std::vector<double> PVarHandler::getValues() * @brief PVarHandler::getErrors
{ * @return
std::vector<double> result; */
std::vector<double> PVarHandler::getErrors()
return result; {
} std::vector<double> result;
//-------------------------------------------------------------------------- return result;
/** }
* @brief PVarHandler::getErrors
* @return //--------------------------------------------------------------------------
*/ /**
std::vector<double> PVarHandler::getErrors() * @brief PVarHandler::injectPredefVariables
{ */
std::vector<double> result; void PVarHandler::injectPredefVariables()
{
return result; mupp::ast::statement var_stat;
} mupp::ast::variable_declaration var;
std::string varName, errVarName;
for (int i=0; i<fColl->GetNoOfRuns(); i++) {
varName = fColl->GetRun(i).GetName().toLatin1().data();
errVarName = varName + "Err";
// inject err_name
var.lhs.name = errVarName;
var_stat = var;
fAst.push_front(var_stat);
// inject var_name
var.lhs.name = varName;
var_stat = var;
fAst.push_front(var_stat);
}
}