PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/doc-engine.frt

https://bitbucket.org/alexandervantsev/forth
Forth | 74 lines | 55 code | 9 blank | 10 comment | 11 complexity | 663166e9cc699e3211a5b2266c48d602 MD5 | raw file
Possible License(s): GPL-3.0
  1. (
  2. This file implements Forthress documentation engine. To use it, place a
  3. string on the stack before defining a new word, and use `wit-doc` after
  4. semicolon to create documentation entry for the last word defined.
  5. )
  6. global doc-start
  7. 0 doc-start !
  8. struct
  9. cell% field >doc-next
  10. cell% field >doc-addr
  11. cell% field >doc-string
  12. end-struct doc-header%
  13. ( word-address docstring )
  14. : doc-word
  15. swap
  16. doc-header% allot >r
  17. swap
  18. r@ >doc-string !
  19. doc-start @ r@ >doc-next !
  20. r@ >doc-addr !
  21. r> doc-start !
  22. ;
  23. ( string - )
  24. : with-doc last_word @ cfa swap doc-word ;
  25. g"
  26. ( addr - doc-header? )
  27. Given an XT of a word, finds a relevant `doc-header` in the documentation DB
  28. "
  29. : doc-find
  30. doc-start @
  31. repeat
  32. dup 0 = if 2drop 0 1 ( return 0 )
  33. else
  34. 2dup >doc-addr @ = if
  35. swap drop 1
  36. else >doc-next @ 0
  37. then
  38. then
  39. until
  40. ; with-doc
  41. g"
  42. ( addr - )
  43. Display documentation for the word address
  44. "
  45. : doc-show dup doc-find dup if
  46. >doc-string @ dup if
  47. cr
  48. ." # Documentation for " swap ? cr
  49. prints cr
  50. ." ------" cr
  51. else 2drop ." Error: empty documentation string " then
  52. else drop ." No documentation for " ? cr then
  53. ; with-doc
  54. g"
  55. Alias for `doc-show`
  56. "
  57. : ?? doc-show ; with-doc
  58. ' doc-word g"
  59. ( word-address docstring )
  60. Document an existing word with a documenting string. Prefer using global strings for that matter.
  61. " doc-word
  62. ' doc-start g"
  63. Global variable storing the address of documentation database.
  64. The database itself is a linked list of `doc-header` structures.
  65. " doc-word