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*);
80 const Object builtin$true = { BOOLEAN, (Instance)(bool){ true } };
81 const Object builtin$false = { BOOLEAN, (Instance)(bool){ false } };
83 struct EnvironmentNode
87 EnvironmentNode* next;
96 EnvironmentNode* root;
99 void Environment_initialize(Environment* self, Environment* parent)
101 self->parent = parent;
104 // We are currently only ever initializing environments at the beginning of running functions, so
105 // for now at least we can assume that we want it to be live immediately.
109 void Environment_deinitialize(Environment* self)
111 EnvironmentNode* next;
112 for(EnvironmentNode* node = self->root; node != NULL; node = next)
119 void Environment_setLive(Environment* self, bool live)
124 void Environment_mark(Environment* self)
126 if(self == NULL) return;
127 if(self->mark) return; // Prevents infinite recursion in the case of cycles
131 Environment_mark(self->parent);
133 for(EnvironmentNode* node = self->root; node != NULL; node = node->next)
135 switch(node->value.type)
143 Environment_mark(node->value.instance.closure.closed);
152 // This need not be thread safe because environments exist on one thread only
153 void Environment_set(Environment* self, const char* const key, Object value)
155 EnvironmentNode* node = malloc(sizeof(EnvironmentNode));
158 node->next = self->root;
162 Object Environment_get(Environment* self, const char* const symbol)
164 for(EnvironmentNode* node = self->root; node != NULL; node = node->next)
166 // We can compare pointers because pointers are unique in the SYMBOL_LIST
167 if(node->key == symbol)
173 if(self->parent != NULL)
175 return Environment_get(self->parent, symbol);
178 // TODO Handle symbol errors
182 # define POOL_SIZE 64
183 struct EnvironmentPool
186 bool allocatedFlags[POOL_SIZE];
187 Environment environments[POOL_SIZE];
188 EnvironmentPool* overflow;
191 EnvironmentPool* EnvironmentPool_construct();
192 void EnvironmentPool_initialize(EnvironmentPool*);
193 void EnvironmentPool_deinitialize(EnvironmentPool*);
194 void EnvironmentPool_destruct(EnvironmentPool*);
196 EnvironmentPool* EnvironmentPool_construct()
198 EnvironmentPool* result = malloc(sizeof(EnvironmentPool));
199 EnvironmentPool_initialize(result);
203 void EnvironmentPool_initialize(EnvironmentPool* self)
205 self->overflow = NULL;
208 for(size_t i = 0; i < POOL_SIZE; i++)
210 self->allocatedFlags[i] = false;
211 self->environments[i].live = false;
215 void EnvironmentPool_deinitialize(EnvironmentPool* self)
217 // We can assume if this is being called, none of the Environments are live
218 for(int8_t i = 0; i < POOL_SIZE; i++)
220 if(self->allocatedFlags[i]) Environment_deinitialize(&(self->environments[i]));
223 EnvironmentPool_destruct(self->overflow);
226 void EnvironmentPool_destruct(EnvironmentPool* self)
228 if(self == NULL) return;
229 EnvironmentPool_deinitialize(self);
233 void EnvironmentPool_GC(EnvironmentPool* self)
235 // Unmark all the environments
236 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
238 for(int8_t i = 0; i < POOL_SIZE; i++)
240 current->environments[i].mark = false;
244 // Mark live enviroments and environments referenced by live environments
245 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
247 for(int8_t i = 0; i < POOL_SIZE; i++)
249 if(current->environments[i].live)
251 Environment_mark(&(current->environments[i]));
256 // TODO We never free pools until the very end--we could free a pool if two pools are empty
257 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
259 for(int8_t i = POOL_SIZE - 1; i >= 0; i--)
261 if(!current->environments[i].mark && current->allocatedFlags[i])
263 Environment_deinitialize(&(current->environments[i]));
264 current->allocatedFlags[i] = false;
265 current->freeIndex = i;
271 Environment* EnvironmentPool_allocate(EnvironmentPool* self)
273 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
275 for(; current->freeIndex < POOL_SIZE; current->freeIndex++)
277 if(!current->allocatedFlags[current->freeIndex])
279 current->allocatedFlags[current->freeIndex] = true;
280 return &(current->environments[current->freeIndex]);
285 EnvironmentPool_GC(self);
287 EnvironmentPool* previous;
288 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
290 for(; current->freeIndex < POOL_SIZE; current->freeIndex++)
292 if(!current->allocatedFlags[current->freeIndex])
294 current->allocatedFlags[current->freeIndex] = true;
295 return &(current->environments[current->freeIndex]);
304 previous->overflow = EnvironmentPool_construct();
305 return EnvironmentPool_allocate(previous->overflow);
308 Object integerLiteral(int32_t literal)
311 result.type = INTEGER;
312 result.instance.integer = literal;
316 Object stringLiteral(const char* literal)
319 result.type = STRING;
320 result.instance.string = literal;
324 // TODO Make this conditionally added
325 Object operator$negate(Object input)
327 assert(input.type == INTEGER);
330 result.type = INTEGER;
331 result.instance.integer = -input.instance.integer;
335 {% for id in infix_declarations %}
336 Object operator${{ id.name }}(Object left, Object right)
338 assert(left.type == {{ id.in_type.upper() }});
339 assert(right.type == {{ id.in_type.upper() }});
342 result.type = {{ id.out_type.upper() }};
343 result.instance.{{ id.out_type.lower() }} = left.instance.{{ id.in_type.lower() }} {{ id.operator }} right.instance.{{ id.in_type.lower() }};
348 {% if 'pow' in builtins %}
349 Object builtin$pow$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Object* args)
353 Object base = args[0];
354 Object exponent = args[1];
356 assert(base.type == INTEGER);
357 assert(exponent.type == INTEGER);
360 result.type = INTEGER;
361 result.instance.integer = pow(base.instance.integer, exponent.instance.integer);
365 Object builtin$pow = { CLOSURE, (Instance)(Closure){ NULL, builtin$pow$implementation } };
368 {% if 'print' in builtins %}
369 Object builtin$print$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Object* args)
371 for(size_t i = 0; i < argc; i++)
373 Object output = args[i];
377 fputs(output.instance.boolean ? "true" : "false", stdout);
381 // TODO Print something better
386 printf("%" PRId32, output.instance.integer);
390 // Using fwrite instead of printf to handle size_t length
391 printf("%s", output.instance.string);
399 // TODO Return something better
400 return builtin$false;
403 Object builtin$print = { CLOSURE, (Instance)(Closure){ NULL, builtin$print$implementation } };
405 {% for function_definition in function_definition_list %}
406 {{ function_definition }}
409 int main(int argc, char** argv)
411 EnvironmentPool* environmentPool = EnvironmentPool_construct();
412 Environment* environment = EnvironmentPool_allocate(environmentPool);
413 Environment_initialize(environment, NULL);
415 // TODO Use the symbol from SYMBOL_LIST
416 {% for builtin in builtins %}
417 Environment_set(environment, "{{ builtin }}", builtin${{ builtin }});
420 {% for statement in statements %}
424 Environment_setLive(environment, false);
425 EnvironmentPool_destruct(environmentPool);