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.

171 lines
8.2KB

  1. Be sure to read "usage.txt" for a guide to Collapse OS' Forth.
  2. *** Glossary ***
  3. Stack notation: "<stack before> -- <stack after>". Rightmost is top of stack
  4. (TOS). For example, in "a b -- c d", b is TOS before, d is TOS after. "R:" means
  5. that the Return Stack is modified. "I:" prefix means "IMMEDIATE", that is, that
  6. this stack transformation is made at compile time.
  7. Word references (wordref): When we say we have a "word reference", it's a
  8. pointer to a words *code link*. For example, the label "PLUS:" in this unit is a
  9. word reference. Why not refer to the beginning of the word struct? Because we
  10. actually seldom refer to the name and prev link, except during compilation, so
  11. defining "word reference" this way makes the code easier to understand.
  12. Atom: A word of the type compiledWord contains, in its PF, a list of what we
  13. call "atoms". Those atoms are most of the time word references, but they can
  14. also be references to NUMBER and LIT.
  15. Words between "()" are "support words" that aren't really meant to be used
  16. directly, but as part of another word.
  17. "*I*" in description indicates an IMMEDIATE word.
  18. *** Defining words ***
  19. (find) a -- a f Read at a and find it in dict. If found, f=1 and
  20. a = wordref. If not found, f=0 and a = string addr.
  21. : x ... -- Define a new word
  22. ; R:I -- Exit a colon definition
  23. , n -- Write n in HERE and advance it.
  24. ' x -- a Push addr of word x to a. If not found, aborts
  25. ['] x -- *I* Like "'", but spits the addr as a number
  26. literal. If not found, aborts.
  27. ( -- *I* Comment. Ignore rest of line until ")" is read.
  28. ALLOT n -- Move HERE by n bytes
  29. C, b -- Write byte b in HERE and advance it.
  30. CREATE x -- Create cell named x. Doesn't allocate a PF.
  31. [COMPILE] x -- Compile word x and write it to HERE. IMMEDIATE
  32. words are *not* executed.
  33. COMPILE x -- Meta compiles. Kind of blows the mind. See below.
  34. CONSTANT x n -- Creates cell x that when called pushes its value
  35. DOES> -- See description at top of file
  36. IMMED? a -- f Checks whether wordref at a is immediate.
  37. IMMEDIATE -- Flag the latest defined word as immediate.
  38. LITN n -- Write number n as a literal.
  39. VARIABLE c -- Creates cell x with 2 bytes allocation.
  40. *** Flow ***
  41. Note about flow words: flow words can only be used in definitions. In the
  42. INTERPRET loop, they don't have the desired effect because each word from the
  43. input stream is executed immediately. In this context, branching doesn't work.
  44. (br) -- Branches by the number specified in the 2 following
  45. bytes. Can be negative.
  46. (?br) f -- Branch if f is false.
  47. [ -- Begin interetative mode. In a definition, words
  48. between here and "]" will be executed instead of
  49. compiled.
  50. ] -- End interpretative mode.
  51. ABORT -- Resets PS and RS and returns to interpreter
  52. ABORT" x" -- *I* Compiles a ." followed by a ABORT.
  53. AGAIN I:a -- *I* Jump backwards to preceeding BEGIN.
  54. BEGIN -- I:a *I* Marker for backward branching with AGAIN.
  55. ELSE I:a -- *I* Compiles a (fbr) and set branching cell at a.
  56. EXECUTE a -- Execute wordref at addr a
  57. IF -- I:a *I* Compiles a (fbr?) and pushes its cell's addr
  58. INTERPRET -- Get a line from stdin, compile it in tmp memory,
  59. then execute the compiled contents.
  60. QUIT R:drop -- Return to interpreter prompt immediately
  61. RECURSE R:I -- R:I-2 Run the current word again.
  62. THEN I:a -- *I* Set branching cell at a.
  63. UNTIL f -- *I* Jump backwards to BEGIN if f is *false*.
  64. *** Parameter Stack ***
  65. DROP a --
  66. DUP a -- a a
  67. OVER a b -- a b a
  68. ROT a b c -- b c a
  69. SWAP a b -- b a
  70. 2DROP a a --
  71. 2DUP a b -- a b a b
  72. 2OVER a b c d -- a b c d a b
  73. 2SWAP a b c d -- c d a b
  74. *** Return Stack ***
  75. >R n -- R:n Pops PS and push to RS
  76. R> R:n -- n Pops RS and push to PS
  77. I -- n Copy RS TOS to PS
  78. I' -- n Copy RS second item to PS
  79. J -- n Copy RS third item to PS
  80. *** Memory ***
  81. @ a -- n Set n to value at address a
  82. ! n a -- Store n in address a
  83. ? a -- Print value of addr a
  84. +! n a -- Increase value of addr a by n
  85. C@ a -- c Set c to byte at address a
  86. C! c a -- Store byte c in address a
  87. CURRENT -- a Set a to wordref of last added entry.
  88. HERE -- a Push HERE's address
  89. H@ -- a HERE @
  90. MOVE a1 a2 u -- Copy u bytes from a1 to a2, starting with a1, going
  91. up.
  92. *** Arithmetic / Bits ***
  93. + a b -- c a + b -> c
  94. - a b -- c a - b -> c
  95. -^ a b -- c b - a -> c
  96. * a b -- c a * b -> c
  97. / a b -- c a / b -> c
  98. MOD a b -- c a % b -> c
  99. /MOD a b -- r q r:remainder q:quotient
  100. AND a b -- c a & b -> c
  101. OR a b -- c a | b -> c
  102. XOR a b -- c a ^ b -> c
  103. *** Logic ***
  104. = n1 n2 -- f Push true if n1 == n2
  105. < n1 n2 -- f Push true if n1 < n2
  106. > n1 n2 -- f Push true if n1 > n2
  107. CMP n1 n2 -- n Compare n1 and n2 and set n to -1, 0, or 1.
  108. n=0: a1=a2. n=1: a1>a2. n=-1: a1<a2.
  109. NOT f -- f Push the logical opposite of f
  110. *** Strings ***
  111. LIT -- Write a LIT entry. You're expected to write actual
  112. string to HERE right afterwards.
  113. LIT< x -- Read following word and write to HERE as a string
  114. literal.
  115. LITS a -- Write word at addr a as a atring literal.
  116. SCMP a1 a2 -- n Compare strings a1 and a2. See CMP
  117. SCPY a -- Copy string at addr a into HERE.
  118. SLEN a -- n Push length of str at a.
  119. *** I/O ***
  120. (parse) a -- n Parses string at a as a number and push the result
  121. in n as well as whether parsing was a success in f
  122. (false = failure, true = success)
  123. (parse.) a -- n f Sub-parsing words. They all have the same signature.
  124. Parses string at a as a number and push the result
  125. in n as well as whether parsing was a success in f
  126. (0 = failure, 1 = success)
  127. (parse*) -- a Variable holding the current pointer for system
  128. number parsing. By default, (parse).
  129. (print) a -- Print string at addr a.
  130. . n -- Print n in its decimal form
  131. .x n -- Print n's LSB in hex form. Always 2 characters.
  132. .X n -- Print n in hex form. Always 4 characters. Numbers
  133. are never considered negative. "-1 .X" --> ffff
  134. ." xxx" -- *I* Compiles string literal xxx followed by a call
  135. to (print)
  136. C< -- c Read one char from buffered input.
  137. DUMP n a -- Prints n bytes at addr a in a hexdump format.
  138. Prints in chunks of 8 bytes. Doesn't do partial
  139. lines. Output is designed to fit in 32 columns.
  140. EMIT c -- Spit char c to output stream
  141. IN> -- a Address of variable containing current pos in input
  142. buffer.
  143. KEY -- c Get char c from direct input
  144. PC! c a -- Spit c to port a
  145. PC@ a -- c Fetch c from port a
  146. WORD -- a Read one word from buffered input and push its addr
  147. There are also ascii const emitters:
  148. BS
  149. CR
  150. LF
  151. SPC