Add a struct to contain groups of mutually recursive definitions.

This commit is contained in:
Danila Fedorin 2020-05-25 22:11:45 -07:00
parent 1905601aaa
commit 3865abfb4d
3 changed files with 14 additions and 10 deletions

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <map>
#include <set> #include <set>
#include "instruction.hpp" #include "instruction.hpp"
#include "llvm_context.hpp" #include "llvm_context.hpp"
@ -70,3 +71,8 @@ struct definition_data {
}; };
using definition_data_ptr = std::unique_ptr<definition_data>; using definition_data_ptr = std::unique_ptr<definition_data>;
struct definition_group {
std::map<std::string, definition_data_ptr> defs_data;
std::map<std::string, definition_defn_ptr> defs_defn;
};

View File

@ -21,8 +21,7 @@ void yy::parser::error(const std::string& msg) {
std::cout << "An error occured: " << msg << std::endl; std::cout << "An error occured: " << msg << std::endl;
} }
extern std::map<std::string, definition_data_ptr> defs_data; extern definition_group global_defs;
extern std::map<std::string, definition_defn_ptr> defs_defn;
void typecheck_program( void typecheck_program(
const std::map<std::string, definition_data_ptr>& defs_data, const std::map<std::string, definition_data_ptr>& defs_data,
@ -180,7 +179,7 @@ int main() {
type_env_ptr env(new type_env); type_env_ptr env(new type_env);
parser.parse(); parser.parse();
for(auto& def_defn : defs_defn) { for(auto& def_defn : global_defs.defs_defn) {
std::cout << def_defn.second->name; std::cout << def_defn.second->name;
for(auto& param : def_defn.second->params) std::cout << " " << param; for(auto& param : def_defn.second->params) std::cout << " " << param;
std::cout << ":" << std::endl; std::cout << ":" << std::endl;
@ -188,9 +187,9 @@ int main() {
def_defn.second->body->print(1, std::cout); def_defn.second->body->print(1, std::cout);
} }
try { try {
typecheck_program(defs_data, defs_defn, mgr, env); typecheck_program(global_defs.defs_data, global_defs.defs_defn, mgr, env);
compile_program(defs_defn); compile_program(global_defs.defs_defn);
gen_llvm(defs_data, defs_defn); gen_llvm(global_defs.defs_data, global_defs.defs_defn);
} catch(unification_error& err) { } catch(unification_error& err) {
std::cout << "failed to unify types: " << std::endl; std::cout << "failed to unify types: " << std::endl;
std::cout << " (1) \033[34m"; std::cout << " (1) \033[34m";

View File

@ -7,8 +7,7 @@
#include "parser.hpp" #include "parser.hpp"
#include "parsed_type.hpp" #include "parsed_type.hpp"
std::map<std::string, definition_data_ptr> defs_data; definition_group global_defs;
std::map<std::string, definition_defn_ptr> defs_defn;
extern yy::parser::symbol_type yylex(); extern yy::parser::symbol_type yylex();
@ -63,8 +62,8 @@ definitions
; ;
definition definition
: defn { auto name = $1->name; defs_defn[name] = std::move($1); } : defn { auto name = $1->name; global_defs.defs_defn[name] = std::move($1); }
| data { auto name = $1->name; defs_data[name] = std::move($1); } | data { auto name = $1->name; global_defs.defs_data[name] = std::move($1); }
; ;
defn defn