Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ cat namespace.fs
- \ Copyright (c) 2021 Christopher Leonard, MIT Licence
- \ Namespace functionality, see examples/namespaces.fs
- : ?PUSH-WORDLIST
- >R
- ( ) ( R: wl)
- GET-ORDER
- ( wl_n ... wl_1 n)
- OVER R@ = IF
- 0 ?DO DROP LOOP RDROP
- ELSE
- NIP R> SWAP SET-ORDER
- THEN ;
- \ Create vocabulary word for namespace
- ( wl " vocab" --)
- : NS-VOCAB
- CREATE ,
- \ First search order wordlist set to namespace's
- ( --)
- DOES>
- @ ?PUSH-WORDLIST ;
- \ Create word for postponing name in namespace's wordlist
- ( wl " prefix" --)
- : NS-PREFIX
- CREATE , IMMEDIATE
- \ Find given name in namespace's wordlist and compile it
- ( " name" --)
- DOES>
- @ ?PUSH-WORDLIST PARSE-NAME EVALUATE ;
- \ Create namespace, with vocabulary and prefix words, set namespace wordlist to
- \ current definitions wordlist, return previous definitions wordlist
- ( " vocab" " prefix" -- prev-wl)
- : NAMESPACE
- WORDLIST
- ( wl " vocab" " prefix")
- DUP NS-VOCAB
- ( wl " prefix")
- DUP NS-PREFIX
- ( wl)
- GET-CURRENT SWAP SET-CURRENT ;
- $ gforth namespaces.fs
- 12
- Gforth 0.7.3, Copyright (C) 1995-2008 Free Software Foundation, Inc.
- Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
- Type `bye' to exit
- LOCAL: X ok
- LOCAL: Y ok
- LOCAL: 9 ok
- .S <3> 5 7 9 ok
- LOCAL: DROP ok
- BYE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement