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.

282 lines
11KB

  1. # Dictionary
  2. List of words defined in Inner core (B390), Core words (B420)
  3. and Extra words (B150).
  4. # Glossary
  5. Stack notation: "<stack before> -- <stack after>". Rightmost is
  6. top of stack (TOS). For example, in "a b -- c d", b is TOS
  7. before, d is TOS after. "R:" means that the Return Stack is
  8. modified. "I:" prefix means "IMMEDIATE", that is, that this
  9. stack transformation is made at compile time.
  10. Word references (wordref): When we say we have a "word
  11. reference", it's a pointer to a word's *code link*. For example,
  12. the address that "' DUP" puts on the stack is a wordref, that
  13. is, a reference to the code link of the word DUP.
  14. PF: Parameter field. The area following the code link of a
  15. word. For example, "' H@ 1+" points to the PF of the word H@.
  16. Atom: A word of the type compiledWord contains, in its PF, a
  17. list of what we call "atoms". Those atoms are most of the time
  18. word references, but they can also be references to NUMBER and
  19. LIT.
  20. Words between "()" are "support words" that aren't really meant
  21. to be used directly, but as part of another word.
  22. "*I*" in description indicates an IMMEDIATE word.
  23. # Symbols
  24. Throughout words, different symbols are used in different
  25. contexts, but we try to been consistent in their use. Here's
  26. their definitions:
  27. ! - Store
  28. @ - Fetch
  29. $ - Initialize
  30. ^ - Arguments in their opposite order
  31. < - Input
  32. > - 1. Pointer in a buffer 2. Opposite of "<".
  33. ( - Lower boundary
  34. ) - Upper boundary
  35. * - Word indirection (pointer to word)
  36. ? - Is it ...? (example: IMMED?)
  37. # Entry management
  38. '? x -- a f Find x it in dict. If found, f=1 and
  39. a = wordref. If not found, f=0 and
  40. a = string addr.
  41. ' x -- a Push addr of word x to a. If not found,
  42. aborts.
  43. ['] x -- *I* Like "'", but spits the addr as a number
  44. literal. If not found, aborts.
  45. , n -- Write n in HERE and advance it.
  46. ALLOT n -- Move HERE by n bytes
  47. C, b -- Write byte b in HERE and advance it.
  48. FIND w -- a f Like '?, but for w.
  49. EMPTY -- Rewind HERE and CURRENT where they were at
  50. system initialization.
  51. FORGET x -- Rewind the dictionary (both CURRENT and HERE)
  52. up to x's previous entry.
  53. PREV a -- a Return a wordref's previous entry.
  54. WORD( a -- a Get wordref's beginning addr.
  55. # Defining words
  56. : x ... -- Define a new word
  57. ; R:I -- Exit a colon definition
  58. CREATE x -- Create cell named x. Doesn't allocate a PF.
  59. [COMPILE] x -- Compile word x and write it to HERE.
  60. IMMEDIATE words are *not* executed.
  61. COMPILE x -- Meta compiles. See B6.
  62. CONSTANT x n -- Creates cell x that when called pushes its
  63. value.
  64. DOES> -- See primer.txt
  65. IMMED? a -- f Checks whether wordref at a is immediate.
  66. IMMEDIATE -- Flag the latest defined word as immediate.
  67. LITN n -- Write number n as a literal.
  68. VARIABLE c -- Creates cell x with 2 bytes allocation.
  69. # Flow
  70. Note that flow words can only be used in definitions. In the
  71. INTERPRET loop, they don't have the desired effect because each
  72. word from the input stream is executed immediately. In this
  73. context, branching doesn't work.
  74. f IF A ELSE B THEN: if f is true, execute A, if false, execute
  75. B. ELSE is optional.
  76. [IF] .. [THEN]: Meta-IF. Works outside definitions. No [ELSE].
  77. BEGIN .. f UNTIL: if f is false, branch to BEGIN.
  78. BEGIN .. AGAIN: Always branch to BEGIN.
  79. x y DO .. LOOP: LOOP increments y. if y != x, branch to DO.
  80. x CASE y OF A ENDOF z OF B ENDOF C ENDCASE: If x == y, execute
  81. A, if x == z, execute B. Otherwise, execute C. x is dropped
  82. in case of an OF match, *but it is kept if it reaches C*. You
  83. have to consume it to avoid PSP leak.
  84. (br) -- Branches by the number specified in the 2
  85. following bytes. Can be negative.
  86. (?br) f -- Branch if f is false.
  87. ( -- *I* Comment. Ignore input until ")" is read.
  88. [ -- Begin interpretative mode. In a definition,
  89. execute words instead of compiling them.
  90. ] -- End interpretative mode.
  91. ABORT -- Resets PS and RS and returns to interpreter.
  92. ABORT" x" -- *I* Compiles a ." followed by a ABORT.
  93. ERR a -- Prints a and ABORT. Defined early and used by
  94. drivers.
  95. EXECUTE a -- Execute wordref at addr a
  96. INTERPRET -- Get a line from stdin, compile it in tmp memory,
  97. then execute the compiled contents.
  98. LEAVE -- In a DO..LOOP, exit at the next LOOP call.
  99. QUIT -- Return to interpreter prompt immediately
  100. # Parameter Stack
  101. DROP a --
  102. DUP a -- a a
  103. ?DUP DUP if a is nonzero
  104. NIP a b -- b
  105. OVER a b -- a b a
  106. ROT a b c -- b c a
  107. SWAP a b -- b a
  108. TUCK a b -- b a b
  109. 2DROP a a --
  110. 2DUP a b -- a b a b
  111. 2OVER a b c d -- a b c d a b
  112. 2SWAP a b c d -- c d a b
  113. 'S Returns current stack pointer, not counting the
  114. push it's making right now.
  115. S0 Returns address of PSP TOS. When PSP is empty,
  116. 'S == S0
  117. PICK Pick nth item from stack. "0 PICK" = DUP,
  118. "1 PICK" = OVER.
  119. ROLL Rotate PSP over n items. "1 ROLL" = SWAP,
  120. "2 ROLL" = ROT. 0 is noop.
  121. # Return Stack
  122. >R n -- R:n Pops PS and push to RS
  123. 2>R x y -- R:x y Equivalent to SWAP >R >R
  124. R> R:n -- n Pops RS and push to PS
  125. 2R> R:x y -- x y Equivalent to R> R> SWAP
  126. I -- n Copy RS TOS to PS
  127. I' -- n Copy RS second item to PS
  128. J -- n Copy RS third item to PS
  129. # Memory
  130. @ a -- n Set n to value at address a
  131. ! n a -- Store n in address a
  132. ? a -- Print value of addr a
  133. +! n a -- Increase value of addr a by n
  134. BIT@ b a -- f Get bit b from addr a.
  135. BIT! f b a -- Set bit b to f in addr a.
  136. C@ a -- c Set c to byte at address a
  137. C@+ a -- a+1 c Fetch c from a and inc a.
  138. C@- a -- a-1 c Fetch c from a and dec a.
  139. C! c a -- Store byte c in address a
  140. C!+ c a -- a+1 Store byte c in a and inc a.
  141. C!- c a -- a-1 Store byte c in a and dec a.
  142. CURRENT -- a Set a to wordref of last added entry.
  143. CURRENT* -- a A pointer to active CURRENT*. Useful
  144. when we have multiple active dicts.
  145. FILL a n b -- Fill n bytes at addr a with val b.
  146. HERE -- a Push HERE's address
  147. H@ -- a HERE @
  148. MOVE a1 a2 u -- Copy u bytes from a1 to a2, starting
  149. with a1, going up.
  150. MOVE- a1 a2 u -- Copy u bytes from a1 to a2, starting
  151. with a1+u, going down.
  152. MOVE, a u -- Copy u bytes from a to HERE.
  153. # Addressed devices
  154. ADEV$ -- Initialize adev subsystem
  155. A@ a -- c Indirect C@
  156. A! c a -- Indirect C!
  157. A@* -- a Address for A@ word
  158. A!* -- a Address for A! word
  159. AMOVE src dst u -- Same as MOVE, but with A@ and A!
  160. # Arithmetic / Bits
  161. + a b -- c a + b -> c
  162. - a b -- c a - b -> c
  163. -^ a b -- c b - a -> c
  164. * a b -- c a * b -> c
  165. / a b -- c a / b -> c
  166. MOD a b -- c a % b -> c
  167. /MOD a b -- r q r:remainder q:quotient
  168. AND a b -- c a & b -> c
  169. OR a b -- c a | b -> c
  170. XOR a b -- c a ^ b -> c
  171. LSHIFT a u -- c a << u -> c
  172. RSHIFT a u -- c a >> u -> c
  173. Shortcuts: 1+ 2+ 1- 2-
  174. # Logic
  175. = n1 n2 -- f Push true if n1 == n2
  176. < n1 n2 -- f Push true if n1 < n2
  177. > n1 n2 -- f Push true if n1 > n2
  178. >< n l h -- f Push true if l < n < h
  179. =><= n l h -- f Push true if l <= n <= h
  180. CMP n1 n2 -- n Compare n1 and n2 and set n to -1, 0, or 1.
  181. n=0: a1=a2. n=1: a1>a2. n=-1: a1<a2.
  182. MIN a b -- n Returns the lowest of a and b
  183. MAX a b -- n Returns the highest of a and b
  184. NOT f -- f Push the logical opposite of f
  185. # Strings
  186. LIT -- Write a LIT entry. You're expected to write
  187. actual string to HERE right afterwards.
  188. LIT< x -- Read following word and write to HERE as a
  189. string literal.
  190. S= a1 a2 -- f Returns whether string a1 == a2.
  191. # I/O
  192. (parse) a -- n Parses string at a as a number and push the
  193. result in n as well as whether parsing was a
  194. success in f (false = failure, true =
  195. success)
  196. (print) a -- Print string at addr a. Stops at 0x0 or 0xd.
  197. . n -- Print n in its decimal form
  198. .x n -- Print n's LSB in hex form. Always 2
  199. characters.
  200. .X n -- Print n in hex form. Always 4 characters.
  201. Numbers are never considered negative.
  202. "-1 .X" --> ffff
  203. ," xxx" -- Write xxx to HERE
  204. ." xxx" -- *I* Compiles string literal xxx followed by a
  205. call to (print).
  206. C<? -- f Returns whether there's a char waiting in buf.
  207. C< -- c Read one char from buffered input.
  208. DUMP n a -- Prints n bytes at addr a in a hexdump format.
  209. Prints in chunks of 8 bytes. Doesn't do partial
  210. lines. Output is designed to fit in 32 columns.
  211. EMIT c -- Spit char c to output stream
  212. IN> -- a Address of variable containing current pos in
  213. input buffer.
  214. KEY -- c Get char c from direct input
  215. PC! c a -- Spit c to port a
  216. PC@ a -- c Fetch c from port a
  217. WORD -- a Read one word from buffered input and push its
  218. addr. Always null terminated. If ASCII EOT is
  219. encountered, a will point to it (it is cons-
  220. idered a word).
  221. There are also ascii const emitters:
  222. BS CR LF SPC CRLF
  223. NL is an indirect word (see SYSVARS in impl.txt) that aliases to
  224. CRLF by default and that should generally be used when we want
  225. to emit a newline.
  226. # Disk
  227. BLK> -- a Address of the current block variable.
  228. BLK( -- a Beginning addr of blk buf.
  229. BLK) -- a Ending addr of blk buf.
  230. COPY s d -- Copy contents of s block to d block.
  231. FLUSH -- Write current block to disk if dirty.
  232. FREEBLKS? a b -- List free blocks between blocks a and b.
  233. LIST n -- Prints the contents of the block n on screen
  234. in the form of 16 lines of 64 columns.
  235. LOAD n -- Interprets Forth code from block n
  236. LOAD+ n -- Relative load. Loads active block + n.
  237. LOADR n1 n2 -- Load block range between n1 and n2, inclusive.
  238. LOADR+ n1 n2 -- Relative ranged load.
  239. WIPE -- Empties current block
  240. WIPED? -- f Whether current block is empty