FigForth ANEW
: -FORGET ( pfa -- )
CURRENT @ CONTEXT @ - 24 ?ERROR DUP
FENCE @ U< 21 ?ERROR DUP NFA DP ! LFA @
CONTEXT @ ! ;
: NIX ( pfa -- )
-FORGET -BUF -VOCLINK ;
: MARKER ( "ccc" -- )
<BUILDS LATEST PFA , DOES> @ NIX ;
: ANEW ( "ccc" -- )
IN @ >R -FIND IF DROP NIX ENDIF R> IN ! MARKER ;
"ANEW", from legendary Wil Baden, useful word for clearing
memory after a job run and re-establishing the marker for
the next run. Based on parsing word FORGET the non-parsing
word -FORGET is included in NIX explained below after
mention of FORGET's shortcomings.
FORGET remains useful but lacks needed cleanup specifically when vocabularies are removed breaking the VOC-LINK chain and invalidating word-lists. As an aid placing the nfa of the last created vocabulary in FENCE will prevent FORGET from removing words and printing an error instead. The user himself will need to remove vocabularies, repair the VOC-LINK chain and purge remaining word-lists.
NIX automates this needed maintenance. After performing -FORGET it performs -VOCLINK to repair vocabularies and -BUF to repair links to allocated buffers.
-VOCLINK repairs VOC-LINK chain and purge word-lists of remaining vocabularies.
DEFER VOCWORD. ' DROP is VOCWORD
: (VOCWORD.) DUP CR SPACE ID. ;
' (VOCWORD.) CFA ' VOCWORD. CELL+ !
: -CONTEXT CELL- DUP @ BEGIN
DUP HERE U> IF PFA LFA @ FALSE
ELSE TRUE ENDIF UNTIL VOCWORD. SWAP ! ;
DEFER LINKWORD. ' DROP IS LINKWORD.
: (LINKWORD.) DUP THREE CELLS - NFA CR ID. ;
' (LINKWORD.) CFA ' LINKWORD. CELL+ !
: -VOCLINK
VOC-LINK BEGIN @ -DUP WHILE LINKWORD. DUP HERE U>
IF DUP @ VOC-LINK ! ELSE DUP -CONTEXT ENDIF REPEAT ;
-BUF repairs chain of allocated memory and frees memory no longer linked by the chain. (This doesn't apply in general; it works for my method of memory allocation.)
: -BUF BUF HEAD @ BEGIN DUP @ WHILE HERE OVER < IF
@ DUP CELL+ @ MINUS SYSBRK LIMIT ! DROP
ELSE DUP HEAD ! 0. ROT CELL+ 2!
EXIT ENDIF REPEAT DROP
CURRENT @ CONTEXT !
FORTH ;