-
Notifications
You must be signed in to change notification settings - Fork 4
/
extra-stacks.txt
46 lines (35 loc) · 1.14 KB
/
extra-stacks.txt
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
\ Adding simple stacks to ESP32forth - adapted from code found at
\ https://rosettacode.org/wiki/Stack#Forth by Bob Edwards Aug 2022
\ No bounds checking
: tuck ( n1 n2 -- n2 n1 n2 )
swap over
;
cell negate value -cell
: stack ( size -- )
create \ make a new dictionary entry using the name of the stack
here cell+ , \ initialise the stack pointer as an empty stack
cells allot \ allocate the storage space for the stack
;
: push ( n st -- )
tuck ( st n st -- )
@ \ read the stack pointer ( st n -- )
! \ store n on the top of stack ( st -- )
cell swap +! \ and increment the stack pointer
;
: pop ( st -- n )
-cell over +! \ decrement the stack pointer ( st -- )
@ \ read the stack pointer
@ \ read the value top of stack
;
: empty? ( st -- ? )
dup @ - cell+ 0=
;
\ Test words
10 stack st
1 st push
2 st push
3 st push
st empty? . \ 0 (false)
st pop . st pop . st pop . \ 3 2 1
st empty? . \ -1 (true)
\ end of code