X-Git-Url: https://code.kerkeslager.com/?p=fur;a=blobdiff_plain;f=templates%2Fprogram2.c;h=01203fd92eb47ac133e0667280bc3759ef84d05c;hp=d22d595d8c1768ed8bb347106025f168173044a1;hb=c24be69b26aedc427c7831f150e86ca00182d8e0;hpb=c4de9dca8a54bb5eb611a87b4d9fe36b0fe9a0b1 diff --git a/templates/program2.c b/templates/program2.c index d22d595..01203fd 100644 --- a/templates/program2.c +++ b/templates/program2.c @@ -21,7 +21,6 @@ enum Type { enum Builtin; typedef enum Builtin Builtin; enum Builtin { - __FIELD__, NIL, POW, PRINT @@ -126,28 +125,6 @@ union Argument { char* symbol; }; -void callBuiltinField(Thread* thread, size_t argumentCount) { - assert(argumentCount == 2); - - assert(!Stack_isEmpty(&(thread->stack))); - Object key = Stack_pop(&(thread->stack)); - assert(key.type == SYMBOL); - - assert(!Stack_isEmpty(&(thread->stack))); - Object structure = Stack_pop(&(thread->stack)); - assert(structure.type == STRUCTURE); - - while(structure.value.structure != NULL) { - if(strcmp(structure.value.structure->key, key.value.string) == 0) { - Stack_push(&(thread->stack), structure.value.structure->value); - return; - } - structure.value.structure = structure.value.structure->next; - } - - assert(false); // Symbol wasn't found in structure -} - void callBuiltinPow(Thread* thread, size_t argumentCount) { assert(argumentCount == 2); assert(!Stack_isEmpty(&(thread->stack))); @@ -209,10 +186,6 @@ void callBuiltinPrint(Thread* thread, size_t argumentCount) { void callBuiltin(Thread* thread, Builtin b, size_t argumentCount) { switch(b) { - case __FIELD__: - callBuiltinField(thread, argumentCount); - break; - case POW: callBuiltinPow(thread, argumentCount); break; @@ -307,6 +280,26 @@ void inst_end(Thread* thread, Argument argument) { {% include "comparison_instruction.c" %} {% endwith %} +void inst_field(Thread* thread, Argument argument) { + assert(!Stack_isEmpty(&(thread->stack))); + Object key = Stack_pop(&(thread->stack)); + assert(key.type == SYMBOL); + + assert(!Stack_isEmpty(&(thread->stack))); + Object structure = Stack_pop(&(thread->stack)); + assert(structure.type == STRUCTURE); + + while(structure.value.structure != NULL) { + if(strcmp(structure.value.structure->key, key.value.string) == 0) { + Stack_push(&(thread->stack), structure.value.structure->value); + return; + } + structure.value.structure = structure.value.structure->next; + } + + assert(false); // Symbol wasn't found in structure +} + void inst_get(Thread* thread, Argument argument) { assert(!Stack_isEmpty(&(thread->stack))); Object indexObject = Stack_pop(&(thread->stack)); @@ -425,13 +418,7 @@ void inst_pop(Thread* thread, Argument argument) { void inst_push(Thread* thread, Argument argument) { char* argumentString = argument.string; - if(strcmp(argumentString, "__field__") == 0) { - // TODO Make this an instruction - Object result; - result.type = BUILTIN; - result.value.builtin = __FIELD__; - Stack_push(&(thread->stack), result); - } else if(strcmp(argumentString, "false") == 0) { + if(strcmp(argumentString, "false") == 0) { Stack_push(&(thread->stack), (Object){ BOOLEAN, false }); } else if(strcmp(argumentString, "pow") == 0) { Object result;