From bcaa67cc7a0080792a2d5efec72a1967e94e22e6 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 1 Oct 2019 14:34:38 -0700 Subject: [PATCH] Begin implementation of new environment --- code/compiler/06/CMakeLists.txt | 1 + code/compiler/06/ast.hpp | 12 ++-- code/compiler/06/binop.hpp | 9 +++ code/compiler/06/env.hpp | 39 ++++++---- code/compiler/06/instruction.hpp | 83 ++++++++++++++++++++++ code/compiler/06/{env.cpp => type_env.cpp} | 2 +- code/compiler/06/type_env.hpp | 16 +++++ 7 files changed, 142 insertions(+), 20 deletions(-) create mode 100644 code/compiler/06/binop.hpp create mode 100644 code/compiler/06/instruction.hpp rename code/compiler/06/{env.cpp => type_env.cpp} (93%) create mode 100644 code/compiler/06/type_env.hpp diff --git a/code/compiler/06/CMakeLists.txt b/code/compiler/06/CMakeLists.txt index 9d2d571..9109901 100644 --- a/code/compiler/06/CMakeLists.txt +++ b/code/compiler/06/CMakeLists.txt @@ -14,6 +14,7 @@ add_flex_bison_dependency(scanner parser) add_executable(compiler ast.cpp ast.hpp definition.cpp + type_env.cpp type_env.hpp env.cpp env.hpp type.cpp type.hpp error.cpp error.hpp diff --git a/code/compiler/06/ast.hpp b/code/compiler/06/ast.hpp index fcfed19..232b39d 100644 --- a/code/compiler/06/ast.hpp +++ b/code/compiler/06/ast.hpp @@ -2,6 +2,9 @@ #include #include #include "type.hpp" +#include "type_env.hpp" +#include "binop.hpp" +#include "instruction.hpp" #include "env.hpp" struct ast { @@ -9,6 +12,8 @@ struct ast { virtual void print(int indent, std::ostream& to) const = 0; virtual type_ptr typecheck(type_mgr& mgr, const type_env& env) const = 0; + virtual void compile(const env_ptr env, + std::vector& into) const; }; using ast_ptr = std::unique_ptr; @@ -51,13 +56,6 @@ struct definition { using definition_ptr = std::unique_ptr; -enum binop { - PLUS, - MINUS, - TIMES, - DIVIDE -}; - struct ast_int : public ast { int value; diff --git a/code/compiler/06/binop.hpp b/code/compiler/06/binop.hpp new file mode 100644 index 0000000..7c1382a --- /dev/null +++ b/code/compiler/06/binop.hpp @@ -0,0 +1,9 @@ +#pragma once + +enum binop { + PLUS, + MINUS, + TIMES, + DIVIDE +}; + diff --git a/code/compiler/06/env.hpp b/code/compiler/06/env.hpp index 6470bdd..718ddb9 100644 --- a/code/compiler/06/env.hpp +++ b/code/compiler/06/env.hpp @@ -1,16 +1,31 @@ #pragma once -#include -#include "type.hpp" +#include +#include -struct type_env { - std::map names; - type_env const* parent = nullptr; +struct env { + virtual ~env() = default; - type_env(type_env const* p) - : parent(p) {} - type_env() : type_env(nullptr) {} - - type_ptr lookup(const std::string& name) const; - void bind(const std::string& name, type_ptr t); - type_env scope() const; + virtual int get_offset(const std::string& name) const = 0; +}; + +using env_ptr = std::shared_ptr; + +struct env_var { + std::string name; + env_ptr parent; + + env_var(std::string& n, env_ptr p) + : name(std::move(n)), parent(std::move(p)) {} + + virtual int get_offset(const std::string& name) const; +}; + +struct env_offset { + int offset; + env_ptr parent; + + env_offset(int o, env_ptr p) + : offset(o), parent(std::move(p)) {} + + virtual int get_offset(const std::string& name) const; }; diff --git a/code/compiler/06/instruction.hpp b/code/compiler/06/instruction.hpp new file mode 100644 index 0000000..8d846e0 --- /dev/null +++ b/code/compiler/06/instruction.hpp @@ -0,0 +1,83 @@ +#pragma once +#include +#include +#include "binop.hpp" + +struct instruction { + virtual ~instruction() = default; +}; + +using instruction_ptr = std::unique_ptr; + +struct instruction_pushint : public instruction { + int value; + + instruction_pushint(int v) + : value(v) {} +}; + +struct instruction_pushglobal : public instruction { + std::string name; + + instruction_pushglobal(std::string n) + : name(std::move(n)) {} +}; + +struct instruction_push : public instruction { + int offset; + + instruction_push(int o) + : offset(o) {} +}; + +struct instruction_mkapp : public instruction { + +}; + +struct instruction_update : public instruction { + int offset; + + instruction_update(int o) + : offset(o) {} +}; + +struct instruction_pack : public instruction { + int tag; + int size; + + instruction_pack(int t, int s) + : tag(t), size(s) {} +}; + +struct instruction_split : public instruction { + +}; + +struct instruction_slide : public instruction { + int offset; + + instruction_slide(int o) + : offset(o) {} +}; + +struct instruction_binop : public instruction { + binop op; + + instruction_binop(binop o) + : op(o) {} +}; + +struct instruction_eval : public instruction { + +}; + +struct instruction_alloc : public instruction { + int amount; + + instruction_alloc(int a) + : amount(a) {} +}; + +struct instruction_unwind : public instruction { + +}; diff --git a/code/compiler/06/env.cpp b/code/compiler/06/type_env.cpp similarity index 93% rename from code/compiler/06/env.cpp rename to code/compiler/06/type_env.cpp index 74059a8..c11a759 100644 --- a/code/compiler/06/env.cpp +++ b/code/compiler/06/type_env.cpp @@ -1,4 +1,4 @@ -#include "env.hpp" +#include "type_env.hpp" type_ptr type_env::lookup(const std::string& name) const { auto it = names.find(name); diff --git a/code/compiler/06/type_env.hpp b/code/compiler/06/type_env.hpp new file mode 100644 index 0000000..6470bdd --- /dev/null +++ b/code/compiler/06/type_env.hpp @@ -0,0 +1,16 @@ +#pragma once +#include +#include "type.hpp" + +struct type_env { + std::map names; + type_env const* parent = nullptr; + + type_env(type_env const* p) + : parent(p) {} + type_env() : type_env(nullptr) {} + + type_ptr lookup(const std::string& name) const; + void bind(const std::string& name, type_ptr t); + type_env scope() const; +};