Add support for multiple input files

This commit is contained in:
Username404-59 2021-06-10 15:59:57 +02:00
parent a75cbeb129
commit c5d33fd480
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
4 changed files with 31 additions and 22 deletions

View File

@ -4,20 +4,20 @@ using namespace std;
string getFileContent(const string& file) string getFileContent(const string& file)
{ {
ifstream filetoParse; ifstream fileStream;
string lineinput, fullinput; string lineinput, fullinput;
filetoParse.open(file, ios::in); fileStream.open(file, ios::in);
if (filetoParse.is_open()) while (getline(filetoParse, lineinput)) { if (fileStream.is_open()) while (getline(fileStream, lineinput)) {
fullinput.append(lineinput + "\n"); fullinput.append(lineinput + "\n");
} else { } else {
cout << "Could not open the file(s)." << endl; cout << "Could not open \"" << file << "\"." << endl;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
filetoParse.close(); fileStream.close();
return fullinput; return fullinput;
} }
void setOutputFileContent(const string& file, string_view content) { void outputFileContent(const string& file, const string_view content) {
static ofstream outputFile (file, ofstream::out); ofstream outputFile (file, ofstream::out);
outputFile << content; outputFile << content;
} }

View File

@ -8,7 +8,7 @@
consteval const char* getVersion() noexcept { return YBCON_VERSION; } consteval const char* getVersion() noexcept { return YBCON_VERSION; }
string getFileContent(const string& file); string getFileContent(const string& file);
void setOutputFileContent(const string& file, string_view content); void outputFileContent(const string& file, string_view content);
#include "src/headers/parsing/ParseComponents.hpp" #include "src/headers/parsing/ParseComponents.hpp"
ParseTree parseString(const string& toParse); ParseTree parseString(const string& toParse);

View File

@ -1,26 +1,35 @@
#include <iostream> #include <iostream>
#include <set>
using namespace std; using namespace std;
#include "headers/misc.hpp" #include "headers/misc.hpp"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if ((argc == 2) && (((string) argv[1]) == "--version")) { cout << getVersion() << endl; exit(EXIT_SUCCESS); } if ((argc == 2) && (((string) argv[1]) == "--version")) { cout << getVersion() << endl; exit(EXIT_SUCCESS); }
string fileName = (argv[argc - 1] != nullptr) ? argv[argc - 1] : "none" ; string target = ".lua";
if (fileName != "none" and fileName.ends_with(".ybcon")) string_view currentArg;
{ bool printResult = false;
bool printResult = false; if (argc > 0) {
string target = ".lua"; vector<string_view> files;
string currentArg;
for (signed int i = 0; i < argc; ++i) for (signed int i = 0; i < argc; ++i)
{ {
currentArg = static_cast<string>(argv[i]); currentArg = static_cast<string_view>(argv[i]);
if ((currentArg == "--printresult") || (currentArg == "-p")) printResult = true; if ((currentArg == "--printresult") || (currentArg == "-p")) printResult = true;
else if (currentArg.starts_with("--target=")) target = '.' + currentArg.erase(0, 9); else if (currentArg.starts_with("--target=")) {
currentArg.remove_prefix(9);
target = currentArg;
}
else files.push_back(currentArg);
} }
const string transpiledString = transpile(parseString(getFileContent(fileName)), target); const set<string_view> uniqueFiles(files.begin(), files.end());
if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n" << "[WIP]\n" << transpiledString << "\n\n"; for (string_view fileName: uniqueFiles) {
setOutputFileContent(fileName.erase(fileName.find(".ybcon")) + target, transpiledString); if (fileName != "none" and fileName.ends_with(".ybcon")) {
} const string transpiledString = transpile(parseString(getFileContent(fileName.data())), target);
else cout << "No valid file provided.\n"; if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n" << "[WIP]\n" << transpiledString << "\n\n";
fileName.remove_suffix(6);
outputFileContent(target.insert(0, fileName), transpiledString);
}
}
} else cout << "No valid file provided.\n";
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -4,7 +4,7 @@ using namespace std;
enum LANGUAGE: unsigned short {LUA=0,JS=1,PY=2}; enum LANGUAGE: unsigned short {LUA=0,JS=1,PY=2};
pair<LANGUAGE, bool> validLanguage(const string& it) { pair<LANGUAGE, bool> validLanguage(const string_view& it) {
static const string_view languages[3] = {".lua", ".js", ".py"}; static const string_view languages[3] = {".lua", ".js", ".py"};
LANGUAGE selected = LUA; LANGUAGE selected = LUA;
bool valid = false; bool valid = false;