Re: Is there a clean, simple way to do this stack change?
- From: Ian Osgood <iano@xxxxxxxxxxxxx>
- Date: Tue, 22 Dec 2009 10:19:40 -0800 (PST)
On Dec 21, 4:03 pm, "P.M.Lawrence" <pml540...@xxxxxxxxx> wrote:
Elizabeth D Rather wrote:
P.M.Lawrence wrote:
As part of my occasional roughing out of constructs to use for
http://users.beagle.com.au/peterl/furphy.htmlI found it would be
useful to do the following:-
a b --- n1 n2 a n3 n2 b n4
where n1 to n4 are special values that will be the same each time;
doing that is for setting up a special data structure. The cleanest
and second cleanest ways I can see to do this use return stack
manipulation and variables respectively; using PICK and/or ROLL would
be even less clean.
I don't understand. If n1 to n4 are the same every time, why aren't
they named CONSTANTs or VALUEs?
Depending on where this goes in the implementation, they might very
well be set up that way in the end. For the first, high level version,
there will be high level words that generate them from constructs I
already have like FREEZE and THAW. For me, this part is a non-issue.
You don't say what this is being used for.
Background: I'm going to have a word COMPOSE that takes two FREEZE
addresses and sets up a data structure whose address (descriptor) is
functionally equivalent to the FREEZE address of something that, when
THAWed, does the same as THAWing the first two one after the other. So
we have:-
- n1 is a data structure left marker;
- n2 is the address of a literal handler;
- n3 points to the execution address of THAW; and
- n4 points to the execution address of TAILTHAW.
Once everything is ready on the stack, the data structure right close
word wraps it all up by moving everything above the left marker into
allocated memory, then replacing the marker with a pointer to the new
data structure.
I didn't want to bother readers with this level of detail.
If you're setting up
parameters for something like a Windows call, then (assuming you have
VALUES named N1 etc.) you could say,
SWAP 2>R
N1 N2 R> N3 N2 R> N4
...and you're done.
That's close to what I already have, but that strikes me as sub-
optimal. It nags at me.
Data objects such as VALUEs are appropriate for anything that will have
a persistent lifetime, such as appears to be the case here.
I don't see this a a stack manipulation issue but rather as an
application design issue.
If I start reworking further back in the design/implementation cycle,
I could try to do it with a data structure that, when THAWed, uses
continuations by pre-loading the return stack, but I don't want to go
there because it's too "clever". P.M.Lawrence.
Personally, I've found the ability to manually load and drop
continuations from the return stack to be a very flexible way to
implement custom control structures and do other things normally
reserved for functional language programming. Since you appear to be
implementing functional programming primitives yourself, this seems
like an entirely reasonable implementation choice. (In the interests
of portability, it would be nice to separate real return stack
operations >R/R>/R@ from secondary data stack operations push/pop/top
for those systems in which they really are different stacks.)
You shouldn't feel bad about using >R/R> for stack juggling. It is
the feature of Forth which makes it Turing Complete, after all. The
only qualm I have about them myself is their names. I would prefer
either the traditional names "push" and "pop" later used by Chuck
Moore in colorForth, or a choice of punctuation which hints at their
requirement to be balanced, such as "R(" and ")R". Ergonomically,
">R" and "R>" are too similar to scan well when doing code review.
More modern stack languages have hidden the secondary stack behind
other abstractions, like DIP.
Ian
.
- References:
- Is there a clean, simple way to do this stack change?
- From: P.M.Lawrence
- Re: Is there a clean, simple way to do this stack change?
- From: Elizabeth D Rather
- Re: Is there a clean, simple way to do this stack change?
- From: P.M.Lawrence
- Is there a clean, simple way to do this stack change?
- Prev by Date: Re: Is there a clean, simple way to do this stack change?
- Next by Date: Re: Is there a clean, simple way to do this stack change?
- Previous by thread: Re: Is there a clean, simple way to do this stack change?
- Next by thread: Re: Is there a clean, simple way to do this stack change?
- Index(es):
Relevant Pages
|