From 456ced804eaa632f1740bd3b4fd6a0fcbffafb17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Grodzi=C5=84ski?= Date: Wed, 11 Jun 2025 00:04:04 +0900 Subject: [PATCH] Better command line --- .vscode/launch.json | 2 +- src/ModuleBuilder.cpp | 6 +++-- src/ModuleBuilder.h | 5 +++- src/main.cpp | 58 ++++++++++++++++++++++++++++--------------- 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 8b88d86..51cd702 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,7 +6,7 @@ "type": "lldb-dap", "request": "launch", "program": "${workspaceFolder}/brb", - "args": ["${workspaceFolder}/test.brc"], + "args": ["-v", "${workspaceFolder}/test.brc"], "internalConsoleOptions": "openOnSessionStart" } diff --git a/src/ModuleBuilder.cpp b/src/ModuleBuilder.cpp index 6a6cc58..2381de1 100644 --- a/src/ModuleBuilder.cpp +++ b/src/ModuleBuilder.cpp @@ -1,8 +1,10 @@ #include "ModuleBuilder.h" -ModuleBuilder::ModuleBuilder(vector> statements): statements(statements) { +ModuleBuilder::ModuleBuilder(string moduleName, string sourceFileName, vector> statements): +moduleName(moduleName), sourceFileName(sourceFileName), statements(statements) { context = make_shared(); - module = make_shared("dummy", *context); + module = make_shared(moduleName, *context); + module->setSourceFileName(sourceFileName); builder = make_shared>(*context); typeVoid = llvm::Type::getVoidTy(*context); diff --git a/src/ModuleBuilder.h b/src/ModuleBuilder.h index 7369986..df35902 100644 --- a/src/ModuleBuilder.h +++ b/src/ModuleBuilder.h @@ -16,6 +16,9 @@ using namespace std; class ModuleBuilder { private: + string moduleName; + string sourceFileName; + shared_ptr context; shared_ptr module; shared_ptr> builder; @@ -48,7 +51,7 @@ private: llvm::Type *typeForValueType(ValueType valueType); public: - ModuleBuilder(vector> statements); + ModuleBuilder(string moduleName, string sourceFileName, vector> statements); shared_ptr getModule(); }; diff --git a/src/main.cpp b/src/main.cpp index a4aad53..f9b2f36 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,8 @@ #include #include +#include + +#include "llvm/Support/CommandLine.h" #include "Token.h" #include "Lexer.h" @@ -13,10 +16,10 @@ using namespace std; -string readFile(string fileName) { - ifstream file(fileName.c_str(), ios::in | ios::binary | ios::ate); +string readFile(filesystem::path filePath) { + ifstream file(filePath, ios::in | ios::binary | ios::ate); if (!file.is_open()) { - cerr << "Cannot open file " << fileName << endl; + cerr << "Cannot open file " << filePath << endl; exit(1); } @@ -27,33 +30,48 @@ string readFile(string fileName) { return string(fileBytes.data(), fileSize); } -int main(int argc, char **argv) { - if (argc < 2) { - cerr << "Need to provide a file name" << endl; - exit(1); - } +void versionPrinter(llvm::raw_ostream &os) { + os << "Bits Runner Code, Version 1.0.0 (pre-alpha)\n"; +} + +int main(int argc, char **argv) { + llvm::cl::SetVersionPrinter(versionPrinter); + llvm::cl::extrahelp("\nADDITIONAL HELP:\n\n This is the extra help\n"); + + llvm::cl::opt isVerbose("v", llvm::cl::desc("Verbos output"), llvm::cl::init(false)); + llvm::cl::opt inputFileName(llvm::cl::Positional, llvm::cl::desc(""), llvm::cl::Required); + llvm::cl::ParseCommandLineOptions(argc, argv, "Bits Runner Builder - LLVM based compiler for the Bits Runner Code language"); + + filesystem::path inputFilePath((string(inputFileName))); + string source = readFile(inputFilePath); + string moduleName = inputFilePath.filename().replace_extension(); - string source = readFile(string(argv[1])); Lexer lexer(source); vector> tokens = lexer.getTokens(); - for (int i=0; itoString(); - if (i < tokens.size() - 1) - cout << ", "; + if (isVerbose) { + for (int i=0; itoString(); + if (i < tokens.size() - 1) + cout << ", "; + } + cout << endl << endl; } - cout << endl << endl; Parser parser(tokens); vector> statements = parser.getStatements(); - for (shared_ptr &statement : statements) { - cout << statement->toString(0); - cout << endl; + if (isVerbose) { + for (shared_ptr &statement : statements) { + cout << statement->toString(0); + cout << endl; + } + cout << endl << endl; } - cout << endl << endl; - ModuleBuilder moduleBuilder(statements); + ModuleBuilder moduleBuilder(moduleName, inputFilePath, statements); shared_ptr module = moduleBuilder.getModule(); - module->print(llvm::outs(), nullptr); + if (isVerbose) { + module->print(llvm::outs(), nullptr); + } //CodeGenerator codeGenerator(module); //codeGenerator.generateObjectFile("dummy.s");