+ Type type;
+ Instance instance;
+};
+
+const Object TRUE = {
+ BOOLEAN,
+ { true }
+};
+
+const Object FALSE = {
+ BOOLEAN,
+ { false }
+};
+
+struct EnvironmentNode
+{
+ const char* key;
+ Object value;
+ EnvironmentNode* next;
+};
+
+struct Environment
+{
+ bool mark;
+ bool live;
+
+ Environment* parent;
+ EnvironmentNode* root;
+};
+
+void Environment_initialize(Environment* self, Environment* parent)
+{
+ self->parent = parent;
+ self->root = NULL;
+
+ // We are currently only ever initializing environments at the beginning of running functions, so
+ // for now at least we can assume that we want it to be live immediately.
+ self->live = true;
+}
+
+void Environment_deinitialize(Environment* self)
+{
+ EnvironmentNode* next;
+ for(EnvironmentNode* node = self->root; node != NULL; node = next)
+ {
+ next = node->next;
+ free(node);
+ }
+}
+
+void Environment_setLive(Environment* self, bool live)
+{
+ self->live = live;
+}
+
+void Environment_mark(Environment* self)
+{
+ if(self == NULL) return;
+ if(self->mark) return; // Prevents infinite recursion in the case of cycles
+
+ self->mark = true;
+
+ Environment_mark(self->parent);
+
+ for(EnvironmentNode* node = self->root; node != NULL; node = node->next)
+ {
+ switch(node->value.type)
+ {
+ case BOOLEAN:
+ case INTEGER:
+ case STRING:
+ break;
+
+ case CLOSURE:
+ Environment_mark(node->value.instance.closure.closed);
+ break;
+
+ default:
+ assert(false);
+ }
+ }
+}
+
+// This need not be thread safe because environments exist on one thread only
+void Environment_set(Environment* self, const char* const key, Object value)
+{
+ EnvironmentNode* node = malloc(sizeof(EnvironmentNode));
+ node->key = key;
+ node->value = value;
+ node->next = self->root;
+ self->root = node;
+}
+
+Object Environment_get(Environment* self, const char* const symbol)
+{
+ for(EnvironmentNode* node = self->root; node != NULL; node = node->next)
+ {
+ // We can compare pointers because pointers are unique in the SYMBOL_LIST
+ if(node->key == symbol)
+ {
+ return node->value;
+ }
+ }
+
+ if(self->parent != NULL)
+ {
+ return Environment_get(self->parent, symbol);
+ }
+
+ // TODO Handle symbol errors
+ assert(false);
+}
+
+# define POOL_SIZE 64
+struct EnvironmentPool
+{
+ int8_t freeIndex;
+ bool allocatedFlags[POOL_SIZE];
+ Environment environments[POOL_SIZE];
+ EnvironmentPool* overflow;
+};
+
+EnvironmentPool* EnvironmentPool_construct();
+void EnvironmentPool_initialize(EnvironmentPool*);
+void EnvironmentPool_deinitialize(EnvironmentPool*);
+void EnvironmentPool_destruct(EnvironmentPool*);
+
+EnvironmentPool* EnvironmentPool_construct()
+{
+ EnvironmentPool* result = malloc(sizeof(EnvironmentPool));
+ EnvironmentPool_initialize(result);