Add arguments.hpp, remove a few useless files, and add a library target to the CMakeLists.txt file.

This commit is contained in:
Username404 2021-07-09 18:58:06 +02:00
parent b1c8bc5b7f
commit 1bfb4cb54b
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
7 changed files with 92 additions and 23 deletions

View File

@ -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)

62
src/headers/arguments.hpp Normal file
View File

@ -0,0 +1,62 @@
#ifndef YERBACON_ARGUMENTS_HPP
#define YERBACON_ARGUMENTS_HPP
#include <iostream>
#include <utility>
#include <string>
#include <string_view>
#include <span>
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<string_view> 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<string_view>& 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<string_view>()) {}; // 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

View File

@ -2,6 +2,8 @@
#define YERBACON_PARSER_HPP
#include <string>
#include "../lex.hpp"
#include "../parsing/ParseComponents.hpp"
namespace Parser {
ParseTree parseVector(const vector<tok>& lexed) {

View File

@ -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<string_view, 3> languages { ".lua", ".js", ".py"};
shared_ptr<Target> 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<LANGUAGE>(i + 1);
selected = static_cast<LANGUAGE>(i);
break;
}
}
@ -41,8 +48,10 @@ shared_ptr<Target> 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);
}
}

View File

@ -1,4 +1,5 @@
#include <iostream>
#include <experimental/array>
#include <set>
#include <future>
#include <sstream>
@ -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<string_view> files;
for (signed int i = 0; i < argc; ++i)
{
currentArg = static_cast<string_view>(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()) {

View File

@ -1,5 +1,3 @@
#include "../headers/lex.hpp"
#include "../headers/parsing/ParseComponents.hpp"
#include "../headers/parsing/Parser.hpp"
using namespace std;

View File

@ -1,11 +0,0 @@
#include <string_view>
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);
}