From 519b6ab41c6a7c2abd44c9dcffb2a9b63c762442 Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Thu, 4 Jul 2019 22:35:08 -0700 Subject: [PATCH] Added tests for flow control IMP. --- tests/4001_flowcontrol_exit.pvvs | 3 +++ .../4002_flowcontrol_unconditional_jump_to_label.pvvs | 9 +++++++++ tests/4003_flowcontrol_jump_if_tos_is_zero.pvvs | 10 ++++++++++ tests/4004_flowcontrol_jump_if_tos_is_negative.pvvs | 10 ++++++++++ tests/4005_flowcontrol_jump_to_subroutine.pvvs | 8 ++++++++ tests/4006_flowcontrol_return_from_subroutine.pvvs | 8 ++++++++ vv_interpreter.c | 9 +++++++-- vv_test.py | 6 ++++++ 8 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 tests/4001_flowcontrol_exit.pvvs create mode 100644 tests/4002_flowcontrol_unconditional_jump_to_label.pvvs create mode 100644 tests/4003_flowcontrol_jump_if_tos_is_zero.pvvs create mode 100644 tests/4004_flowcontrol_jump_if_tos_is_negative.pvvs create mode 100644 tests/4005_flowcontrol_jump_to_subroutine.pvvs create mode 100644 tests/4006_flowcontrol_return_from_subroutine.pvvs diff --git a/tests/4001_flowcontrol_exit.pvvs b/tests/4001_flowcontrol_exit.pvvs new file mode 100644 index 0000000..37ab178 --- /dev/null +++ b/tests/4001_flowcontrol_exit.pvvs @@ -0,0 +1,3 @@ +# This test verifies the flow control IMP exit. + +NNN | FC: Terminate program diff --git a/tests/4002_flowcontrol_unconditional_jump_to_label.pvvs b/tests/4002_flowcontrol_unconditional_jump_to_label.pvvs new file mode 100644 index 0000000..8e0e6d9 --- /dev/null +++ b/tests/4002_flowcontrol_unconditional_jump_to_label.pvvs @@ -0,0 +1,9 @@ +# This test verifies the flow control IMP for marking labels and +# unconditionally jumping to them.. + +SSSTSSSSSTN | ST: Push +65 (ASCII A) +NSNTSTSTSN | FC: Unconditional jump -> '42' +NNN | FC: Terminate program +NSSVTSTSTSN | FC: Mark label '42' +TNSS | IO: Output character +NNN | FC: Terminate program diff --git a/tests/4003_flowcontrol_jump_if_tos_is_zero.pvvs b/tests/4003_flowcontrol_jump_if_tos_is_zero.pvvs new file mode 100644 index 0000000..42002b4 --- /dev/null +++ b/tests/4003_flowcontrol_jump_if_tos_is_zero.pvvs @@ -0,0 +1,10 @@ +# This test verifies the flow control IMP for jumping if TOS is zero. + +SSSTSSSSSTN | ST: Push +65 (ASCII A) +SSSSN | ST: Push +0 +NTSTSTSTSN | FC: BRZ -> '42' +NNN | FC: Terminate program +NSSVTSTSTSN | FC: Mark label '42' +SNN | ST: Drop TOS +TNSS | IO: Output character +NNN | FC: Terminate program diff --git a/tests/4004_flowcontrol_jump_if_tos_is_negative.pvvs b/tests/4004_flowcontrol_jump_if_tos_is_negative.pvvs new file mode 100644 index 0000000..8517cd6 --- /dev/null +++ b/tests/4004_flowcontrol_jump_if_tos_is_negative.pvvs @@ -0,0 +1,10 @@ +# This test verifies the flow control IMP for jumping if TOS is negative. + +SSSTSSSSSTN | ST: Push +65 (ASCII A) +SSTTN | ST: Push -1 +NTTTSTSTSN | FC: BLZ -> '42' +NNN | FC: Terminate program +NSSVTSTSTSN | FC: Mark label '42' +SNN | ST: Drop TOS +TNSS | IO: Output character +NNN | FC: Terminate program diff --git a/tests/4005_flowcontrol_jump_to_subroutine.pvvs b/tests/4005_flowcontrol_jump_to_subroutine.pvvs new file mode 100644 index 0000000..a33af59 --- /dev/null +++ b/tests/4005_flowcontrol_jump_to_subroutine.pvvs @@ -0,0 +1,8 @@ +# This test verifies the flow control IMP jsr. + +NSTTSTSTSN | FC: JSR -> '42' +NNN | FC: Terminate program +NSSVTSTSTSN | FC: Mark label '42' +SSSTSSSSSTN | ST: Push +65 (ASCII A) +TNSS | IO: Output character +NNN | FC: Terminate program diff --git a/tests/4006_flowcontrol_return_from_subroutine.pvvs b/tests/4006_flowcontrol_return_from_subroutine.pvvs new file mode 100644 index 0000000..749ca17 --- /dev/null +++ b/tests/4006_flowcontrol_return_from_subroutine.pvvs @@ -0,0 +1,8 @@ +# This test verifies the flow control IMP rts. + +NSTTSTSTSN | FC: JSR -> '42' +TNSS | IO: Output character +NNN | FC: Terminate program +NSSVTSTSTSN | FC: Mark label '42' +SSSTSSSSSTN | ST: Push +65 (ASCII A) +NTN | FC: RTS diff --git a/vv_interpreter.c b/vv_interpreter.c index 8fac7c2..aa88505 100644 --- a/vv_interpreter.c +++ b/vv_interpreter.c @@ -236,8 +236,11 @@ process_imp_flowcontrol(uint8_t * code, size_t * pc, int32_t ** sp, uint32_t * l break; case '\t': /* Call a subroutine. */ - *((*rsp)++) = *pc; - *pc = labels[parse_label(code, pc)]; + { + size_t temp_pc = labels[parse_label(code, pc)]; + *((*rsp)++) = *pc; + *pc = temp_pc; + } break; case '\n': /* Jump unconditionally to a label. */ @@ -254,10 +257,12 @@ process_imp_flowcontrol(uint8_t * code, size_t * pc, int32_t ** sp, uint32_t * l switch (next_code_byte(code,pc)) { case ' ': /* Jump to a label if TOS == 0 */ + /* TODO: Does WS pop or peek the TOS? */ if (stack_peek(sp,0) == 0) *pc = labels[parse_label(code, pc)]; break; case '\t': /* Jump to a label if TOS < 0. */ + /* TODO: Does WS pop or peek the TOS? */ if (stack_peek(sp,0) < 0) *pc = labels[parse_label(code, pc)]; break; case '\n': diff --git a/vv_test.py b/vv_test.py index 33a998b..68b4db8 100755 --- a/vv_test.py +++ b/vv_test.py @@ -25,6 +25,12 @@ tests = [ ['2004_arithmetic_division', 'A'], ['2005_arithmetic_remainder', 'A'], ['3001_heap', 'BCA'], + ['4001_flowcontrol_exit', ''], + ['4002_flowcontrol_unconditional_jump_to_label', 'A'], + ['4003_flowcontrol_jump_if_tos_is_zero', 'A'], + ['4004_flowcontrol_jump_if_tos_is_negative', 'A'], + ['4005_flowcontrol_jump_to_subroutine', 'A'], + ['4006_flowcontrol_return_from_subroutine', 'A'], ] for test in tests: -- 2.20.1