From 1bfb4cb54b8dbf73d8a9123f0a317836f390bc9c Mon Sep 17 00:00:00 2001 From: Username404 Date: Fri, 9 Jul 2021 18:58:06 +0200 Subject: [PATCH] Add arguments.hpp, remove a few useless files, and add a library target to the CMakeLists.txt file. --- CMakeLists.txt | 6 ++- src/headers/arguments.hpp | 62 +++++++++++++++++++++++++++++++ src/headers/parsing/Parser.hpp | 2 + src/headers/transpiler/Target.hpp | 17 +++++++-- src/main.cpp | 15 +++++--- src/parser/MainParse.cpp | 2 - src/transpiler/MainTranspile.cpp | 11 ------ 7 files changed, 92 insertions(+), 23 deletions(-) create mode 100644 src/headers/arguments.hpp delete mode 100644 src/transpiler/MainTranspile.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7914157..5951532 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,9 +83,13 @@ set(CPACK_PACKAGE_INSTALL_DIRECTORY "Yerbacon ${CMAKE_PROJECT_VERSION_MAJOR}.${C set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${CMAKE_PROJECT_VERSION}-${TIME}") include_directories(${CMAKE_CURRENT_LIST_DIR}) -add_executable(${EXENAME} src/main.cpp ${CMAKE_CURRENT_BINARY_DIR}/processed/${PROJECT_NAME}.rc src/parser/MainParse.cpp src/transpiler/MainTranspile.cpp src/etc/filefuncs.cpp src/etc/lexer.cpp src/headers/lex.hpp src/headers/misc.hpp src/headers/parsing/ParseComponents.hpp src/headers/transpiler/Target.hpp src/headers/transpiler/implementations/Lua.hpp src/headers/transpiler/implementations/Js.hpp src/headers/transpiler/implementations/Py.hpp src/headers/parsing/Parser.hpp src/headers/Yerbacon.hpp) +add_executable(${EXENAME} src/main.cpp ${CMAKE_CURRENT_BINARY_DIR}/processed/${PROJECT_NAME}.rc src/parser/MainParse.cpp src/etc/filefuncs.cpp src/etc/lexer.cpp src/headers/lex.hpp src/headers/misc.hpp src/headers/parsing/ParseComponents.hpp src/headers/transpiler/Target.hpp src/headers/transpiler/implementations/Lua.hpp src/headers/transpiler/implementations/Js.hpp src/headers/transpiler/implementations/Py.hpp src/headers/parsing/Parser.hpp src/headers/Yerbacon.hpp src/headers/arguments.hpp) target_compile_definitions(ybcon PRIVATE YBCON_VERSION="${CODENAME} ${PROJECT_VERSION}") +string(TOLOWER "${PROJECT_NAME}" YBCON_LIB) +add_library(${YBCON_LIB} SHARED src/headers/lex.hpp src/headers/parsing/ParseComponents.hpp src/headers/transpiler/Target.hpp src/headers/transpiler/implementations/Lua.hpp src/headers/transpiler/implementations/Js.hpp src/headers/transpiler/implementations/Py.hpp src/headers/parsing/Parser.hpp src/headers/Yerbacon.hpp) +set_target_properties(${YBCON_LIB} PROPERTIES LINKER_LANGUAGE CXX) + # lpkg = linux package, wpkg = windows package set(PNAME ${PROJECT_NAME}-${CODENAME}-${TIME}) if (UNIX AND NOT MINGW) diff --git a/src/headers/arguments.hpp b/src/headers/arguments.hpp new file mode 100644 index 0000000..8b5db4b --- /dev/null +++ b/src/headers/arguments.hpp @@ -0,0 +1,62 @@ +#ifndef YERBACON_ARGUMENTS_HPP +#define YERBACON_ARGUMENTS_HPP + +#include +#include +#include +#include +#include + +using namespace std; + +class Argument { +protected: + const string longVersion; + virtual bool is(string_view str) { + bool hasPrefix = str.starts_with("--"); + if (hasPrefix) str.remove_prefix(2); + return (not str.empty()) && longVersion == str && hasPrefix; + } +public: + bool operator ==(const string_view it) { return is(it); } + explicit Argument(string name): longVersion(move(name)) {} +}; + +class ArgumentAssignable: public Argument { + vector values; +public: + static string getValueFor(const string& str) { + unsigned long definePos = str.find_last_of('=') + 1; + return str.size() > definePos ? str.substr(definePos) : string(); + } + ArgumentAssignable(string name, const span& possibleValues) : Argument(move(name)), values() { + values.reserve(possibleValues.size()); + move(possibleValues.begin(), possibleValues.end(), back_inserter(values)); + } + explicit ArgumentAssignable(const string& name): ArgumentAssignable(name, span()) {}; // To use when we don't want the argument's possible values to be checked +protected: + bool is(string_view str) override { + if (str.find_last_of('=') != string_view::npos && !str.empty()) { + auto isValid = [&]() -> bool { return str.starts_with("--" + longVersion + '='); }; + if (values.empty()) return isValid(); + for (const auto& value : values) { + if (str.ends_with(value.substr(1, value.size()))) { + return isValid(); + } + } + } + return false; + } +}; + +struct ArgumentShort: public Argument { + const string shortVersion; + ArgumentShort(string shortName, string name): Argument(move(name)), shortVersion(move(shortName)) {} + explicit ArgumentShort(string name): ArgumentShort(string(1, name.at(0)), name) {} +protected: + bool is(string_view string) override { + return Argument::is(string) || (!string.empty() && shortVersion == string.substr(1, string.size())); + } +}; + +#endif //YERBACON_ARGUMENTS_HPP \ No newline at end of file diff --git a/src/headers/parsing/Parser.hpp b/src/headers/parsing/Parser.hpp index 901f31e..25c336f 100644 --- a/src/headers/parsing/Parser.hpp +++ b/src/headers/parsing/Parser.hpp @@ -2,6 +2,8 @@ #define YERBACON_PARSER_HPP #include +#include "../lex.hpp" +#include "../parsing/ParseComponents.hpp" namespace Parser { ParseTree parseVector(const vector& lexed) { diff --git a/src/headers/transpiler/Target.hpp b/src/headers/transpiler/Target.hpp index 9d4f32c..49403ee 100644 --- a/src/headers/transpiler/Target.hpp +++ b/src/headers/transpiler/Target.hpp @@ -16,15 +16,22 @@ struct Target { }; }; +string transpile(const ParseTree& tree, const string_view& language) { + return Target::forName(language)->transpileWithTree(tree); +} + #include "implementations/Lua.hpp" #include "implementations/Js.hpp" #include "implementations/Py.hpp" +enum LANGUAGE: signed short { NONE = -1, LUA, JS, PY }; +constinit array languages { ".lua", ".js", ".py"}; + shared_ptr Target::forName(string_view name) { LANGUAGE selected = NONE; - for (unsigned short i = 0; (i <= (languages->size() - 2)); ++i) { + for (unsigned int i = 0; i < languages.size(); ++i) { if (name == languages[i]) { - selected = static_cast(i + 1); + selected = static_cast(i); break; } } @@ -41,8 +48,10 @@ shared_ptr Target::forName(string_view name) { #endif case NONE: default: { - cout << '"' << (char) toupper(name.at(1)); - name.remove_prefix(2); cout << name << "\" is not a valid target." << endl; + if (not name.empty()) { + cout << '"' << (char) toupper(name.at(1)); + name.remove_prefix(2); cout << name << "\" is not a valid target." << endl; + } else cout << "No target was provided." << endl; exit(0); } } diff --git a/src/main.cpp b/src/main.cpp index 0945f56..f124355 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -6,6 +7,8 @@ using namespace std; #include "headers/misc.hpp" #include "headers/Yerbacon.hpp" +#include "headers/arguments.hpp" +#include "headers/transpiler/Target.hpp" int main(int argc, char* argv[]) { if ((argc == 2) && (((string) argv[1]) == "--version")) { cout << Yerbacon::getVersion() << endl; exit(EXIT_SUCCESS); } @@ -13,22 +16,24 @@ int main(int argc, char* argv[]) { string_view currentArg; bool printResult = false; bool parallel = false; + if (argc > 0) { vector files; for (signed int i = 0; i < argc; ++i) { currentArg = static_cast(argv[i]); - if ((currentArg == "--printresult") || (currentArg == "-p")) { + if (currentArg == ArgumentShort("printresult")) { if (printResult) { cout << "ERROR: --printresult was specified two times" << '\n'; exit(EXIT_SUCCESS); } else printResult = true; } - else if (currentArg.starts_with("--target=")) { - currentArg.remove_prefix(9); - (target = '.') += currentArg; + else if (currentArg == ArgumentAssignable("target")) { + target.assign(string()); + string value = ArgumentAssignable::getValueFor(currentArg.data()); + if (!value.empty()) (target = '.') += value; } - else if (currentArg == "--parallel") parallel = true; + else if (currentArg == Argument("parallel")) parallel = true; else if (currentArg.ends_with(".ybcon")) files.push_back(currentArg); } if (!files.empty()) { diff --git a/src/parser/MainParse.cpp b/src/parser/MainParse.cpp index fe394cc..e75aa99 100644 --- a/src/parser/MainParse.cpp +++ b/src/parser/MainParse.cpp @@ -1,5 +1,3 @@ -#include "../headers/lex.hpp" -#include "../headers/parsing/ParseComponents.hpp" #include "../headers/parsing/Parser.hpp" using namespace std; diff --git a/src/transpiler/MainTranspile.cpp b/src/transpiler/MainTranspile.cpp deleted file mode 100644 index 9302938..0000000 --- a/src/transpiler/MainTranspile.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include -using namespace std; - -enum LANGUAGE: unsigned short { NONE, LUA, JS, PY }; -const string_view languages[3] = { ".lua", ".js", ".py"}; - -#include "../headers/transpiler/Target.hpp" - -string transpile(const ParseTree& tree, const string_view& language) { - return Target::forName(language)->transpileWithTree(tree); -} \ No newline at end of file