From aea858380ed276bdf924c7cbcb4310ffde2a1d02 Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Thu, 4 Jul 2019 20:15:29 -0700 Subject: [PATCH] Added tests for heap IMP. --- tests/3001_heap.pvvs | 21 +++++++++++++++++++++ vv_interpreter.c | 18 +++++++++++++++--- vv_test.py | 1 + 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 tests/3001_heap.pvvs diff --git a/tests/3001_heap.pvvs b/tests/3001_heap.pvvs new file mode 100644 index 0000000..0b572e8 --- /dev/null +++ b/tests/3001_heap.pvvs @@ -0,0 +1,21 @@ +# This test verifies the heap IMP. + +SSSTSSSSSTN | ST: Push +65 (ASCII A) +SSSTN | ST: Push +1 (used as an address) +SSSTSSSSTSN | ST: Push +66 (ASCII B) +TTS | HP: Store +SSSTSSSSTTN | ST: Push +67 (ASCII C) +SSSTN | ST: Push +1 (used as an address) +TTT | HP: Load + + +TNSS +TNSS +TNSS +NNN + + +TNSS | IO: Output character +TNSS | IO: Output character +TNSS | IO: Output character +NNN | FC: Terminate program diff --git a/vv_interpreter.c b/vv_interpreter.c index 8d4db24..8fac7c2 100644 --- a/vv_interpreter.c +++ b/vv_interpreter.c @@ -280,9 +280,21 @@ void process_imp_heap(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp) { switch (next_code_byte(code,pc)) { - case ' ' : /* Store to heap */ *(*hp + *((*sp)-1)) = **sp; *sp -= 2; break; - case '\t': /* Retrieve from heap */ **sp = *(*hp + **sp); break; - default : ws_die(pc, "malformed heap IMP"); break; + case ' ' : + /* Store to heap */ + { + int32_t value = stack_pop(sp); + int32_t addr = stack_pop(sp); + *(*hp + addr) = value; + } + break; + case '\t': + /* Retrieve from heap */ + stack_push(sp, *(*hp + stack_pop(sp))); + break; + default: + ws_die(pc, "malformed heap IMP"); + break; } } diff --git a/vv_test.py b/vv_test.py index 4a31280..33a998b 100755 --- a/vv_test.py +++ b/vv_test.py @@ -24,6 +24,7 @@ tests = [ ['2003_arithmetic_multiplication', 'B'], ['2004_arithmetic_division', 'A'], ['2005_arithmetic_remainder', 'A'], + ['3001_heap', 'BCA'], ] for test in tests: -- 2.20.1