diff options
author | elioat <elioat@tilde.institute> | 2025-01-02 08:22:55 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2025-01-02 08:22:55 -0500 |
commit | 2c154a34f478285680efeee7c15ba02116ee882d (patch) | |
tree | 2d56292f9a827352ade4ee114a2ee6fc93718384 /awk/forth/test.forth | |
parent | 9cb6522022901d118ce3afb1120c2e8456046154 (diff) | |
download | tour-2c154a34f478285680efeee7c15ba02116ee882d.tar.gz |
*
Diffstat (limited to 'awk/forth/test.forth')
-rw-r--r-- | awk/forth/test.forth | 63 |
1 files changed, 46 insertions, 17 deletions
diff --git a/awk/forth/test.forth b/awk/forth/test.forth index ef8a4c7..2d4197b 100644 --- a/awk/forth/test.forth +++ b/awk/forth/test.forth @@ -67,27 +67,56 @@ variable counter increment-counter counter @ test 6 -testing Conditionals - basic if/then -: test-if-1 ( n -- ) 5 > if ." Greater than 5" then ; -6 test-if-1 ( should print "Greater than 5" ) -4 test-if-1 ( should print nothing ) - -testing Conditionals - if/else/then -: test-if-2 ( n -- ) 5 > if ." Greater than 5" else ." Less than or equal to 5" then ; -6 test-if-2 ( should print "Greater than 5" ) -4 test-if-2 ( should print "Less than or equal to 5" ) - -testing Conditionals - nested if/then -: test-if-3 ( n -- ) +testing Basic conditional - if/then +: test-if-1 ( n -- n ) dup 5 > if ." Greater than 5" then ; +6 test-if-1 test 6 ( should print "Greater than 5" and leave 6 ) +4 test-if-1 test 4 ( should print nothing and leave 4 ) + +testing Basic conditional - if/else/then +: test-if-2 ( n -- n ) dup 5 > if ." Greater" else ." Less=" then ; +6 test-if-2 test 6 ( should print "Greater" and leave 6 ) +4 test-if-2 test 4 ( should print "Less=" and leave 4 ) +5 test-if-2 test 5 ( should print "Less=" and leave 5 ) + +testing Nested conditionals +: test-if-3 ( n -- n ) dup 10 > if dup 20 > if - ." Greater than 20" + ." >20 " then - ." Greater than 10" + ." >10 " then ; -25 test-if-3 ( should print "Greater than 20Greater than 10" ) -15 test-if-3 ( should print "Greater than 10" ) -5 test-if-3 ( should print nothing ) +25 test-if-3 test 25 ( should print ">20 >10 " and leave 25 ) +15 test-if-3 test 15 ( should print ">10 " and leave 15 ) +5 test-if-3 test 5 ( should print nothing and leave 5 ) + +testing Conditional with stack operations +: test-if-4 ( n -- n n ) + dup 5 > if + dup + then ; +6 test-if-4 swap test 6 test 6 ( should leave 6 6 ) +4 test-if-4 test 4 ( should leave just 4 ) + +testing Complex nested conditionals +: test-if-5 ( n -- n ) + dup 0 < if + ." negative " + else + dup 100 > if + ." big " + else + dup 50 > if + ." medium " + else + ." small " + then + then + then ; +-5 test-if-5 test -5 ( should print "negative " ) +150 test-if-5 test 150 ( should print "big " ) +75 test-if-5 test 75 ( should print "medium " ) +25 test-if-5 test 25 ( should print "small " ) testing Conditionals in word definitions : abs-test ( n -- |n| ) dup 0 < if negate then ; |