Better command line

This commit is contained in:
Rafał Grodziński
2025-06-11 00:04:04 +09:00
parent 7d141bec14
commit 456ced804e
4 changed files with 47 additions and 24 deletions

2
.vscode/launch.json vendored
View File

@@ -6,7 +6,7 @@
"type": "lldb-dap", "type": "lldb-dap",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/brb", "program": "${workspaceFolder}/brb",
"args": ["${workspaceFolder}/test.brc"], "args": ["-v", "${workspaceFolder}/test.brc"],
"internalConsoleOptions": "openOnSessionStart" "internalConsoleOptions": "openOnSessionStart"
} }

View File

@@ -1,8 +1,10 @@
#include "ModuleBuilder.h" #include "ModuleBuilder.h"
ModuleBuilder::ModuleBuilder(vector<shared_ptr<Statement>> statements): statements(statements) { ModuleBuilder::ModuleBuilder(string moduleName, string sourceFileName, vector<shared_ptr<Statement>> statements):
moduleName(moduleName), sourceFileName(sourceFileName), statements(statements) {
context = make_shared<llvm::LLVMContext>(); context = make_shared<llvm::LLVMContext>();
module = make_shared<llvm::Module>("dummy", *context); module = make_shared<llvm::Module>(moduleName, *context);
module->setSourceFileName(sourceFileName);
builder = make_shared<llvm::IRBuilder<>>(*context); builder = make_shared<llvm::IRBuilder<>>(*context);
typeVoid = llvm::Type::getVoidTy(*context); typeVoid = llvm::Type::getVoidTy(*context);

View File

@@ -16,6 +16,9 @@ using namespace std;
class ModuleBuilder { class ModuleBuilder {
private: private:
string moduleName;
string sourceFileName;
shared_ptr<llvm::LLVMContext> context; shared_ptr<llvm::LLVMContext> context;
shared_ptr<llvm::Module> module; shared_ptr<llvm::Module> module;
shared_ptr<llvm::IRBuilder<>> builder; shared_ptr<llvm::IRBuilder<>> builder;
@@ -48,7 +51,7 @@ private:
llvm::Type *typeForValueType(ValueType valueType); llvm::Type *typeForValueType(ValueType valueType);
public: public:
ModuleBuilder(vector<shared_ptr<Statement>> statements); ModuleBuilder(string moduleName, string sourceFileName, vector<shared_ptr<Statement>> statements);
shared_ptr<llvm::Module> getModule(); shared_ptr<llvm::Module> getModule();
}; };

View File

@@ -1,5 +1,8 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <filesystem>
#include "llvm/Support/CommandLine.h"
#include "Token.h" #include "Token.h"
#include "Lexer.h" #include "Lexer.h"
@@ -13,10 +16,10 @@
using namespace std; using namespace std;
string readFile(string fileName) { string readFile(filesystem::path filePath) {
ifstream file(fileName.c_str(), ios::in | ios::binary | ios::ate); ifstream file(filePath, ios::in | ios::binary | ios::ate);
if (!file.is_open()) { if (!file.is_open()) {
cerr << "Cannot open file " << fileName << endl; cerr << "Cannot open file " << filePath << endl;
exit(1); exit(1);
} }
@@ -27,33 +30,48 @@ string readFile(string fileName) {
return string(fileBytes.data(), fileSize); return string(fileBytes.data(), fileSize);
} }
int main(int argc, char **argv) { void versionPrinter(llvm::raw_ostream &os) {
if (argc < 2) { os << "Bits Runner Code, Version 1.0.0 (pre-alpha)\n";
cerr << "Need to provide a file name" << endl; }
exit(1);
} 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<bool> isVerbose("v", llvm::cl::desc("Verbos output"), llvm::cl::init(false));
llvm::cl::opt<string> inputFileName(llvm::cl::Positional, llvm::cl::desc("<input file>"), 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); Lexer lexer(source);
vector<shared_ptr<Token>> tokens = lexer.getTokens(); vector<shared_ptr<Token>> tokens = lexer.getTokens();
for (int i=0; i<tokens.size(); i++) { if (isVerbose) {
cout << i << "|" << tokens.at(i)->toString(); for (int i=0; i<tokens.size(); i++) {
if (i < tokens.size() - 1) cout << i << "|" << tokens.at(i)->toString();
cout << ", "; if (i < tokens.size() - 1)
cout << ", ";
}
cout << endl << endl;
} }
cout << endl << endl;
Parser parser(tokens); Parser parser(tokens);
vector<shared_ptr<Statement>> statements = parser.getStatements(); vector<shared_ptr<Statement>> statements = parser.getStatements();
for (shared_ptr<Statement> &statement : statements) { if (isVerbose) {
cout << statement->toString(0); for (shared_ptr<Statement> &statement : statements) {
cout << endl; cout << statement->toString(0);
cout << endl;
}
cout << endl << endl;
} }
cout << endl << endl;
ModuleBuilder moduleBuilder(statements); ModuleBuilder moduleBuilder(moduleName, inputFilePath, statements);
shared_ptr<llvm::Module> module = moduleBuilder.getModule(); shared_ptr<llvm::Module> module = moduleBuilder.getModule();
module->print(llvm::outs(), nullptr); if (isVerbose) {
module->print(llvm::outs(), nullptr);
}
//CodeGenerator codeGenerator(module); //CodeGenerator codeGenerator(module);
//codeGenerator.generateObjectFile("dummy.s"); //codeGenerator.generateObjectFile("dummy.s");