+void EnvironmentPool_initialize(EnvironmentPool* self)
+{
+ self->overflow = NULL;
+ self->freeIndex = 0;
+
+ for(size_t i = 0; i < POOL_SIZE; i++)
+ {
+ self->allocatedFlags[i] = false;
+ self->environments[i].live = false;
+ }
+}
+
+void EnvironmentPool_deinitialize(EnvironmentPool* self)
+{
+ // We can assume if this is being called, none of the Environments are live
+ for(int8_t i = 0; i < POOL_SIZE; i++)
+ {
+ if(self->allocatedFlags[i]) Environment_deinitialize(&(self->environments[i]));
+ }
+
+ EnvironmentPool_destruct(self->overflow);
+}
+
+void EnvironmentPool_destruct(EnvironmentPool* self)
+{
+ if(self == NULL) return;
+ EnvironmentPool_deinitialize(self);
+ free(self);
+}
+
+void EnvironmentPool_GC(EnvironmentPool* self)
+{
+ // Unmark all the environments
+ for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
+ {
+ for(int8_t i = 0; i < POOL_SIZE; i++)
+ {
+ current->environments[i].mark = false;
+ }
+ }
+
+ // Mark live enviroments and environments referenced by live environments
+ for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
+ {
+ for(int8_t i = 0; i < POOL_SIZE; i++)
+ {
+ if(current->environments[i].live)
+ {
+ Environment_mark(&(current->environments[i]));
+ }
+ }
+ }
+
+ // TODO We never free pools until the very end--we could free a pool if two pools are empty
+ for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
+ {
+ for(int8_t i = POOL_SIZE - 1; i >= 0; i--)
+ {
+ if(!current->environments[i].mark && current->allocatedFlags[i])
+ {
+ Environment_deinitialize(&(current->environments[i]));
+ current->allocatedFlags[i] = false;
+ current->freeIndex = i;
+ }
+ }
+ }
+}
+
+Environment* EnvironmentPool_allocate(EnvironmentPool* self)
+{
+ for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
+ {
+ for(; current->freeIndex < POOL_SIZE; current->freeIndex++)
+ {
+ if(!current->allocatedFlags[current->freeIndex])
+ {
+ current->allocatedFlags[current->freeIndex] = true;
+ return &(current->environments[current->freeIndex]);
+ }
+ }
+ }
+
+ EnvironmentPool_GC(self);
+
+ EnvironmentPool* previous;
+ for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
+ {
+ for(; current->freeIndex < POOL_SIZE; current->freeIndex++)
+ {
+ if(!current->allocatedFlags[current->freeIndex])
+ {
+ current->allocatedFlags[current->freeIndex] = true;
+ return &(current->environments[current->freeIndex]);
+ }
+ else
+ {
+ previous = current;
+ }
+ }
+ }
+
+ previous->overflow = EnvironmentPool_construct();
+ return EnvironmentPool_allocate(previous->overflow);
+}
+
+Environment* Environment_construct(EnvironmentPool* environmentPool, Environment* parent)
+{
+ Environment* environment = EnvironmentPool_allocate(environmentPool);
+ Environment_initialize(environment, parent);
+ return environment;
+}
+
+Object integerLiteral(int32_t literal)
+{
+ Object result;
+ result.type = INTEGER;
+ result.instance.integer = literal;
+ return result;
+}
+
+Object stringLiteral(const char* literal)
+{
+ Object result;
+ result.type = STRING_LITERAL;
+ result.instance.string_literal = literal;
+ return result;
+}
+
+{% if 'pow' in builtins %}
+Object builtin$pow$implementation(
+ EnvironmentPool* environmentPool,
+ Environment* parent,
+ size_t argc,
+ Stack* stack,
+ const unsigned long line,
+ jmp_buf parentJump)
+{
+ // Must unload items in reverse order
+ Object exponent = Stack_pop(stack);
+ Object base = Stack_pop(stack);
+
+ assert(base.type == INTEGER);
+ assert(exponent.type == INTEGER);
+
+ Object result;
+ result.type = INTEGER;
+ result.instance.integer = pow(base.instance.integer, exponent.instance.integer);
+ return result;
+}
+
+Object builtin$pow = { CLOSURE, (Instance)(Closure){ NULL, builtin$pow$implementation } };
+{% endif %}
+
+Object builtin$negate$implementation(
+ EnvironmentPool* environmentPool,
+ Environment* parent,
+ size_t argc,
+ Stack* stack,
+ const unsigned long line,
+ jmp_buf parentJump)
+{
+ assert(argc == 1);
+
+ Object argument = Stack_pop(stack);
+
+ assert(argument.type == INTEGER);
+
+ Object result = (Object){
+ INTEGER,
+ (Instance)(int32_t) (-argument.instance.integer)
+ };
+
+ return result;
+}
+Object builtin$negate = { CLOSURE, (Instance)(Closure){ NULL, builtin$negate$implementation } };
+
+{% for op in ['lt', 'gt', 'lte', 'gte', 'eq', 'neq'] %}
+Object builtin${{ op }}$implementation(
+ EnvironmentPool* environmentPool,
+ Environment* parent,
+ size_t argc,
+ Stack* stack,
+ const unsigned long line,
+ jmp_buf parentJump)
+{
+ assert(argc == 2);
+
+ Object right = Stack_pop(stack);
+ Object left = Stack_pop(stack);
+
+ assert(left.type == INTEGER);
+ assert(right.type == INTEGER);
+
+ {% if op == 'lt' %}
+ if(left.instance.integer < right.instance.integer)
+ {% elif op == 'gt' %}
+ if(left.instance.integer > right.instance.integer)
+ {% elif op == 'lte' %}
+ if(left.instance.integer <= right.instance.integer)
+ {% elif op == 'gte' %}
+ if(left.instance.integer >= right.instance.integer)
+ {% elif op == 'eq' %}
+ if(left.instance.integer == right.instance.integer)
+ {% elif op == 'neq' %}
+ if(left.instance.integer != right.instance.integer)
+ {% endif %}
+ {
+ return builtin$true;
+ }
+ else
+ {
+ return builtin$false;
+ }
+}
+Object builtin${{ op }} = { CLOSURE, (Instance)(Closure){ NULL, builtin${{ op }}$implementation } };
+{% endfor %}
+
+Object builtin$concat$implementation(
+ EnvironmentPool* environmentPool,
+ Environment* parent,
+ size_t argc,
+ Stack* stack,
+ const unsigned long line,
+ jmp_buf parentJump)
+{
+ assert(argc == 2);
+
+ Object right = Stack_pop(stack);
+ Object left = Stack_pop(stack);
+
+ switch(left.type) {
+ case STRING_CONCATENATION:
+ case STRING_LITERAL:
+ break;
+
+ default:
+ assert(false);
+ }
+
+ switch(right.type) {
+ case STRING_CONCATENATION:
+ case STRING_LITERAL:
+ break;
+
+ default:
+ assert(false);
+ }
+
+ StringConcatenation* concatenation = malloc(sizeof(StringConcatenation));
+ concatenation->referenceCount = 1;
+ concatenation->left = Object_rereference(left);
+ concatenation->right = Object_rereference(right);
+
+ Object result = { STRING_CONCATENATION, (Instance)concatenation };
+ return result;
+}
+Object builtin$concat = { CLOSURE, (Instance)(Closure){ NULL, builtin$concat$implementation } };
+
+Object builtin$add$implementation(
+ EnvironmentPool* environmentPool,
+ Environment* parent,
+ size_t argc,
+ Stack* stack,
+ const unsigned long line,
+ jmp_buf parentJump)
+{
+ assert(argc == 2);
+
+ Object right = Stack_pop(stack);
+ Object left = Stack_pop(stack);
+
+ assert(left.type == INTEGER);
+ assert(right.type == INTEGER);
+
+ Object result = (Object){
+ INTEGER,
+ (Instance)(int32_t) (left.instance.integer + right.instance.integer)
+ };
+
+ return result;
+}
+Object builtin$add = { CLOSURE, (Instance)(Closure){ NULL, builtin$add$implementation } };
+
+Object builtin$subtract$implementation(
+ EnvironmentPool* environmentPool,
+ Environment* parent,
+ size_t argc,
+ Stack* stack,
+ const unsigned long line,
+ jmp_buf parentJump)
+{
+ assert(argc == 2);
+
+ Object right = Stack_pop(stack);
+ Object left = Stack_pop(stack);
+
+ assert(left.type == INTEGER);
+ assert(right.type == INTEGER);
+
+ Object result = (Object){
+ INTEGER,
+ (Instance)(int32_t) (left.instance.integer - right.instance.integer)
+ };
+
+ return result;
+}
+Object builtin$subtract = { CLOSURE, (Instance)(Closure){ NULL, builtin$subtract$implementation } };
+
+Object builtin$multiply$implementation(
+ EnvironmentPool* environmentPool,
+ Environment* parent,
+ size_t argc,
+ Stack* stack,
+ const unsigned long line,
+ jmp_buf parentJump)
+{
+ assert(argc == 2);
+
+ Object right = Stack_pop(stack);
+ Object left = Stack_pop(stack);
+
+ assert(left.type == INTEGER);
+ assert(right.type == INTEGER);
+
+ Object result = (Object){
+ INTEGER,
+ (Instance)(int32_t) (left.instance.integer * right.instance.integer)
+ };
+
+ return result;
+}
+Object builtin$multiply = { CLOSURE, (Instance)(Closure){ NULL, builtin$multiply$implementation } };
+
+Object builtin$integer_divide$implementation(
+ EnvironmentPool* environmentPool,
+ Environment* parent,
+ size_t argc,
+ Stack* stack,
+ const unsigned long line,
+ jmp_buf parentJump)
+{
+ assert(argc == 2);
+
+ Object right = Stack_pop(stack);
+ Object left = Stack_pop(stack);
+
+ assert(left.type == INTEGER);
+ assert(right.type == INTEGER);
+
+ if(right.instance.integer == 0)
+ {
+ fprintf(stderr, "DivisionByZeroError on line %zu\n", line);
+ longjmp(parentJump, 1);
+ }
+
+ Object result = (Object){
+ INTEGER,
+ (Instance)(int32_t) (left.instance.integer / right.instance.integer)
+ };
+
+ return result;
+}
+Object builtin$integer_divide = { CLOSURE, (Instance)(Closure){ NULL, builtin$integer_divide$implementation } };
+
+Object builtin$modular_divide$implementation(
+ EnvironmentPool* environmentPool,
+ Environment* parent,
+ size_t argc,
+ Stack* stack,
+ const unsigned long line,
+ jmp_buf parentJump)
+{
+ assert(argc == 2);
+
+ Object right = Stack_pop(stack);
+ Object left = Stack_pop(stack);
+
+ assert(left.type == INTEGER);
+ assert(right.type == INTEGER);
+
+ if(right.instance.integer == 0)
+ {
+ fprintf(stderr, "DivisionByZeroError on line %zu\n", line);
+ longjmp(parentJump, 1);
+ }
+
+ Object result = (Object){
+ INTEGER,
+ (Instance)(int32_t) (left.instance.integer % right.instance.integer)
+ };
+
+ return result;
+}
+Object builtin$modular_divide = { CLOSURE, (Instance)(Closure){ NULL, builtin$modular_divide$implementation } };
+
+Object builtin$field$implementation(
+ EnvironmentPool* environmentPool,
+ Environment* parent,
+ size_t argc,
+ Stack* stack,
+ const unsigned long line,
+ jmp_buf parentJump)
+{
+ assert(argc == 2);
+
+ Object right = Stack_pop(stack);
+ Object left = Stack_pop(stack);
+
+ assert(left.type == STRUCTURE);
+ assert(right.type == STRING_LITERAL);
+
+ Object result = (Object){
+ INTEGER,
+ (Instance)(int32_t) (left.instance.integer % right.instance.integer)
+ };
+
+ return result;
+}
+Object builtin$field = { CLOSURE, (Instance)(Closure){ NULL, builtin$field$implementation } };
+
+Object builtin$get$implementation(
+ EnvironmentPool* environmentPool,
+ Environment* parent,
+ size_t argc,
+ Stack* stack,
+ const unsigned long line,
+ jmp_buf parentJump)
+{
+ assert(argc == 2);
+
+ Object right = Stack_pop(stack);
+ Object left = Stack_pop(stack);
+
+ return List_get(&left, right);
+}
+Object builtin$get = { CLOSURE, (Instance)(Closure){ NULL, builtin$get$implementation } };
+