Add arguments.hpp, remove a few useless files, and add a library target to the CMakeLists.txt file.
This commit is contained in:
parent
b1c8bc5b7f
commit
1bfb4cb54b
|
@ -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}")
|
set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${CMAKE_PROJECT_VERSION}-${TIME}")
|
||||||
|
|
||||||
include_directories(${CMAKE_CURRENT_LIST_DIR})
|
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}")
|
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
|
# lpkg = linux package, wpkg = windows package
|
||||||
set(PNAME ${PROJECT_NAME}-${CODENAME}-${TIME})
|
set(PNAME ${PROJECT_NAME}-${CODENAME}-${TIME})
|
||||||
if (UNIX AND NOT MINGW)
|
if (UNIX AND NOT MINGW)
|
||||||
|
|
|
@ -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
|
|
@ -2,6 +2,8 @@
|
||||||
#define YERBACON_PARSER_HPP
|
#define YERBACON_PARSER_HPP
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include "../lex.hpp"
|
||||||
|
#include "../parsing/ParseComponents.hpp"
|
||||||
|
|
||||||
namespace Parser {
|
namespace Parser {
|
||||||
ParseTree parseVector(const vector<tok>& lexed) {
|
ParseTree parseVector(const vector<tok>& lexed) {
|
||||||
|
|
|
@ -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/Lua.hpp"
|
||||||
#include "implementations/Js.hpp"
|
#include "implementations/Js.hpp"
|
||||||
#include "implementations/Py.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) {
|
shared_ptr<Target> Target::forName(string_view name) {
|
||||||
LANGUAGE selected = NONE;
|
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]) {
|
if (name == languages[i]) {
|
||||||
selected = static_cast<LANGUAGE>(i + 1);
|
selected = static_cast<LANGUAGE>(i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,8 +48,10 @@ shared_ptr<Target> Target::forName(string_view name) {
|
||||||
#endif
|
#endif
|
||||||
case NONE:
|
case NONE:
|
||||||
default: {
|
default: {
|
||||||
|
if (not name.empty()) {
|
||||||
cout << '"' << (char) toupper(name.at(1));
|
cout << '"' << (char) toupper(name.at(1));
|
||||||
name.remove_prefix(2); cout << name << "\" is not a valid target." << endl;
|
name.remove_prefix(2); cout << name << "\" is not a valid target." << endl;
|
||||||
|
} else cout << "No target was provided." << endl;
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
15
src/main.cpp
15
src/main.cpp
|
@ -1,4 +1,5 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <experimental/array>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
@ -6,6 +7,8 @@ using namespace std;
|
||||||
|
|
||||||
#include "headers/misc.hpp"
|
#include "headers/misc.hpp"
|
||||||
#include "headers/Yerbacon.hpp"
|
#include "headers/Yerbacon.hpp"
|
||||||
|
#include "headers/arguments.hpp"
|
||||||
|
#include "headers/transpiler/Target.hpp"
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if ((argc == 2) && (((string) argv[1]) == "--version")) { cout << Yerbacon::getVersion() << endl; exit(EXIT_SUCCESS); }
|
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;
|
string_view currentArg;
|
||||||
bool printResult = false;
|
bool printResult = false;
|
||||||
bool parallel = false;
|
bool parallel = false;
|
||||||
|
|
||||||
if (argc > 0) {
|
if (argc > 0) {
|
||||||
vector<string_view> files;
|
vector<string_view> files;
|
||||||
for (signed int i = 0; i < argc; ++i)
|
for (signed int i = 0; i < argc; ++i)
|
||||||
{
|
{
|
||||||
currentArg = static_cast<string_view>(argv[i]);
|
currentArg = static_cast<string_view>(argv[i]);
|
||||||
if ((currentArg == "--printresult") || (currentArg == "-p")) {
|
if (currentArg == ArgumentShort("printresult")) {
|
||||||
if (printResult) {
|
if (printResult) {
|
||||||
cout << "ERROR: --printresult was specified two times" << '\n';
|
cout << "ERROR: --printresult was specified two times" << '\n';
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
} else printResult = true;
|
} else printResult = true;
|
||||||
}
|
}
|
||||||
else if (currentArg.starts_with("--target=")) {
|
else if (currentArg == ArgumentAssignable("target")) {
|
||||||
currentArg.remove_prefix(9);
|
target.assign(string());
|
||||||
(target = '.') += currentArg;
|
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);
|
else if (currentArg.ends_with(".ybcon")) files.push_back(currentArg);
|
||||||
}
|
}
|
||||||
if (!files.empty()) {
|
if (!files.empty()) {
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
#include "../headers/lex.hpp"
|
|
||||||
#include "../headers/parsing/ParseComponents.hpp"
|
|
||||||
#include "../headers/parsing/Parser.hpp"
|
#include "../headers/parsing/Parser.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
Loading…
Reference in New Issue