Browse Source

Make a few more things classes.

search
Danila Fedorin 1 year ago
parent
commit
7a631b3557
  1. 12
      code/compiler/13/ast.cpp
  2. 15
      code/compiler/13/compiler.cpp
  3. 5
      code/compiler/13/compiler.hpp
  4. 4
      code/compiler/13/definition.cpp
  5. 47
      code/compiler/13/error.hpp
  6. 2
      code/compiler/13/parse_driver.cpp
  7. 20
      code/compiler/13/parse_driver.hpp

12
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<instruction_ptr>& into) const {
type_app* app_type = dynamic_cast<type_app*>(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<instruction_ptr>& into) c
if(app_type && (data = dynamic_cast<type_data*>(app_type->constructor.get()))) {
compile_case<case_strategy_data>(*this, env, data, into);
return;
} else if(app_type && (internal = dynamic_cast<type_internal*>(app_type->constructor.get()))) {
if(internal->name == "Bool") {
compile_case<case_strategy_bool>(*this, env, data, into);
return;
}
} else if (app_type && (base = dynamic_cast<type_base *>(app_type->constructor.get()))) {
if (base->name == "Bool") {
compile_case<case_strategy_bool>(*this, env, data, into);
return;
}
}
throw type_error("attempting unsupported case analysis", of->loc);

15
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<instruction_ptr> 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<instruction_ptr> 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();
}

5
code/compiler/13/compiler.hpp

@ -10,14 +10,13 @@
class compiler {
private:
std::map<binop, std::string> 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();

4
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)));
}

47
code/compiler/13/error.hpp

@ -7,38 +7,43 @@
using maybe_location = std::optional<yy::location>;
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<yy::location> 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);
};

2
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;

20
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) {}
bool run_parse();
yy::location& get_current_location();
file_mgr& get_file_manager() const;
definition_group& get_global_defs() const;
parse_driver(
file_mgr& mgr,
definition_group& defs,
const std::string& file)
: file_name(file), file_m(&mgr), global_defs(&defs) {}
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)

Loading…
Cancel
Save