Implement new ordered typing in compiler series
This commit is contained in:
63
10/main.cpp
63
10/main.cpp
@@ -2,6 +2,7 @@
|
||||
#include <iostream>
|
||||
#include "binop.hpp"
|
||||
#include "definition.hpp"
|
||||
#include "graph.hpp"
|
||||
#include "instruction.hpp"
|
||||
#include "llvm_context.hpp"
|
||||
#include "parser.hpp"
|
||||
@@ -20,12 +21,12 @@ void yy::parser::error(const std::string& msg) {
|
||||
std::cout << "An error occured: " << msg << std::endl;
|
||||
}
|
||||
|
||||
extern std::vector<definition_data_ptr> defs_data;
|
||||
extern std::vector<definition_defn_ptr> defs_defn;
|
||||
extern std::map<std::string, definition_data_ptr> defs_data;
|
||||
extern std::map<std::string, definition_defn_ptr> defs_defn;
|
||||
|
||||
void typecheck_program(
|
||||
const std::vector<definition_data_ptr>& defs_data,
|
||||
const std::vector<definition_defn_ptr>& defs_defn,
|
||||
const std::map<std::string, definition_data_ptr>& defs_data,
|
||||
const std::map<std::string, definition_defn_ptr>& defs_defn,
|
||||
type_mgr& mgr, type_env_ptr& env) {
|
||||
type_ptr int_type = type_ptr(new type_base("Int"));
|
||||
type_ptr binop_type = type_ptr(new type_arr(
|
||||
@@ -38,20 +39,36 @@ void typecheck_program(
|
||||
env->bind("/", binop_type);
|
||||
|
||||
for(auto& def_data : defs_data) {
|
||||
def_data->insert_types(mgr, env);
|
||||
def_data.second->insert_types(mgr, env);
|
||||
}
|
||||
for(auto& def_data : defs_data) {
|
||||
def_data->insert_constructors();
|
||||
def_data.second->insert_constructors();
|
||||
}
|
||||
|
||||
function_graph dependency_graph;
|
||||
|
||||
for(auto& def_defn : defs_defn) {
|
||||
def_defn->find_free(mgr, env);
|
||||
def_defn.second->find_free(mgr, env);
|
||||
dependency_graph.add_function(def_defn.second->name);
|
||||
|
||||
for(auto& dependency : def_defn.second->free_variables) {
|
||||
if(defs_defn.find(dependency) == defs_defn.end())
|
||||
throw 0;
|
||||
dependency_graph.add_edge(def_defn.second->name, dependency);
|
||||
}
|
||||
}
|
||||
for(auto& def_defn : defs_defn) {
|
||||
def_defn->insert_types(mgr);
|
||||
}
|
||||
for(auto& def_defn : defs_defn) {
|
||||
def_defn->typecheck(mgr);
|
||||
|
||||
std::vector<group_ptr> groups = dependency_graph.compute_order();
|
||||
for(auto it = groups.rbegin(); it != groups.rend(); it++) {
|
||||
auto& group = *it;
|
||||
for(auto& def_defnn_name : group->members) {
|
||||
auto& def_defn = defs_defn.find(def_defnn_name)->second;
|
||||
def_defn->insert_types(mgr);
|
||||
}
|
||||
for(auto& def_defnn_name : group->members) {
|
||||
auto& def_defn = defs_defn.find(def_defnn_name)->second;
|
||||
def_defn->typecheck(mgr);
|
||||
}
|
||||
}
|
||||
|
||||
for(auto& pair : env->names) {
|
||||
@@ -61,11 +78,11 @@ void typecheck_program(
|
||||
}
|
||||
}
|
||||
|
||||
void compile_program(const std::vector<definition_defn_ptr>& defs_defn) {
|
||||
void compile_program(const std::map<std::string, definition_defn_ptr>& defs_defn) {
|
||||
for(auto& def_defn : defs_defn) {
|
||||
def_defn->compile();
|
||||
def_defn.second->compile();
|
||||
|
||||
for(auto& instruction : def_defn->instructions) {
|
||||
for(auto& instruction : def_defn.second->instructions) {
|
||||
instruction->print(0, std::cout);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
@@ -130,8 +147,8 @@ void output_llvm(llvm_context& ctx, const std::string& filename) {
|
||||
}
|
||||
|
||||
void gen_llvm(
|
||||
const std::vector<definition_data_ptr>& defs_data,
|
||||
const std::vector<definition_defn_ptr>& defs_defn) {
|
||||
const std::map<std::string, definition_data_ptr>& defs_data,
|
||||
const std::map<std::string, definition_defn_ptr>& defs_defn) {
|
||||
llvm_context ctx;
|
||||
gen_llvm_internal_op(ctx, PLUS);
|
||||
gen_llvm_internal_op(ctx, MINUS);
|
||||
@@ -139,13 +156,13 @@ void gen_llvm(
|
||||
gen_llvm_internal_op(ctx, DIVIDE);
|
||||
|
||||
for(auto& def_data : defs_data) {
|
||||
def_data->generate_llvm(ctx);
|
||||
def_data.second->generate_llvm(ctx);
|
||||
}
|
||||
for(auto& def_defn : defs_defn) {
|
||||
def_defn->declare_llvm(ctx);
|
||||
def_defn.second->declare_llvm(ctx);
|
||||
}
|
||||
for(auto& def_defn : defs_defn) {
|
||||
def_defn->generate_llvm(ctx);
|
||||
def_defn.second->generate_llvm(ctx);
|
||||
}
|
||||
|
||||
ctx.module.print(llvm::outs(), nullptr);
|
||||
@@ -159,11 +176,11 @@ int main() {
|
||||
|
||||
parser.parse();
|
||||
for(auto& def_defn : defs_defn) {
|
||||
std::cout << def_defn->name;
|
||||
for(auto& param : def_defn->params) std::cout << " " << param;
|
||||
std::cout << def_defn.second->name;
|
||||
for(auto& param : def_defn.second->params) std::cout << " " << param;
|
||||
std::cout << ":" << std::endl;
|
||||
|
||||
def_defn->body->print(1, std::cout);
|
||||
def_defn.second->body->print(1, std::cout);
|
||||
}
|
||||
try {
|
||||
typecheck_program(defs_data, defs_defn, mgr, env);
|
||||
|
||||
Reference in New Issue
Block a user