Small progress. Have to continue here..

This commit is contained in:
Mario Hüttel 2024-01-20 20:17:36 +01:00
parent 223de7f190
commit fc2744d7fa
4 changed files with 120 additions and 3 deletions

View File

@ -22,7 +22,7 @@ public:
m_source_file = source_file;
}
int analyze();
virtual int analyze() = 0;
const std::string &get_src_file()
{

View File

@ -11,8 +11,7 @@ class TprFrontend : public TempLangFrontend {
private:
public:
TprFrontend(const std::string &source_file);
int analyze();
int analyze() override;
};

View File

@ -0,0 +1,47 @@
#ifndef _TPR_TYPES_HPP_
#define _TPR_TYPES_HPP_
#include <vector>
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<bool> param_is_whole_num;
};
class TprCommand {
private:
static const CommandSpec m_specs[11];
CommandType m_type;
std::vector<float> m_parameters;
bool is_whole_number(float num) const;
public:
TprCommand(CommandType type);
TprCommand(CommandType type, const std::vector<float> &params);
TprCommand(CommandType type, const std::vector<float> &&params);
bool check_command(bool print_status) const;
}
}
#endif /* _TPR_TYPES_HPP_ */

View File

@ -0,0 +1,71 @@
#include <cstdlib>
#include <tpr/tpr-types.hpp>
#include <cmath>
#include <iostream>
namespace tpr {
const CommandSpec TprCommand::m_specs[11] = {
{.type = CommandType::beep, .param_is_whole_num = std::vector<bool>{true}},
{.type = CommandType::clear_flags, .param_is_whole_num = std::vector<bool>{}},
{.type = CommandType::digio_conf, .param_is_whole_num = std::vector<bool>{true, true}},
{.type = CommandType::digio_set, .param_is_whole_num = std::vector<bool>{true, true}},
{.type = CommandType::digio_wait, .param_is_whole_num = std::vector<bool>{true, true}},
{.type = CommandType::wait_temp, .param_is_whole_num = std::vector<bool>{false}},
{.type = CommandType::wait_time, .param_is_whole_num = std::vector<bool>{false}},
{.type = CommandType::temp_off, .param_is_whole_num = std::vector<bool>{}},
{.type = CommandType::temp_ramp, .param_is_whole_num = std::vector<bool>{true, true}},
{.type = CommandType::pid_conf, .param_is_whole_num = std::vector<bool>{false, false, false, false, false, false}},
{.type = CommandType::temp_set, .param_is_whole_num = std::vector<bool>{false}}
};
TprCommand::TprCommand(CommandType type)
{
m_type = type;
}
TprCommand::TprCommand(CommandType type, const std::vector<float> &params) : TprCommand(type)
{
m_parameters = params;
}
TprCommand::TprCommand(CommandType type, const std::vector<float> &&params) : 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;
}
}