From fc2744d7fa071156350ce6315142a261f81fe42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sat, 20 Jan 2024 20:17:36 +0100 Subject: [PATCH] Small progress. Have to continue here.. --- tprcc/include/lang/temp-lang-frontend.hpp | 2 +- tprcc/include/tpr/tpr-frontend.hpp | 3 +- tprcc/include/tpr/tpr-types.hpp | 47 +++++++++++++++ tprcc/src/tpr/tpr-types.cpp | 71 +++++++++++++++++++++++ 4 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 tprcc/include/tpr/tpr-types.hpp create mode 100644 tprcc/src/tpr/tpr-types.cpp diff --git a/tprcc/include/lang/temp-lang-frontend.hpp b/tprcc/include/lang/temp-lang-frontend.hpp index 3aa3fba..ad990fc 100644 --- a/tprcc/include/lang/temp-lang-frontend.hpp +++ b/tprcc/include/lang/temp-lang-frontend.hpp @@ -22,7 +22,7 @@ public: m_source_file = source_file; } - int analyze(); + virtual int analyze() = 0; const std::string &get_src_file() { diff --git a/tprcc/include/tpr/tpr-frontend.hpp b/tprcc/include/tpr/tpr-frontend.hpp index 8093678..2326316 100644 --- a/tprcc/include/tpr/tpr-frontend.hpp +++ b/tprcc/include/tpr/tpr-frontend.hpp @@ -11,8 +11,7 @@ class TprFrontend : public TempLangFrontend { private: public: TprFrontend(const std::string &source_file); - - int analyze(); + int analyze() override; }; diff --git a/tprcc/include/tpr/tpr-types.hpp b/tprcc/include/tpr/tpr-types.hpp new file mode 100644 index 0000000..320cbce --- /dev/null +++ b/tprcc/include/tpr/tpr-types.hpp @@ -0,0 +1,47 @@ +#ifndef _TPR_TYPES_HPP_ +#define _TPR_TYPES_HPP_ + +#include + +namespace tpr { + +enum class CommandType { + pid_conf, + temp_set, + wait_temp, + wait_time, + temp_ramp, + beep, + temp_off, + clear_flags, + digio_conf, + digio_set, + digio_wait, +}; + +struct CommandSpec { + CommandType type; + std::vector param_is_whole_num; +}; + +class TprCommand { +private: + static const CommandSpec m_specs[11]; + + CommandType m_type; + std::vector m_parameters; + bool is_whole_number(float num) const; + +public: + TprCommand(CommandType type); + TprCommand(CommandType type, const std::vector ¶ms); + TprCommand(CommandType type, const std::vector &¶ms); + + + bool check_command(bool print_status) const; +} + + +} + +#endif /* _TPR_TYPES_HPP_ */ \ No newline at end of file diff --git a/tprcc/src/tpr/tpr-types.cpp b/tprcc/src/tpr/tpr-types.cpp new file mode 100644 index 0000000..0de17e7 --- /dev/null +++ b/tprcc/src/tpr/tpr-types.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +namespace tpr { + +const CommandSpec TprCommand::m_specs[11] = { + {.type = CommandType::beep, .param_is_whole_num = std::vector{true}}, + {.type = CommandType::clear_flags, .param_is_whole_num = std::vector{}}, + {.type = CommandType::digio_conf, .param_is_whole_num = std::vector{true, true}}, + {.type = CommandType::digio_set, .param_is_whole_num = std::vector{true, true}}, + {.type = CommandType::digio_wait, .param_is_whole_num = std::vector{true, true}}, + {.type = CommandType::wait_temp, .param_is_whole_num = std::vector{false}}, + {.type = CommandType::wait_time, .param_is_whole_num = std::vector{false}}, + {.type = CommandType::temp_off, .param_is_whole_num = std::vector{}}, + {.type = CommandType::temp_ramp, .param_is_whole_num = std::vector{true, true}}, + {.type = CommandType::pid_conf, .param_is_whole_num = std::vector{false, false, false, false, false, false}}, + {.type = CommandType::temp_set, .param_is_whole_num = std::vector{false}} +}; + +TprCommand::TprCommand(CommandType type) +{ + m_type = type; +} + +TprCommand::TprCommand(CommandType type, const std::vector ¶ms) : TprCommand(type) +{ + m_parameters = params; +} + +TprCommand::TprCommand(CommandType type, const std::vector &¶ms) : TprCommand(type) +{ + m_parameters = std::move(params); +} + +bool TprCommand::is_whole_number(float num) const +{ + if (floor(num) == num) + return true; + else + return false; +} + + +bool TprCommand::check_command(bool print_status) const +{ + for (unsigned int i = 0; i < 11; i++) { + if (m_specs[i].type == m_type) { + /* Check size of param vector. This is just to be safe and should never fail */ + if (m_parameters.size() != m_specs[i].param_is_whole_num.size()) { + std::cerr << "[ERR] Parameter count mismatch in command. This is a compiler bug!" << std::endl; + std::abort(); + } + + for (unsigned int param_idx = 0; i < m_parameters.size(); i++) { + if (is_whole_number(m_parameters[param_idx]) ^ m_specs[i].param_is_whole_num[param_idx]) { + if (print_status) { + std::cerr << "[WARN]" // TODO:::: Continue here!! + } + return false; + } + } + return true; + } + } + + return false; +} + +} \ No newline at end of file