From c84ff11d0dcded399eefbc4a022e677881b771ff Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 26 May 2020 00:52:54 -0700 Subject: [PATCH] Add typechecking to let/in expressions. --- code/compiler/12/ast.cpp | 26 ++++++++++++++++++++++++++ code/compiler/12/ast.hpp | 14 ++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/code/compiler/12/ast.cpp b/code/compiler/12/ast.cpp index 5bdd00c..e6d8cbb 100644 --- a/code/compiler/12/ast.cpp +++ b/code/compiler/12/ast.cpp @@ -226,6 +226,32 @@ void ast_case::compile(const env_ptr& env, std::vector& into) c } } +void ast_let::print(int indent, std::ostream& to) const { + print_indent(indent, to); + to << "LET: " << std::endl; + in->print(indent + 1, to); +} + +void ast_let::find_free(type_mgr& mgr, type_env_ptr& env, std::set& into) { + this->env = env; + definitions.find_free(mgr, env, into); + std::set all_free; + in->find_free(mgr, definitions.env, all_free); + for(auto& free_var : all_free) { + if(definitions.defs_defn.find(free_var) == definitions.defs_defn.end()) + into.insert(free_var); + } +} + +type_ptr ast_let::typecheck(type_mgr& mgr) { + definitions.typecheck(mgr); + return in->typecheck(mgr); +} + +void ast_let::compile(const env_ptr& env, std::vector& into) const { + throw 0; +} + void pattern_var::print(std::ostream& to) const { to << var; } diff --git a/code/compiler/12/ast.hpp b/code/compiler/12/ast.hpp index 6c66636..68082ab 100644 --- a/code/compiler/12/ast.hpp +++ b/code/compiler/12/ast.hpp @@ -7,6 +7,7 @@ #include "binop.hpp" #include "instruction.hpp" #include "env.hpp" +#include "definition.hpp" struct ast { type_env_ptr env; @@ -120,6 +121,19 @@ struct ast_case : public ast { void compile(const env_ptr& env, std::vector& into) const; }; +struct ast_let : public ast { + definition_group definitions; + ast_ptr in; + + ast_let(ast_ptr i, definition_group g) + : in(std::move(i)), definitions(std::move(g)) {} + + void print(int indent, std::ostream& to) const; + void find_free(type_mgr& mgr, type_env_ptr& env, std::set& into); + type_ptr typecheck(type_mgr& mgr); + void compile(const env_ptr& env, std::vector& into) const; +}; + struct pattern_var : public pattern { std::string var;