diff --git a/06/ast.cpp b/06/ast.cpp index 8c5c3f9..00d92a3 100644 --- a/06/ast.cpp +++ b/06/ast.cpp @@ -176,7 +176,7 @@ void ast_case::resolve(const type_mgr& mgr) const { } void ast_case::compile(const env_ptr& env, std::vector& into) const { - type_data* type = dynamic_cast(node_type.get()); + type_data* type = dynamic_cast(of->node_type.get()); of->compile(env, into); into.push_back(instruction_ptr(new instruction_eval())); @@ -201,9 +201,15 @@ void ast_case::compile(const env_ptr& env, std::vector& into) c } jump_instruction->branches.push_back(std::move(branch_instructions)); } else if((cpat = dynamic_cast(branch->pat.get()))) { + env_ptr new_env = env; + for(auto it = cpat->params.rbegin(); it != cpat->params.rend(); it++) { + new_env = env_ptr(new env_var(*it, new_env)); + } + branch_instructions.push_back(instruction_ptr(new instruction_split())); - branch->expr->compile(env_ptr(new env_offset(cpat->params.size(), env)), - branch_instructions); + branch->expr->compile(new_env, branch_instructions); + branch_instructions.push_back(instruction_ptr(new instruction_slide( + cpat->params.size()))); int new_tag = type->constructors[cpat->constr].tag; if(jump_instruction->tag_mappings.find(new_tag) != diff --git a/06/ast.hpp b/06/ast.hpp index 21e1f61..c536d17 100644 --- a/06/ast.hpp +++ b/06/ast.hpp @@ -59,6 +59,7 @@ struct definition { virtual void typecheck_first(type_mgr& mgr, type_env& env) = 0; virtual void typecheck_second(type_mgr& mgr, const type_env& env) const = 0; virtual void resolve(const type_mgr& mgr) = 0; + virtual void compile() = 0; }; using definition_ptr = std::unique_ptr; @@ -168,6 +169,8 @@ struct definition_defn : public definition { type_ptr return_type; std::vector param_types; + std::vector instructions; + definition_defn(std::string n, std::vector p, ast_ptr b) : name(std::move(n)), params(std::move(p)), body(std::move(b)) { @@ -176,6 +179,7 @@ struct definition_defn : public definition { void typecheck_first(type_mgr& mgr, type_env& env); void typecheck_second(type_mgr& mgr, const type_env& env) const; void resolve(const type_mgr& mgr); + void compile(); }; struct definition_data : public definition { @@ -188,4 +192,5 @@ struct definition_data : public definition { void typecheck_first(type_mgr& mgr, type_env& env); void typecheck_second(type_mgr& mgr, const type_env& env) const; void resolve(const type_mgr& mgr); + void compile(); }; diff --git a/06/definition.cpp b/06/definition.cpp index 5047055..34c6f66 100644 --- a/06/definition.cpp +++ b/06/definition.cpp @@ -41,6 +41,15 @@ void definition_defn::resolve(const type_mgr& mgr) { } } +void definition_defn::compile() { + env_ptr new_env = env_ptr(new env_offset(0, nullptr)); + for(auto it = params.rbegin(); it != params.rend(); it++) { + new_env = env_ptr(new env_var(*it, new_env)); + } + body->compile(new_env, instructions); + instructions.push_back(instruction_ptr(new instruction_update(params.size()))); +} + void definition_data::typecheck_first(type_mgr& mgr, type_env& env) { type_data* this_type = new type_data(name); type_ptr return_type = type_ptr(this_type); @@ -67,3 +76,6 @@ void definition_data::resolve(const type_mgr& mgr) { // Nothing } +void definition_data::compile() { + +} diff --git a/06/instruction.cpp b/06/instruction.cpp index b540ff6..0272e20 100644 --- a/06/instruction.cpp +++ b/06/instruction.cpp @@ -21,12 +21,12 @@ void instruction_push::print(int indent, std::ostream& to) const { void instruction_mkapp::print(int indent, std::ostream& to) const { print_indent(indent, to); - to << "Push()" << std::endl; + to << "MkApp()" << std::endl; } void instruction_update::print(int indent, std::ostream& to) const { print_indent(indent, to); - to << "Offset(" << offset << ")" << std::endl; + to << "Update(" << offset << ")" << std::endl; } void instruction_pack::print(int indent, std::ostream& to) const { @@ -48,6 +48,8 @@ void instruction_jump::print(int indent, std::ostream& to) const { } to << std::endl; } + print_indent(indent, to); + to << ")" << std::endl; } void instruction_slide::print(int indent, std::ostream& to) const { diff --git a/06/main.cpp b/06/main.cpp index c5313fd..ffbd4e5 100644 --- a/06/main.cpp +++ b/06/main.cpp @@ -42,6 +42,19 @@ void typecheck_program( } } +void compile_program(const std::vector& prog) { + for(auto& def : prog) { + def->compile(); + + definition_defn* defn = dynamic_cast(def.get()); + if(!defn) continue; + for(auto& instruction : defn->instructions) { + instruction->print(0, std::cout); + } + std::cout << std::endl; + } +} + int main() { yy::parser parser; type_mgr mgr; @@ -60,6 +73,7 @@ int main() { } try { typecheck_program(program, mgr, env); + compile_program(program); } catch(unification_error& err) { std::cout << "failed to unify types: " << std::endl; std::cout << " (1) \033[34m";