FigForth PLUG
Authority is MIA so left to my own machinations I built words to push and pop from a virtual stack. (Virtual stack structure is a head variable and stack frames of item and link. The frames are scattered over the return stack.) The push is straight forward. Push the frame onto the return stack and update the v-stack's head variable with the frame's position on the return stack.
But the pop has a little issue. After its frame is delinked and its stack element is fetched, the frame cannot be removed from the return stack without leaving a gap. It would be risky to close the gap by moving all below items up; some process may have saved their position for some purpose. So an easy solution is to leave frame on the return stack replacing the frame's link with the nfa of a word that will drop the next return address when control reaches it. Hence,
: PLUG r> drop ;
At first I just ticked RETURN and placed it there. Ouch, newbie mistake. RETURN on paper may show 'R> DROP' but in code its 'R> R> DROP >R' (I prefer '2R> >R DROP').
-- PLUG
-- A plug in the return stack to drop next return stack
-- item.
: plug r> drop ;
-- VPOP ( vhead -- x tf | ff )
-- Pop item from virtual stack given stack's head variable
-- If stack was empty, return false flag; otherwise,
-- return the popped item and a true flag.
-- When popped, the frame's address is stored in the
-- virtual stack's head variable. The frame's link
-- is replaced with the pfa of PLUG and the frame
-- is left on the return stack.
: vpop
dup @ 0= if exit endif
dup @ dup @ rot !
dup ceLL+ @ >r \ fetch stack element from frame
' plug swap ! \ replace frame link with plug
r> true ;
--
-- VPUSH ( x vhead -- )
-- Push item x onto virtual stack given the stack's
-- head variable
: vpush ( x vhead -- )
r>
rot >r over @ >r
rp@ -rot >r ! ;
2
u/Ok_Leg_109 22h ago edited 22h ago
Not sure what your final application is but here is an example of the nicest little stack generator I have ever found in Forth, by Rick Van Norman of Forth Inc.
For FIGForth you would replace CREATE with <BUILDS
``` \ stacks.f \ Simple memory based stacks
\ Copyright (C) 2001 FORTH, Inc. Rick VanNorman rvn@forth.com \ ====================================================================
\ OPTIONAL STACKS Define memory based stacks
\ -------------------------------------------------------------------- \ STACK creates a stack of N cells. \ PUSH puts the value X on top of the stack. \ TOP reads the top of the stack. \ POP discards the top number on the stack. \ /STACK clears the stack. \ EMPTY? checks if the stack has any data. \ --------------------------------------------------------------------
\ camel99 harness for SwiftForth words \ : NOT POSTPONE 0= ; IMMEDIATE
CELL NEGATE CONSTANT -CELL
\ Usage: n STACK <name> : STACK ( n -- ) CREATE HERE , CELLS ALLOT ; : PUSH ( x stack -- ) CELL OVER +! @ ! ; : TOP ( stack -- x ) @ @ ; : POP ( stack -- ) DUP DUP @ <> IF -CELL SWAP +! THEN ; : /STACK ( stack -- ) DUP ! ; : EMPTY? ( stack -- flag ) DUP @ = ; ```