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 : defstruct ( -- struct-sys )
30 \ Commence a new structure.
33 : slot ( "name" struct-sys u -- struct-sys' )
34 \ Add a new slot called `name', `u' units in size. The word `name'
35 \ applies the necessary offset to find the slot given the structure's
37 create over , + does> @ + ;
39 : endstruct ( "name" struct-sys' -- )
40 \ End a structure definition. The word `name' becomes a constant
41 \ containing the requires size of the structure.
46 : ouch ( a-addr u -- program exits )
47 \ Report an error message on stderr and exit with a nonzero status.
48 quis stderr write-file drop
49 s" : " stderr write-file drop
50 2dup stderr write-line drop
51 1 (bye) \ Gforth specific
54 \ ---------------------------------------------------------------------------
55 \ Coroutines. Largely very scary.
57 \ A coroutine descriptor consists of a single cell containing the coroutine's
58 \ return-stack pointer. This cell is only valid when the coroutine is
61 \ Coroutines have distinct return stacks, but share the main value stack and
62 \ floating-point stack, which they can use for communication with other
63 \ coroutines. A coroutine will therefore typically stash state on the return
66 \ There's no current provision for Gforth's separate locals stack.
68 \ The amount of return-stack storage we allocate to a coroutine.
69 256 cells constant cr-space
71 \ Coroutine descriptors.
76 \ The current coroutine. This initially points to an uninitialized
77 \ descriptor which we'll fill in during the first coroutine switch.
79 here current-cr ! cr-size allot
81 \ The coroutine which invoked this one. This is used by `yield'.
85 \ Make `cr' the current coroutine, and tell it that it was called by this
87 rp@ current-cr @ cr-sp !
88 current-cr @ caller-cr !
94 \ Make the calling coroutine current again.
98 : start-cr ( cr xt -- )
99 \ Switch to the new coroutine `cr', and have it execute the token `xt'.
101 rp@ current-cr @ cr-sp !
102 current-cr @ caller-cr !
108 : init-cr ( a-addr -- cr )
109 \ Initialize a chunk of memory at `a-addr' and turn it into a pointer to
110 \ a coroutine descriptor `cr' ready for use by `start-cr'.
111 [ cr-space cr-size - ] literal +
115 : [alloc-cr] ( -- cr ; R: -- cr-sys )
116 \ Compile-time word: adjust the return stack pointer, returning a
117 \ coroutine descriptor `cr'. The space can be recovered using
118 \ `[drop-cr]'. This must be done at compile time, because returning is
119 \ hard after you've messed with the return stack pointer.
120 postpone rp@ postpone cr-space postpone - postpone dup
121 postpone rp! postpone init-cr
124 : [drop-cr] ( R: cr-sys -- )
125 \ Compile-time word: adjust the return-stack pointer to reclaim the space
126 \ used by a coroutine.
127 postpone rp@ postpone cr-space postpone + postpone rp!
130 \ ---------------------------------------------------------------------------
133 \ An iterator is a coroutine which yields a word and a flag. While there are
134 \ items available, it yields items paired with `true' flags; when all items
135 \ are exhausted, it yields a word and a `false' flag. After that, invoking
136 \ the coroutine again is invalid.
138 : print-iterator ( cr -- )
139 \ Print the characters returned by the iterator coroutine `cr'.
140 begin dup switch-cr while emit repeat
144 : same-iterators-p ( cr0 cr1 -- f )
145 \ Report true if the iterator coroutines `cr0' and `cr1' return the same
146 \ items in the same order, as determined by `='.
148 over switch-cr ( cr0 cr1 x0 f0 )
149 2 pick switch-cr ( cr0 cr1 x0 f0 x1 f1 )
150 rot ( cr0 cr1 x0 x1 f1 f0 )
151 over <> if 2drop 2drop drop false exit then
152 0= if 2drop 2drop true exit then
153 <> if 2drop false exit then
157 \ ---------------------------------------------------------------------------
160 \ A leaf is an empty tree. The address of this variable is important; its
164 \ Binary tree structure.
171 : make-tree ( a-addr-left w-datum a-addr-right -- a-addr-tree )
172 \ Construct a binary tree from components on the stack, returning the
173 \ address of the tree node.
174 here >r \ stash pointer
175 swap rot , , , \ reorder and store
179 : parse-subtree ( c-addr-limit c-addr -- c-addr-limit c-addr' tree )
180 \ Parse a subtree from the string on the stack (in limit/base form).
181 \ Update the string to reflect how much we consumed, and leave the tree
182 \ address for the caller. See `parse-tree' for the syntax.
183 2dup > if dup c@ [char] ( <> else true then if
187 leaf 0 leaf make-tree >r
188 recurse r@ tree-left !
189 2dup <= if s" no data" ouch then
190 dup c@ r@ tree-datum ! char+
191 recurse r@ tree-right !
192 2dup <= if true else dup c@ [char] ) <> then if
200 : parse-tree ( c-addr u -- tree )
201 \ Parse a tree from the string on the stack.
203 \ The syntax is simple:
205 \ tree :: empty | `(' tree char tree `)'
207 \ The ambiguity is resolved by always treating `(' as a tree when a tree
211 <> if s" trailing junk" ouch then
215 : do-tree-fringe ( tree -- yields: x f )
216 \ Helper word for `tree-fringe' below. Recursively yields up the items
217 \ of the subtree rooted at `tree'.
222 r@ tree-left @ recurse
223 r@ tree-datum @ true yield
224 r> tree-right @ recurse
228 : tree-fringe ( tree -- yields: x f )
229 \ Yield up the items of `tree' in order, according to the iteration
236 \ ---------------------------------------------------------------------------
240 \ Main program: parse arguments and do what's asked for.
244 \ One proper argument: parse a tree and print its fringe.
246 1 arg parse-tree over ['] tree-fringe start-cr
252 \ Two arguments: parse two trees and compare them.
253 [alloc-cr] 1 arg parse-tree over ['] tree-fringe start-cr
254 [alloc-cr] 2 arg parse-tree over ['] tree-fringe start-cr
257 if ." match" else ." no match" then cr
266 \ Gforth image magic.
273 \ ---------------------------------------------------------------------------