diff options
author | elioat <elioat@tilde.institute> | 2025-01-02 07:57:24 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2025-01-02 07:57:24 -0500 |
commit | 9cb6522022901d118ce3afb1120c2e8456046154 (patch) | |
tree | 945879046998299c4428e13edc9f5db9ff7d9d66 /awk/forth/test.forth | |
parent | e170508a1a6ab735b97d79369c1d6fc624ff57b9 (diff) | |
download | tour-9cb6522022901d118ce3afb1120c2e8456046154.tar.gz |
*
Diffstat (limited to 'awk/forth/test.forth')
-rw-r--r-- | awk/forth/test.forth | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/awk/forth/test.forth b/awk/forth/test.forth index c743478..ef8a4c7 100644 --- a/awk/forth/test.forth +++ b/awk/forth/test.forth @@ -67,4 +67,62 @@ 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 -- ) + dup 10 > if + dup 20 > if + ." Greater than 20" + then + ." Greater than 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 ) + +testing Conditionals in word definitions +: abs-test ( n -- |n| ) dup 0 < if negate then ; +-5 abs-test test 5 +5 abs-test test 5 + +testing Complex conditional word +: max-test ( n1 n2 -- max ) + 2dup > if + drop + else + nip + then ; +5 3 max-test test 5 +3 5 max-test test 5 + +( Try to use if outside of a definition - should error ) +testing Compile-only words +5 4 > if 42 then ( should print error about compile-only word ) + +testing Comparison operators +5 3 > test 1 ( 5 > 3 is true ) +3 5 > test 0 ( 3 > 5 is false ) +3 5 < test 1 ( 3 < 5 is true ) +5 3 < test 0 ( 5 < 3 is false ) +5 5 < test 0 ( 5 < 5 is false ) +5 5 > test 0 ( 5 > 5 is false ) + +testing Comparison in conditionals +: test-compare ( n -- ) +dup 5 > if ." Greater than 5" else +dup 5 < if ." Less than 5" else +." Equal to 5" then then ; +6 test-compare ( should print "Greater than 5" ) +4 test-compare ( should print "Less than 5" ) +5 test-compare ( should print "Equal to 5" ) + bye \ No newline at end of file |