From 9a67610abec50162e757d965c6da1b6fa2f0e8a9 Mon Sep 17 00:00:00 2001 From: Fiwo735 Date: Sat, 16 Mar 2024 19:00:34 +0000 Subject: [PATCH 1/3] Renamed cli.h into cli.hpp, made cli and compiler more consistent --- include/{cli.h => cli.hpp} | 0 src/cli.cpp | 2 +- src/compiler.cpp | 57 +++++++++++++++++++++++--------------- 3 files changed, 35 insertions(+), 24 deletions(-) rename include/{cli.h => cli.hpp} (100%) diff --git a/include/cli.h b/include/cli.hpp similarity index 100% rename from include/cli.h rename to include/cli.hpp diff --git a/src/cli.cpp b/src/cli.cpp index 1e581e7..fc0610f 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -1,4 +1,4 @@ -#include +#include CommandLineArguments ParseCommandLineArgs(int argc, char **argv) { diff --git a/src/compiler.cpp b/src/compiler.cpp index 865673b..18d0ca5 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -1,69 +1,80 @@ #include #include -#include "cli.h" +#include "cli.hpp" #include "ast.hpp" using ast::NodePtr; -NodePtr Parse(const CommandLineArguments& args); +// Wrapper for ParseAST defined in YACC +NodePtr Parse(const std::string& compile_source_path); -// Output the pretty print version of what was parsed to the .printed output -// file. -void PrettyPrint(const NodePtr& root, const CommandLineArguments& args); +// Output the pretty print version of what was parsed to the .printed output file. +void PrettyPrint(const NodePtr& root, const std::string& compile_output_path); -// Compile from the root of the AST and output this to the -// args.compiledOutputPath file. -void Compile(const NodePtr& root, const CommandLineArguments& args); +// Compile from the root of the AST and output this to the compiledOutputPath file. +void Compile(const NodePtr& root, const std::string& compile_output_path); int main(int argc, char **argv) { // Parse CLI arguments to fetch the source file to compile and the path to output to. // This retrives [source-file.c] and [dest-file.s], when the compiler is invoked as follows: // ./bin/c_compiler -S [source-file.c] -o [dest-file.s] - auto command_line_arguments = ParseCommandLineArgs(argc, argv); + const auto [compile_source_path, compile_output_path] = ParseCommandLineArgs(argc, argv); - // Parse input and generate AST - auto ast_root = Parse(command_line_arguments); + // Parse input and generate AST. + auto ast_root = Parse(compile_source_path); + + // Check something was actually returned by parseAST(). if (ast_root == nullptr) { - // Check something was actually returned by parseAST(). - std::cerr << "The root of the AST was a null pointer. Likely the root was never initialised correctly during parsing." << std::endl; + std::cerr << "The root of the AST was a null pointer. "; + std::cerr << "Likely the root was never initialised correctly during parsing." << std::endl; return 3; } - PrettyPrint(ast_root, command_line_arguments); - Compile(ast_root, command_line_arguments); + // Print AST in a human-readable way. It's not assessed, but exists for your convenience. + PrettyPrint(ast_root, compile_output_path); + + // Compile to RISC-V assembly, the main goal of this project. + Compile(ast_root, compile_output_path); } -NodePtr Parse(const CommandLineArguments& args) +NodePtr Parse(const std::string& compile_source_path) { - std::cout << "Parsing: " << args.compile_source_path << std::endl; - NodePtr root = ParseAST(args.compile_source_path); + std::cout << "Parsing ..." << compile_source_path << std::endl; + + NodePtr root = ParseAST(compile_source_path); + std::cout << "AST parsing complete" << std::endl; + return root; } -void PrettyPrint(const NodePtr& root, const CommandLineArguments& args) +void PrettyPrint(const NodePtr& root, const std::string& compile_output_path) { - auto output_path = args.compile_output_path + ".printed"; + auto output_path = compile_output_path + ".printed"; std::cout << "Printing parsed AST..." << std::endl; + std::ofstream output(output_path, std::ios::trunc); root->Print(output); output.close(); + std::cout << "Printed parsed AST to: " << output_path << std::endl; } -void Compile(const NodePtr& root, const CommandLineArguments& args) +void Compile(const NodePtr& root, const std::string& compile_output_path) { // Create a Context. This can be used to pass around information about // what's currently being compiled (e.g. function scope and variable names). ast::Context ctx; std::cout << "Compiling parsed AST..." << std::endl; - std::ofstream output(args.compile_output_path, std::ios::trunc); + + std::ofstream output(compile_output_path, std::ios::trunc); root->EmitRISC(output, ctx); output.close(); - std::cout << "Compiled to: " << args.compile_output_path << std::endl; + + std::cout << "Compiled to: " << compile_output_path << std::endl; } From 0f4dda4ef009975a9ab9ad28e1f1fe4783013e7a Mon Sep 17 00:00:00 2001 From: Filip Wojcicki <50636446+Fiwo735@users.noreply.github.com> Date: Tue, 19 Mar 2024 18:23:27 +0100 Subject: [PATCH 2/3] Update src/compiler.cpp Co-authored-by: Jpnock --- src/compiler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler.cpp b/src/compiler.cpp index 18d0ca5..dda5ce6 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -28,7 +28,7 @@ int main(int argc, char **argv) // Check something was actually returned by parseAST(). if (ast_root == nullptr) { - std::cerr << "The root of the AST was a null pointer. "; + std::cerr << "The root of the AST is a null pointer. "; std::cerr << "Likely the root was never initialised correctly during parsing." << std::endl; return 3; } From ee7d4694414d51cdb69f9b070a29839a4f12b1cb Mon Sep 17 00:00:00 2001 From: Filip Wojcicki <50636446+Fiwo735@users.noreply.github.com> Date: Sat, 23 Mar 2024 03:15:13 +0000 Subject: [PATCH 3/3] Removed unnecessary calls to .close() for ofstream --- src/compiler.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/compiler.cpp b/src/compiler.cpp index dda5ce6..6e24f24 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -59,7 +59,6 @@ void PrettyPrint(const NodePtr& root, const std::string& compile_output_path) std::ofstream output(output_path, std::ios::trunc); root->Print(output); - output.close(); std::cout << "Printed parsed AST to: " << output_path << std::endl; } @@ -74,7 +73,6 @@ void Compile(const NodePtr& root, const std::string& compile_output_path) std::ofstream output(compile_output_path, std::ios::trunc); root->EmitRISC(output, ctx); - output.close(); std::cout << "Compiled to: " << compile_output_path << std::endl; }