7 /* Some terminology used in function names:
8 * - initialize: These functions take a pointer and potentially some other arguments, and use those
9 * to initialize the value pointed to by self. Initialize functions DO NOT allocate the function,
10 * so they can be used to initialize stack-allocated variables.
11 * - construct: This allocates a value for a pointer, initializes it, and returns it. This is for
12 * heap-allocated values. It may be as simple as allocating the memory, calling an initialize, and
14 * - deinitialize: These functions dereference or free any objects pointed to by the self pointer's
15 * value, but they don't actually free the self pointer. This is useful for stack-allocated objects
16 * which point to heap-allocated objects.
17 * - destruct: This dereferences or frees memory pointed to by the self argument, and all the
18 * pointers on the self argument.
21 {% for standard_library in standard_libraries %}
22 #include <{{standard_library}}>
26 typedef enum Type Type;
28 typedef union Instance Instance;
30 typedef struct Object Object;
31 struct EnvironmentNode;
32 typedef struct EnvironmentNode EnvironmentNode;
34 typedef struct Environment Environment;
35 struct EnvironmentPool;
36 typedef struct EnvironmentPool EnvironmentPool;
38 typedef struct Stack Stack;
40 const char* const STRING_LITERAL_LIST[] = {
41 {% for string_literal in string_literal_list %}
42 "{{ string_literal }}",
46 const char* const SYMBOL_LIST[] = {
47 {% for symbol in symbol_list %}
65 typedef struct Closure Closure;
69 Object (*call)(EnvironmentPool*, Environment*, size_t, Stack*);
73 typedef struct List List;
81 struct StringConcatenation;
82 typedef struct StringConcatenation StringConcatenation;
85 typedef struct Structure Structure;
88 size_t reference_count;
90 const char** symbol_list;
100 StringConcatenation* string_concatenation;
101 const char* string_literal;
102 Structure* structure;
111 const Object builtin$true = { BOOLEAN, (Instance)(bool){ true } };
112 const Object builtin$false = { BOOLEAN, (Instance)(bool){ false } };
113 const Object builtin$nil = { VOID, { 0 } };
115 struct StringConcatenation
117 size_t referenceCount;
122 Object List_construct(size_t allocate)
124 Object* items = malloc(sizeof(Object) * allocate);
125 Object result = { LIST, (Instance)(List){ allocate, 0, items } };
129 void List_append(Object* list, Object item)
131 assert(list->type == LIST);
133 if(list->instance.list.allocated == list->instance.list.length)
135 list->instance.list.allocated *= 2;
136 list->instance.list.items = realloc(
137 list->instance.list.items,
138 sizeof(Object) * list->instance.list.allocated
142 list->instance.list.items[list->instance.list.length] = item;
143 list->instance.list.length++;
146 Object List_get(Object* list, Object index)
148 assert(list->type == LIST);
149 assert(index.type == INTEGER);
151 return list->instance.list.items[index.instance.integer];
160 void Stack_initialize(Stack* self)
165 void Stack_push(Stack* self, Object item)
167 assert(self->length < 256);
168 self->items[self->length] = item;
172 Object Stack_pop(Stack* self)
174 assert(self->length > 0);
176 return self->items[self->length];
179 Object Object_rereference(Object self)
190 case STRING_CONCATENATION:
191 self.instance.string_concatenation->referenceCount++;
195 self.instance.structure->reference_count++;
203 Object Structure_construct(size_t length, const char** symbol_list, Object* value_list)
205 Structure* structure = malloc(sizeof(Structure));
206 structure->reference_count = 1;
207 structure->length = length;
208 structure->symbol_list = malloc(sizeof(const char*) * length);
209 structure->value_list = malloc(sizeof(Object) * length);
211 // TODO Don't allow assignment of mutable structures, as this screws up reference counting
212 for(size_t i = 0; i < length; i++)
214 structure->symbol_list[i] = symbol_list[i];
215 structure->value_list[i] = Object_rereference(value_list[i]);
218 Object result = { STRUCTURE, (Instance)structure };
223 Object Structure_get(Object* self, const char* symbol)
225 assert(self->type == STRUCTURE);
227 for(size_t i = 0; i < self->instance.structure->length; i++)
229 if(self->instance.structure->symbol_list[i] == symbol)
231 return self->instance.structure->value_list[i];
238 struct EnvironmentNode
242 EnvironmentNode* next;
251 EnvironmentNode* root;
254 void Environment_initialize(Environment* self, Environment* parent)
256 self->parent = parent;
259 // We are currently only ever initializing environments at the beginning of running functions, so
260 // for now at least we can assume that we want it to be live immediately.
264 void Object_deinitialize(Object* self)
280 for(size_t i = 0; i < self->instance.list.length; i++) {
281 Object_deinitialize(&(self->instance.list.items[i]));
284 free(self->instance.list.items);
287 case STRING_CONCATENATION:
288 self->instance.string_concatenation->referenceCount--;
290 if(self->instance.string_concatenation->referenceCount == 0)
292 Object_deinitialize(&(self->instance.string_concatenation->left));
293 Object_deinitialize(&(self->instance.string_concatenation->right));
294 free(self->instance.string_concatenation);
299 self->instance.structure->reference_count--;
301 if(self->instance.structure->reference_count == 0)
303 for(size_t i = 0; i < self->instance.structure->length; i++)
305 Object_deinitialize(&(self->instance.structure->value_list[i]));
307 free(self->instance.structure->symbol_list);
308 free(self->instance.structure->value_list);
309 free(self->instance.structure);
318 void Environment_deinitialize(Environment* self)
320 EnvironmentNode* next;
321 for(EnvironmentNode* node = self->root; node != NULL; node = next)
324 Object_deinitialize(&(node->value));
329 void Environment_setLive(Environment* self, bool live)
334 void Environment_mark(Environment* self)
336 if(self == NULL) return;
337 if(self->mark) return; // Prevents infinite recursion in the case of cycles
341 Environment_mark(self->parent);
343 for(EnvironmentNode* node = self->root; node != NULL; node = node->next)
345 switch(node->value.type)
354 Environment_mark(node->value.instance.closure.closed);
363 // This need not be thread safe because environments exist on one thread only
364 void Environment_set(Environment* self, const char* const key, Object value)
366 EnvironmentNode* node = malloc(sizeof(EnvironmentNode));
369 node->next = self->root;
373 Object Environment_get(Environment* self, const char* const symbol)
375 for(EnvironmentNode* node = self->root; node != NULL; node = node->next)
377 // We can compare pointers because pointers are unique in the SYMBOL_LIST
378 if(node->key == symbol)
384 if(self->parent != NULL)
386 return Environment_get(self->parent, symbol);
389 // TODO Handle symbol errors
393 # define POOL_SIZE 64
394 struct EnvironmentPool
397 bool allocatedFlags[POOL_SIZE];
398 Environment environments[POOL_SIZE];
399 EnvironmentPool* overflow;
402 EnvironmentPool* EnvironmentPool_construct();
403 void EnvironmentPool_initialize(EnvironmentPool*);
404 void EnvironmentPool_deinitialize(EnvironmentPool*);
405 void EnvironmentPool_destruct(EnvironmentPool*);
407 EnvironmentPool* EnvironmentPool_construct()
409 EnvironmentPool* result = malloc(sizeof(EnvironmentPool));
410 EnvironmentPool_initialize(result);
414 void EnvironmentPool_initialize(EnvironmentPool* self)
416 self->overflow = NULL;
419 for(size_t i = 0; i < POOL_SIZE; i++)
421 self->allocatedFlags[i] = false;
422 self->environments[i].live = false;
426 void EnvironmentPool_deinitialize(EnvironmentPool* self)
428 // We can assume if this is being called, none of the Environments are live
429 for(int8_t i = 0; i < POOL_SIZE; i++)
431 if(self->allocatedFlags[i]) Environment_deinitialize(&(self->environments[i]));
434 EnvironmentPool_destruct(self->overflow);
437 void EnvironmentPool_destruct(EnvironmentPool* self)
439 if(self == NULL) return;
440 EnvironmentPool_deinitialize(self);
444 void EnvironmentPool_GC(EnvironmentPool* self)
446 // Unmark all the environments
447 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
449 for(int8_t i = 0; i < POOL_SIZE; i++)
451 current->environments[i].mark = false;
455 // Mark live enviroments and environments referenced by live environments
456 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
458 for(int8_t i = 0; i < POOL_SIZE; i++)
460 if(current->environments[i].live)
462 Environment_mark(&(current->environments[i]));
467 // TODO We never free pools until the very end--we could free a pool if two pools are empty
468 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
470 for(int8_t i = POOL_SIZE - 1; i >= 0; i--)
472 if(!current->environments[i].mark && current->allocatedFlags[i])
474 Environment_deinitialize(&(current->environments[i]));
475 current->allocatedFlags[i] = false;
476 current->freeIndex = i;
482 Environment* EnvironmentPool_allocate(EnvironmentPool* self)
484 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
486 for(; current->freeIndex < POOL_SIZE; current->freeIndex++)
488 if(!current->allocatedFlags[current->freeIndex])
490 current->allocatedFlags[current->freeIndex] = true;
491 return &(current->environments[current->freeIndex]);
496 EnvironmentPool_GC(self);
498 EnvironmentPool* previous;
499 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
501 for(; current->freeIndex < POOL_SIZE; current->freeIndex++)
503 if(!current->allocatedFlags[current->freeIndex])
505 current->allocatedFlags[current->freeIndex] = true;
506 return &(current->environments[current->freeIndex]);
515 previous->overflow = EnvironmentPool_construct();
516 return EnvironmentPool_allocate(previous->overflow);
519 Object integerLiteral(int32_t literal)
522 result.type = INTEGER;
523 result.instance.integer = literal;
527 Object stringLiteral(const char* literal)
530 result.type = STRING_LITERAL;
531 result.instance.string_literal = literal;
535 // TODO Make this conditionally added
536 Object operator$negate(Object input)
538 assert(input.type == INTEGER);
541 result.type = INTEGER;
542 result.instance.integer = -input.instance.integer;
546 // TODO Make this conditionally added
547 Object operator$concatenate(Object left, Object right)
550 case STRING_CONCATENATION:
559 case STRING_CONCATENATION:
567 StringConcatenation* concatenation = malloc(sizeof(StringConcatenation));
568 concatenation->referenceCount = 1;
569 concatenation->left = Object_rereference(left);
570 concatenation->right = Object_rereference(right);
572 Object result = { STRING_CONCATENATION, (Instance)concatenation };
576 {% for id in infix_declarations %}
577 Object operator${{ id.name }}(Object left, Object right)
579 assert(left.type == {{ id.in_type.upper() }});
580 assert(right.type == {{ id.in_type.upper() }});
583 result.type = {{ id.out_type.upper() }};
584 result.instance.{{ id.out_type.lower() }} = left.instance.{{ id.in_type.lower() }} {{ id.operator }} right.instance.{{ id.in_type.lower() }};
589 {% if 'pow' in builtins %}
590 Object builtin$pow$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack)
592 // Must unload items in reverse order
593 Object exponent = Stack_pop(stack);
594 Object base = Stack_pop(stack);
596 assert(base.type == INTEGER);
597 assert(exponent.type == INTEGER);
600 result.type = INTEGER;
601 result.instance.integer = pow(base.instance.integer, exponent.instance.integer);
605 Object builtin$pow = { CLOSURE, (Instance)(Closure){ NULL, builtin$pow$implementation } };
608 {% if 'print' in builtins %}
609 Object builtin$print$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack)
612 Stack_initialize(&reverse_stack);
614 for(size_t i = 0; i < argc; i++)
616 Stack_push(&reverse_stack, Stack_pop(stack));
619 while(reverse_stack.length > 0)
621 Object output = Stack_pop(&reverse_stack);
625 fputs(output.instance.boolean ? "true" : "false", stdout);
629 // TODO Print something better
634 printf("%" PRId32, output.instance.integer);
637 case STRING_CONCATENATION:
638 Stack_push(stack, output.instance.string_concatenation->left);
639 builtin$print$implementation(NULL, NULL, 1, stack);
640 Stack_push(stack, output.instance.string_concatenation->right);
641 builtin$print$implementation(NULL, NULL, 1, stack);
645 // Using fwrite instead of printf to handle size_t length
646 printf("%s", output.instance.string_literal);
656 Object_deinitialize(&output);
659 // TODO Return something better
660 return builtin$false;
663 Object builtin$print = { CLOSURE, (Instance)(Closure){ NULL, builtin$print$implementation } };
665 {% for function_definition in function_definition_list %}
666 {{ function_definition }}
669 int main(int argc, char** argv)
671 EnvironmentPool* environmentPool = EnvironmentPool_construct();
672 Environment* environment = EnvironmentPool_allocate(environmentPool);
673 Environment_initialize(environment, NULL);
676 Stack* stack = &stackMemory;
677 Stack_initialize(stack);
679 // TODO Use the symbol from SYMBOL_LIST
680 {% for builtin in builtins %}
681 Environment_set(environment, "{{ builtin }}", builtin${{ builtin }});
684 {% for statement in statements %}
688 Environment_setLive(environment, false);
689 EnvironmentPool_destruct(environmentPool);