-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle.os
78 lines (65 loc) · 2.57 KB
/
style.os
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
!
! BRACY/STYLE. Stacks of style codes.
!
! Copyright © 2012 James B. Moen.
!
! This program is free software: you can redistribute it and/or modify it
! under the terms of the GNU General Public License as published by the Free
! Software Foundation, either version 3 of the License, or (at your option)
! any later version.
!
! This program is distributed in the hope that it will be useful, but WITHOUT
! ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
! FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
! more details.
!
! You should have received a copy of the GNU General Public License along with
! this program. If not, see <http://www.gnu.org/licenses/>.
!
(prog
! STYLE STACK. A linked stack, implemented as a chain of LAYERs.
styleStack :− ref layer
! LAYER. Whenever the scanner encounters an OPEN TOKEN, we push a LAYER on the
! style stack. It holds STYLE, the current set of style codes, and TOKEN, the
! token corresponding to OPEN TOKEN's matching CLOSE TOKEN. We keep the stack
! shallow by run-length encoding, so COUNT tells how many times in a row we've
! pushed identical copies of STYLE and TOKEN. NEXT points to the next LAYER in
! the chain. See:
!
! Steele, Guy Lewis Jr. Sussman, Gerald J. "The Dream of a Lifetime: A Lazy
! Variable Extent Mechanism." Conference Record of the 1980 Lisp Conference.
! Stanford, California. August 1980, pp. 163-172.
layer :−
(tuple
var int count,
set style,
int token,
var ref layer next)
! IS EMPTY. Test if STACK is empty.
isEmpty :−
(form (styleStack stack) bool:
stack = nil)
! MAKE STYLE STACK. Return a new empty style stack.
makeStyleStack :−
(form () styleStack:
nil)
! PUSH. Push TOKEN and STYLE on STACK.
push :−
(form (var styleStack stack, int token, set style) void:
(if isEmpty(stack) ∨ stack↑.style ≠ style ∨ stack↑.token ≠ token
then (with ref var layer temp :− fromHeap(var layer)
do temp↑.count := 1
temp↑.style := style
temp↑.token := token
temp↑.next := stack
stack := temp{ref layer})
else stack↑.count += 1))
! POP. Pop TOKEN and STYLE off STACK. We assume STACK isn't empty.
pop :−
(form (var int token, var set style, var styleStack stack) void:
token := stack↑.token
style := stack↑.style
(if stack↑.count = 1
then stack := (stack↑.next also toHeap(stack))
else stack↑.count −= 1))
)