Re: using MISC (post 1987 Forth hardware) opcodes
- From: John Doty <jpd@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 17 Jul 2006 10:47:03 -0600
Adam Warner wrote:
We agree I did not provide a good enough example to support my claim. This
one should be bulletproof: You have a reason to trace though a deep
tree-like data structure.
Ok, that might be an application requirement. Not a particularly common one in my experience.
Mechanically this involves pushing paths you
still need to travel onto a stack every time you branch within the tree.
Recursion makes this mechanical process invisible.
Here's part of the data structure. I'll describe tracing through the right
size of the solid region:
a
/ \
b c
. / \
. d e
. . . / \
. . f g
. . / \
. h i
. / \
. j k
. / \
. l m
. / \
. n o
. / \
. p q
. .
. .
. . .
. . .
1. Push (b) and process (c).
2. Push (d) and process (e).
3. Push (f) and process (g).
4. Push (h) and process (i).
5. Push (j) and process (k).
6. Push (l) and process (m).
7. Push (n) and process (o).
8. Push (p) and process (q). Return.
9. Pop (p) and process. Return.
10. Pop (n) and process. Return.
11. Pop (l) and process. Return.
12. Pop (j) and process. Return.
13. Pop (h) and process. Return.
14. Pop (f) and process. Return.
15. Pop (d) and process. Return.
16. Pop (b) and process. Return.
An environment with six cell stacks cannot traverse this data structure
(without implementing userspace stacks out of heap memory).
So put a stack in memory. It doesn't necessarily have to be heap if you have a reasonable upper bound on the required stack size.
No application requirement forces you to use the number stack to store a complicated data structure. Indeed, I expect most experienced Forthers would not do it this way.
When I did something similar many years ago, I found it useful to keep the tree traversal state on a separate stack even though I had a big number stack in that particular Forth implementation. For this kind of problem, a separate stack helps minimize stack gymnastics and facilitates factoring. These advantages more than compensate for the trivial extra complexity of explicitly creating and destroying frames on an auxiliary stack.
A language likes C creates and destroys frames suitable for this kind of thing willy-nilly whether you need them or not. Forth makes you ask. That's not much of a burden as far as I'm concerned. It's rare to have an application requirement push you in this direction in Forth.
So, it seems to me that your objection to small stacks comes down to "They won't let me write the code the way I want". That's not an application requirement, so you don't have a serious problem unless the small stack makes it significantly more difficult to code readably to the real application requirements. That's not the case here: the extra difficulty is trivial, and I think in a real application the way you want to do it would actually be more difficult to code and read.
--
John Doty, Noqsi Aerospace, Ltd.
--
Beheading a man with a stone axe is difficult, and the axe blade quickly loses its sharp edge and is tedious to resharpen. -Jared Diamond
.
- Follow-Ups:
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: ward mcfarland
- Re: using MISC (post 1987 Forth hardware) opcodes
- References:
- using MISC (post 1987 Forth hardware) opcodes
- From: Jeff Fox
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Bernd Paysan
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Jeff Fox
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Adam Warner
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Jeff Fox
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Adam Warner
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Marc Olschok
- Re: using MISC (post 1987 Forth hardware) opcodes
- From: Adam Warner
- using MISC (post 1987 Forth hardware) opcodes
- Prev by Date: Re: OT Advice on good forth coding
- Next by Date: Re: OT Advice on good forth coding
- Previous by thread: Re: using MISC (post 1987 Forth hardware) opcodes
- Next by thread: Re: using MISC (post 1987 Forth hardware) opcodes
- Index(es):
Relevant Pages
|