: ( ') word sp@ ! sp@ ! ;

( Creation of variables and constants )
: constant create 4 x, does> @ ;
: variable create 4 x, ;

( Useful constants )
-1 constant true
 0 constant false
 4 constant cell-size
 1 constant char-size

( Numeric bases )
: hex 16 base ! ;
: decimal 10 base ! ;

( Tiny inline assembler )
: here h @ ;

0 variable codeX
: :code depth codeX ! create here -5 + h ! ;
: c, here c! here 1 + h ! ;
: bytes times c, repeat bytes ;
: ;code depth codeX @ - bytes $c3 c, ;

( Some useful primitives )
:code swap $06 $87 ;code
:code not $d0 $f7 ;code
:code mod $d0 $89 $fb $f7 $d2 $31 $ad $c3 $89 ;code
:code abs $d8 $29 $d8 $31 $1f $fb $c1 $c3 $89 ;code
:code fill $fb $e2 $47 $17 $89 $ad $c7 $89 $ad $c1 $89 $ad $c2 $89 ;code
:code and $04 $c6 $83 $06 $23 ;code
:code max $04 $C6 $83 $06 $8B $06 $89 $02 $7C $06 $3B ;code
:code min $04 $C6 $83 $06 $8B $06 $89 $02 $7F $06 $3B ;code
:code = $04 $C6 $83 $C0 $19 $01 $E8 $83 $06 $2B ;code
:code < $04 $C6 $83 $FF $FF $FF $FF $B8 $C3 $04 $C6 $83 $C0 $31 $06 $7C $06 $3B ;code

( Inlining bytes to HERE )
: , 4 x, ;
: 1, 1 x, ;
: 2, 2 x, ;
: 3, 3 x, ;

( Memory Management )
: pad here 80 + ;
: align 0 here cell-size mod x, ;
: allot x, ;
: cell+ cell-size + ;
: cells cell-size * ;
: char+ char-size + ;
: chars char-size * ;

( Stack Operations )
: dup depth 0 = if ;; then sp@ @ ;
: drop sp@ ! ;
: over sp@ cell-size + @ ;
: pick cell-size * sp@ + @ ;
: rot >r swap r> swap ;
: -rot rot rot ;
: nip swap drop ;
: tuck swap over ;
: 2dup over over ;
: 2drop drop drop ;
: neg -1 * ;

( Math extensions )
: /mod over over / rot rot mod ;
: */ * / ;
: */mod * /mod ;

( Extensions for IF/THEN )
: <> = not ;
: > swap < ;
: ?dup dup 0 = if ;; then dup ;
: ?negate dup 0 < not if ;; then neg ;

( Variable handling )
: off false swap ! ;
: on true swap ! ;
: +! swap over @ + swap ! ;
: ++ 1 +! ;
: -- -1 +! ;

( Create arrays and LIFO stacks )
: array create cells allot does> swap cells + ;
: stack: create cells dup here cell+ dup , + , allot ;
: push 1 cells over +! @ ! ;
: pop dup @ @ -1 cells rot +! ;

( String Handling )
: " '" word ;
: count dup >r c@ r> char+ swap ;
: place over over >r >r char+ swap cmove r> r> c! ;
: copy$ >r count swap 1 - swap r> swap cmove ;
: ' 32 word find ;
: /string over min rot over + -rot - ;
: >type tuck pad swap cmove pad swap type ;
: .( ') word >type ;

( Compiler Extensions )
: >body 5 - ;
: defined 32 word find ;
: execute >r ;

( Reset dictionary to 'clean' point )
0 variable cold.h
0 variable cold.d
: cold cold.d @ last ! cold.h @ h ! ;
: save-state here cold.h ! d @ cold.d ! ;

drop
