From c7f381fbcb57ba1b7e33558a28fdb34f31234c07 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Tue, 1 Oct 2019 01:19:09 -0400 Subject: [PATCH] Implement negatives, use typedef --- c_generation.py | 1 + crossplatform_ir_generation.py | 5 +++-- templates/program2.c | 26 ++++++++++++++++++-------- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/c_generation.py b/c_generation.py index 2f4a9de..fba8f50 100644 --- a/c_generation.py +++ b/c_generation.py @@ -47,6 +47,7 @@ def generate_argument(instruction): 'idiv': generate_size_t_argument, 'mod': generate_size_t_argument, 'mul': generate_size_t_argument, + 'neg': generate_size_t_argument, 'pop': generate_symbol_argument, 'push': generate_symbol_argument, 'push_integer': generate_integer_argument, diff --git a/crossplatform_ir_generation.py b/crossplatform_ir_generation.py index 089b076..c452126 100644 --- a/crossplatform_ir_generation.py +++ b/crossplatform_ir_generation.py @@ -40,10 +40,11 @@ def generate_instruction_name_from_builtin(builtin): try: return { '__add__': 'add', - '__subtract__': 'sub', - '__multiply__': 'mul', '__integer_divide__': 'idiv', '__modular_divide__': 'mod', + '__multiply__': 'mul', + '__negate__': 'neg', + '__subtract__': 'sub', }[builtin] except KeyError: diff --git a/templates/program2.c b/templates/program2.c index 76fc2f8..8602054 100644 --- a/templates/program2.c +++ b/templates/program2.c @@ -72,7 +72,7 @@ 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; @@ -142,13 +142,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='/' %} @@ -163,7 +163,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))); @@ -179,7 +189,7 @@ void pop(struct Thread* thread, const union Argument argument) { 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) { @@ -202,7 +212,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; @@ -210,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; @@ -225,7 +235,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; }; -- 2.20.1