X-Git-Url: https://code.kerkeslager.com/?p=fur;a=blobdiff_plain;f=templates%2Fprogram2.c;h=8602054d666f93c206724e184b9c53d752939027;hp=6e21ce07d13d26937a3427bf7558ed999e108472;hb=c7f381fbcb57ba1b7e33558a28fdb34f31234c07;hpb=c45c61444aae3df328e4dc84acf7e31e58a1f64e diff --git a/templates/program2.c b/templates/program2.c index 6e21ce0..8602054 100644 --- a/templates/program2.c +++ b/templates/program2.c @@ -17,6 +17,7 @@ enum Builtin; typedef enum Builtin Builtin; enum Builtin { NIL, + POW, PRINT }; @@ -40,21 +41,25 @@ struct Object { void Object_deinitialize(Object* self) { } +{% include "environment.c" %} {% include "stack.c" %} struct Thread; typedef struct Thread Thread; struct Thread { + Environment* environment; Stack stack; size_t program_counter; }; void Thread_initialize(Thread* self, size_t program_counter) { + self->environment = Environment_construct(); Stack_initialize(&(self->stack)); self->program_counter = program_counter; } void Thread_deinitialize(Thread* self) { + Environment_destruct(self->environment); Stack_deinitialize(&(self->stack)); } @@ -67,13 +72,38 @@ union Argument { int32_t integer; }; -void call(struct Thread* thread, const union Argument argument) { +void call(struct Thread* thread, Argument argument) { assert(!Stack_isEmpty(&(thread->stack))); Object f = Stack_pop(&(thread->stack)); + size_t argumentCount = argument.label; switch(f.type) { case BUILTIN: switch(f.value.builtin) { + case POW: + { + assert(argumentCount == 2); + assert(!Stack_isEmpty(&(thread->stack))); + Object exponent = Stack_pop(&(thread->stack)); + assert(exponent.type == INTEGER); + assert(exponent.value.integer >= 0); + + assert(!Stack_isEmpty(&(thread->stack))); + Object base = Stack_pop(&(thread->stack)); + assert(base.type == INTEGER); + + Object result; + result.type = INTEGER; + result.value.integer = 1; + + while(exponent.value.integer > 0) { + result.value.integer *= base.value.integer; + exponent.value.integer--; + } + + Stack_push(&(thread->stack), result); + } + break; case PRINT: { // TODO Handle multiple arguments @@ -107,31 +137,82 @@ void call(struct Thread* thread, const union Argument argument) { } } -void drop(struct Thread* thread, const union Argument argument) { +{% with name='add', operation='+' %} + {% include "arithmetic_instruction.c" %} +{% endwith %} + + +void drop(struct Thread* thread, Argument argument) { assert(!Stack_isEmpty(&(thread->stack))); Object result = Stack_pop(&(thread->stack)); Object_deinitialize(&result); } -void end(struct Thread* thread, const union Argument argument) { +void end(struct Thread* thread, Argument argument) { } -void push(struct Thread* thread, const union Argument argument) { +{% with name='idiv', operation='/' %} + {% include "arithmetic_instruction.c" %} +{% endwith %} + +{% with name='mod', operation='%' %} + {% include "arithmetic_instruction.c" %} +{% endwith %} + +{% with name='mul', operation='*' %} + {% include "arithmetic_instruction.c" %} +{% endwith %} + +void neg(struct Thread* thread, Argument argument) { + assert(!Stack_isEmpty(&(thread->stack))); + Object result = Stack_pop(&(thread->stack)); + assert(result.type == INTEGER); + + result.value.integer = -(result.value.integer); + + Stack_push(&(thread->stack), result); +} + +void pop(struct Thread* thread, Argument argument) { char* argumentString = argument.string; - Object result; + assert(!Stack_isEmpty(&(thread->stack))); + Object result = Stack_pop(&(thread->stack)); + + if(strcmp(argumentString, "print") == 0) { + assert(false); + } else if(strcmp(argumentString, "pow") == 0) { + assert(false); + } + + + Environment_set(thread->environment, argumentString, result); +} + +void push(struct Thread* thread, Argument argument) { + char* argumentString = argument.string; if(strcmp(argumentString, "print") == 0) { + Object result; result.type = BUILTIN; result.value.builtin = PRINT; + Stack_push(&(thread->stack), result); + } else if(strcmp(argumentString, "pow") == 0) { + Object result; + result.type = BUILTIN; + result.value.builtin = POW; + Stack_push(&(thread->stack), result); } else { - assert(false); + Environment_get_Result result = Environment_get(thread->environment, argumentString); + if(!result.found) { + fprintf(stderr, "Variable `%s` not found", argumentString); + assert(false); + } + Stack_push(&(thread->stack), result.result); } - - Stack_push(&(thread->stack), result); } -void push_integer(struct Thread* thread, const union Argument argument) { +void push_integer(struct Thread* thread, Argument argument) { Object result; result.type = INTEGER; result.value.integer = argument.integer; @@ -139,7 +220,7 @@ void push_integer(struct Thread* thread, const union Argument argument) { Stack_push(&(thread->stack), result); } -void push_string(struct Thread* thread, const union Argument argument) { +void push_string(struct Thread* thread, Argument argument) { Object result; result.type = STRING; result.value.string = argument.string; @@ -147,10 +228,14 @@ void push_string(struct Thread* thread, const union Argument argument) { Stack_push(&(thread->stack), result); } +{% with name='sub', operation='-' %} + {% include "arithmetic_instruction.c" %} +{% endwith %} + struct Instruction; typedef const struct Instruction Instruction; struct Instruction { - void (*instruction)(struct Thread*,const union Argument); + void (*instruction)(struct Thread*,Argument); Argument argument; };