commit ac17ed2067ded142c8bc4a2da088690364029b1f Author: Maxwell Jeffress Date: Fri Apr 11 16:46:03 2025 +1000 Initial commit diff --git a/Bobfile b/Bobfile new file mode 100644 index 0000000..c219094 --- /dev/null +++ b/Bobfile @@ -0,0 +1,4 @@ +compiler "g++"; +binary "bob"; +source "src/main.cpp"; +compile; diff --git a/bob b/bob new file mode 100755 index 0000000..5b6503b Binary files /dev/null and b/bob differ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..4d2a54f --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include + +using namespace std; + +string version = "0.0.1"; + +bool isDebug = false; + +void error(string input) { + cout << "\033[31mError: \033[39m" << input << endl; +} + +void debug(string input) { + if (isDebug) cout << "\033[35mDebug: \033[39m" << input << endl; +} + +void log(string input) { + cout << "\033[37mInfo: \033[39m" << input << endl; +} + +int compile(string compiler, string source, string binary) { + string cmd = compiler + " " + source + " -o " + binary; + log("Compiling with command " + cmd); + return system(cmd.c_str()); +} + +int main(int argc, char* argv[]) { + string bobfile; + bool bobfileLock = false; + for (int i = 1; i < argc; i++) { + string currentArg = argv[i]; + if (currentArg == "--version") { + cout << version << endl; + return 0; + } else if (currentArg == "--help") { + cout << "bob" << endl; + cout << "Bob is a builder frontend for C and C++." << endl; + cout << "Create a Bobfile and follow the documentation online." << endl; + cout << "Then, run 'bob' and build your project!" << endl; + cout << "Arguments:" << endl; + cout << " (filename) Specify a custom bobfile location" << endl; + cout << " --help Print out this message" << endl; + cout << " --debug Get debug info for bug hunting" << endl; + cout << " --version Get Bob's version number" << endl; + return 0; + } else if (currentArg == "--debug") { + isDebug = true; + } else if (currentArg.substr(0,2) == "--") { + error("Argument " + currentArg + " not understood"); + return 1; + } else { + if (!bobfileLock) { + if (!filesystem::exists(currentArg)) { + error("Couldn't find the file " + currentArg); + return 1; + } + bobfile = currentArg; + bobfileLock = true; + } + } + } + + if (bobfile.empty()) { + if (!filesystem::exists("Bobfile")) { + error("Please provide an argument, or write a Bobfile in the current directory."); + return 0; + } else { + bobfile = "Bobfile"; + } + } + log("Using bobfile " + bobfile); + ifstream file(bobfile); + string content; + string line; + while (getline(file, line)) { + content += line; + } + debug("Bobfile content: " + content); + + vector> parsed; + vector buffera; + string bufferb; + bool isString = false; + debug("Parsing..."); + for (long unsigned int i = 0; i < content.size(); i++) { + if (content[i] == '"') { + debug("String time"); + isString = !isString; + if (!isString && !bufferb.empty()) { + buffera.push_back(bufferb); + bufferb.clear(); + } + } else if (content[i] == ';') { + debug("Next term..."); + parsed.push_back(buffera); + buffera.clear(); + } else if (content[i] == ' ') { + if (!isString) { + debug("Next thing..."); + buffera.push_back(bufferb); + bufferb.clear(); + } + } else { + bufferb += content[i]; + } + } + buffera.push_back(bufferb); + parsed.push_back(buffera); + if (isDebug) { + debug("Finished parsing bobfile. Contents:"); + for (long unsigned int i = 0; i < parsed.size(); i++) { + for (long unsigned int j = 0; j < parsed[i].size(); j++) { + cout << parsed[i][j] << " "; + } + cout << endl; + } + } + string compiler; + string binary; + string source; + + string editingTarget; + + for (int i = 0; i < parsed.size(); i++) { + for (int j = 0; j < parsed[i].size(); j++) { + debug("Token is " + parsed[i][j]); + if (j == 0) { + if (parsed[i][j] == "compile") { + compile(compiler, source, binary); + break; + } + editingTarget = parsed[i][j]; + debug("Editing target set to " + editingTarget); + } else { + if (editingTarget == "compiler") { + debug("Setting compiler"); + compiler = parsed[i][j]; + } else if (editingTarget == "binary") { + debug("Setting binary"); + binary = parsed[i][j]; + } else if (editingTarget == "source") { + debug("Setting source"); + source = parsed[i][j]; + } + } + } + } + return 0; +}