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 %}
60 typedef struct Closure Closure;
64 Object (*call)(EnvironmentPool*, Environment*, size_t, Object*);
81 const Object builtin$true = { BOOLEAN, (Instance)(bool){ true } };
82 const Object builtin$false = { BOOLEAN, (Instance)(bool){ false } };
83 const Object builtin$nil = { VOID, { 0 } };
85 struct EnvironmentNode
89 EnvironmentNode* next;
98 EnvironmentNode* root;
101 void Environment_initialize(Environment* self, Environment* parent)
103 self->parent = parent;
106 // We are currently only ever initializing environments at the beginning of running functions, so
107 // for now at least we can assume that we want it to be live immediately.
111 void Environment_deinitialize(Environment* self)
113 EnvironmentNode* next;
114 for(EnvironmentNode* node = self->root; node != NULL; node = next)
121 void Environment_setLive(Environment* self, bool live)
126 void Environment_mark(Environment* self)
128 if(self == NULL) return;
129 if(self->mark) return; // Prevents infinite recursion in the case of cycles
133 Environment_mark(self->parent);
135 for(EnvironmentNode* node = self->root; node != NULL; node = node->next)
137 switch(node->value.type)
146 Environment_mark(node->value.instance.closure.closed);
155 // This need not be thread safe because environments exist on one thread only
156 void Environment_set(Environment* self, const char* const key, Object value)
158 EnvironmentNode* node = malloc(sizeof(EnvironmentNode));
161 node->next = self->root;
165 Object Environment_get(Environment* self, const char* const symbol)
167 for(EnvironmentNode* node = self->root; node != NULL; node = node->next)
169 // We can compare pointers because pointers are unique in the SYMBOL_LIST
170 if(node->key == symbol)
176 if(self->parent != NULL)
178 return Environment_get(self->parent, symbol);
181 // TODO Handle symbol errors
185 # define POOL_SIZE 64
186 struct EnvironmentPool
189 bool allocatedFlags[POOL_SIZE];
190 Environment environments[POOL_SIZE];
191 EnvironmentPool* overflow;
194 EnvironmentPool* EnvironmentPool_construct();
195 void EnvironmentPool_initialize(EnvironmentPool*);
196 void EnvironmentPool_deinitialize(EnvironmentPool*);
197 void EnvironmentPool_destruct(EnvironmentPool*);
199 EnvironmentPool* EnvironmentPool_construct()
201 EnvironmentPool* result = malloc(sizeof(EnvironmentPool));
202 EnvironmentPool_initialize(result);
206 void EnvironmentPool_initialize(EnvironmentPool* self)
208 self->overflow = NULL;
211 for(size_t i = 0; i < POOL_SIZE; i++)
213 self->allocatedFlags[i] = false;
214 self->environments[i].live = false;
218 void EnvironmentPool_deinitialize(EnvironmentPool* self)
220 // We can assume if this is being called, none of the Environments are live
221 for(int8_t i = 0; i < POOL_SIZE; i++)
223 if(self->allocatedFlags[i]) Environment_deinitialize(&(self->environments[i]));
226 EnvironmentPool_destruct(self->overflow);
229 void EnvironmentPool_destruct(EnvironmentPool* self)
231 if(self == NULL) return;
232 EnvironmentPool_deinitialize(self);
236 void EnvironmentPool_GC(EnvironmentPool* self)
238 // Unmark all the environments
239 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
241 for(int8_t i = 0; i < POOL_SIZE; i++)
243 current->environments[i].mark = false;
247 // Mark live enviroments and environments referenced by live environments
248 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
250 for(int8_t i = 0; i < POOL_SIZE; i++)
252 if(current->environments[i].live)
254 Environment_mark(&(current->environments[i]));
259 // TODO We never free pools until the very end--we could free a pool if two pools are empty
260 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
262 for(int8_t i = POOL_SIZE - 1; i >= 0; i--)
264 if(!current->environments[i].mark && current->allocatedFlags[i])
266 Environment_deinitialize(&(current->environments[i]));
267 current->allocatedFlags[i] = false;
268 current->freeIndex = i;
274 Environment* EnvironmentPool_allocate(EnvironmentPool* self)
276 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
278 for(; current->freeIndex < POOL_SIZE; current->freeIndex++)
280 if(!current->allocatedFlags[current->freeIndex])
282 current->allocatedFlags[current->freeIndex] = true;
283 return &(current->environments[current->freeIndex]);
288 EnvironmentPool_GC(self);
290 EnvironmentPool* previous;
291 for(EnvironmentPool* current = self; current != NULL; current = current->overflow)
293 for(; current->freeIndex < POOL_SIZE; current->freeIndex++)
295 if(!current->allocatedFlags[current->freeIndex])
297 current->allocatedFlags[current->freeIndex] = true;
298 return &(current->environments[current->freeIndex]);
307 previous->overflow = EnvironmentPool_construct();
308 return EnvironmentPool_allocate(previous->overflow);
311 Object integerLiteral(int32_t literal)
314 result.type = INTEGER;
315 result.instance.integer = literal;
319 Object stringLiteral(const char* literal)
322 result.type = STRING;
323 result.instance.string = literal;
327 // TODO Make this conditionally added
328 Object operator$negate(Object input)
330 assert(input.type == INTEGER);
333 result.type = INTEGER;
334 result.instance.integer = -input.instance.integer;
338 {% for id in infix_declarations %}
339 Object operator${{ id.name }}(Object left, Object right)
341 assert(left.type == {{ id.in_type.upper() }});
342 assert(right.type == {{ id.in_type.upper() }});
345 result.type = {{ id.out_type.upper() }};
346 result.instance.{{ id.out_type.lower() }} = left.instance.{{ id.in_type.lower() }} {{ id.operator }} right.instance.{{ id.in_type.lower() }};
351 {% if 'pow' in builtins %}
352 Object builtin$pow$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Object* args)
356 Object base = args[0];
357 Object exponent = args[1];
359 assert(base.type == INTEGER);
360 assert(exponent.type == INTEGER);
363 result.type = INTEGER;
364 result.instance.integer = pow(base.instance.integer, exponent.instance.integer);
368 Object builtin$pow = { CLOSURE, (Instance)(Closure){ NULL, builtin$pow$implementation } };
371 {% if 'print' in builtins %}
372 Object builtin$print$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Object* args)
374 for(size_t i = 0; i < argc; i++)
376 Object output = args[i];
380 fputs(output.instance.boolean ? "true" : "false", stdout);
384 // TODO Print something better
389 printf("%" PRId32, output.instance.integer);
393 // Using fwrite instead of printf to handle size_t length
394 printf("%s", output.instance.string);
406 // TODO Return something better
407 return builtin$false;
410 Object builtin$print = { CLOSURE, (Instance)(Closure){ NULL, builtin$print$implementation } };
412 {% for function_definition in function_definition_list %}
413 {{ function_definition }}
416 int main(int argc, char** argv)
418 EnvironmentPool* environmentPool = EnvironmentPool_construct();
419 Environment* environment = EnvironmentPool_allocate(environmentPool);
420 Environment_initialize(environment, NULL);
422 // TODO Use the symbol from SYMBOL_LIST
423 {% for builtin in builtins %}
424 Environment_set(environment, "{{ builtin }}", builtin${{ builtin }});
427 {% for statement in statements %}
431 Environment_setLive(environment, false);
432 EnvironmentPool_destruct(environmentPool);