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 const char* const STRING_LITERAL_LIST[] = {
39 {% for string_literal in string_literal_list %}
40 "{{ string_literal }}",
44 const char* const SYMBOL_LIST[] = {
45 {% for symbol in symbol_list %}
59 typedef struct Closure Closure;
63 Object (*call)(EnvironmentPool*, Environment*, size_t, Object*);
85 const Object FALSE = {
90 struct EnvironmentNode
94 EnvironmentNode* next;
103 EnvironmentNode* root;
106 void Environment_initialize(Environment* self, Environment* parent)
108 self->parent = parent;
111 // We are currently only ever initializing environments at the beginning of running functions, so
112 // for now at least we can assume that we want it to be live immediately.
116 void Environment_deinitialize(Environment* self)
118 EnvironmentNode* next;
119 for(EnvironmentNode* node = self->root; node != NULL; node = next)
126 void Environment_setLive(Environment* self, bool live)
131 void Environment_mark(Environment* self)
133 if(self == NULL) return;
134 if(self->mark) return; // Prevents infinite recursion in the case of cycles
138 Environment_mark(self->parent);
140 for(EnvironmentNode* node = self->root; node != NULL; node = node->next)
142 switch(node->value.type)
150 Environment_mark(node->value.instance.closure.closed);
159 // This need not be thread safe because environments exist on one thread only
160 void Environment_set(Environment* self, const char* const key, Object value)
162 EnvironmentNode* node = malloc(sizeof(EnvironmentNode));
165 node->next = self->root;
169 Object Environment_get(Environment* self, const char* const symbol)
171 for(EnvironmentNode* node = self->root; node != NULL; node = node->next)
173 // We can compare pointers because pointers are unique in the SYMBOL_LIST
174 if(node->key == symbol)
180 if(self->parent != NULL)
182 return Environment_get(self->parent, symbol);
185 // TODO Handle symbol errors
189 # define POOL_SIZE 64
190 struct EnvironmentPool
193 bool allocatedFlags[POOL_SIZE];
194 Environment environments[POOL_SIZE];
195 EnvironmentPool* overflow;
198 EnvironmentPool* EnvironmentPool_construct();
199 void EnvironmentPool_initialize(EnvironmentPool*);
200 void EnvironmentPool_deinitialize(EnvironmentPool*);
201 void EnvironmentPool_destruct(EnvironmentPool*);
203 EnvironmentPool* EnvironmentPool_construct()
205 EnvironmentPool* result = malloc(sizeof(EnvironmentPool));
206 EnvironmentPool_initialize(result);
210 void EnvironmentPool_initialize(EnvironmentPool* self)
212 self->overflow = NULL;
215 for(size_t i = 0; i < POOL_SIZE; i++)
217 self->allocatedFlags[i] = false;
218 self->environments[i].live = false;
222 void EnvironmentPool_deinitialize(EnvironmentPool* self)
224 // We can assume if this is being called, none of the Environments are live
225 for(int8_t i = 0; i < POOL_SIZE; i++)
227 if(self->allocatedFlags[i]) Environment_deinitialize(&(self->environments[i]));
230 EnvironmentPool_destruct(self->overflow);
233 void EnvironmentPool_destruct(EnvironmentPool* self)
235 if(self == NULL) return;
236 EnvironmentPool_deinitialize(self);
240 void EnvironmentPool_GC(EnvironmentPool* self)
242 // Unmark all the environments
243 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
245 for(int8_t i = 0; i < POOL_SIZE; i++)
247 current->environments[i].mark = false;
251 // Mark live enviroments and environments referenced by live environments
252 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
254 for(int8_t i = 0; i < POOL_SIZE; i++)
256 if(current->environments[i].live)
258 Environment_mark(&(current->environments[i]));
263 // TODO We never free pools until the very end--we could free a pool if two pools are empty
264 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
266 for(int8_t i = POOL_SIZE - 1; i >= 0; i--)
268 if(!current->environments[i].mark && current->allocatedFlags[i])
270 Environment_deinitialize(&(current->environments[i]));
271 current->allocatedFlags[i] = false;
272 current->freeIndex = i;
278 Environment* EnvironmentPool_allocate(EnvironmentPool* self)
280 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
282 for(; current->freeIndex < POOL_SIZE; current->freeIndex++)
284 if(!current->allocatedFlags[current->freeIndex])
286 current->allocatedFlags[current->freeIndex] = true;
287 return &(current->environments[current->freeIndex]);
292 EnvironmentPool_GC(self);
294 EnvironmentPool* previous;
295 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
297 for(; current->freeIndex < POOL_SIZE; current->freeIndex++)
299 if(!current->allocatedFlags[current->freeIndex])
301 current->allocatedFlags[current->freeIndex] = true;
302 return &(current->environments[current->freeIndex]);
311 previous->overflow = EnvironmentPool_construct();
312 return EnvironmentPool_allocate(previous->overflow);
315 Object integerLiteral(int32_t literal)
318 result.type = INTEGER;
319 result.instance.integer = literal;
323 Object stringLiteral(const char* literal)
326 result.type = STRING;
327 result.instance.string = literal;
331 // TODO Make this conditionally added
332 Object operator$negate(Object input)
334 assert(input.type == INTEGER);
337 result.type = INTEGER;
338 result.instance.integer = -input.instance.integer;
342 {% for id in infix_declarations %}
343 Object operator${{ id.name }}(Object left, Object right)
345 assert(left.type == {{ id.in_type.upper() }});
346 assert(right.type == {{ id.in_type.upper() }});
349 result.type = {{ id.out_type.upper() }};
350 result.instance.{{ id.out_type.lower() }} = left.instance.{{ id.in_type.lower() }} {{ id.operator }} right.instance.{{ id.in_type.lower() }};
355 {% if 'pow' in builtins %}
356 Object builtin$pow$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Object* args)
360 Object base = args[0];
361 Object exponent = args[1];
363 assert(base.type == INTEGER);
364 assert(exponent.type == INTEGER);
367 result.type = INTEGER;
368 result.instance.integer = pow(base.instance.integer, exponent.instance.integer);
372 Object builtin$pow = { CLOSURE, (Instance)(Closure){ NULL, builtin$pow$implementation } };
375 {% if 'print' in builtins %}
376 Object builtin$print$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Object* args)
378 for(size_t i = 0; i < argc; i++)
380 Object output = args[i];
384 fputs(output.instance.boolean ? "true" : "false", stdout);
388 // TODO Print something better
393 printf("%" PRId32, output.instance.integer);
397 // Using fwrite instead of printf to handle size_t length
398 printf("%s", output.instance.string);
406 // TODO Return something better
410 Object builtin$print = { CLOSURE, (Instance)(Closure){ NULL, builtin$print$implementation } };
413 {% for function_definition in function_definition_list %}
414 Object user${{function_definition.name}}$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Object* args)
416 assert(argc == {{ function_definition.argument_name_list|length }});
418 Environment* environment = EnvironmentPool_allocate(environmentPool);
419 Environment_initialize(environment, parent);
421 {% for argument_name in function_definition.argument_name_list %}
422 Environment_set(environment, "{{ argument_name }}", args[{{ loop.index0 }}]);
425 {% for statement in function_definition.statement_list[:-1] %}
426 {{ generate_statement(statement) }}
429 Object result = {{ generate_statement(function_definition.statement_list[-1]) }}
431 Environment_setLive(environment, false);
436 int main(int argc, char** argv)
438 EnvironmentPool* environmentPool = EnvironmentPool_construct();
439 Environment* environment = EnvironmentPool_allocate(environmentPool);
440 Environment_initialize(environment, NULL);
442 // TODO Use the symbol from SYMBOL_LIST
443 {% for builtin in builtins %}
444 Environment_set(environment, "{{ builtin }}", builtin${{ builtin }});
447 {% for statement in statements %}
448 {{ generate_statement(statement) }}
451 Environment_setLive(environment, false);
452 EnvironmentPool_destruct(environmentPool);