#include <assert.h>
#include <inttypes.h>
+#include <setjmp.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
typedef struct Environment Environment;
struct EnvironmentPool;
typedef struct EnvironmentPool EnvironmentPool;
+struct Stack;
+typedef struct Stack Stack;
const char* const STRING_LITERAL_LIST[] = {
{% for string_literal in string_literal_list %}
LIST,
STRING_CONCATENATION,
STRING_LITERAL,
+ STRUCTURE,
VOID
};
struct Closure
{
Environment* closed;
- Object (*call)(EnvironmentPool*, Environment*, size_t, Object*);
+ Object (*call)(EnvironmentPool*, Environment*, size_t, Stack*, jmp_buf);
};
struct List;
struct StringConcatenation;
typedef struct StringConcatenation StringConcatenation;
+struct Structure;
+typedef struct Structure Structure;
+struct Structure
+{
+ size_t reference_count;
+ size_t length;
+ const char** symbol_list;
+ Object* value_list;
+};
+
union Instance
{
bool boolean;
List list;
StringConcatenation* string_concatenation;
const char* string_literal;
+ Structure* structure;
};
struct Object
return list->instance.list.items[index.instance.integer];
}
+struct Stack
+{
+ uint16_t length;
+ Object items[256];
+};
+
+void Stack_initialize(Stack* self)
+{
+ self->length = 0;
+}
+
+void Stack_push(Stack* self, Object item)
+{
+ assert(self->length < 256);
+ self->items[self->length] = item;
+ self->length++;
+}
+
+Object Stack_pop(Stack* self)
+{
+ assert(self->length > 0);
+ self->length--;
+ return self->items[self->length];
+}
+
+Object Object_rereference(Object self)
+{
+ switch(self.type)
+ {
+ case BOOLEAN:
+ case CLOSURE:
+ case INTEGER:
+ case STRING_LITERAL:
+ case VOID:
+ return self;
+
+ case STRING_CONCATENATION:
+ self.instance.string_concatenation->referenceCount++;
+ return self;
+
+ case STRUCTURE:
+ self.instance.structure->reference_count++;
+ return self;
+
+ default:
+ assert(false);
+ }
+}
+
+Object Structure_construct(size_t length, const char** symbol_list, Object* value_list)
+{
+ Structure* structure = malloc(sizeof(Structure));
+ structure->reference_count = 1;
+ structure->length = length;
+ structure->symbol_list = malloc(sizeof(const char*) * length);
+ structure->value_list = malloc(sizeof(Object) * length);
+
+ // TODO Don't allow assignment of mutable structures, as this screws up reference counting
+ for(size_t i = 0; i < length; i++)
+ {
+ structure->symbol_list[i] = symbol_list[i];
+ structure->value_list[i] = Object_rereference(value_list[i]);
+ }
+
+ Object result = { STRUCTURE, (Instance)structure };
+
+ return result;
+}
+
+Object Structure_get(Object* self, const char* symbol)
+{
+ assert(self->type == STRUCTURE);
+
+ for(size_t i = 0; i < self->instance.structure->length; i++)
+ {
+ if(self->instance.structure->symbol_list[i] == symbol)
+ {
+ return self->instance.structure->value_list[i];
+ }
+ }
+
+ assert(false);
+}
+
struct EnvironmentNode
{
const char* key;
}
break;
+ case STRUCTURE:
+ self->instance.structure->reference_count--;
+
+ if(self->instance.structure->reference_count == 0)
+ {
+ for(size_t i = 0; i < self->instance.structure->length; i++)
+ {
+ Object_deinitialize(&(self->instance.structure->value_list[i]));
+ }
+ free(self->instance.structure->symbol_list);
+ free(self->instance.structure->value_list);
+ free(self->instance.structure);
+ }
+ break;
+
default:
assert(false);
}
}
// TODO Make this conditionally added
-Object operator$concatenate(Object left, Object right)
+Object operator$concatenate(Stack* stack, jmp_buf parent_jump)
{
+ Object right = Stack_pop(stack);
+ Object left = Stack_pop(stack);
+
switch(left.type) {
case STRING_CONCATENATION:
- left.instance.string_concatenation->referenceCount++;
- break;
-
case STRING_LITERAL:
break;
switch(right.type) {
case STRING_CONCATENATION:
- right.instance.string_concatenation->referenceCount++;
- break;
-
case STRING_LITERAL:
break;
StringConcatenation* concatenation = malloc(sizeof(StringConcatenation));
concatenation->referenceCount = 1;
- concatenation->left = left;
- concatenation->right = right;
+ concatenation->left = Object_rereference(left);
+ concatenation->right = Object_rereference(right);
Object result = { STRING_CONCATENATION, (Instance)concatenation };
return result;
}
{% for id in infix_declarations %}
-Object operator${{ id.name }}(Object left, Object right)
+Object operator${{ id.name }}(Stack* stack, jmp_buf parent_jump)
{
+ Object right = Stack_pop(stack);
+ Object left = Stack_pop(stack);
+
assert(left.type == {{ id.in_type.upper() }});
assert(right.type == {{ id.in_type.upper() }});
{% endfor %}
{% if 'pow' in builtins %}
-Object builtin$pow$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Object* args)
+Object builtin$pow$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack, jmp_buf parent_jump)
{
- assert(argc == 2);
-
- Object base = args[0];
- Object exponent = args[1];
+ // Must unload items in reverse order
+ Object exponent = Stack_pop(stack);
+ Object base = Stack_pop(stack);
assert(base.type == INTEGER);
assert(exponent.type == INTEGER);
{% endif %}
{% if 'print' in builtins %}
-Object builtin$print$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Object* args)
+Object builtin$print$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack, jmp_buf parent_jump)
{
+ Stack reverse_stack;
+ Stack_initialize(&reverse_stack);
+
for(size_t i = 0; i < argc; i++)
{
- Object output = args[i];
+ Stack_push(&reverse_stack, Stack_pop(stack));
+ }
+
+ while(reverse_stack.length > 0)
+ {
+ Object output = Stack_pop(&reverse_stack);
switch(output.type)
{
case BOOLEAN:
break;
case STRING_CONCATENATION:
- builtin$print$implementation(NULL, NULL, 1, &(output.instance.string_concatenation->left));
- builtin$print$implementation(NULL, NULL, 1, &(output.instance.string_concatenation->right));
+ Stack_push(stack, output.instance.string_concatenation->left);
+ builtin$print$implementation(NULL, NULL, 1, stack, parent_jump);
+ Stack_push(stack, output.instance.string_concatenation->right);
+ builtin$print$implementation(NULL, NULL, 1, stack, parent_jump);
break;
case STRING_LITERAL:
Environment* environment = EnvironmentPool_allocate(environmentPool);
Environment_initialize(environment, NULL);
+ Stack stackMemory;
+ Stack* stack = &stackMemory;
+ Stack_initialize(stack);
+
+ jmp_buf jump;
+ if(setjmp(jump) != 0)
+ {
+ fprintf(stderr, "Error in __main__\n");
+ Environment_setLive(environment, false);
+ EnvironmentPool_destruct(environmentPool);
+ return 1;
+ }
+
// TODO Use the symbol from SYMBOL_LIST
{% for builtin in builtins %}
Environment_set(environment, "{{ builtin }}", builtin${{ builtin }});