diff --git a/src/musredit_qt5/mupp/CMakeLists.txt b/src/musredit_qt5/mupp/CMakeLists.txt index da3fd196..7f9619fd 100644 --- a/src/musredit_qt5/mupp/CMakeLists.txt +++ b/src/musredit_qt5/mupp/CMakeLists.txt @@ -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 + $ + $ + $ +) + #--- use the Widgets and XML modules from Qt5 --------------------------------- target_link_libraries(mupp Qt5::Widgets Qt5::Xml) diff --git a/src/musredit_qt5/mupp/PmuppScript.h b/src/musredit_qt5/mupp/PmuppScript.h index dbb4a996..e732251d 100644 --- a/src/musredit_qt5/mupp/PmuppScript.h +++ b/src/musredit_qt5/mupp/PmuppScript.h @@ -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 fVarHandler; + bool foundLabel(PmuppCollection *coll, const QString label); void minMax(QVector dvec, double &min, double &max); QString getNicerLabel(const QString label); diff --git a/src/musredit_qt5/mupp/var/CMakeLists.txt b/src/musredit_qt5/mupp/var/CMakeLists.txt new file mode 100644 index 00000000..febd4f0a --- /dev/null +++ b/src/musredit_qt5/mupp/var/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(src) diff --git a/src/musredit_qt5/mupp/var/include/PAst.hpp b/src/musredit_qt5/mupp/var/include/PAst.hpp new file mode 100644 index 00000000..c2afa127 --- /dev/null +++ b/src/musredit_qt5/mupp/var/include/PAst.hpp @@ -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 +#include +#include +#include +#include +#include + +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 + // (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 + , boost::recursive_wrapper + , boost::recursive_wrapper + , boost::recursive_wrapper + > + operand; + + struct unary + { + optoken operator_; + operand operand_; + }; + + struct operation + { + optoken operator_; + operand operand_; + }; + + struct expression + { + operand first; + std::list 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 rhs; + }; + + typedef boost::variant< + variable_declaration + , assignment> + statement; + + typedef std::list 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, 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, rhs) +) + +BOOST_FUSION_ADAPT_STRUCT( + mupp::ast::assignment, + (mupp::ast::variable, lhs) + (mupp::ast::expression, rhs) +) + +#endif // _PAST_HPP_ diff --git a/src/musredit_qt5/mupp/var/include/PVarHandler.h b/src/musredit_qt5/mupp/var/include/PVarHandler.h new file mode 100644 index 00000000..98f607fd --- /dev/null +++ b/src/musredit_qt5/mupp/var/include/PVarHandler.h @@ -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 +#include + +#include + +#include + +class PVarHandler { + public: + PVarHandler(); + + void setInput(QString &str) { fInput = str.toLatin1().data(); } + bool parse(); + bool semcheck(); + std::vector getValues(); + std::vector getErrors(); + + private: + std::string fInput; ///< the variable input to be parsed + mupp::ast::statement_list fAst; ///< the AST +}; + +#endif //_PVARHANDLER_H_ diff --git a/src/musredit_qt5/mupp/var/src/CMakeLists.txt b/src/musredit_qt5/mupp/var/src/CMakeLists.txt new file mode 100644 index 00000000..ed93922e --- /dev/null +++ b/src/musredit_qt5/mupp/var/src/CMakeLists.txt @@ -0,0 +1,4 @@ +target_sources(mupp + PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/PVarHandler.cpp +) diff --git a/src/musredit_qt5/mupp/var/src/PVarHandler.cpp b/src/musredit_qt5/mupp/var/src/PVarHandler.cpp new file mode 100644 index 00000000..b04b7569 --- /dev/null +++ b/src/musredit_qt5/mupp/var/src/PVarHandler.cpp @@ -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 PVarHandler::getValues() +{ + std::vector result; + + return result; +} + +//-------------------------------------------------------------------------- +/** + * @brief PVarHandler::getErrors + * @return + */ +std::vector PVarHandler::getErrors() +{ + std::vector result; + + return result; +}