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.

202 lines
6.1KB

  1. # Forth Primer
  2. # First steps
  3. Before you read this primer, let's try a few commands, just for
  4. fun.
  5. 42 .
  6. This will push the number 42 to the stack, then print the number
  7. at the top of the stack.
  8. 4 2 + .
  9. This pushes 4, then 2 to the stack, then adds the 2 numbers on
  10. the top of the stack, then prints the result.
  11. 42 0x8000 C! 0x8000 C@ .
  12. This writes the byte "42" at address 0x8000, and then reads
  13. back that bytes from the same address and print it.
  14. # Interpreter loop
  15. Forth's main interpeter loop is very simple:
  16. 1. Read a word from input
  17. 2. Look it up in the dictionary
  18. 3. Found? Execute.
  19. 4. Not found?
  20. 4.1. Is it a number?
  21. 4.2. Yes? Parse and push on the Parameter Stack.
  22. 4.3. No? Error.
  23. 5. Repeat
  24. # Word
  25. A word is a string of non-whitepace characters. We consider that
  26. we're finished reading a word when we encounter a whitespace
  27. after having read at least one non-whitespace character
  28. # Character encoding
  29. Collapse OS doesn't support any other encoding than 7bit ASCII.
  30. A character smaller than 0x21 is considered a whitespace,
  31. others are considered non-whitespace.
  32. Characters above 0x7f have no special meaning and can be used in
  33. words (if your system has glyphs for them).
  34. # Dictionary
  35. Forth's dictionary link words to code. On boot, this dictionary
  36. contains the system's words (look in B30 for a list of them),
  37. but you can define new words with the ":" word. For example:
  38. : FOO 42 . ;
  39. defines a new word "FOO" with the code "42 ." linked to it. The
  40. word ";" closes the definition. Once defined, a word can be
  41. executed like any other word.
  42. You can define a word that already exists. In that case, the new
  43. definition will overshadow the old one. However, any word def-
  44. ined *before* the overshadowing took place will still use the
  45. old word.
  46. # Cell size
  47. The cell size in Collapse OS is 16 bit, that is, each item in
  48. stacks is 16 bit, @ and ! read and write 16 bit numbers.
  49. Whenever we refer to a number, a pointer, we speak of 16 bit.
  50. To read and write bytes, use C@ and C!.
  51. # Number literals
  52. Traditional Forth often uses HEX/DEC switches to go from deci-
  53. mal to hexadecimal parsing. Collapse OS parses literals in a
  54. way that is closer to C.
  55. Straight numbers are decimals, numbers starting with "0x"
  56. are hexadecimals (example "0x12ef"), "0b" prefixes indicate
  57. binary (example "0b1010"), char literals are single characters
  58. surrounded by ' (example 'X'). Char literals can't be used for
  59. whitespaces.
  60. # Parameter Stack
  61. Unlike most programming languages, Forth execute words directly,
  62. without arguments. The Parameter Stack (PS) replaces them. There
  63. is only one, and we're constantly pushing to and popping from
  64. it. All the time.
  65. For example, the word "+" pops the 2 number on the Top Of Stack
  66. (TOS), adds them, then pushes back the result on the same stack.
  67. It thus has the "stack signature" of "a b -- n". Every word in
  68. a dictionary specifies that signature because stack balance, as
  69. you can guess, is paramount. It's easy to get confused so you
  70. need to know the stack signature of words you use very well.
  71. # Return Stack
  72. There's a second stack, the Return Stack (RS), which is used to
  73. keep track of execution, that is, to know where to go back after
  74. we've executed a word. It is also used in other contexts, but
  75. this is outside of the scope of this primer.
  76. # Conditional execution
  77. Code can be executed conditionally with IF/ELSE/THEN. IF pops
  78. PS and checks whether its nonzero. If it is, it does nothing.
  79. If it's zero, it jumps to the following ELSE or the following
  80. THEN. Similarly, when ELSE is encountered in the context of a
  81. nonzero IF, we jump to the following THEN.
  82. Because IFs involve jumping, they only work inside word defin-
  83. itions. You can't use IF directly in the interpreter loop.
  84. Example usage:
  85. : FOO IF 42 ELSE 43 THEN . ;
  86. 0 FOO --> 42
  87. 1 FOO --> 43
  88. # Loops
  89. Loops work a bit like conditionals, and there's 3 forms:
  90. BEGIN..AGAIN --> Loop forever
  91. BEGIN..UNTIL --> Loop conditionally
  92. DO..LOOP --> Loop X times
  93. UNTIL works exactly like IF, but instead of jumping forward to
  94. THEN, it jumps backward to BEGIN.
  95. DO pops the lower, then the higher bounds of the loop to be
  96. executed, then pushes them on RS. Then, each time LOOP is
  97. encountered, RS' TOS is increased. As long as the 2 numbers at
  98. RS' TOS aren't equal, we jump back to DO.
  99. The word "I" copies RS' TOS to PS, which can be used to get our
  100. "loop counter".
  101. Beware: the bounds arguments for DO are unintuitive. We begin
  102. with the upper bound. Example:
  103. 42 0 DO I . SPC LOOP
  104. Will print numbers 0 to 41, separated by a space.
  105. # Variables
  106. We can read and write to arbitrary memory address with @ and !
  107. (C@ and C! for bytes). For example, "1234 0x8000 !" writes the
  108. word 1234 to address 0x8000.
  109. The word "VARIABLE" link a name to an address. For example,
  110. "VARIABLE FOO" defines the word "FOO" and "reserves" 2 bytes of
  111. memory. Then, when FOO is executed, it pushes the address of the
  112. "reserved" area to PS.
  113. For example, "1234 FOO !" writes 1234 to memory address reserved
  114. for FOO.
  115. # IMMEDIATE
  116. We approach the end of our primer. So far, we've covered the
  117. "cute and cuddly" parts of the language. However, that's not
  118. what makes Forth powerful. Forth becomes mind-bending when we
  119. throw IMMEDIATE into the mix.
  120. A word can be declared immediate thus:
  121. : FOO ; IMMEDIATE
  122. That is, when the IMMEDIATE word is executed, it makes the
  123. latest defined word immediate.
  124. An immediate word, when used in a definition, is executed
  125. immediately instead of being compiled. This seemingly simple
  126. mechanism (and it *is* simple) has very wide implications.
  127. For example, The words "(" and ")" are comment indicators. In
  128. the definition:
  129. : FOO 42 ( this is a comment ) . ;
  130. The word "(" is read like any other word. What prevents us from
  131. trying to compile "this" and generate an error because the word
  132. doesn't exist? Because "(" is immediate. Then, that word reads
  133. from input stream until a ")" is met, and then returns to word
  134. compilation.
  135. Words like "IF", "DO", ";" are all regular Forth words, but
  136. their "power" come from the fact that they're immediate.
  137. Starting Forth by Leo Brodie explain all of this in details.
  138. Read this if you can. If you can't, well, let this sink in for
  139. a while, browse the dictionary (B30) and try to understand why
  140. this or that word is immediate. Good luck!