Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.5KB

  1. Collapse OS usage guide
  2. This document is not meant to be an introduction to Forth, but to instruct the
  3. user about the peculiarities of this Forth implemenation. Be sure to refer to
  4. dictionary.txt for a word reference.
  5. *** DOES>
  6. Used inside a colon definition that itself uses CREATE, DOES> transforms that
  7. newly created word into a "does cell", that is, a regular cell ( when called,
  8. puts the cell's addr on PS), but right after that, it executes words that appear
  9. after the DOES>.
  10. "does cells" always allocate 4 bytes (2 for the cell, 2 for the DOES> link) and
  11. there is no need for ALLOT in colon definition.
  12. At compile time, colon definition stops processing words when reaching the
  13. DOES>.
  14. Example: ": CONSTANT CREATE HERE @ ! DOES> @ ;"
  15. *** Compilation vs meta-compilation
  16. Compilation vs meta-compilation. When you compile a word with "[COMPILE] foo",
  17. its straightforward: It writes down to HERE wither the address of the word or
  18. a number literal.
  19. When you *meta* compile, it's a bit more mind blowing. It fetches the address
  20. of the word specified by the caller, then writes that number as a literal,
  21. followed by a reference to ",".
  22. Example: ": foo [COMPILE] bar;" is the equivalent of ": foo bar ;" if bar is
  23. not an immediate. However, ": foo COMPILE bar ;" is the equivalent of
  24. ": foo ['] bar , ;". Got it?
  25. Meta-compile only works with real words, not number literals.
  26. *** I/O
  27. A little word about inputs. There are two kind of inputs: direct and buffered.
  28. As a general rule, we read line in a buffer, then feed words in it to the
  29. interpreter. That's what "WORD" does. If it's at the End Of Line, it blocks and
  30. wait until another line is entered.
  31. KEY input, however, is direct. Regardless of the input buffer's state, KEY will
  32. return the next typed key.
  33. PARSING AND BOOTSTRAP: Parsing number literal is a very "core" activity of
  34. Forth, and therefore generally seen as having to be implemented in native code.
  35. However, Collapse OS' Forth supports many kinds of literals: decimal, hex, char,
  36. binary. This incurs a significant complexity penalty.
  37. What if we could implement those parsing routines in Forth? "But it's a core
  38. routine!" you say. Yes, but here's the deal: at its native core, only decimal
  39. parsing is supported. It lives in the "(parsed)" word. The interpreter's main
  40. loop is initially set to simply call that word.
  41. However, in core.fs, "(parsex)", "(parsec)" and "(parseb)" are implemented, in
  42. Forth, then "(parse)", which goes through them all is defined. Then, "(parsef)",
  43. which is the variable in which the interpreter's word pointer is set, is
  44. updated to that new "(parse)" word.
  45. This way, we have a full-featured (and extensible) parsing with a tiny native
  46. core.
  47. *** Chained comparisons
  48. The unit "cmp.fs" contains words to facilitate chained comparisons with a single
  49. reference number. This allows, for example, to easily express "a == b or a == c"
  50. or "a > b and a < c".
  51. The way those chained comparison words work is that, unlike single comparison
  52. operators, they don't have a "n1 n2 -- f" signature, but rather a "n1 f n2 -- n1
  53. f" signature. That is, each operator "carries over" the reference number in
  54. addition to the latest flag.
  55. You open a chain with "<>{" and you close a chain with "<>}". Then, in between
  56. those words, you can chain operators. For example, to check whether A == B or A
  57. == C, you would write:
  58. A <>{ B &= C |= <>}
  59. The first operator must be of the "&" type because the chain starts with its
  60. flag to true. For example, "<>{ <>}" yields true.
  61. To check whether A is in between B and C inclusively, you would write:
  62. A <>{ B 1 - &> C 1 + &< <>}