struct Closure
{
Environment* closed;
- Object (*call)(EnvironmentPool*, Environment*, size_t, Stack*, jmp_buf);
+ Object (*call)(EnvironmentPool*, Environment*, size_t, Stack*, const unsigned long, jmp_buf);
};
struct List;
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);
}
}
+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;
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;
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;
}
-// TODO Make this conditionally added
-Object operator$concatenate(Stack* stack, jmp_buf parent_jump, size_t line)
+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 %}
+
+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);
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, jmp_buf parent_jump, size_t line)
+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)
+ };
+
+ return result;
+}
+Object builtin$add = { CLOSURE, (Instance)(Closure){ NULL, builtin$add$implementation } };
+
+Object builtin$subtract$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$subtract = { CLOSURE, (Instance)(Closure){ NULL, builtin$subtract$implementation } };
+
+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 id.name == 'integerDivide' or id.name == 'modularDivide' %}
if(right.instance.integer == 0)
{
fprintf(stderr, "DivisionByZeroError on line %zu\n", line);
- longjmp(parent_jump, 1);
+ 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 = (Object){
+ INTEGER,
+ (Instance)(int32_t) (left.instance.integer / right.instance.integer)
+ };
+
return result;
}
-{% endfor %}
+Object builtin$integer_divide = { CLOSURE, (Instance)(Closure){ NULL, builtin$integer_divide$implementation } };
-{% if 'pow' in builtins %}
-Object builtin$pow$implementation(EnvironmentPool* environmentPool, Environment* parent, size_t argc, Stack* stack, jmp_buf parent_jump)
+Object builtin$modular_divide$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);
+
+ 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)
+ };
- Object result;
- result.type = INTEGER;
- result.instance.integer = pow(base.instance.integer, exponent.instance.integer);
return result;
}
+Object builtin$modular_divide = { CLOSURE, (Instance)(Closure){ NULL, builtin$modular_divide$implementation } };
-Object builtin$pow = { CLOSURE, (Instance)(Closure){ NULL, builtin$pow$implementation } };
-{% endif %}
+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, jmp_buf parent_jump)
+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);
case STRING_CONCATENATION:
Stack_push(stack, output.instance.string_concatenation->left);
- builtin$print$implementation(NULL, NULL, 1, stack, parent_jump);
+ builtin$print$implementation(NULL, NULL, 1, stack, line, parentJump);
Stack_push(stack, output.instance.string_concatenation->right);
- builtin$print$implementation(NULL, NULL, 1, stack, parent_jump);
+ builtin$print$implementation(NULL, NULL, 1, stack, line, parentJump);
break;
case STRING_LITERAL:
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);
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 %}