From f72d49709d1c97ff968da34477d69b36f80afd8a Mon Sep 17 00:00:00 2001 From: DolphyWind Date: Fri, 24 Nov 2023 18:52:50 +0300 Subject: [PATCH] Update tiny details. --- CMakeLists.txt | 2 +- include/Electra.hpp | 52 +++++++++++++++++++-------------------------- include/Global.hpp | 3 +++ src/Bomb.cpp | 2 +- src/Electra.cpp | 46 +++++++++++++-------------------------- src/Global.cpp | 13 ++++++++++++ src/main.cpp | 4 ++-- 7 files changed, 57 insertions(+), 65 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ab9d0cb..46aa3c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0.0) +cmake_minimum_required(VERSION 3.15) set(CMAKE_CXX_STANDARD 17) if(NOT DEFINED CMAKE_BUILD_TYPE) diff --git a/include/Electra.hpp b/include/Electra.hpp index 5778500..f68bddf 100644 --- a/include/Electra.hpp +++ b/include/Electra.hpp @@ -34,12 +34,27 @@ SOFTWARE. #include #include #include -#include #include -#include #include -#include #include +#include +#include +#include +#include +#include +#if __has_include() + #include + namespace fs = std::filesystem; +#elif __has_include() + #include + namespace fs = std::experimental::filesystem; +#else + #error "Missing the header." +#endif + +#include +#include +#include #include #include #include @@ -54,19 +69,6 @@ SOFTWARE. #include #include #include -#include -#include -#include -#include -#if __has_include() - #include - namespace fs = std::filesystem; -#elif __has_include() - #include - namespace fs = std::experimental::filesystem; -#else - #error "Missing the header." -#endif typedef std::vector GeneratorData; @@ -107,24 +109,17 @@ class Electra // Wstring converter static std::wstring_convert, wchar_t> wstring_converter; -protected: - Electra() {} public: + /// @brief Parses command line arguments, and initializes component and generators. /// /// @param argc Command line argument count /// @param argv Command line arguments - void initialize(int argc, char* argv[]); - - // Delete the copy constructor - Electra(const Electra&) = delete; + Electra(int argc, char** argv); - static Electra &instance(); + ~Electra() = default; - /// @brief Clears components and generators - ~Electra(); - - /// @brief Runs the electra + /// @brief Runs the electra code. void run(); /// @brief Recursively includes a file. @@ -154,9 +149,6 @@ class Electra /// - Create new currents void mainLoop(); - /// @brief Safely exits the program - void safe_exit(int exit_code); - // Methods for mainLoop() method. void moveCurrents(); void generateGenerators(); diff --git a/include/Global.hpp b/include/Global.hpp index 06ed766..fb3681f 100644 --- a/include/Global.hpp +++ b/include/Global.hpp @@ -32,6 +32,7 @@ typedef double var_t; typedef std::stack* StackPtr; typedef wchar_t char_t; +class Electra; namespace Global { // Pops the top value of stack. Returns defaultValue if there is no item. @@ -52,6 +53,8 @@ namespace Global // Replaces "lookFor" with "replaceWith" in "originalStr" std::wstring replaceString(std::wstring& originalStr, const std::wstring& lookFor, const std::wstring& replaceWith); + void safe_exit(int exit_code, int sig_code=-1); + // Empty struct struct Empty{}; } diff --git a/src/Bomb.cpp b/src/Bomb.cpp index ab4a69e..779bc69 100644 --- a/src/Bomb.cpp +++ b/src/Bomb.cpp @@ -29,6 +29,6 @@ bool Bomb::work(CurrentPtr current, std::vector *currentVector) if(!Component::work(current, currentVector)) return false; - Electra::instance().safe_exit(0); + Global::safe_exit(0); return Cable::work(current, currentVector); } \ No newline at end of file diff --git a/src/Electra.cpp b/src/Electra.cpp index 163c2b4..e9d6a1a 100644 --- a/src/Electra.cpp +++ b/src/Electra.cpp @@ -27,7 +27,7 @@ SOFTWARE. std::wstring_convert, wchar_t> Electra::wstring_converter; -void Electra::initialize(int argc, char* argv[]) +Electra::Electra(int argc, char** argv) { // Get the current folder m_currentPath = fs::current_path(); @@ -55,21 +55,21 @@ void Electra::initialize(int argc, char* argv[]) { parser.printHelpMessage(); defaultlogger.log(LogType::INFO, L"Printed help message. Exiting with code 0."); - safe_exit(0); + Global::safe_exit(0); } if(bool_map["version"]) { parser.printVersionMessage(); defaultlogger.log(LogType::INFO, L"Printed current version of electra. Exiting with code 0."); - safe_exit(0); + Global::safe_exit(0); } if(alone_args.size() == 0) { parser.printHelpMessage(); defaultlogger.log(LogType::INFO, L"No arguments specified. Printing help message. Exiting with code 1."); - safe_exit(1); + Global::safe_exit(1); } std::string stack_count_str = string_map["stack-count"]; @@ -94,13 +94,13 @@ void Electra::initialize(int argc, char* argv[]) { defaultlogger.log(LogType::ERROR, L"\"{}\" is invalid for stack-count.\n"); std::wcerr << L'\"' << std::to_wstring(stack_count_str) << L"\" is invalid for stack-count.\n"; - safe_exit(1); + Global::safe_exit(1); } catch (const std::out_of_range &e) { defaultlogger.log(LogType::ERROR, L"\"{}\" is out of range for stack-count.", stack_count_str); std::wcerr << L'\"' << std::to_wstring(stack_count_str) << L"\" is out of range for stack-count." << std::endl; - safe_exit(1); + Global::safe_exit(1); } // Parses --stack argument @@ -112,7 +112,7 @@ void Electra::initialize(int argc, char* argv[]) { std::wcerr << L"You entered inital values for " << splitted_by_comma.size() << L" stacks but stack count is " << m_stacks.size() << L"!" << std::endl; defaultlogger.log(LogType::ERROR, L"You entered inital values for {} stacks but stack count is {}!", splitted_by_comma.size(), m_stacks.size()); - safe_exit(1); + Global::safe_exit(1); } for(auto &splitted : splitted_by_comma) { @@ -128,13 +128,13 @@ void Electra::initialize(int argc, char* argv[]) { defaultlogger.log(LogType::ERROR, L"The value {} is too big or small for var_t.", i); std::wcerr << L"The value " << std::to_wstring(i) << L" is too big or small for var_t." << std::endl; - safe_exit(1); + Global::safe_exit(1); } catch(const std::invalid_argument &e) { defaultlogger.log(LogType::ERROR, L"Can\'t convert {} to var_t.", i); std::wcerr << L"Can\'t convert " << std::to_wstring(i) << L" to var_t." << std::endl; - safe_exit(1); + Global::safe_exit(1); } } index ++; @@ -290,17 +290,6 @@ void Electra::initialize(int argc, char* argv[]) #endif } -Electra &Electra::instance() -{ - static Electra m_instance; - return m_instance; -} - -Electra::~Electra() -{ - -} - void Electra::run() { m_sourceCode = includeFile(m_currentPath, Electra::wstring_converter.from_bytes(m_filename)); @@ -331,11 +320,6 @@ void Electra::mainLoop() defaultlogger.log(LogType::INFO, L"Program finished. Total ticks: {}", tickCount); } -void Electra::safe_exit(int exit_code) -{ - std::exit(exit_code); -} - std::vector Electra::includeFile(fs::path currentPath, const std::wstring& filename, std::size_t start, std::size_t end, bool allow_reinclusion) { // Start cannot be greater then the end @@ -343,7 +327,7 @@ std::vector Electra::includeFile(fs::path currentPath, const std:: { std::wcerr << L"Inclusion failed: Start index must be less than the end index." << std::endl; defaultlogger.log(LogType::ERROR, L"Inclusion failed: Start index must be less than the end index."); - safe_exit(1); + Global::safe_exit(1); } if(!allow_reinclusion) @@ -385,7 +369,7 @@ std::vector Electra::includeFile(fs::path currentPath, const std:: { defaultlogger.log(LogType::ERROR, L"Cannot parse \"{}\". Source code contains tab character. Exiting with code 1.", filename); std::wcerr << "Error while reading file: Source code contains tab!" << std::endl; - safe_exit(1); + Global::safe_exit(1); } // Split by the new line and slice file according to given parameters @@ -429,7 +413,7 @@ std::vector Electra::includeFile(fs::path currentPath, const std:: { std::wcerr << L"Cannot convert \"" << split_from_colon.at(0) << "\" to a number." << std::endl; defaultlogger.log(LogType::ERROR, L"Cannot convert \"{}\" to a number.", split_from_colon.at(0)); - safe_exit(1); + Global::safe_exit(1); } try @@ -441,7 +425,7 @@ std::vector Electra::includeFile(fs::path currentPath, const std:: { std::wcerr << L"Cannot convert \"" << split_from_colon.at(1) << "\" to a number." << std::endl; defaultlogger.log(LogType::ERROR, L"Cannot convert \"{}\" to a number.", split_from_colon.at(1)); - safe_exit(1); + Global::safe_exit(1); } } @@ -458,7 +442,7 @@ std::vector Electra::includeFile(fs::path currentPath, const std:: { std::wcerr << L"Cannot open \"" << filename << L"\"" << std::endl; defaultlogger.log(LogType::ERROR, L"Cannot open \"{}\". Exiting with code 1.", filename); - safe_exit(1); + Global::safe_exit(1); } return contents; @@ -660,5 +644,5 @@ void Electra::createCurrents() void Electra::sigHandler(int signal) { - Electra::instance().safe_exit(0); + Global::safe_exit(1, signal); } diff --git a/src/Global.cpp b/src/Global.cpp index f4bd18e..e980447 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -23,6 +23,8 @@ SOFTWARE. */ #include +#include +#include namespace Global { @@ -101,4 +103,15 @@ namespace Global return originalStr.replace(pos, lookFor.length(), replaceWith); } + + void safe_exit(int exit_code, int sig_code) + { + if(sig_code != -1) + { + defaultlogger.log(LogType::ERROR, L"Got signal {}. Exiting...", ::strsignal(sig_code)); + } + + std::exit(exit_code); + } + } // namespace Global diff --git a/src/main.cpp b/src/main.cpp index 79075c4..cc2ee0d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,7 +47,7 @@ int main(int argc, char *argv[]) #endif std::wcout << std::setprecision(15); - Electra::instance().initialize(argc, argv); - Electra::instance().run(); + Electra electraInterpreter(argc, argv); + electraInterpreter.run(); return 0; } \ No newline at end of file