diff --git a/06/ast.cpp b/06/ast.cpp index 7da9306..549ded0 100644 --- a/06/ast.cpp +++ b/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& 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& 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& 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& 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())); } diff --git a/06/env.hpp b/06/env.hpp index 02d9663..a8fbbec 100644 --- a/06/env.hpp +++ b/06/env.hpp @@ -11,7 +11,7 @@ struct env { using env_ptr = std::shared_ptr; -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;