Cart-Forth is a small Forth subset compiled to spriteCart's stack-VM bytecode. Words are whitespace-separated; data flows through a single 16-bit signed stack.
Stack primitives
Word
Stack effect
Summary
dup
( a -- a a )
Duplicate top of stack.
drop
( a -- )
Discard top of stack.
swap
( a b -- b a )
Swap top two cells.
over
( a b -- a b a )
Copy second cell to top.
rot
( a b c -- b c a )
Rotate top three cells.
nip
( a b -- b )
Discard second cell.
tuck
( a b -- b a b )
Copy top below second.
pick
( xn ... x0 n -- xn ... x0 xn )
Copy the nth cell from the top (0-indexed) to the top; 0 pick = dup, 1 pick = over.
2dup
( a b -- a b a b )
Duplicate the top cell pair (equivalent to `over over`).
+
( a b -- a+b )
Add.
-
( a b -- a-b )
Subtract.
*
( a b -- a*b )
Multiply.
/
( a b -- a/b )
Divide (signed; division by zero traps).
mod
( a b -- a%b )
Modulo.
/mod
( a b -- a%b a/b )
Push modulus then quotient (both signed).
negate
( a -- -a )
Two's complement negation.
abs
( a -- |a| )
Absolute value.
min
( a b -- min )
Minimum (signed).
max
( a b -- max )
Maximum (signed).
=
( a b -- flag )
Push -1 if equal, else 0.
<>
( a b -- flag )
Push -1 if not equal, else 0.
<
( a b -- flag )
Push -1 if a < b (signed), else 0.
>
( a b -- flag )
Push -1 if a > b (signed), else 0.
<=
( a b -- flag )
Push -1 if a ≤ b (signed), else 0.
>=
( a b -- flag )
Push -1 if a ≥ b (signed), else 0.
and
( a b -- a&b )
Bitwise AND.
or
( a b -- a|b )
Bitwise OR.
xor
( a b -- a^b )
Bitwise XOR.
invert
( a -- ~a )
Bitwise NOT.
lshift
( a n -- a<<n )
Logical left shift by n bits (shift count masked to 4 bits).
rshift
( a n -- a>>n )
Logical right shift by n bits (unsigned; shift count masked to 4 bits).
@
( addr -- v )
Fetch cell at addr.
!
( v addr -- )
Store v at addr.
c@
( addr -- byte )
Fetch byte at addr.
c!
( byte addr -- )
Store byte at addr.
+!
( v addr -- )
Add v to the cell at addr (fetch, add, store).
Defining words
Word
Stack effect
Summary
:
( -- )
Begin a colon-definition; ends with `;`.
;
( -- )
End a colon-definition.
variable
( -- )
Define a 1-cell variable; the new word pushes its address.
constant
( v -- )
Define a constant; the new word pushes `v`.
Control flow
Word
Stack effect
Summary
if
( flag -- )
Begin a conditional. Paired with `else` (optional) and `then`.
else
( -- )
Else branch of an `if`.
then
( -- )
End an `if` (no relation to other Forths' postfix-then).
begin
( -- )
Top of a `begin … until` loop.
until
( flag -- )
Loop back to `begin` while flag is 0; exit when non-zero.
do
( limit start -- )
Begin a counted loop body; `i` accesses the index.