more work on var handling. Inject collection variables.

This commit is contained in:
suter_a 2020-04-29 17:10:24 +02:00
parent 29064c44be
commit 74cfa3ecbf
4 changed files with 186 additions and 90 deletions

View File

@ -36,6 +36,8 @@
#include <QDateTime>
#include <QProcess>
#include <QtGlobal> // Q_ASSERT
#include "PmuppScript.h"
//--------------------------------------------------------------------------
@ -85,7 +87,7 @@ int PmuppScript::executeScript()
}
QString cmd;
int status=0;
int status;
for (int i=0; i<fScript.size(); i++) {
cmd = fScript.at(i);
if (cmd.startsWith("loadPath")) {
@ -109,7 +111,7 @@ int PmuppScript::executeScript()
} else if (cmd.startsWith("macro")) {
status = macro(cmd);
} 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")) {
std::cout << "debug> will eventually handle linking of a variable to a collection ..." << std::endl;
} else {
@ -663,6 +665,47 @@ int PmuppScript::macro(const QString str, const QString plotFln)
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
@ -749,3 +792,34 @@ QString PmuppScript::getNicerLabel(const QString label)
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 plot(const QString str);
int macro(const QString str, const QString plotFln="");
int var_cmd(const QString str);
public slots:
int executeScript();
@ -90,6 +91,8 @@ class PmuppScript : public QObject
bool foundLabel(PmuppCollection *coll, const QString label);
void minMax(QVector<double> dvec, double &min, double &max);
QString getNicerLabel(const QString label);
int getCollectionIndex(const QString var_name);
};
#endif // _PMUPPSCRIPT_H_

View File

@ -35,21 +35,25 @@
#include <QString>
#include "Pmupp.h"
#include <PAst.hpp>
class PVarHandler {
public:
PVarHandler();
PVarHandler(PmuppCollection *coll, std::string parse_str);
void setInput(QString &str) { fInput = str.toLatin1().data(); }
bool parse();
bool semcheck();
bool isValid() { return fIsValid; }
std::vector<double> getValues();
std::vector<double> getErrors();
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
void injectPredefVariables();
};
#endif //_PVARHANDLER_H_

View File

@ -1,83 +1,98 @@
/***************************************************************************
PVarHandler.cpp
Author: Andreas Suter
e-mail: andreas.suter@psi.ch
***************************************************************************/
/***************************************************************************
* Copyright (C) 2007-2020 by Andreas Suter *
* andreas.suter@psi.ch *
* *
* 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 *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "PVarHandler.h"
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::PVarHandler
*/
PVarHandler::PVarHandler()
{
fInput = "";
}
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::parse
* @return
*/
bool PVarHandler::parse()
{
return true;
}
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::semcheck
* @return
*/
bool PVarHandler::semcheck()
{
return true;
}
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::getValues
* @return
*/
std::vector<double> PVarHandler::getValues()
{
std::vector<double> result;
return result;
}
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::getErrors
* @return
*/
std::vector<double> PVarHandler::getErrors()
{
std::vector<double> result;
return result;
}
/***************************************************************************
PVarHandler.cpp
Author: Andreas Suter
e-mail: andreas.suter@psi.ch
***************************************************************************/
/***************************************************************************
* Copyright (C) 2007-2020 by Andreas Suter *
* andreas.suter@psi.ch *
* *
* 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 *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include "PVarHandler.h"
/*
#include "PSkipper.hpp"
#include "PErrorHandler.hpp"
#include "PStatement.hpp"
#include "PAstDump.hpp"
#include "PProgram.hpp"
*/
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::PVarHandler
*/
PVarHandler::PVarHandler(PmuppCollection *coll, std::string parse_str) :
fColl(coll), fParseStr(parse_str), fIsValid(false)
{
injectPredefVariables();
}
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::getValues
* @return
*/
std::vector<double> PVarHandler::getValues()
{
std::vector<double> result;
return result;
}
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::getErrors
* @return
*/
std::vector<double> PVarHandler::getErrors()
{
std::vector<double> result;
return result;
}
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::injectPredefVariables
*/
void PVarHandler::injectPredefVariables()
{
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);
}
}