From 7a631b3557770060d99f5879b57e1394297a68ff Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 17 Sep 2020 18:30:41 -0700 Subject: [PATCH] Make a few more things classes. --- code/compiler/13/ast.cpp | 12 ++++---- code/compiler/13/compiler.cpp | 15 +++++----- code/compiler/13/compiler.hpp | 5 ++-- code/compiler/13/definition.cpp | 4 +-- code/compiler/13/error.hpp | 47 +++++++++++++++++-------------- code/compiler/13/parse_driver.cpp | 2 +- code/compiler/13/parse_driver.hpp | 18 ++++++------ 7 files changed, 54 insertions(+), 49 deletions(-) diff --git a/code/compiler/13/ast.cpp b/code/compiler/13/ast.cpp index b6ce2e9..8fc5d78 100644 --- a/code/compiler/13/ast.cpp +++ b/code/compiler/13/ast.cpp @@ -415,7 +415,7 @@ void compile_case(const ast_case& node, const env_ptr& env, const type* type, st void ast_case::compile(const env_ptr& env, std::vector& into) const { type_app* app_type = dynamic_cast(input_type.get()); type_data* data; - type_internal* internal; + type_base *base; of->compile(env, into); into.push_back(instruction_ptr(new instruction_eval())); @@ -423,11 +423,11 @@ void ast_case::compile(const env_ptr& env, std::vector& into) c if(app_type && (data = dynamic_cast(app_type->constructor.get()))) { compile_case(*this, env, data, into); return; - } else if(app_type && (internal = dynamic_cast(app_type->constructor.get()))) { - if(internal->name == "Bool") { - compile_case(*this, env, data, into); - return; - } + } else if (app_type && (base = dynamic_cast(app_type->constructor.get()))) { + if (base->name == "Bool") { + compile_case(*this, env, data, into); + return; + } } throw type_error("attempting unsupported case analysis", of->loc); diff --git a/code/compiler/13/compiler.cpp b/code/compiler/13/compiler.cpp index 3d4b5ff..be06d10 100644 --- a/code/compiler/13/compiler.cpp +++ b/code/compiler/13/compiler.cpp @@ -16,13 +16,12 @@ #include "llvm/Target/TargetMachine.h" void compiler::add_default_types() { - global_env->bind_type("Int", type_ptr(new type_internal("Int"))); - global_env->bind_type("Bool", type_ptr(new type_internal("Bool"))); + global_env->bind_type("Int", type_ptr(new type_base("Int"))); + global_env->bind_type("Bool", type_ptr(new type_base("Bool"))); } void compiler::add_binop_type(binop op, type_ptr type) { auto name = mng.new_mangled_name(op_action(op)); - binop_names[op] = name; global_env->bind(op_name(op), std::move(type), visibility::global); global_env->set_mangled_name(op_name(op), name); } @@ -53,7 +52,7 @@ void compiler::add_default_function_types() { } void compiler::parse() { - if(!driver.run_parse()) + if(!driver()) throw compiler_error("failed to open file"); } @@ -78,7 +77,8 @@ void compiler::compile() { } void compiler::create_llvm_binop(binop op) { - auto new_function = ctx.create_custom_function(binop_names.at(op), 2); + auto new_function = + ctx.create_custom_function(global_env->get_mangled_name(op_name(op)), 2); std::vector instructions; instructions.push_back(instruction_ptr(new instruction_push(1))); instructions.push_back(instruction_ptr(new instruction_eval())); @@ -95,7 +95,8 @@ void compiler::create_llvm_binop(binop op) { } void compiler::create_llvm_bool(bool b) { - auto new_function = ctx.create_custom_function(b ? "True" : "False", 0); + auto new_function = ctx.create_custom_function( + global_env->get_mangled_name(b ? "True" : "False"), 0); std::vector instructions; instructions.push_back(instruction_ptr(new instruction_pushint(b))); instructions.push_back(instruction_ptr(new instruction_update(0))); @@ -158,7 +159,7 @@ void compiler::output_llvm(const std::string& into) { compiler::compiler(const std::string& filename) : file_m(), global_defs(), driver(file_m, global_defs, filename), - mng(), global_scp(mng), global_env(new type_env), type_m(), ctx() { + global_env(new type_env), type_m(), mng(), global_scp(mng), ctx() { add_default_types(); add_default_function_types(); } diff --git a/code/compiler/13/compiler.hpp b/code/compiler/13/compiler.hpp index a5873be..f26d5d6 100644 --- a/code/compiler/13/compiler.hpp +++ b/code/compiler/13/compiler.hpp @@ -10,14 +10,13 @@ class compiler { private: - std::map binop_names; file_mgr file_m; definition_group global_defs; parse_driver driver; - mangler mng; - global_scope global_scp; type_env_ptr global_env; type_mgr type_m; + mangler mng; + global_scope global_scp; llvm_context ctx; void add_default_types(); diff --git a/code/compiler/13/definition.cpp b/code/compiler/13/definition.cpp index 8ccbd55..ae4896d 100644 --- a/code/compiler/13/definition.cpp +++ b/code/compiler/13/definition.cpp @@ -65,8 +65,8 @@ void definition_data::insert_constructors() const { for(auto& var : vars) { if(var_set.find(var) != var_set.end()) throw compiler_error( - std::string("type variable ") + - var + std::string(" used twice in data type definition."), loc); + "type variable " + var + + " used twice in data type definition.", loc); var_set.insert(var); return_app->arguments.push_back(type_ptr(new type_var(var))); } diff --git a/code/compiler/13/error.hpp b/code/compiler/13/error.hpp index 42a6404..af5d705 100644 --- a/code/compiler/13/error.hpp +++ b/code/compiler/13/error.hpp @@ -7,38 +7,43 @@ using maybe_location = std::optional; -struct compiler_error : std::exception { - std::string description; - maybe_location loc; +class compiler_error : std::exception { + private: + std::string description; + maybe_location loc; - compiler_error(std::string d, maybe_location l = std::nullopt) - : description(std::move(d)), loc(std::move(l)) {} + public: + compiler_error(std::string d, maybe_location l = std::nullopt) + : description(std::move(d)), loc(std::move(l)) {} - const char* what() const noexcept override; + const char* what() const noexcept override; - void print_about(std::ostream& to); - void print_location(std::ostream& to, file_mgr& fm, bool highlight = false); + void print_about(std::ostream& to); + void print_location(std::ostream& to, file_mgr& fm, bool highlight = false); - void pretty_print(std::ostream& to, file_mgr& fm); + void pretty_print(std::ostream& to, file_mgr& fm); }; -struct type_error : compiler_error { - std::optional loc; +class type_error : compiler_error { + private: - type_error(std::string d, maybe_location l = std::nullopt) - : compiler_error(std::move(d), std::move(l)) {} + public: + type_error(std::string d, maybe_location l = std::nullopt) + : compiler_error(std::move(d), std::move(l)) {} - const char* what() const noexcept override; - void pretty_print(std::ostream& to, file_mgr& fm); + const char* what() const noexcept override; + void pretty_print(std::ostream& to, file_mgr& fm); }; -struct unification_error : public type_error { - type_ptr left; - type_ptr right; +class unification_error : public type_error { + private: + type_ptr left; + type_ptr right; - unification_error(type_ptr l, type_ptr r, maybe_location loc = std::nullopt) - : left(std::move(l)), right(std::move(r)), + public: + unification_error(type_ptr l, type_ptr r, maybe_location loc = std::nullopt) + : left(std::move(l)), right(std::move(r)), type_error("failed to unify types", std::move(loc)) {} - void pretty_print(std::ostream& to, file_mgr& fm, type_mgr& mgr); + void pretty_print(std::ostream& to, file_mgr& fm, type_mgr& mgr); }; diff --git a/code/compiler/13/parse_driver.cpp b/code/compiler/13/parse_driver.cpp index 86c2fc7..f353e45 100644 --- a/code/compiler/13/parse_driver.cpp +++ b/code/compiler/13/parse_driver.cpp @@ -45,7 +45,7 @@ void file_mgr::print_location( stream.write(content + highlight_end, print_end - highlight_end); } -bool parse_driver::run_parse() { +bool parse_driver::operator()() { FILE* stream = fopen(file_name.c_str(), "r"); if(!stream) return false; yyscan_t scanner; diff --git a/code/compiler/13/parse_driver.hpp b/code/compiler/13/parse_driver.hpp index 2364c58..bf1a492 100644 --- a/code/compiler/13/parse_driver.hpp +++ b/code/compiler/13/parse_driver.hpp @@ -41,16 +41,16 @@ class parse_driver { file_mgr* file_m; public: - parse_driver( - file_mgr& mgr, - definition_group& defs, - const std::string& file) - : file_name(file), file_m(&mgr), global_defs(&defs) {} + parse_driver( + file_mgr& mgr, + definition_group& defs, + const std::string& file) + : file_name(file), file_m(&mgr), global_defs(&defs) {} - bool run_parse(); - yy::location& get_current_location(); - file_mgr& get_file_manager() const; - definition_group& get_global_defs() const; + bool operator()(); + yy::location& get_current_location(); + file_mgr& get_file_manager() const; + definition_group& get_global_defs() const; }; #define YY_DECL yy::parser::symbol_type yylex(yyscan_t yyscanner, parse_driver& drv)