Add new comments
This commit is contained in:
parent
1aed5c93f1
commit
44f9438839
12
src/main.cpp
12
src/main.cpp
@ -59,17 +59,19 @@ struct CompileOutput {
|
|||||||
|
|
||||||
/// @brief Compiles the contents from the given filename and returns output as strings
|
/// @brief Compiles the contents from the given filename and returns output as strings
|
||||||
std::optional<CompileOutput> compile(std::string_view in_filename) {
|
std::optional<CompileOutput> compile(std::string_view in_filename) {
|
||||||
std::string out{ read_file(in_filename) };
|
|
||||||
|
|
||||||
|
// Read contents of the file, print contents
|
||||||
|
std::string out{ read_file(in_filename) };
|
||||||
std::cout << out << std::endl;
|
std::cout << out << std::endl;
|
||||||
|
|
||||||
|
// Tokenize contents, print tokens
|
||||||
auto tokens = token::tokenize(out, std::string{ in_filename });
|
auto tokens = token::tokenize(out, std::string{ in_filename });
|
||||||
for (token::Token token : tokens) {
|
for (token::Token token : tokens) {
|
||||||
std::cout << token << std::endl;
|
std::cout << token << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse tokens
|
||||||
auto stream = token::TokenStream{ tokens };
|
auto stream = token::TokenStream{ tokens };
|
||||||
|
|
||||||
std::vector<std::unique_ptr<AST::TopLevelStatement>> statements;
|
std::vector<std::unique_ptr<AST::TopLevelStatement>> statements;
|
||||||
auto statement = parsing::parse_top_level_statement(stream);
|
auto statement = parsing::parse_top_level_statement(stream);
|
||||||
while (statement.ok()) {
|
while (statement.ok()) {
|
||||||
@ -82,7 +84,7 @@ std::optional<CompileOutput> compile(std::string_view in_filename) {
|
|||||||
}
|
}
|
||||||
stream.expect(token::Type::Eof);
|
stream.expect(token::Type::Eof);
|
||||||
|
|
||||||
|
// Prepare compiler
|
||||||
auto LLVMContext = std::make_unique<llvm::LLVMContext>();
|
auto LLVMContext = std::make_unique<llvm::LLVMContext>();
|
||||||
auto LLVMModule = std::make_unique<llvm::Module>("test.c", *LLVMContext);
|
auto LLVMModule = std::make_unique<llvm::Module>("test.c", *LLVMContext);
|
||||||
auto LLVMBuilder = std::make_unique<llvm::IRBuilder<>>(*LLVMContext);
|
auto LLVMBuilder = std::make_unique<llvm::IRBuilder<>>(*LLVMContext);
|
||||||
@ -96,6 +98,7 @@ std::optional<CompileOutput> compile(std::string_view in_filename) {
|
|||||||
|
|
||||||
codegen::Scope scope{};
|
codegen::Scope scope{};
|
||||||
|
|
||||||
|
// Compile parsed output
|
||||||
try {
|
try {
|
||||||
for (auto& tls : statements) {
|
for (auto& tls : statements) {
|
||||||
std::cout << tls->formatted() << std::endl;
|
std::cout << tls->formatted() << std::endl;
|
||||||
@ -109,6 +112,7 @@ std::optional<CompileOutput> compile(std::string_view in_filename) {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare to print module as output
|
||||||
llvm::InitializeAllTargetInfos();
|
llvm::InitializeAllTargetInfos();
|
||||||
llvm::InitializeAllTargets();
|
llvm::InitializeAllTargets();
|
||||||
llvm::InitializeAllTargetMCs();
|
llvm::InitializeAllTargetMCs();
|
||||||
@ -124,12 +128,14 @@ std::optional<CompileOutput> compile(std::string_view in_filename) {
|
|||||||
builder.mod->setDataLayout(TargetMachine->createDataLayout());
|
builder.mod->setDataLayout(TargetMachine->createDataLayout());
|
||||||
builder.mod->setTargetTriple(TargetMachine->getTargetTriple());
|
builder.mod->setTargetTriple(TargetMachine->getTargetTriple());
|
||||||
|
|
||||||
|
// Print output to string
|
||||||
std::string llvm_ir_string;
|
std::string llvm_ir_string;
|
||||||
llvm::raw_string_ostream llvm_ir_dest{ llvm_ir_string };
|
llvm::raw_string_ostream llvm_ir_dest{ llvm_ir_string };
|
||||||
|
|
||||||
builder.mod->print(llvm_ir_dest, nullptr);
|
builder.mod->print(llvm_ir_dest, nullptr);
|
||||||
llvm_ir_dest.flush();
|
llvm_ir_dest.flush();
|
||||||
|
|
||||||
|
// Print output to obj-file
|
||||||
std::error_code EC;
|
std::error_code EC;
|
||||||
std::string obj_string;
|
std::string obj_string;
|
||||||
llvm::raw_string_ostream obj_stream{ obj_string };
|
llvm::raw_string_ostream obj_stream{ obj_string };
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user