Mirror of CollapseOS
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

250 líneas
7.9KB

  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. # Memory access and HERE
  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. We call the @ and ! actions
  109. "fetch" and "store".
  110. There's a 3rd kind of memory-related action: "," (write). This
  111. action stores value on PS at where a special variable called
  112. "HERE" points to, and then advances HERE by 2 (there's also
  113. "C," for bytes).
  114. Note that the HERE word returns the address containing the
  115. pointer (it doesn't change). There's a shortcut word for
  116. "HERE @" named "H@", which is used much more often.
  117. HERE is initialized at the first writable address in RAM, often
  118. directly following the latest entry in the dictionary. Explain-
  119. ing the "culture of HERE" is beyond the scope of this primer,
  120. but know that it's a very important concept in Forth. For examp-
  121. le, new word definitions are written to HERE.
  122. # Variables
  123. The word "VARIABLE" links a name to an address. For example,
  124. "VARIABLE FOO" defines the word "FOO" and "reserves" 2 bytes of
  125. memory. Then, when FOO is executed, it pushes the address of the
  126. "reserved" area to PS.
  127. For example, "1234 FOO !" writes 1234 to memory address reserved
  128. for FOO.
  129. Another way to create a variable is with the word CREATE, which
  130. creates a variable entry without reserving anything for it: it's
  131. your responsibility to reserve memory for it after you call it.
  132. It can be useful for arrays. For example, look at VARIABLE's
  133. definition:
  134. : VARIABLE CREATE 2 ALLOT ;
  135. # DOES>
  136. Calling DOES> makes the newly created entry into a special
  137. "does word" which behaves like a variable, that is, it pushes
  138. the address of the "reserved" space to PS, but with additional
  139. behavior attached to it.
  140. DOES> must be called in the context of a word definition and
  141. calling it stops the definition right there. Every word follow-
  142. ing the DOES> is our new entry's behavior. For example, let's
  143. look at CONSTANT's definition:
  144. : CONSTANT CREATE , DOES> @ ;
  145. A constant is created with "42 CONSTANT FOO" and FOO, instead
  146. of putting FOO's address on PS, puts 42 on it.
  147. You can see above that after we've created our FOO entry, we
  148. write it to HERE and then assign the behavior "@" to it, which
  149. means that it will transform the address currently on PS to its
  150. value.
  151. # IMMEDIATE
  152. We approach the end of our primer. So far, we've covered the
  153. "cute and cuddly" parts of the language. However, that's not
  154. what makes Forth powerful. Forth becomes mind-bending when we
  155. throw IMMEDIATE into the mix.
  156. A word can be declared immediate thus:
  157. : FOO ; IMMEDIATE
  158. That is, when the IMMEDIATE word is executed, it makes the
  159. latest defined word immediate.
  160. An immediate word, when used in a definition, is executed
  161. immediately instead of being compiled. This seemingly simple
  162. mechanism (and it *is* simple) has very wide implications.
  163. For example, The words "(" and ")" are comment indicators. In
  164. the definition:
  165. : FOO 42 ( this is a comment ) . ;
  166. The word "(" is read like any other word. What prevents us from
  167. trying to compile "this" and generate an error because the word
  168. doesn't exist? Because "(" is immediate. Then, that word reads
  169. from input stream until a ")" is met, and then returns to word
  170. compilation.
  171. Words like "IF", "DO", ";" are all regular Forth words, but
  172. their "power" come from the fact that they're immediate.
  173. Starting Forth by Leo Brodie explain all of this in details.
  174. Read this if you can. If you can't, well, let this sink in for
  175. a while, browse the dictionary (B30) and try to understand why
  176. this or that word is immediate. Good luck!