X-Git-Url: https://code.kerkeslager.com/?a=blobdiff_plain;f=templates%2Fprogram.c;h=f8095bf838a3934733e628d67976060c69109054;hb=379bdcf727cc66ee58d36a112e306b81e427fcf1;hp=db1abdfb5b73b076932628a29a1fecb0ee8c9cdf;hpb=728158b2204b6454eecc12bf2988b3f0cbb9ec81;p=fur diff --git a/templates/program.c b/templates/program.c index db1abdf..f8095bf 100644 --- a/templates/program.c +++ b/templates/program.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -66,7 +67,7 @@ typedef struct Closure Closure; struct Closure { Environment* closed; - Object (*call)(EnvironmentPool*, Environment*, size_t, Stack*); + Object (*call)(EnvironmentPool*, Environment*, size_t, Stack*, const unsigned long, jmp_buf); }; struct List; @@ -162,6 +163,23 @@ void Stack_initialize(Stack* self) self->length = 0; } +Stack* Stack_construct() +{ + Stack* result = malloc(sizeof(Stack)); + Stack_initialize(result); + return result; +} + +void Stack_destruct(Stack* self) +{ + free(self); +} + +bool Stack_any(Stack* self) +{ + return self->length > 0; +} + void Stack_push(Stack* self, Object item) { assert(self->length < 256); @@ -315,6 +333,22 @@ void Object_deinitialize(Object* self) } } +typedef uint32_t StackSnapshot; + +StackSnapshot Stack_takeSnapshot(Stack* self) +{ + return (StackSnapshot) self->length; +} + +void Stack_rewind(Stack* self, StackSnapshot snapshot) +{ + while(self->length > snapshot) + { + Object item = Stack_pop(self); + Object_deinitialize(&item); + } +} + void Environment_deinitialize(Environment* self) { EnvironmentNode* next; @@ -516,6 +550,13 @@ Environment* EnvironmentPool_allocate(EnvironmentPool* self) return EnvironmentPool_allocate(previous->overflow); } +Environment* Environment_construct(EnvironmentPool* environmentPool, Environment* parent) +{ + Environment* environment = EnvironmentPool_allocate(environmentPool); + Environment_initialize(environment, parent); + return environment; +} + Object integerLiteral(int32_t literal) { Object result; @@ -532,20 +573,105 @@ Object stringLiteral(const char* literal) return result; } -// TODO Make this conditionally added -Object operator$negate(Object input) +{% if 'pow' in builtins %} +Object builtin$pow$implementation( + EnvironmentPool* environmentPool, + Environment* parent, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) { - assert(input.type == INTEGER); + // Must unload items in reverse order + Object exponent = Stack_pop(stack); + Object base = Stack_pop(stack); + + assert(base.type == INTEGER); + assert(exponent.type == INTEGER); Object result; result.type = INTEGER; - result.instance.integer = -input.instance.integer; + result.instance.integer = pow(base.instance.integer, exponent.instance.integer); + return result; +} + +Object builtin$pow = { CLOSURE, (Instance)(Closure){ NULL, builtin$pow$implementation } }; +{% endif %} + +Object builtin$negate$implementation( + EnvironmentPool* environmentPool, + Environment* parent, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) +{ + assert(argc == 1); + + Object argument = Stack_pop(stack); + + assert(argument.type == INTEGER); + + Object result = (Object){ + INTEGER, + (Instance)(int32_t) (-argument.instance.integer) + }; + return result; } +Object builtin$negate = { CLOSURE, (Instance)(Closure){ NULL, builtin$negate$implementation } }; + +{% for op in ['lt', 'gt', 'lte', 'gte', 'eq', 'neq'] %} +Object builtin${{ op }}$implementation( + EnvironmentPool* environmentPool, + Environment* parent, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) +{ + assert(argc == 2); + + Object right = Stack_pop(stack); + Object left = Stack_pop(stack); + + assert(left.type == INTEGER); + assert(right.type == INTEGER); + + {% if op == 'lt' %} + if(left.instance.integer < right.instance.integer) + {% elif op == 'gt' %} + if(left.instance.integer > right.instance.integer) + {% elif op == 'lte' %} + if(left.instance.integer <= right.instance.integer) + {% elif op == 'gte' %} + if(left.instance.integer >= right.instance.integer) + {% elif op == 'eq' %} + if(left.instance.integer == right.instance.integer) + {% elif op == 'neq' %} + if(left.instance.integer != right.instance.integer) + {% endif %} + { + return builtin$true; + } + else + { + return builtin$false; + } +} +Object builtin${{ op }} = { CLOSURE, (Instance)(Closure){ NULL, builtin${{ op }}$implementation } }; +{% endfor %} -// TODO Make this conditionally added -Object operator$concatenate(Stack* stack) +Object builtin$concat$implementation( + EnvironmentPool* environmentPool, + Environment* parent, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) { + assert(argc == 2); + Object right = Stack_pop(stack); Object left = Stack_pop(stack); @@ -575,44 +701,195 @@ Object operator$concatenate(Stack* stack) Object result = { STRING_CONCATENATION, (Instance)concatenation }; return result; } +Object builtin$concat = { CLOSURE, (Instance)(Closure){ NULL, builtin$concat$implementation } }; -{% for id in infix_declarations %} -Object operator${{ id.name }}(Stack* stack) +Object builtin$add$implementation( + EnvironmentPool* environmentPool, + Environment* parent, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) { + assert(argc == 2); + Object right = Stack_pop(stack); Object left = Stack_pop(stack); - assert(left.type == {{ id.in_type.upper() }}); - assert(right.type == {{ id.in_type.upper() }}); + assert(left.type == INTEGER); + assert(right.type == INTEGER); + + Object result = (Object){ + INTEGER, + (Instance)(int32_t) (left.instance.integer + right.instance.integer) + }; - Object result; - result.type = {{ id.out_type.upper() }}; - result.instance.{{ id.out_type.lower() }} = left.instance.{{ id.in_type.lower() }} {{ id.operator }} right.instance.{{ id.in_type.lower() }}; return result; } -{% endfor %} +Object builtin$add = { CLOSURE, (Instance)(Closure){ NULL, builtin$add$implementation } }; -{% if 'pow' in builtins %} -Object builtin$pow$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack) +Object builtin$subtract$implementation( + EnvironmentPool* environmentPool, + Environment* parent, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) { - // Must unload items in reverse order - Object exponent = Stack_pop(stack); - Object base = Stack_pop(stack); + assert(argc == 2); - assert(base.type == INTEGER); - assert(exponent.type == INTEGER); + Object right = Stack_pop(stack); + Object left = Stack_pop(stack); + + assert(left.type == INTEGER); + assert(right.type == INTEGER); + + Object result = (Object){ + INTEGER, + (Instance)(int32_t) (left.instance.integer - right.instance.integer) + }; - Object result; - result.type = INTEGER; - result.instance.integer = pow(base.instance.integer, exponent.instance.integer); return result; } +Object builtin$subtract = { CLOSURE, (Instance)(Closure){ NULL, builtin$subtract$implementation } }; -Object builtin$pow = { CLOSURE, (Instance)(Closure){ NULL, builtin$pow$implementation } }; -{% endif %} +Object builtin$multiply$implementation( + EnvironmentPool* environmentPool, + Environment* parent, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) +{ + assert(argc == 2); + + Object right = Stack_pop(stack); + Object left = Stack_pop(stack); + + assert(left.type == INTEGER); + assert(right.type == INTEGER); + + Object result = (Object){ + INTEGER, + (Instance)(int32_t) (left.instance.integer * right.instance.integer) + }; + + return result; +} +Object builtin$multiply = { CLOSURE, (Instance)(Closure){ NULL, builtin$multiply$implementation } }; + +Object builtin$integer_divide$implementation( + EnvironmentPool* environmentPool, + Environment* parent, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) +{ + assert(argc == 2); + + Object right = Stack_pop(stack); + Object left = Stack_pop(stack); + + assert(left.type == INTEGER); + assert(right.type == INTEGER); + + if(right.instance.integer == 0) + { + fprintf(stderr, "DivisionByZeroError on line %zu\n", line); + longjmp(parentJump, 1); + } + + Object result = (Object){ + INTEGER, + (Instance)(int32_t) (left.instance.integer / right.instance.integer) + }; + + return result; +} +Object builtin$integer_divide = { CLOSURE, (Instance)(Closure){ NULL, builtin$integer_divide$implementation } }; + +Object builtin$modular_divide$implementation( + EnvironmentPool* environmentPool, + Environment* parent, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) +{ + assert(argc == 2); + + Object right = Stack_pop(stack); + Object left = Stack_pop(stack); + + assert(left.type == INTEGER); + assert(right.type == INTEGER); + + if(right.instance.integer == 0) + { + fprintf(stderr, "DivisionByZeroError on line %zu\n", line); + longjmp(parentJump, 1); + } + + Object result = (Object){ + INTEGER, + (Instance)(int32_t) (left.instance.integer % right.instance.integer) + }; + + return result; +} +Object builtin$modular_divide = { CLOSURE, (Instance)(Closure){ NULL, builtin$modular_divide$implementation } }; + +Object builtin$field$implementation( + EnvironmentPool* environmentPool, + Environment* parent, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) +{ + assert(argc == 2); + + Object right = Stack_pop(stack); + Object left = Stack_pop(stack); + + assert(left.type == STRUCTURE); + assert(right.type == STRING_LITERAL); + + Object result = (Object){ + INTEGER, + (Instance)(int32_t) (left.instance.integer % right.instance.integer) + }; + + return result; +} +Object builtin$field = { CLOSURE, (Instance)(Closure){ NULL, builtin$field$implementation } }; + +Object builtin$get$implementation( + EnvironmentPool* environmentPool, + Environment* parent, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) +{ + assert(argc == 2); + + Object right = Stack_pop(stack); + Object left = Stack_pop(stack); + + return List_get(&left, right); +} +Object builtin$get = { CLOSURE, (Instance)(Closure){ NULL, builtin$get$implementation } }; {% if 'print' in builtins %} -Object builtin$print$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack) +Object builtin$print$implementation( + EnvironmentPool* environmentPool, + Environment* parent, + size_t argc, + Stack* stack, + const unsigned long line, + jmp_buf parentJump) { Stack reverse_stack; Stack_initialize(&reverse_stack); @@ -642,9 +919,9 @@ Object builtin$print$implementation(EnvironmentPool* environmentPool, Environmen case STRING_CONCATENATION: Stack_push(stack, output.instance.string_concatenation->left); - builtin$print$implementation(NULL, NULL, 1, stack); + builtin$print$implementation(NULL, NULL, 1, stack, line, parentJump); Stack_push(stack, output.instance.string_concatenation->right); - builtin$print$implementation(NULL, NULL, 1, stack); + builtin$print$implementation(NULL, NULL, 1, stack, line, parentJump); break; case STRING_LITERAL: @@ -682,11 +959,44 @@ int main(int argc, char** argv) Stack* stack = &stackMemory; Stack_initialize(stack); + jmp_buf jump; + if(setjmp(jump) != 0) + { + fprintf(stderr, "\tin __main__\n"); + + while(Stack_any(stack)) + { + Object item = Stack_pop(stack); + Object_deinitialize(&item); + } + Environment_setLive(environment, false); + EnvironmentPool_destruct(environmentPool); + + // TODO We would like to return something nonzero here, but that messes up Valgrind so we couldn't catch memory leaks + return 0; + } + // TODO Use the symbol from SYMBOL_LIST {% for builtin in builtins %} Environment_set(environment, "{{ builtin }}", builtin${{ builtin }}); {% endfor %} + Environment_set(environment, "true", builtin$true); + Environment_set(environment, "false", builtin$false); + Environment_set(environment, "__add__", builtin$add); + Environment_set(environment, "__subtract__", builtin$subtract); + Environment_set(environment, "__multiply__", builtin$multiply); + Environment_set(environment, "__integer_divide__", builtin$integer_divide); + Environment_set(environment, "__modular_divide__", builtin$modular_divide); + Environment_set(environment, "__negate__", builtin$negate); + Environment_set(environment, "__concat__", builtin$concat); + Environment_set(environment, "__field__", builtin$field); + Environment_set(environment, "__get__", builtin$get); + + {% for op in ['lt', 'gt', 'lte', 'gte', 'eq', 'neq'] %} + Environment_set(environment, "__{{ op }}__", builtin${{ op }}); + {% endfor %} + {% for statement in statements %} {{ statement }} {% endfor %}