Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uwm.edu!zaphod.mps.ohio-state.edu!usc!ucsd!ucbvax!UNHH.BITNET!D_FELDMA From: D_FELDMA@UNHH.BITNET Newsgroups: comp.lang.forth Subject: A word on my Forth wishlist Message-ID: <9001221802.AA23796@jade.berkeley.edu> Date: 22 Jan 90 07:01:00 GMT Sender: daemon@ucbvax.BERKELEY.EDU Reply-To: Forth Interest Group International List Organization: The Internet Lines: 49 For me, the most tedious aspect of programming in Forth is getting items on the data stack in the desired order. It would be nice to have a word, say, REORGANIZE that would lift this burden. I can think of two ways that this might work: RUNTIME version: a_1 a_2 ... a_n str1 str2 REORGANIZE ====> a_j_1 ... a_j_m where the a_i are arbitrary data elements, and str1 and str2 are (addresses of) strings, so that for example: a_1 a_2 a_3 (abc) (cbbac) REORGANIZE ===> a_3 a_2 a_2 a_1 a_3 This would be easy enough to write, but slow. COMPILETIME version: str1 str2 REORGANIZE as a string of Forth words to accomplish the desired rearrangement. For example: (a) (aa) REORGANIZE would compile as dup (ab) (ba) REORGANIZE would compile as swap (ab) (aba) REORGANIZE would compile as over (ba) (baaba) REORGANIZE might compile as over over swap over or dup dup 4 pick swap or ??? (I hope I have the numbering convention for pick correct; I've been writing a lot of PostScript lately.) The point is that to write such a word, one must solve a (possibly implementation dependent) optimization problem. The reason for the dependency on implementation is that on any given system, e.g. pick might be more or less expensive than swap. So problem: given the relative cost of Forth's stack primitives in a given implementation, generate the cheapest sequence for rearranging the stack in a given way. Or easier, just find one way to implement REORGANIZE that's presumably close to optimal. If the word generated different code on different systems, but all with the same runtime behavior, would it still be considered portable? Having such a word would make Forth programs more readable (because a series of stack manipulations is seldom transparent at first glance), and possibly more efficient (if REORGANIZE could be optimized.) In particular, it would allow one to do on the stack what one might do less efficiently with reads and writes to memory while not compromising the readability of one's code. Any thoughts on how this might be done? On on why one would or wouldn't want to, if it could? David Feldman d_feldman@UNHH