From 131102ad6622f41719f7fc609ed1f3cf222d54d4 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 26 Aug 2019 00:11:38 -0700 Subject: [PATCH] Add environment code --- 03/env.cpp | 16 ++++++++++++++++ 03/env.hpp | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 03/env.cpp create mode 100644 03/env.hpp diff --git a/03/env.cpp b/03/env.cpp new file mode 100644 index 0000000..74059a8 --- /dev/null +++ b/03/env.cpp @@ -0,0 +1,16 @@ +#include "env.hpp" + +type_ptr type_env::lookup(const std::string& name) const { + auto it = names.find(name); + if(it != names.end()) return it->second; + if(parent) return parent->lookup(name); + return nullptr; +} + +void type_env::bind(const std::string& name, type_ptr t) { + names[name] = t; +} + +type_env type_env::scope() const { + return type_env(this); +} diff --git a/03/env.hpp b/03/env.hpp new file mode 100644 index 0000000..6470bdd --- /dev/null +++ b/03/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; +};