next up previous contents index
Next: Util.4th Up: Dreams Previous: Macros.4th   Contents   Index



Stacks.4th


\ Defining word and methods for stacks.

  CONSULT ANS       \ ANS Forth X3J14 BASIS6 compatibility.

: Stack CREATE ( size -- ) \ stack \
           HERE CELL+ , CELLS ALLOT
           DOES> ;             ( -- ^sp )


: Push ( w stack -- )      \ push a word onto a stack
       DUP >R @            \ fetch sp
       cell +              \ pre-increment sp
       DUP R> !            \ save new sp
       ! ;                 \ store word in stack


: Pop ( stack -- w )       \ pop a word from a stack
      DUP >R @             \ fetch sp
      DUP @                \ fetch word from stack
      SWAP cell -          \ post-decrement sp
      R> ! ;               \ save new sp


: Top ( stack -- w )       \ fetch top of stack
      @ @ ;                \ fetch the word the pointer points to


: Empty? ( stack -- flag ) \ test for an empty stack
         DUP @ = ;         \ its empty if pointer points to its self



Robert J. Brown
1999-09-26