add skeleton for variable handling.

This commit is contained in:
suter_a 2020-04-29 14:13:23 +02:00
parent 10271dab9a
commit 29064c44be
7 changed files with 357 additions and 3 deletions

View File

@ -42,7 +42,7 @@ set(GENERATED_HEADER_FILES
)
set_property(SOURCE mupp_version.h PROPERTY SKIP_AUTOMOC ON) # needed for cmake 3.x
set(SOURCE_FILES
set(MUPP_SOURCE_FILES
mupp.cpp
PmuppAdmin.cpp
Pmupp.cpp
@ -54,13 +54,24 @@ set(SOURCE_FILES
if (APPLE)
set(RESOURCE_FILES icons/mupp.icns)
add_executable(mupp
MACOSX_BUNDLE ${GENERATED_HEADER_FILES} ${SOURCE_FILES}
MACOSX_BUNDLE ${GENERATED_HEADER_FILES} ${MUPP_SOURCE_FILES}
qrc_mupp.cpp ${RESOURCE_FILES}
)
else (APPLE)
add_executable(mupp ${GENERATED_HEADER_FILES} ${SOURCE_FILES} qrc_mupp.cpp)
add_executable(mupp ${GENERATED_HEADER_FILES} ${MUPP_SOURCE_FILES} qrc_mupp.cpp)
endif (APPLE)
#--- add the variable related sources -----------------------------------------
add_subdirectory(var)
#--- add the necessary header includes ----------------------------------------
target_include_directories(mupp
BEFORE PRIVATE
$<BUILD_INTERFACE:${Boost_INCLUDE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/var/include>
)
#--- use the Widgets and XML modules from Qt5 ---------------------------------
target_link_libraries(mupp Qt5::Widgets Qt5::Xml)

View File

@ -35,6 +35,7 @@
#include "PmuppAdmin.h"
#include "Pmupp.h"
#include "PVarHandler.h"
typedef struct {
int collIdx;
@ -84,6 +85,8 @@ class PmuppScript : public QObject
QString fLoadPath;
QString fSavePath;
QVector<PVarHandler> fVarHandler;
bool foundLabel(PmuppCollection *coll, const QString label);
void minMax(QVector<double> dvec, double &min, double &max);
QString getNicerLabel(const QString label);

View File

@ -0,0 +1 @@
add_subdirectory(src)

View File

@ -0,0 +1,197 @@
/***************************************************************************
PAst.hpp
Author: Andreas Suter
e-mail: andreas.suter@psi.ch
Based on Joel de Guzman example on calc7,
see https://github.com/boostorg/spirit
***************************************************************************/
/***************************************************************************
* Copyright (C) 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. *
***************************************************************************/
#ifndef _PAST_HPP_
#define _PAST_HPP_
#include <boost/config/warning_disable.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/optional.hpp>
#include <list>
namespace mupp { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// The AST
///////////////////////////////////////////////////////////////////////////
struct tagged
{
int id; // Used to annotate the AST with the iterator position.
// This id is used as a key to a map<int, Iterator>
// (not really part of the AST.)
};
enum optoken
{
op_plus,
op_minus,
op_times,
op_divide,
op_positive,
op_negative,
};
enum funid
{
fun_max,
fun_min,
fun_abs,
fun_sin,
fun_cos,
fun_tan,
fun_exp,
fun_log,
fun_ln
};
struct nil {};
struct unary;
struct expression;
struct function;
struct power;
struct variable : tagged
{
variable(std::string const& name = "") : name(name) {}
std::string name;
};
typedef boost::variant<
nil
, double
, variable
, boost::recursive_wrapper<function>
, boost::recursive_wrapper<power>
, boost::recursive_wrapper<unary>
, boost::recursive_wrapper<expression>
>
operand;
struct unary
{
optoken operator_;
operand operand_;
};
struct operation
{
optoken operator_;
operand operand_;
};
struct expression
{
operand first;
std::list<operation> rest;
};
struct function
{
funid func_id;
expression arg;
};
struct power
{
expression base;
expression pow;
};
struct assignment
{
variable lhs;
expression rhs;
};
struct variable_declaration
{
variable lhs;
boost::optional<expression> rhs;
};
typedef boost::variant<
variable_declaration
, assignment>
statement;
typedef std::list<statement> statement_list;
// print functions for debugging
inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; return out; }
inline std::ostream& operator<<(std::ostream& out, variable const& var) { out << var.name; return out; }
}}
BOOST_FUSION_ADAPT_STRUCT(
mupp::ast::unary,
(mupp::ast::optoken, operator_)
(mupp::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
mupp::ast::operation,
(mupp::ast::optoken, operator_)
(mupp::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
mupp::ast::expression,
(mupp::ast::operand, first)
(std::list<mupp::ast::operation>, rest)
)
BOOST_FUSION_ADAPT_STRUCT(
mupp::ast::function,
(mupp::ast::funid, func_id)
(mupp::ast::expression, arg)
)
BOOST_FUSION_ADAPT_STRUCT(
mupp::ast::power,
(mupp::ast::expression, base)
(mupp::ast::expression, pow)
)
BOOST_FUSION_ADAPT_STRUCT(
mupp::ast::variable_declaration,
(mupp::ast::variable, lhs)
(boost::optional<mupp::ast::expression>, rhs)
)
BOOST_FUSION_ADAPT_STRUCT(
mupp::ast::assignment,
(mupp::ast::variable, lhs)
(mupp::ast::expression, rhs)
)
#endif // _PAST_HPP_

View File

@ -0,0 +1,55 @@
/***************************************************************************
PVarHandler.h
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. *
***************************************************************************/
#ifndef _PVARHANDLER_H_
#define _PVARHANDLER_H_
#include <string>
#include <vector>
#include <QString>
#include <PAst.hpp>
class PVarHandler {
public:
PVarHandler();
void setInput(QString &str) { fInput = str.toLatin1().data(); }
bool parse();
bool semcheck();
std::vector<double> getValues();
std::vector<double> getErrors();
private:
std::string fInput; ///< the variable input to be parsed
mupp::ast::statement_list fAst; ///< the AST
};
#endif //_PVARHANDLER_H_

View File

@ -0,0 +1,4 @@
target_sources(mupp
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/PVarHandler.cpp
)

View File

@ -0,0 +1,83 @@
/***************************************************************************
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;
}