Advertisement
Guest User

Untitled

a guest
Mar 27th, 2021
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. $ cat namespace.fs
  2. \ Copyright (c) 2021 Christopher Leonard, MIT Licence
  3.  
  4. \ Namespace functionality, see examples/namespaces.fs
  5.  
  6. : ?PUSH-WORDLIST
  7. >R
  8. ( ) ( R: wl)
  9. GET-ORDER
  10. ( wl_n ... wl_1 n)
  11. OVER R@ = IF
  12. 0 ?DO DROP LOOP RDROP
  13. ELSE
  14. NIP R> SWAP SET-ORDER
  15. THEN ;
  16.  
  17. \ Create vocabulary word for namespace
  18. ( wl " vocab" --)
  19. : NS-VOCAB
  20. CREATE ,
  21. \ First search order wordlist set to namespace's
  22. ( --)
  23. DOES>
  24. @ ?PUSH-WORDLIST ;
  25.  
  26. \ Create word for postponing name in namespace's wordlist
  27. ( wl " prefix" --)
  28. : NS-PREFIX
  29. CREATE , IMMEDIATE
  30. \ Find given name in namespace's wordlist and compile it
  31. ( " name" --)
  32. DOES>
  33. @ ?PUSH-WORDLIST PARSE-NAME EVALUATE ;
  34.  
  35. \ Create namespace, with vocabulary and prefix words, set namespace wordlist to
  36. \ current definitions wordlist, return previous definitions wordlist
  37. ( " vocab" " prefix" -- prev-wl)
  38. : NAMESPACE
  39. WORDLIST
  40. ( wl " vocab" " prefix")
  41. DUP NS-VOCAB
  42. ( wl " prefix")
  43. DUP NS-PREFIX
  44. ( wl)
  45. GET-CURRENT SWAP SET-CURRENT ;
  46.  
  47.  
  48. $ gforth namespaces.fs
  49. 12
  50. Gforth 0.7.3, Copyright (C) 1995-2008 Free Software Foundation, Inc.
  51. Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
  52. Type `bye' to exit
  53. LOCAL: X ok
  54. LOCAL: Y ok
  55. LOCAL: 9 ok
  56. .S <3> 5 7 9 ok
  57. LOCAL: DROP ok
  58. BYE
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement