From 44f94388391d8cf0b6c5a630f7b8b90c47939286 Mon Sep 17 00:00:00 2001 From: Sofia Date: Sat, 11 Apr 2026 19:17:47 +0300 Subject: [PATCH] Add new comments --- src/main.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6cdac88..82d36de 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,17 +59,19 @@ struct CompileOutput { /// @brief Compiles the contents from the given filename and returns output as strings std::optional 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; + // Tokenize contents, print tokens auto tokens = token::tokenize(out, std::string{ in_filename }); for (token::Token token : tokens) { std::cout << token << std::endl; } + // Parse tokens auto stream = token::TokenStream{ tokens }; - std::vector> statements; auto statement = parsing::parse_top_level_statement(stream); while (statement.ok()) { @@ -82,7 +84,7 @@ std::optional compile(std::string_view in_filename) { } stream.expect(token::Type::Eof); - + // Prepare compiler auto LLVMContext = std::make_unique(); auto LLVMModule = std::make_unique("test.c", *LLVMContext); auto LLVMBuilder = std::make_unique>(*LLVMContext); @@ -96,6 +98,7 @@ std::optional compile(std::string_view in_filename) { codegen::Scope scope{}; + // Compile parsed output try { for (auto& tls : statements) { std::cout << tls->formatted() << std::endl; @@ -109,6 +112,7 @@ std::optional compile(std::string_view in_filename) { return {}; } + // Prepare to print module as output llvm::InitializeAllTargetInfos(); llvm::InitializeAllTargets(); llvm::InitializeAllTargetMCs(); @@ -124,12 +128,14 @@ std::optional compile(std::string_view in_filename) { builder.mod->setDataLayout(TargetMachine->createDataLayout()); builder.mod->setTargetTriple(TargetMachine->getTargetTriple()); + // Print output to string std::string llvm_ir_string; llvm::raw_string_ostream llvm_ir_dest{ llvm_ir_string }; builder.mod->print(llvm_ir_dest, nullptr); llvm_ir_dest.flush(); + // Print output to obj-file std::error_code EC; std::string obj_string; llvm::raw_string_ostream obj_stream{ obj_string };