\ Anonymous "things" NLM900607 These screens were part of my article Anonymous "Things," published in Forth Dimensions, May/June 1990. They permit you to create and use anonymous words of any kind, including variables, constants, and colon definitions. The letter N in these words stands for "Nameless." I couldn't use A because A: is already defined in F83. N: is more complicated than the others. It was necessary to redefine : and ; Screen 2 defines NVARIABLE, NCONSTANT, N:, and IMPERSONATOR. It is the only required screen. Screen 3 has NCODE, NCREATE, and NDOES>. Screen 7 maintains compatibility with an older version. \ ANONYMOUS "THINGS" (Load screen) NLM900607 2 LOAD \ Basic definitions 3 LOAD \ NCODE NCREATE NDOES> \ Examples and tests 4 LOAD \ Create & test N2CONSTANT 5 LOAD \ Create & test 3CONSTANT & N3CONSTANT 6 LOAD \ Create & test 4CONSTANT & N4CONSTANT \ 7 LOAD \ Compatibility with previous version\ 8 LOAD \ Test small n display cr .( Anonymous "things" loaded.) \ ANONYMOUS "THINGS" (BASIC DEFINTIONS) NLM900607 VARIABLE DEBUGGING DEBUGGING OFF : PSEUDONAME DEBUGGING @ IF 129 C, 238 C, THEN ; : NVARIABLE ( ... addr) PSEUDONAME HERE ['] >IN @ , 0 , ; : NCONSTANT ( n ... addr) PSEUDONAME HERE >R ['] B/BUF @ , , R> ; : IMPERSONATOR CREATE ['] CRASH , IMMEDIATE DOES> @ STATE @ IF , ELSE EXECUTE THEN ; IMPERSONATOR C-PART IMPERSONATOR D-PART NVARIABLE IS C-PART : N: PSEUDONAME HERE C-PART ON ['] NVARIABLE @ , !CSP CURRENT @ CONTEXT ! ] ; : : C-PART OFF [COMPILE] : ; : ; C-PART @ IF ?CSP COMPILE UNNEST [COMPILE] [ C-PART OFF ELSE [COMPILE] ; THEN ; IMMEDIATE \ NCREATE NDOES> NCODE NLM900607 : (NCREATE) PSEUDONAME HERE 2 ALLOT ; : N(;CODE) R> OVER ! ; : (NDOES>) COMPILE N(;CODE) 232 C, 333 HERE 2+ - , ; IMMEDIATE : NCREATE COMPILE (NCREATE) COMPILE >R ; IMMEDIATE : NDOES> COMPILE R> [COMPILE] (NDOES>) ; IMMEDIATE : NCODE PSEUDONAME HERE DUP 2+ , CONTEXT @ AVOC ! ASSEMBLER ; \ Defining and Testing N2CONSTANT by Method 1 NLM881223 \ Step 1. Define a 2CONSTANT 0. 2CONSTANT ZERO \ Step 2. Use the defined 2CONSTANT to define others. : N2CONSTANT ( -- addr) PSEUDONAME HERE >R [ ' ZERO @ ] LITERAL , , , R> ; \ Testing 5. N2CONSTANT IS C-PART : TEST4 C-PART D. ; CR .( SHOULD PRINT 5 ) TEST4 CR \ CREATING A NEW TYPE OF ANONYMOUS "THING" (Method 1) NLM881223 \ Defining 3CONSTANT and N3CONSTANT \ Step 1. Define the "Create" part as an anonymous. N: , , , ; IS C-PART \ Step 2. Define a normal creator. : 3CONSTANT CREATE C-PART DOES> DUP 2+ 2@ ROT @ ; \ Step 3. Create an example. 0 0 0 3CONSTANT 3ZEROES \ Step 4. Use the example as a basis for the anonymous creator. : N3CONSTANT PSEUDONAME HERE >R ['] 3ZEROES @ , C-PART R> ; CR .( SHOULD PRINT 0 0 0 ) 3ZEROES . . . CR 1 2 3 N3CONSTANT IS C-PART CR .( SHOULD PRINT 3 2 1 ) C-PART . . . CR \ CREATING A NEW TYPE OF ANONYMOUS "THING" (Method 2) NLM881223 \ DEFINING 4CONSTANT AND N4CONSTANT \ Write CREATE and DOES> parts; assign them to impersonators N: ( n4 n3 n2 n1 -- ) 2SWAP , , , , ; IS C-PART N: ( adr -- n4 n3 n2 n1) DUP 2@ ROT 4 + 2@ ; IS D-PART \ Write the definers according the the following set formula : 4CONSTANT CREATE C-PART DOES> D-PART ; : N4CONSTANT NCREATE C-PART NDOES> D-PART ; \ Testing 1 2 3 4 4CONSTANT XYZ 5 6 7 8 N4CONSTANT IS C-PART CR .( SHOULD PRINT 4 3 2 1 ) XYZ . . . . CR CR .( SHOULD PRINT 8 7 6 5 ) C-PART . . . . CR \ Compatibility with previous version (ANON etc.) NLM900607 IMPERSONATOR ANON : MAKEANON NVARIABLE IS [COMPILE] ANON ; : ANON+ [COMPILE] [ [COMPILE] ANON + [COMPILE] LITERAL ] ; IMMEDIATE : STORESTACK ( n -- ) 2* BOUNDS ?DO I ! 2 +LOOP ; \S Testing makeanon 10 allot 2 anon 2+ ! : xyz [ 2 ] anon+ ? ; CR .( SHOULD PRINT 2 ) XYZ \ TESTING SEE AND DEBUG NLM881222 NVARIABLE IS C-PART : TEST7 C-PART OFF C-PART ? ; CR .( SHOULD PRINT 0 ) TEST7 CR CR .( Result will depend on mode ) CR .( MAY SHOW GARBAGE) CR .( MAY SHOW "n" FOR ANONYMOUS ) SEE TEST7 CR \ ANONYMOUS "THINGS" (BASIC DEFINTIONS) NLM900607 DEBUGGING, PSEUDONAME If the variable DEBUGGING is on, then anonymous things have a pseudoname field of lower case n. NVARIABLE NCONSTANT N: Define an anonymous thing and leave its cfa on the stack. IMPERSONATOR Something like DEFER but compiles or executes, according to the contents of STATE. Example: IMPERSONATOR TEMP \ define an impersonator 3 NCONSTANT \ Define an anonymous constant. Note that it \ has no name. The cfa is left on the stack IS TEMP \ Make TEMP act like a constant with the \ value 3. : FOO TEMP . ; \ FOO will display 3 NVARIABLE IS TEMP \ TEMP is now a variable, but FOO still \ displays 3 \ NCREATE NDOES> NCODE NLM900607 NCODE Use with END-CODE to create an anonymous code definition. Example: NCODE AX POP NEXT END-CODE IS TEMP \ The impersonator TEMP now has the same action as DROP NCREATE NDOES> Create a new anonymous class. See examples on screens 4-6 \ TESTING ANONYMOUS WORDS NLM881201 IMPERSONATOR TEMP Create an impersonator 3 NCONSTANT IS TEMP Create an anonymous constant with the value 3 and assign it to TEMP. TEMP will act as if you had defined it in the usual way by 3 CONSTANT TEMP -- the assign- ment will remain in effect until TEMP is given a new action. NVARIABLE IS TEMP Define an anonymous variable, and assign it to TEMP. TEMP will act as if you had written VARIABLE TEMP N: ." HELLO " ; IS TEMP Create an anonymous colon definition and assign it to TEMP. TEMP will now act as if you had redifined it by writing : TEMP ." HELLO " ; \ A WORD ABOUT IMPERSONATORS NLM900607 The number of impersonators required is determined by how many need to be active at the same time. Three or four are usually enough. Extras may improve readability. \ TESTING ANONYMOUS WORDS NLM881201 IMPERSONATOR TEMP 3 NCONSTANT IS TEMP : TEST1 TEMP . ; CR .( SHOULD PRINT 3 ) TEST1 CR NVARIABLE IS TEMP : TEST2 4 TEMP ! TEMP ? ; CR .( SHOULD PRINT 4 ) TEST2 CR N: ." HELLO " ; IS TEMP : TEST3 TEMP ; CR .( SHOULD SAY HELLO ) TEST3 CR