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)
{
ifstream filetoParse;
ifstream fileStream;
string lineinput, fullinput;
filetoParse.open(file, ios::in);
if (filetoParse.is_open()) while (getline(filetoParse, lineinput)) {
fileStream.open(file, ios::in);
if (fileStream.is_open()) while (getline(fileStream, lineinput)) {
fullinput.append(lineinput + "\n");
} else {
cout << "Could not open the file(s)." << endl;
cout << "Could not open \"" << file << "\"." << endl;
exit(EXIT_FAILURE);
}
filetoParse.close();
fileStream.close();
return fullinput;
}
void setOutputFileContent(const string& file, string_view content) {
static ofstream outputFile (file, ofstream::out);
void outputFileContent(const string& file, const string_view content) {
ofstream outputFile (file, ofstream::out);
outputFile << content;
}

View File

@ -8,7 +8,7 @@
consteval const char* getVersion() noexcept { return YBCON_VERSION; }
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"
ParseTree parseString(const string& toParse);

View File

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

View File

@ -4,7 +4,7 @@ using namespace std;
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"};
LANGUAGE selected = LUA;
bool valid = false;