Restructure bison generation to allow multiple forntends

This commit is contained in:
Mario Hüttel 2024-01-22 21:20:10 +01:00
parent b83f057e49
commit 0e5ef46512
7 changed files with 68 additions and 72 deletions

View File

@ -7,7 +7,9 @@ add_compile_options(-Wall -Wextra)
aux_source_directory("src" SRC_DIR)
aux_source_directory("src/tpr" SRC_TPR_DIR)
set (SRC_GENERATED "${CMAKE_CURRENT_BINARY_DIR}/tpr-parser.cpp" "${CMAKE_CURRENT_BINARY_DIR}/tpr-scanner.cpp")
set(TPR_PARSER_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated-tpr")
set (SRC_GENERATED "${TPR_PARSER_DIR}/tpr-parser.cpp" "${TPR_PARSER_DIR}/tpr-scanner.cpp")
set (SOURCES
${SRC_DIR}
@ -15,30 +17,30 @@ set (SOURCES
${SRC_GENERATED}
)
add_custom_command(
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.l
OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/tpr-scanner.cpp
${TPR_PARSER_DIR}/tpr-scanner.cpp
COMMAND
flex -+ -o "${CMAKE_CURRENT_BINARY_DIR}/tpr-scanner.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.l"
mkdir -p "${TPR_PARSER_DIR}" && flex -+ -o "${TPR_PARSER_DIR}/tpr-scanner.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.l"
)
add_custom_command(
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.ypp
OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/tpr-parser.cpp
${TPR_PARSER_DIR}/tpr-parser.cpp
COMMAND
mkdir -p "${CMAKE_CURRENT_BINARY_DIR}/generated" && bison -o"${CMAKE_CURRENT_BINARY_DIR}/tpr-parser.cpp" --header="${CMAKE_CURRENT_BINARY_DIR}/generated/tpr-parser.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.ypp"
mkdir -p "${TPR_PARSER_DIR}/include/tpr-parser" && ${CMAKE_CURRENT_SOURCE_DIR}/bison-wrapper.sh "${TPR_PARSER_DIR}/tpr-parser.cpp" "${TPR_PARSER_DIR}/include/tpr-parser/tpr-parser.hpp" "${TPR_PARSER_DIR}/include/tpr-parser/location.hh" --header=tpr-parser.hpp ${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.ypp
)
SET_SOURCE_FILES_PROPERTIES(${SRC_GENERATED} PROPERTIES GENERATED 1)
add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated")
target_include_directories(${PROJECT_NAME} PRIVATE "${TPR_PARSER_DIR}/include" "${TPR_PARSER_DIR}/include/tpr-parser")
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")
# TEMPORARY FIx:
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
#target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")

31
tprcc/bison-wrapper.sh Executable file
View File

@ -0,0 +1,31 @@
#!/bin/bash
set -e
# Usage: bison-wrapper.sh <c-file> <include-file> <location.hh> <bison_file> <bison-parameters>
if [[ $# -lt 4 ]]; then
echo "Error. Not enough parameters"
exit -1
fi
cfile=$1
shift
include=$1
shift
location=$1
shift
tmpdir=`mktemp -d`
cd $tmpdir
echo "Using $tmpdir"
echo cp *.tab.cpp "$cfile"
echo cp *.hpp "$include"
echo cp location.hh "$location"
bison $@
cp *.tab.cpp "$cfile"
cp *.hpp "$include"
cp location.hh "$location"
rm -rfv "$tmpdir"

View File

@ -6,8 +6,8 @@
#include <FlexLexer.h>
#endif
#include "tpr-parser.hpp"
#include "location.hh"
#include <tpr-parser/tpr-parser.hpp>
#include <tpr-parser/location.hh>
namespace tpr {

View File

@ -19,26 +19,17 @@ enum class CommandType {
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;
};

View File

@ -19,7 +19,9 @@ using token = tpr::TempProfileParser::token;
NEWLINE "\n"|"\r\n"
COMMENT_LINE "#".*\n
SPACE "\t"|" "
NUM [-+]?([0-9]*\.[0-9]+|[0-9]+)
NUM_INT [-+]?([0-9]+)
NUM_FLOAT [-+]?([0-9]*\.[0-9]+)
%%
%{
@ -29,7 +31,8 @@ NUM [-+]?([0-9]*\.[0-9]+|[0-9]+)
<*>{SPACE} { /*Ignore spaces */}
{COMMENT_LINE} {loc->lines(); return token::lineend;}
{NEWLINE} {loc->lines(); return token::lineend;}
{NUM} {yylval->build<float>(std::stof(std::string(yytext))); return token::number;}
{NUM_FLOAT} {yylval->build<float>(std::stof(std::string(yytext))); return token::number_float;}
{NUM_INT} {yylval->build<float>(std::stof(std::string(yytext))); return token::number_int;}
pid_conf { return token::kw_pid_conf; }
temp_set { return token::kw_temp_set; }

View File

@ -5,6 +5,7 @@
%define api.namespace {tpr}
%define api.parser.class {TempProfileParser}
%define parse.error verbose
%code requires{
@ -22,6 +23,7 @@
#include <utility>
#include <tuple>
#include <tpr/tpr-scanner.hpp>
#include <tpr/tpr-types.hpp>
#undef yylex
#define yylex scanner.yylex
}
@ -30,7 +32,8 @@
%locations
%start tpr_file
%token<float> number
%token<float> number_float
%token<float> number_int
%token lineend
%token kw_pid_conf
%token kw_temp_set
@ -45,6 +48,8 @@
%token kw_digio_wait
%token unexpected_input
%type<float>number number_truncated
%%
tpr_file: tpr_command
@ -81,17 +86,25 @@ cmd_wait_time: kw_wait_time number;
cmd_temp_ramp: kw_temp_ramp number number;
cmd_beep: kw_beep number;
cmd_beep: kw_beep number_truncated;
cmd_temp_off: kw_temp_off;
cmd_clear_flags: kw_clear_flags;
cmd_digio_conf: kw_digio_conf number number;
cmd_digio_conf: kw_digio_conf number_truncated number_truncated;
cmd_digio_set: kw_digio_set number number;
cmd_digio_set: kw_digio_set number_truncated number_truncated;
cmd_digio_wait: kw_digio_wait number number;
cmd_digio_wait: kw_digio_wait number_truncated number_truncated;
number_truncated: number_float {$$ = $1; std::cerr << "[WARN] Floating point number " << $1 << " will be truncated to an integer (" << (int)$1 << ") at location (" << @1 << ")" << std::endl;}
| number_int {$$ = $1;}
;
number: number_float {$$ = $1;}
| number_int {$$ = $1;}
;
%%

View File

@ -1,3 +1,4 @@
#include "tprcc/logger.hpp"
#include <cstdlib>
#include <tpr/tpr-types.hpp>
#include <cmath>
@ -5,19 +6,6 @@
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)
{
@ -34,37 +22,5 @@ TprCommand::TprCommand(CommandType type, const std::vector<float> &&params) : Tp
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) {
// TODO:::: Continue here!!
}
return false;
}
}
return true;
}
}
return false;
}
}