blob: d746e6680d1cb92c484995f7851f8c37391b918d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
( Basic Forth test suite )
testing "Basic stack operations"
5 dup . . test 5 test 5
testing "Addition"
3 4 + test 7
testing "Subtraction"
10 3 - test 7
testing "Multiplication"
6 7 * test 42
testing "Division"
20 4 / test 5
testing "Stack manipulation - rot"
1 2 3 rot test 1
testing "Stack manipulation - drop"
1 2 3 drop test 2
testing "Stack manipulation - nip"
1 2 3 nip test 3
testing "Stack manipulation - tuck"
1 2 tuck test 2
testing "Stack manipulation - over"
1 2 over test 1
testing "Variables"
variable x
5 x !
x @ test 5
10 x !
x ? ( should print 10 )
testing "Negate"
5 negate test -5
testing "Absolute value"
-7 abs test 7
testing "Maximum"
3 8 max test 8
testing "Minimum"
3 8 min test 3
testing "Modulo"
17 5 mod test 2
testing "Equality"
5 5 = test 1
5 6 = test 0
testing "Word definition"
: square dup * ;
5 square test 25
testing "Complex word definition"
variable counter
: increment-counter counter @ 1 + counter ! ;
5 counter !
increment-counter
counter @ test 6
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
." >20 "
then
." >10 "
then ;
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 ;
-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
|