Add typechecking to let/in expressions.

This commit is contained in:
Danila Fedorin 2020-05-26 00:52:54 -07:00
parent e966e74487
commit c84ff11d0d
2 changed files with 40 additions and 0 deletions

View File

@ -226,6 +226,32 @@ void ast_case::compile(const env_ptr& env, std::vector<instruction_ptr>& 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<std::string>& into) {
this->env = env;
definitions.find_free(mgr, env, into);
std::set<std::string> 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<instruction_ptr>& into) const {
throw 0;
}
void pattern_var::print(std::ostream& to) const { void pattern_var::print(std::ostream& to) const {
to << var; to << var;
} }

View File

@ -7,6 +7,7 @@
#include "binop.hpp" #include "binop.hpp"
#include "instruction.hpp" #include "instruction.hpp"
#include "env.hpp" #include "env.hpp"
#include "definition.hpp"
struct ast { struct ast {
type_env_ptr env; type_env_ptr env;
@ -120,6 +121,19 @@ struct ast_case : public ast {
void compile(const env_ptr& env, std::vector<instruction_ptr>& into) const; void compile(const env_ptr& env, std::vector<instruction_ptr>& 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<std::string>& into);
type_ptr typecheck(type_mgr& mgr);
void compile(const env_ptr& env, std::vector<instruction_ptr>& into) const;
};
struct pattern_var : public pattern { struct pattern_var : public pattern {
std::string var; std::string var;