home *** CD-ROM | disk | FTP | other *** search
- \ Forth source file
- \ Copyright Mads Meisner-Jensen, 6 Apr 1993
- \ Example showing how to implement DEFERred words
-
- \ The default action of a word defined by DEFER is to issue an error message
- : (defer-error) ( -- )
- -1 ABORT" DEFERred word not initialised" ;
-
- \ DEFER is a defining word.
- : DEFER ( "name" -- )
- CREATE ['] (defer-error) , DOES> @ EXECUTE ;
-
- \ Execution semantics of IS
- : (IS) ( w1 w2 -- )
- >BODY ! ;
-
- \ IS assigns "name" to execute the word represented by the execution token w.
- \ IS is statesmart, meaning it can both be used inside and outside of a
- \ definition.
- : IS ( w "name" -- ) \ Compilation: ( "name" -- )
- BL WORD COUNT ( c-addr u ) \ parse name delimited by space
- FORTH-WORDLIST SEARCH-WORDLIST \ get its execution token
- IF \ ..found
- STATE @ IF \ compiling..
- POSTPONE LITERAL \ compile execution semantics of IS..
- ['] (IS) COMPILE,
- ELSE \ interpreting
- (IS)
- THEN
- ELSE
- -1 ABORT" DEFERred Word not found"
- THEN
- ; IMMEDIATE
-
-
-
- \ Example of usage ____________________________________________________________
-
- \ Try typing G1 followed by GREET at the Forth prompt
- \ Then see what happens when you afterwards type G2 followed by GREET.
-
- DEFER GREET
-
- : HELLO ( -- ) ." Hello world." ;
- : HI ( -- ) ." Hi there." ;
-
- : G1
- ['] HELLO IS GREET ;
- : G2
- ['] HI IS GREET ;
-