diff --git a/tprcc/.gitignore b/tprcc/.gitignore index 228cff3..f33240e 100644 --- a/tprcc/.gitignore +++ b/tprcc/.gitignore @@ -1,2 +1,3 @@ *.o build/* +.cache/* diff --git a/tprcc/CMakeLists.txt b/tprcc/CMakeLists.txt index 167ecfc..99757cf 100644 --- a/tprcc/CMakeLists.txt +++ b/tprcc/CMakeLists.txt @@ -2,13 +2,16 @@ cmake_minimum_required(VERSION 3.8) project(tprcc LANGUAGES CXX) set (CMAKE_CXX_STANDARD 17) +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 (SOURCES ${SRC_DIR} + ${SRC_TPR_DIR} ${SRC_GENERATED} ) diff --git a/tprcc/include/lang/temp-lang-frontend.hpp b/tprcc/include/lang/temp-lang-frontend.hpp new file mode 100644 index 0000000..3aa3fba --- /dev/null +++ b/tprcc/include/lang/temp-lang-frontend.hpp @@ -0,0 +1,52 @@ +#ifndef _TEMP_LANG_FRONTEND_HPP_ +#define _TEMP_LANG_FRONTEND_HPP_ + +#include +#include +#include +#include +#include + +class TempLangFrontend { +public: + enum class LogLevel { + DEBUG, + INFO, + WARNING, + ERROR, + }; + + + TempLangFrontend(const std::string &source_file) + { + m_source_file = source_file; + } + + int analyze(); + + const std::string &get_src_file() + { + return m_source_file; + } + +protected: + std::string m_source_file; + + void log(LogLevel lvl, const std::string &message) const + { + switch (lvl) { + case LogLevel::ERROR: + std::cerr << "[ERR]" << message << std::endl; + break; + case LogLevel::WARNING: + std::cerr << "[WARN]" << message << std::endl; + break; + default: + std::cout << message << std::endl; + break; + } + } + +}; + +#endif /* _TEMP_LANG_FRONTEND_HPP_ */ \ No newline at end of file diff --git a/tprcc/include/tpr/tpr-frontend.hpp b/tprcc/include/tpr/tpr-frontend.hpp new file mode 100644 index 0000000..8093678 --- /dev/null +++ b/tprcc/include/tpr/tpr-frontend.hpp @@ -0,0 +1,21 @@ +#ifndef _TPR_FRONTEND_HPP_ +#define _TPR_FRONTEND_HPP_ + +#include +#include +#include + +namespace tpr { + +class TprFrontend : public TempLangFrontend { +private: +public: + TprFrontend(const std::string &source_file); + + int analyze(); + +}; + +} + +#endif /* _TPR_FRONTEND_HPP_ */ \ No newline at end of file diff --git a/tprcc/parser/tpr.l b/tprcc/parser/tpr.l index d3ccb6b..19afde4 100644 --- a/tprcc/parser/tpr.l +++ b/tprcc/parser/tpr.l @@ -22,6 +22,9 @@ SPACE "\t"|" " NUM [-+]?([0-9]*\.[0-9]+|[0-9]+) %% +%{ + yylval = lval; +%} <*>{SPACE} { /*Ignore spaces */} {COMMENT_LINE} {loc->lines(); return token::lineend;} @@ -40,4 +43,9 @@ digio_conf { return token::kw_digio_conf; } digio_set { return token::kw_digio_set; } digio_wait { return token::kw_digio_wait; } +. { + std::cerr << "[ERR] Failed to parse: " << yytext << " @ " << *loc << std::endl; + return token::unexpected_input; +} + %% diff --git a/tprcc/parser/tpr.ypp b/tprcc/parser/tpr.ypp index 1cec5ee..3d4cd1a 100644 --- a/tprcc/parser/tpr.ypp +++ b/tprcc/parser/tpr.ypp @@ -43,6 +43,7 @@ %token kw_digio_conf %token kw_digio_set %token kw_digio_wait +%token unexpected_input %% @@ -71,7 +72,8 @@ tpr_command_inner: cmd_pid_conf; cmd_pid_conf: kw_pid_conf number number number number number number; -cmd_temp_set: kw_temp_set number; +cmd_temp_set: kw_temp_set number {std::cout << "Set Temperature" << $2 << std::endl;} + ; cmd_wait_temp: kw_wait_temp number; @@ -95,6 +97,6 @@ cmd_digio_wait: kw_digio_wait number number; void tpr::TempProfileParser::error(const location_type &l, const std::string &err_message) { - std::cerr << "Error " << err_message << " at " << l << std::endl; + std::cerr << "[ERR] Parser Error: '" << err_message << "' at " << l << std::endl; std::abort(); } \ No newline at end of file diff --git a/tprcc/src/main.cpp b/tprcc/src/main.cpp index 9bcdbe2..7817899 100644 --- a/tprcc/src/main.cpp +++ b/tprcc/src/main.cpp @@ -1,8 +1,15 @@ #include +#include int main(int argc, char **argv) { std::cout << "Hello world" << std::endl; + if (argc > 1) { + auto fe = tpr::TprFrontend(std::string(argv[1])); + + fe.analyze(); + } + return 0; } \ No newline at end of file diff --git a/tprcc/src/tpr/tpr-frontend.cpp b/tprcc/src/tpr/tpr-frontend.cpp new file mode 100644 index 0000000..7a3c7bd --- /dev/null +++ b/tprcc/src/tpr/tpr-frontend.cpp @@ -0,0 +1,26 @@ +#include + +namespace tpr { + +TprFrontend::TprFrontend(const std::string &source_file) : TempLangFrontend(source_file) +{ +} + +int TprFrontend::analyze() +{ + std::ifstream input_stream(m_source_file); + + if (!input_stream.good()) { + log(TempLangFrontend::LogLevel::ERROR, "Cannot read input file " + m_source_file); + return -1; + } + + auto scanner = TempProfileScanner(&input_stream); + auto parser = TempProfileParser(scanner); + + parser.parse(); + + return 0; +} + +} \ No newline at end of file