Add word "FORGET"

This commit is contained in:
Virgil Dupras 2020-04-13 08:09:36 -04:00
parent 4c7dfe0dfe
commit 1e0b40a876
3 changed files with 33 additions and 23 deletions

View File

@ -38,25 +38,31 @@ $ - Initialize
~ - Container for native code. Usually not an executable word. ~ - Container for native code. Usually not an executable word.
? - Is it ...? (example: IMMED?) ? - Is it ...? (example: IMMED?)
*** Defining words *** *** Entry management ***
(find) a -- a f Read at a and find it in dict. If found, f=1 and (find) a -- a f Read at a and find it in dict. If found, f=1 and
a = wordref. If not found, f=0 and a = string addr. a = wordref. If not found, f=0 and a = string addr.
: x ... -- Define a new word
; R:I -- Exit a colon definition
, n -- Write n in HERE and advance it.
' x -- a Push addr of word x to a. If not found, aborts ' x -- a Push addr of word x to a. If not found, aborts
['] x -- *I* Like "'", but spits the addr as a number ['] x -- *I* Like "'", but spits the addr as a number
literal. If not found, aborts. literal. If not found, aborts.
( -- *I* Comment. Ignore rest of line until ")" is read. , n -- Write n in HERE and advance it.
ALLOT n -- Move HERE by n bytes ALLOT n -- Move HERE by n bytes
C, b -- Write byte b in HERE and advance it. C, b -- Write byte b in HERE and advance it.
DELW a -- Delete wordref at a. If it shadows another
definition, that definition is unshadowed.
FORGET x -- Rewind the dictionary (both CURRENT and HERE) up to
x's previous entry.
PREV a -- a Return a wordref's previous entry.
WHLEN a -- n Get word header length from wordref. That is, name
length + 3. a is a wordref
*** Defining words ***
: x ... -- Define a new word
; R:I -- Exit a colon definition
CREATE x -- Create cell named x. Doesn't allocate a PF. CREATE x -- Create cell named x. Doesn't allocate a PF.
[COMPILE] x -- Compile word x and write it to HERE. IMMEDIATE [COMPILE] x -- Compile word x and write it to HERE. IMMEDIATE
words are *not* executed. words are *not* executed.
COMPILE x -- Meta compiles. Kind of blows the mind. See below. COMPILE x -- Meta compiles. Kind of blows the mind. See below.
CONSTANT x n -- Creates cell x that when called pushes its value CONSTANT x n -- Creates cell x that when called pushes its value
DELW a -- Delete wordref at a. If it shadows another
definition, that definition is unshadowed.
DOES> -- See description in usage.txt DOES> -- See description in usage.txt
IMMED? a -- f Checks whether wordref at a is immediate. IMMED? a -- f Checks whether wordref at a is immediate.
IMMEDIATE -- Flag the latest defined word as immediate. IMMEDIATE -- Flag the latest defined word as immediate.
@ -72,6 +78,7 @@ input stream is executed immediately. In this context, branching doesn't work.
(br) -- Branches by the number specified in the 2 following (br) -- Branches by the number specified in the 2 following
bytes. Can be negative. bytes. Can be negative.
(?br) f -- Branch if f is false. (?br) f -- Branch if f is false.
( -- *I* Comment. Ignore rest of line until ")" is read.
[ -- Begin interetative mode. In a definition, words [ -- Begin interetative mode. In a definition, words
between here and "]" will be executed instead of between here and "]" will be executed instead of
compiled. compiled.

View File

@ -117,3 +117,22 @@
: DELW : DELW
1 - 0 SWAP C! 1 - 0 SWAP C!
; ;
: PREV
3 - DUP @ ( a o )
- ( a-o )
;
: WHLEN
1 - C@ ( name len field )
127 AND ( 0x7f. remove IMMEDIATE flag )
3 + ( fixed header len )
;
: FORGET
' DUP ( w w )
( HERE must be at the end of prev's word, that is, at the
beginning of w. )
DUP WHLEN - HERE ! ( w )
PREV CURRENT !
;

View File

@ -39,15 +39,6 @@
1 + 1 +
; ;
( Get word header length from wordref. That is, name length
+ 3. a is a wordref )
( a -- n )
: WHLEN
1 - C@ ( name len field )
0x7f AND ( remove IMMEDIATE flag )
3 + ( fixed header len )
;
( Get word addr, starting at name's address ) ( Get word addr, starting at name's address )
: '< ' DUP WHLEN - ; : '< ' DUP WHLEN - ;
@ -121,13 +112,6 @@
( TODO implement RLCELL ) ( TODO implement RLCELL )
( Get word's prev offset )
( a -- a )
: PREV
3 - DUP @ ( a o )
- ( a-o )
;
( Copy dict from target wordref, including header, up to HERE. ( Copy dict from target wordref, including header, up to HERE.
We're going to compact the space between that word and its We're going to compact the space between that word and its
prev word. To do this, we're copying this whole memory area prev word. To do this, we're copying this whole memory area