projects
/
fur
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Allocate Fur stacks on the C heap
[fur]
/
templates
/
program.c
diff --git
a/templates/program.c
b/templates/program.c
index
a599881
..
b25f7dc
100644
(file)
--- a/
templates/program.c
+++ b/
templates/program.c
@@
-163,6
+163,23
@@
void Stack_initialize(Stack* self)
self->length = 0;
}
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);
void Stack_push(Stack* self, Object item)
{
assert(self->length < 256);
@@
-517,6
+534,13
@@
Environment* EnvironmentPool_allocate(EnvironmentPool* self)
return EnvironmentPool_allocate(previous->overflow);
}
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;
Object integerLiteral(int32_t literal)
{
Object result;
@@
-545,7
+569,7
@@
Object operator$negate(Object input)
}
// TODO Make this conditionally added
}
// TODO Make this conditionally added
-Object operator$concatenate(Stack* stack, jmp_buf parent
_jump
)
+Object operator$concatenate(Stack* stack, jmp_buf parent
Jump, size_t line
)
{
Object right = Stack_pop(stack);
Object left = Stack_pop(stack);
{
Object right = Stack_pop(stack);
Object left = Stack_pop(stack);
@@
-578,7
+602,7
@@
Object operator$concatenate(Stack* stack, jmp_buf parent_jump)
}
{% for id in infix_declarations %}
}
{% for id in infix_declarations %}
-Object operator${{ id.name }}(Stack* stack, jmp_buf parent
_jump
)
+Object operator${{ id.name }}(Stack* stack, jmp_buf parent
Jump, size_t line
)
{
Object right = Stack_pop(stack);
Object left = Stack_pop(stack);
{
Object right = Stack_pop(stack);
Object left = Stack_pop(stack);
@@
-586,6
+610,14
@@
Object operator${{ id.name }}(Stack* stack, jmp_buf parent_jump)
assert(left.type == {{ id.in_type.upper() }});
assert(right.type == {{ id.in_type.upper() }});
assert(left.type == {{ id.in_type.upper() }});
assert(right.type == {{ id.in_type.upper() }});
+ {% if id.name == 'integerDivide' or id.name == 'modularDivide' %}
+ if(right.instance.integer == 0)
+ {
+ fprintf(stderr, "DivisionByZeroError on line %zu\n", line);
+ longjmp(parentJump, 1);
+ }
+ {% endif %}
+
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() }};
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() }};
@@
-594,7
+626,7
@@
Object operator${{ id.name }}(Stack* stack, jmp_buf parent_jump)
{% endfor %}
{% if 'pow' in builtins %}
{% endfor %}
{% if 'pow' in builtins %}
-Object builtin$pow$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack, jmp_buf parent
_j
ump)
+Object builtin$pow$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack, jmp_buf parent
J
ump)
{
// Must unload items in reverse order
Object exponent = Stack_pop(stack);
{
// Must unload items in reverse order
Object exponent = Stack_pop(stack);
@@
-613,7
+645,7
@@
Object builtin$pow = { CLOSURE, (Instance)(Closure){ NULL, builtin$pow$implement
{% endif %}
{% if 'print' in builtins %}
{% endif %}
{% if 'print' in builtins %}
-Object builtin$print$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack, jmp_buf parent
_j
ump)
+Object builtin$print$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack, jmp_buf parent
J
ump)
{
Stack reverse_stack;
Stack_initialize(&reverse_stack);
{
Stack reverse_stack;
Stack_initialize(&reverse_stack);
@@
-643,9
+675,9
@@
Object builtin$print$implementation(EnvironmentPool* environmentPool, Environmen
case STRING_CONCATENATION:
Stack_push(stack, output.instance.string_concatenation->left);
case STRING_CONCATENATION:
Stack_push(stack, output.instance.string_concatenation->left);
- builtin$print$implementation(NULL, NULL, 1, stack, parent
_j
ump);
+ builtin$print$implementation(NULL, NULL, 1, stack, parent
J
ump);
Stack_push(stack, output.instance.string_concatenation->right);
Stack_push(stack, output.instance.string_concatenation->right);
- builtin$print$implementation(NULL, NULL, 1, stack, parent
_j
ump);
+ builtin$print$implementation(NULL, NULL, 1, stack, parent
J
ump);
break;
case STRING_LITERAL:
break;
case STRING_LITERAL:
@@
-686,10
+718,18
@@
int main(int argc, char** argv)
jmp_buf jump;
if(setjmp(jump) != 0)
{
jmp_buf jump;
if(setjmp(jump) != 0)
{
- fprintf(stderr, "Error in __main__\n");
+ fprintf(stderr, "\tin __main__\n");
+
+ while(Stack_any(stack))
+ {
+ Object item = Stack_pop(stack);
+ Object_deinitialize(&item);
+ }
Environment_setLive(environment, false);
EnvironmentPool_destruct(environmentPool);
Environment_setLive(environment, false);
EnvironmentPool_destruct(environmentPool);
- return 1;
+
+ // 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
}
// TODO Use the symbol from SYMBOL_LIST