| 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 | \ Structures. |
| 28 | |
| 29 | : defstruct ( -- struct-sys ) |
| 30 | \ Commence a new structure. |
| 31 | 0 ; |
| 32 | |
| 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 |
| 36 | \ base address. |
| 37 | create over , + does> @ + ; |
| 38 | |
| 39 | : endstruct ( "name" struct-sys' -- ) |
| 40 | \ End a structure definition. The word `name' becomes a constant |
| 41 | \ containing the requires size of the structure. |
| 42 | create , does> @ ; |
| 43 | |
| 44 | \ Error reporting. |
| 45 | |
| 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 |
| 52 | ; |
| 53 | |
| 54 | \ --------------------------------------------------------------------------- |
| 55 | \ Coroutines. Largely very scary. |
| 56 | |
| 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 |
| 59 | \ inactive. |
| 60 | \ |
| 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 |
| 64 | \ stack. |
| 65 | \ |
| 66 | \ There's no current provision for Gforth's separate locals stack. |
| 67 | |
| 68 | \ The amount of return-stack storage we allocate to a coroutine. |
| 69 | 256 cells constant cr-space |
| 70 | |
| 71 | \ Coroutine descriptors. |
| 72 | defstruct |
| 73 | cell slot cr-sp |
| 74 | endstruct cr-size |
| 75 | |
| 76 | \ The current coroutine. This initially points to an uninitialized |
| 77 | \ descriptor which we'll fill in during the first coroutine switch. |
| 78 | variable current-cr |
| 79 | here current-cr ! cr-size allot |
| 80 | |
| 81 | \ The coroutine which invoked this one. This is used by `yield'. |
| 82 | variable caller-cr |
| 83 | |
| 84 | : switch-cr ( cr -- ) |
| 85 | \ Make `cr' the current coroutine, and tell it that it was called by this |
| 86 | \ one. |
| 87 | rp@ current-cr @ cr-sp ! |
| 88 | current-cr @ caller-cr ! |
| 89 | dup current-cr ! |
| 90 | cr-sp @ rp! |
| 91 | ; |
| 92 | |
| 93 | : yield ( -- ) |
| 94 | \ Make the calling coroutine current again. |
| 95 | caller-cr @ switch-cr |
| 96 | ; |
| 97 | |
| 98 | : start-cr ( cr xt -- ) |
| 99 | \ Switch to the new coroutine `cr', and have it execute the token `xt'. |
| 100 | swap |
| 101 | rp@ current-cr @ cr-sp ! |
| 102 | current-cr @ caller-cr ! |
| 103 | dup current-cr ! |
| 104 | cr-sp @ rp! |
| 105 | execute |
| 106 | ; |
| 107 | |
| 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 + |
| 112 | dup dup cr-sp ! |
| 113 | ; |
| 114 | |
| 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 |
| 122 | ; immediate |
| 123 | |
| 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! |
| 128 | ; immediate |
| 129 | |
| 130 | \ --------------------------------------------------------------------------- |
| 131 | \ Iterator protocol. |
| 132 | \ |
| 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. |
| 137 | |
| 138 | : print-iterator ( cr -- ) |
| 139 | \ Print the characters returned by the iterator coroutine `cr'. |
| 140 | begin dup switch-cr while emit repeat |
| 141 | drop |
| 142 | ; |
| 143 | |
| 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 `='. |
| 147 | begin |
| 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 |
| 154 | again |
| 155 | ; |
| 156 | |
| 157 | \ --------------------------------------------------------------------------- |
| 158 | \ Binary trees. |
| 159 | |
| 160 | \ A leaf is an empty tree. The address of this variable is important; its |
| 161 | \ contents are not. |
| 162 | variable leaf |
| 163 | |
| 164 | \ Binary tree structure. |
| 165 | defstruct |
| 166 | cell slot tree-left |
| 167 | cell slot tree-datum |
| 168 | cell slot tree-right |
| 169 | endstruct tree-size |
| 170 | |
| 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 |
| 176 | r> \ recover pointer |
| 177 | ; |
| 178 | |
| 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 |
| 184 | leaf |
| 185 | else |
| 186 | char+ |
| 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 |
| 193 | s" missing )" ouch |
| 194 | then |
| 195 | char+ |
| 196 | r> |
| 197 | then |
| 198 | ; |
| 199 | |
| 200 | : parse-tree ( c-addr u -- tree ) |
| 201 | \ Parse a tree from the string on the stack. |
| 202 | \ |
| 203 | \ The syntax is simple: |
| 204 | \ |
| 205 | \ tree :: empty | `(' tree char tree `)' |
| 206 | \ |
| 207 | \ The ambiguity is resolved by always treating `(' as a tree when a tree |
| 208 | \ is expected. |
| 209 | string>bounds |
| 210 | parse-subtree >r |
| 211 | <> if s" trailing junk" ouch then |
| 212 | r> |
| 213 | ; |
| 214 | |
| 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'. |
| 218 | dup leaf = if |
| 219 | drop |
| 220 | else |
| 221 | >r |
| 222 | r@ tree-left @ recurse |
| 223 | r@ tree-datum @ true yield |
| 224 | r> tree-right @ recurse |
| 225 | then |
| 226 | ; |
| 227 | |
| 228 | : tree-fringe ( tree -- yields: x f ) |
| 229 | \ Yield up the items of `tree' in order, according to the iteration |
| 230 | \ protocol. |
| 231 | >r yield |
| 232 | r> do-tree-fringe |
| 233 | 0 false yield |
| 234 | ; |
| 235 | |
| 236 | \ --------------------------------------------------------------------------- |
| 237 | \ Main program. |
| 238 | |
| 239 | : main |
| 240 | \ Main program: parse arguments and do what's asked for. |
| 241 | argc @ case |
| 242 | |
| 243 | 2 of |
| 244 | \ One proper argument: parse a tree and print its fringe. |
| 245 | [alloc-cr] |
| 246 | 1 arg parse-tree over ['] tree-fringe start-cr |
| 247 | print-iterator cr |
| 248 | [drop-cr] |
| 249 | endof |
| 250 | |
| 251 | 3 of |
| 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 |
| 255 | same-iterators-p |
| 256 | [drop-cr] [drop-cr] |
| 257 | if ." match" else ." no match" then cr |
| 258 | endof |
| 259 | |
| 260 | \ Default. |
| 261 | s" bad args" ouch |
| 262 | |
| 263 | endcase |
| 264 | ; |
| 265 | |
| 266 | \ Gforth image magic. |
| 267 | :noname |
| 268 | defers 'cold |
| 269 | main |
| 270 | bye |
| 271 | ; is 'cold |
| 272 | |
| 273 | \ --------------------------------------------------------------------------- |