From 9e28c1564b02cfc08f7f83c09b97de77f18e0d90 Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Wed, 31 Jul 2019 19:12:17 -0700 Subject: [PATCH] Updated VVhitespace's modulo function to always return positive values. --- reference.md | 2 +- tests/2005_arithmetic_remainder.pvvs | 24 ++++++++++++++++++++++++ tests/vv_test.py | 2 +- vv_interpreter.c | 2 +- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/reference.md b/reference.md index 411605c..99b4ffe 100644 --- a/reference.md +++ b/reference.md @@ -66,7 +66,7 @@ the IMP [Space]. There are four stack instructions. Arithmetic commands operate on the top two items on the stack, and replace them with the result of the operation. The first item pushed is considered to be -left of the operator. +left of the operator. The modulo command will always return a positive result. | Command | Params | Meaning | | :------------- | :----- | :--------------- | diff --git a/tests/2005_arithmetic_remainder.pvvs b/tests/2005_arithmetic_remainder.pvvs index d54c381..e4ae177 100644 --- a/tests/2005_arithmetic_remainder.pvvs +++ b/tests/2005_arithmetic_remainder.pvvs @@ -1,7 +1,31 @@ # This test verifies arithmetic IMP remainder. +# First, verify basic function with two positive numbers. SSSTTSSSSTSN | ST: Push +194 SSSTSSSSSSTN | ST: Push +128 TSTT | MA: Remainder TNSS | IO: Output character + +# The VVS modulo function should always return a positive value. +# C-derived languages (among others) do not share this convention. + +# This test verifies that -3 mod 2 = 1. +SSTTTN | ST: Push -3 +SSSTSN | ST: Push +2 +TSTT | MA: Remainder +TNST | IO: Output digit + +# This test verifies that -3 mod -2 = 1. +SSTTTN | ST: Push -3 +SSTTSN | ST: Push -2 +TSTT | MA: Remainder +TNST | IO: Output digit + +# This test verifies that -3 mod -(2^63) = 3. +SSTTTN | ST: Push -3 +SSTTSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSN | PUSH -(2^63) +TSTT | MA: Remainder +TNST | IO: Output digit + +# All done NNN | FC: Terminate program diff --git a/tests/vv_test.py b/tests/vv_test.py index c0853ef..fd7ab78 100755 --- a/tests/vv_test.py +++ b/tests/vv_test.py @@ -24,7 +24,7 @@ tests = [ ['2002_arithmetic_subtraction', '', 'A'], ['2003_arithmetic_multiplication', '', 'B'], ['2004_arithmetic_division', '', 'A'], - ['2005_arithmetic_remainder', '', 'A'], + ['2005_arithmetic_remainder', '', 'A113'], ['3001_heap', '', 'BCA'], ['4001_flowcontrol_exit', '', ''], ['4002_flowcontrol_unconditional_jump_to_label', '', 'A'], diff --git a/vv_interpreter.c b/vv_interpreter.c index 5ec36eb..b548a50 100644 --- a/vv_interpreter.c +++ b/vv_interpreter.c @@ -253,7 +253,7 @@ process_imp_arithmetic(uint8_t * code, size_t * pc, int64_t ** sp) case '\t': /* Modulo */ temp = stack_pop(sp); - stack_push(sp, stack_pop(sp)%temp); + stack_push(sp, llabs(stack_pop(sp) % llabs(temp))); break; default: ws_die(pc, "malformed arithmetic IMP"); break; } -- 2.20.1