Fix up compile in compiler blog part 6, and add more text.

This commit is contained in:
Danila Fedorin 2019-10-08 14:09:58 -07:00
parent f49c045516
commit e054cb98cc
2 changed files with 10 additions and 9 deletions

View File

@ -29,7 +29,10 @@ type_ptr ast_lid::typecheck(type_mgr& mgr, const type_env& env) const {
}
void ast_lid::compile(const env_ptr& env, std::vector<instruction_ptr>& into) const {
into.push_back(instruction_ptr(new instruction_pushglobal(id)));
into.push_back(instruction_ptr(
env->has_variable(id) ?
(instruction*) new instruction_push(env->get_offset(id)) :
(instruction*) new instruction_pushglobal(id)));
}
void ast_uid::print(int indent, std::ostream& to) const {
@ -42,10 +45,7 @@ type_ptr ast_uid::typecheck(type_mgr& mgr, const type_env& env) const {
}
void ast_uid::compile(const env_ptr& env, std::vector<instruction_ptr>& into) const {
into.push_back(instruction_ptr(
env->has_variable(id) ?
(instruction*) new instruction_push(env->get_offset(id)) :
(instruction*) new instruction_pushglobal(id)));
into.push_back(instruction_ptr(new instruction_pushglobal(id)));
}
void ast_binop::print(int indent, std::ostream& to) const {
@ -70,8 +70,9 @@ type_ptr ast_binop::typecheck(type_mgr& mgr, const type_env& env) const {
}
void ast_binop::compile(const env_ptr& env, std::vector<instruction_ptr>& into) const {
left->compile(env, into);
right->compile(env, into);
left->compile(env_ptr(new env_offset(1, env)), into);
into.push_back(instruction_ptr(new instruction_pushglobal(op_name(op))));
into.push_back(instruction_ptr(new instruction_mkapp()));
into.push_back(instruction_ptr(new instruction_mkapp()));
@ -95,8 +96,8 @@ type_ptr ast_app::typecheck(type_mgr& mgr, const type_env& env) const {
}
void ast_app::compile(const env_ptr& env, std::vector<instruction_ptr>& into) const {
left->compile(env, into);
right->compile(env, into);
left->compile(env_ptr(new env_offset(1, env)), into);
into.push_back(instruction_ptr(new instruction_mkapp()));
}

View File

@ -11,7 +11,7 @@ struct env {
using env_ptr = std::shared_ptr<env>;
struct env_var {
struct env_var : public env {
std::string name;
env_ptr parent;
@ -22,7 +22,7 @@ struct env_var {
bool has_variable(const std::string& name) const;
};
struct env_offset {
struct env_offset : public env {
int offset;
env_ptr parent;