-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.fs
More file actions
62 lines (47 loc) · 1.92 KB
/
stack.fs
File metadata and controls
62 lines (47 loc) · 1.92 KB
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
include utils.fs
struct
cell% field >stack-depth
cell% field >stack-max-depth
cell% field >stack-data
end-struct stack%
variable stack
: >>stack stack ! ;
: stack-depth stack @ >stack-depth ;
: stack-max-depth stack @ >stack-max-depth ;
: stack-data stack @ >stack-data ;
: deep cells + ;
: stack-init ( depth -- )
dup cells stack% nip + stack @ swap 0 fill
stack-max-depth ! ;
: stack-new dup stack% rot deep %allot >>stack stack-init ;
: stack-pop? stack-depth @ 0 > ;
: stack-push? stack-depth @ 1 + stack-max-depth @ <= ;
: assert-push assert( stack-push? ) ;
: assert-pop assert( stack-pop? ) ;
: stack-incr stack-depth 1+! ;
: stack-decr stack-depth 1-! ;
: stack-cell ( stack -- *cell )
stack-depth @ cells stack-data + ;
: stack-push ( n stack -- ) assert-push
stack-cell ! stack-incr ;
: stack-pop ( stack -- n ) assert-pop
stack-decr stack-cell @ ;
: clear-stack 0 stack-depth ! ;
: index-stack cells stack-data + ;
: revdex-stack cells stack-depth @ 1- swap - cells stack-data + ;
: pair@ dup index-stack @ swap revdex-stack @ ;
: pair! tuck index-stack ! revdex-stack ! ;
: rev-stack stack-depth @ 2 / 0 ?do i pair@ i pair! loop ;
: .stack-info ." depth: " stack-depth @ . ." , max: " stack-max-depth @ . ;
: .stack cr .stack-info cr stack-depth @ 0 ?do i index-stack @ . space loop ;
: stack-test 3 stack-new
assert( 3 stack-max-depth @ = )
assert( 0 stack-depth @ = )
1 stack-push
2 stack-push
3 stack-push
rev-stack
assert( stack-pop 1 = )
assert( stack-pop 2 = )
assert( stack-pop 3 = )
cr ." stack tests passed!" cr ;