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..6e24f24 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -1,69 +1,78 @@ #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 is 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; }