3 \ Same-fringe solver in Forth.
5 \ ---------------------------------------------------------------------------
6 \ Utilities. Most of these are GForth-specific in some way.
8 \ String representation conversions.
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.
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 / ;
20 \ Program name. Want the portion after the rightmost `/'.
22 \ Bodge: gforth doesn't want to hand over the image filename so we'll have to
25 : quis s" forth-fringe" ;
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
37 \ ---------------------------------------------------------------------------
38 \ Coroutines. Largely very scary.
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
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
49 \ There's no current provision for Gforth's separate locals stack.
51 \ The amount of return-stack storage we allocate to a coroutine.
52 256 cells constant cr-space
54 \ The current coroutine. This initially points to an uninitialized
55 \ descriptor which we'll fill in during the first coroutine switch.
57 here current-cr ! cell allot
59 \ The coroutine which invoked this one. This is used by `yield'.
63 \ Make `cr' the current coroutine, and tell it that it was called by this
66 current-cr @ caller-cr !
72 \ Make the calling coroutine current again.
76 : start-cr ( cr xt -- )
77 \ Switch to the new coroutine `cr', and have it execute the token `xt'.
80 current-cr @ caller-cr !
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 +
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
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!
108 \ ---------------------------------------------------------------------------
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.
116 : print-iterator ( cr -- )
117 \ Print the characters returned by the iterator coroutine `cr'.
118 begin dup switch-cr while emit repeat
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 `='.
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
135 \ ---------------------------------------------------------------------------
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
146 \ A leaf is an empty tree. The address of this variable is important; its
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
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
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
177 : parse-tree ( c-addr u -- tree )
178 \ Parse a tree from the string on the stack.
180 \ The syntax is simple:
182 \ tree :: empty | `(' tree char tree `)'
184 \ The ambiguity is resolved by always treating `(' as a tree when a tree
188 <> if s" trailing junk" ouch then
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'.
199 r@ tree-left @ recurse
200 r@ tree-datum @ true yield
201 r> tree-right @ recurse
205 : tree-fringe ( tree -- yields: x f )
206 \ Yield up the items of `tree' in order, according to the iteration
213 \ ---------------------------------------------------------------------------
217 \ Main program: parse arguments and do what's asked for.
221 \ One proper argument: parse a tree and print its fringe.
223 1 arg parse-tree over ['] tree-fringe start-cr
224 dup print-iterator cr
229 \ Two arguments: parse two trees and compare them.
230 [alloc-cr] 1 arg parse-tree over ['] tree-fringe start-cr
232 [alloc-cr] 2 arg parse-tree over ['] tree-fringe start-cr
235 if ." match" else ." no match" then cr
244 \ Gforth image magic.
251 \ ---------------------------------------------------------------------------