X-Git-Url: https://code.kerkeslager.com/?p=fur;a=blobdiff_plain;f=templates%2Fprogram2.c;h=1610a540db5c977f832eb7e163feda85395b5c5b;hp=8abe2a273dd6dedbff59cace4d1d1faee0409022;hb=d28e140fd317ef3d63286ba6d87eb0418ee536c9;hpb=b7a244b6319f5c770582283097f6184e5a6ec192 diff --git a/templates/program2.c b/templates/program2.c index 8abe2a2..1610a54 100644 --- a/templates/program2.c +++ b/templates/program2.c @@ -8,6 +8,7 @@ enum Type; typedef enum Type Type; enum Type { + BOOLEAN, BUILTIN, INTEGER, STRING @@ -17,6 +18,7 @@ enum Builtin; typedef enum Builtin Builtin; enum Builtin { NIL, + POW, PRINT }; @@ -24,6 +26,7 @@ union Value; typedef union Value Value; union Value { Builtin builtin; + bool boolean; char* string; int32_t integer; }; @@ -71,13 +74,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 @@ -85,6 +113,10 @@ void call(struct Thread* thread, const union Argument argument) { Object arg = Stack_pop(&(thread->stack)); switch(arg.type) { + case BOOLEAN: + if(arg.value.boolean) printf("true"); + else printf("false"); + break; case INTEGER: printf("%i", arg.value.integer); break; @@ -116,13 +148,13 @@ void call(struct Thread* thread, const union Argument argument) { {% endwith %} -void drop(struct Thread* thread, const union Argument argument) { +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) { } {% with name='idiv', operation='/' %} @@ -137,7 +169,17 @@ void end(struct Thread* thread, const union Argument argument) { {% include "arithmetic_instruction.c" %} {% endwith %} -void pop(struct Thread* thread, const union Argument argument) { +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; assert(!Stack_isEmpty(&(thread->stack))); @@ -145,19 +187,31 @@ void pop(struct Thread* thread, const union Argument argument) { 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, const union Argument argument) { +void push(struct Thread* thread, Argument argument) { char* argumentString = argument.string; - if(strcmp(argumentString, "print") == 0) { + if(strcmp(argumentString, "false") == 0) { + Stack_push(&(thread->stack), (Object){ BOOLEAN, false }); + }else if(strcmp(argumentString, "pow") == 0) { + Object result; + result.type = BUILTIN; + result.value.builtin = POW; + Stack_push(&(thread->stack), result); + } else if(strcmp(argumentString, "print") == 0) { Object result; result.type = BUILTIN; result.value.builtin = PRINT; Stack_push(&(thread->stack), result); + } else if(strcmp(argumentString, "true") == 0) { + Stack_push(&(thread->stack), (Object){ BOOLEAN, true }); } else { Environment_get_Result result = Environment_get(thread->environment, argumentString); if(!result.found) { @@ -168,7 +222,7 @@ void push(struct Thread* thread, const union Argument argument) { } } -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; @@ -176,7 +230,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; @@ -191,7 +245,7 @@ void push_string(struct Thread* thread, const union Argument argument) { struct Instruction; typedef const struct Instruction Instruction; struct Instruction { - void (*instruction)(struct Thread*,const union Argument); + void (*instruction)(struct Thread*,Argument); Argument argument; };