Skip to content

Commit

Permalink
Update tiny details.
Browse files Browse the repository at this point in the history
  • Loading branch information
DolphyWind committed Nov 24, 2023
1 parent a6c70b7 commit f72d497
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 65 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
52 changes: 22 additions & 30 deletions include/Electra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,27 @@ SOFTWARE.
#include <fstream>
#include <sstream>
#include <algorithm>
#include <Logger.hpp>
#include <tuple>
#include <Printer.hpp>
#include <stack>
#include <ArithmeticalUnit.hpp>
#include <cmath>
#include <csignal>
#include <regex>
#include <codecvt>
#include <stdexcept>
#include <cstring>
#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#elif __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#error "Missing the <filesystem> header."
#endif

#include <Logger.hpp>
#include <Printer.hpp>
#include <ArithmeticalUnit.hpp>
#include <ConstantAdder.hpp>
#include <Cloner.hpp>
#include <ConstantPusher.hpp>
Expand All @@ -54,19 +69,6 @@ SOFTWARE.
#include <Reader.hpp>
#include <StackChecker.hpp>
#include <StackSwitcher.hpp>
#include <csignal>
#include <regex>
#include <codecvt>
#include <stdexcept>
#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#elif __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#error "Missing the <filesystem> header."
#endif

typedef std::vector<Direction> GeneratorData;

Expand Down Expand Up @@ -107,24 +109,17 @@ class Electra
// Wstring converter
static std::wstring_convert<std::codecvt_utf8<wchar_t>, 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.
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions include/Global.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ typedef double var_t;
typedef std::stack<var_t>* StackPtr;
typedef wchar_t char_t;

class Electra;
namespace Global
{
// Pops the top value of stack. Returns defaultValue if there is no item.
Expand All @@ -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{};
}
2 changes: 1 addition & 1 deletion src/Bomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ bool Bomb::work(CurrentPtr current, std::vector<CurrentPtr> *currentVector)
if(!Component::work(current, currentVector))
return false;

Electra::instance().safe_exit(0);
Global::safe_exit(0);
return Cable::work(current, currentVector);
}
46 changes: 15 additions & 31 deletions src/Electra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SOFTWARE.

std::wstring_convert<std::codecvt_utf8<wchar_t>, 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();
Expand Down Expand Up @@ -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"];
Expand All @@ -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
Expand All @@ -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)
{
Expand All @@ -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 ++;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -331,19 +320,14 @@ 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<std::wstring> 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
if(start >= end)
{
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)
Expand Down Expand Up @@ -385,7 +369,7 @@ std::vector<std::wstring> 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
Expand Down Expand Up @@ -429,7 +413,7 @@ std::vector<std::wstring> 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
Expand All @@ -441,7 +425,7 @@ std::vector<std::wstring> 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);
}
}

Expand All @@ -458,7 +442,7 @@ std::vector<std::wstring> 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;
Expand Down Expand Up @@ -660,5 +644,5 @@ void Electra::createCurrents()

void Electra::sigHandler(int signal)
{
Electra::instance().safe_exit(0);
Global::safe_exit(1, signal);
}
13 changes: 13 additions & 0 deletions src/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ SOFTWARE.
*/

#include <Global.hpp>
#include <Logger.hpp>
#include <cstring>

namespace Global
{
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit f72d497

Please sign in to comment.