erlang: Rename language from `erl'.
[fringe] / forth-fringe.fth
1 \ -*-forth-*-
2 \
3 \ Same-fringe solver in Forth.
4
5 \ ---------------------------------------------------------------------------
6 \ Utilities. Most of these are GForth-specific in some way.
7
8 \ String representation conversions.
9
10 : string>bounds ( c-addr u -- c-addr-limit c-addr )
11 \ Convert a string in the usual base/length form to a limit/base form
12 \ which is better suited to iteration. The base is left on the top
13 \ because it's likely to change more frequently.
14 chars over + swap ;
15
16 : bounds>string ( c-addr-limit c-addr -- c-addr u )
17 \ Convert a string in limit/base form back to base/length form.
18 tuck - [ 1 chars ] literal / ;
19
20 \ Program name. Want the portion after the rightmost `/'.
21 \
22 \ Bodge: gforth doesn't want to hand over the image filename so we'll have to
23 \ hardwire.
24
25 : quis s" forth-fringe" ;
26
27 \ Error reporting.
28
29 : ouch ( a-addr u -- program exits )
30 \ Report an error message on stderr and exit with a nonzero status.
31 quis stderr write-file drop
32 s" : " stderr write-file drop
33 2dup stderr write-line drop
34 1 (bye) \ Gforth specific
35 ;
36
37 \ ---------------------------------------------------------------------------
38 \ Coroutines. Largely very scary.
39
40 \ A coroutine descriptor consists of a single cell containing the coroutine's
41 \ return-stack pointer. This cell is only valid when the coroutine is
42 \ inactive.
43 \
44 \ Coroutines have distinct return stacks, but share the main value stack and
45 \ floating-point stack, which they can use for communication with other
46 \ coroutines. A coroutine will therefore typically stash state on the return
47 \ stack.
48 \
49 \ There's no current provision for Gforth's separate locals stack.
50
51 \ The amount of return-stack storage we allocate to a coroutine.
52 256 cells constant cr-space
53
54 \ The current coroutine. This initially points to an uninitialized
55 \ descriptor which we'll fill in during the first coroutine switch.
56 variable current-cr
57 here current-cr ! cell allot
58
59 \ The coroutine which invoked this one. This is used by `yield'.
60 variable caller-cr
61
62 : switch-cr ( cr -- )
63 \ Make `cr' the current coroutine, and tell it that it was called by this
64 \ one.
65 rp@ current-cr @ !
66 current-cr @ caller-cr !
67 dup current-cr !
68 @ rp!
69 ;
70
71 : yield ( -- )
72 \ Make the calling coroutine current again.
73 caller-cr @ switch-cr
74 ;
75
76 : start-cr ( cr xt -- )
77 \ Switch to the new coroutine `cr', and have it execute the token `xt'.
78 swap
79 rp@ current-cr @ !
80 current-cr @ caller-cr !
81 dup current-cr !
82 @ rp!
83 execute
84 ;
85
86 : init-cr ( a-addr -- cr )
87 \ Initialize a chunk of memory at `a-addr' and turn it into a pointer to
88 \ a coroutine descriptor `cr' ready for use by `start-cr'.
89 [ cr-space cell - ] literal +
90 dup dup !
91 ;
92
93 : [alloc-cr] ( -- cr ; R: -- cr-sys )
94 \ Compile-time word: adjust the return stack pointer, returning a
95 \ coroutine descriptor `cr'. The space can be recovered using
96 \ `[drop-cr]'. This must be done at compile time, because returning is
97 \ hard after you've messed with the return stack pointer.
98 postpone rp@ postpone cr-space postpone - postpone dup
99 postpone rp! postpone init-cr
100 ; immediate
101
102 : [drop-cr] ( cr -- ; R: cr-sys -- )
103 \ Compile-time word: adjust the return-stack pointer to reclaim the space
104 \ used for the coroutine `cr' and all those above it on the return stack.
105 postpone cell postpone + postpone rp!
106 ; immediate
107
108 \ ---------------------------------------------------------------------------
109 \ Iterator protocol.
110 \
111 \ An iterator is a coroutine which yields a word and a flag. While there are
112 \ items available, it yields items paired with `true' flags; when all items
113 \ are exhausted, it yields a word and a `false' flag. After that, invoking
114 \ the coroutine again is invalid.
115
116 : print-iterator ( cr -- )
117 \ Print the characters returned by the iterator coroutine `cr'.
118 begin dup switch-cr while emit repeat
119 drop
120 ;
121
122 : same-iterators-p ( cr0 cr1 -- f )
123 \ Report true if the iterator coroutines `cr0' and `cr1' return the same
124 \ items in the same order, as determined by `='.
125 begin
126 over switch-cr ( cr0 cr1 x0 f0 )
127 2 pick switch-cr ( cr0 cr1 x0 f0 x1 f1 )
128 rot ( cr0 cr1 x0 x1 f1 f0 )
129 over <> if 2drop 2drop drop false exit then
130 0= if 2drop 2drop true exit then
131 <> if 2drop false exit then
132 again
133 ;
134
135 \ ---------------------------------------------------------------------------
136 \ Binary trees.
137
138 : make-tree ( a-addr-left w-datum a-addr-right -- a-addr-tree )
139 \ Construct a binary tree from components on the stack, returning the
140 \ address of the tree node.
141 here >r \ stash pointer
142 swap rot , , , \ reorder and store
143 r> \ recover pointer
144 ;
145
146 \ A leaf is an empty tree. The address of this variable is important; its
147 \ contents are not.
148 variable leaf
149
150 \ Binary tree structure.
151 : tree-left ( a-addr -- a-addr' ) ;
152 : tree-datum ( a-addr -- a-addr' ) cell+ ;
153 : tree-right ( a-addr -- a-addr' ) [ 2 cells ] literal + ;
154 3 constant tree-ncells
155
156 : parse-subtree ( c-addr-limit c-addr -- c-addr-limit c-addr' tree )
157 \ Parse a subtree from the string on the stack (in limit/base form).
158 \ Update the string to reflect how much we consumed, and leave the tree
159 \ address for the caller. See `parse-tree' for the syntax.
160 2dup > if dup c@ [char] ( <> else true then if
161 leaf
162 else
163 char+
164 leaf 0 leaf make-tree >r
165 recurse r@ tree-left !
166 2dup <= if s" no data" ouch then
167 dup c@ r@ tree-datum ! char+
168 recurse r@ tree-right !
169 2dup <= if true else dup c@ [char] ) <> then if
170 s" missing )" ouch
171 then
172 char+
173 r>
174 then
175 ;
176
177 : parse-tree ( c-addr u -- tree )
178 \ Parse a tree from the string on the stack.
179 \
180 \ The syntax is simple:
181 \
182 \ tree :: empty | `(' tree char tree `)'
183 \
184 \ The ambiguity is resolved by always treating `(' as a tree when a tree
185 \ is expected.
186 string>bounds
187 parse-subtree >r
188 <> if s" trailing junk" ouch then
189 r>
190 ;
191
192 : do-tree-fringe ( tree -- yields: x f )
193 \ Helper word for `tree-fringe' below. Recursively yields up the items
194 \ of the subtree rooted at `tree'.
195 dup leaf = if
196 drop
197 else
198 >r
199 r@ tree-left @ recurse
200 r@ tree-datum @ true yield
201 r> tree-right @ recurse
202 then
203 ;
204
205 : tree-fringe ( tree -- yields: x f )
206 \ Yield up the items of `tree' in order, according to the iteration
207 \ protocol.
208 >r yield
209 r> do-tree-fringe
210 0 false yield
211 ;
212
213 \ ---------------------------------------------------------------------------
214 \ Main program.
215
216 : main
217 \ Main program: parse arguments and do what's asked for.
218 argc @ case
219
220 2 of
221 \ One proper argument: parse a tree and print its fringe.
222 [alloc-cr]
223 1 arg parse-tree over ['] tree-fringe start-cr
224 dup print-iterator cr
225 [drop-cr]
226 endof
227
228 3 of
229 \ Two arguments: parse two trees and compare them.
230 [alloc-cr] 1 arg parse-tree over ['] tree-fringe start-cr
231 dup
232 [alloc-cr] 2 arg parse-tree over ['] tree-fringe start-cr
233 same-iterators-p
234 swap [drop-cr]
235 if ." match" else ." no match" then cr
236 endof
237
238 \ Default.
239 s" bad args" ouch
240
241 endcase
242 ;
243
244 \ Gforth image magic.
245 :noname
246 defers 'cold
247 main
248 bye
249 ; is 'cold
250
251 \ ---------------------------------------------------------------------------