Fix up compile in compiler blog part 6, and add more text.
This commit is contained in:
parent
f49c045516
commit
e054cb98cc
15
06/ast.cpp
15
06/ast.cpp
|
@ -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 {
|
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 {
|
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 {
|
void ast_uid::compile(const env_ptr& env, std::vector<instruction_ptr>& into) const {
|
||||||
into.push_back(instruction_ptr(
|
into.push_back(instruction_ptr(new instruction_pushglobal(id)));
|
||||||
env->has_variable(id) ?
|
|
||||||
(instruction*) new instruction_push(env->get_offset(id)) :
|
|
||||||
(instruction*) new instruction_pushglobal(id)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_binop::print(int indent, std::ostream& to) const {
|
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 {
|
void ast_binop::compile(const env_ptr& env, std::vector<instruction_ptr>& into) const {
|
||||||
left->compile(env, into);
|
|
||||||
right->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_pushglobal(op_name(op))));
|
||||||
into.push_back(instruction_ptr(new instruction_mkapp()));
|
into.push_back(instruction_ptr(new instruction_mkapp()));
|
||||||
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 {
|
void ast_app::compile(const env_ptr& env, std::vector<instruction_ptr>& into) const {
|
||||||
left->compile(env, into);
|
|
||||||
right->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()));
|
into.push_back(instruction_ptr(new instruction_mkapp()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ struct env {
|
||||||
|
|
||||||
using env_ptr = std::shared_ptr<env>;
|
using env_ptr = std::shared_ptr<env>;
|
||||||
|
|
||||||
struct env_var {
|
struct env_var : public env {
|
||||||
std::string name;
|
std::string name;
|
||||||
env_ptr parent;
|
env_ptr parent;
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ struct env_var {
|
||||||
bool has_variable(const std::string& name) const;
|
bool has_variable(const std::string& name) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct env_offset {
|
struct env_offset : public env {
|
||||||
int offset;
|
int offset;
|
||||||
env_ptr parent;
|
env_ptr parent;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user