Re: Is there a clean, simple way to do this stack change?



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
.



Relevant Pages

  • Re: Is there a clean, simple way to do this stack change?
    ... doing that is for setting up a special data structure. ... I'm going to have a word COMPOSE that takes two FREEZE ... Once everything is ready on the stack, ... word wraps it all up by moving everything above the left marker into ...
    (comp.lang.forth)
  • Re: Yet Another Spinoza Challenge
    ... It's obviously a very, very sensible choice, but it is not the only ... Any searchable dynamic data structure will do. ... In what sense are you using the word "stack" in the above paragraph? ... The push operation ...
    (comp.lang.c)
  • Re: CFileDialog: "My Computer" not shows files and dirs
    ... CFileDialog, the dialog and the programm crashes! ... invalid instruction exception caused by a stack clobber, ... the data structure is actually longer and a memory overwrite occurs. ... You can install ...
    (microsoft.public.vc.mfc)
  • Controlled types and exception safety
    ... I can classify the stack's operations by assigning them any of the above four levels, so that I know what can be expected when an exception is thrown for any reason (like inability to allocate more memory, or alike). ... For example, if the Push method of the stack gives me the strong guarantee, then I *know* that by calling this method either the new element will be appended to the stack, or the stack will remain unchanged, so that even if the exception is thrown, I don't have to worry about the stack's internal consistency. ... Since stack can be a dynamic data structure, assigning one stack object to another may involve destroying one existing data structure *and* creating a new one in its place. ...
    (comp.lang.ada)
  • Re: using MISC (post 1987 Forth hardware) opcodes
    ... still need to travel onto a stack every time you branch within the tree. ... Or without altering the data structure and using a threaded tree. ... restricted environment is also easier to program. ...
    (comp.lang.forth)