+\ ------ Cons cell memory ------ {{{
+
+1000 constant N
+create car-cells N allot
+create car-type-cells N allot
+create cdr-cells N allot
+create cdr-type-cells N allot
+
+variable nextfree
+0 nextfree !
+
+: cons ( car-obj cdr-obj -- pair-obj )
+ cdr-type-cells nextfree @ + !
+ cdr-cells nextfree @ + !
+ car-type-cells nextfree @ + !
+ car-cells nextfree @ + !
+
+ nextfree @ pair-type
+
+ 1 nextfree +!
+;
+
+: car ( pair-obj -- car-obj )
+ drop
+ dup car-cells + @ swap
+ car-type-cells + @
+;
+
+: cdr ( pair-obj -- car-obj )
+ drop
+ dup cdr-cells + @ swap
+ cdr-type-cells + @
+;
+
+: set-car! ( obj pair-obj -- )
+ drop dup
+ rot swap car-type-cells + !
+ car-cells + !
+;
+
+: set-cdr! ( obj pair-obj -- )
+ drop dup
+ rot swap cdr-type-cells + !
+ cdr-cells + !
+;
+
+: caar car car ;
+: cadr cdr car ;
+: cdar car cdr ;
+: cddr cdr cdr ;
+
+: nil 0 nil-type ;
+: nil? nil-type istype? ;
+
+: objvar create nil swap , , ;
+
+: value@ ( objvar -- val ) @ ;
+: type@ ( objvar -- type ) 1+ @ ;
+: value! ( newval objvar -- ) ! ;
+: type! ( newtype objvar -- ) 1+ ! ;
+: setobj ( newobj objvar -- ) dup rot swap 1+ ! ! ;
+: fetchobj ( objvar -- obj ) dup @ swap 1+ @ ;
+
+: objeq? ( obj obj -- bool )
+ rot = -rot = and ;
+
+: 2rot ( a1 a2 b1 b2 c1 c2 -- b1 b2 c1 c2 a1 a2 )
+ >R >R ( a1 a2 b1 b2 )
+ 2swap ( b1 b2 a1 a2 )
+ R> R> ( b1 b2 a1 a2 c1 c2 )
+ 2swap
+;
+
+: -2rot ( a1 a2 b1 b2 c1 c2 -- c1 c2 a1 a2 b1 b2 )
+ 2swap ( a1 a2 c1 c2 b1 b2 )
+ >R >R ( a1 a2 c1 c2 )
+ 2swap ( c1 c2 a1 a2 )
+ R> R>
+;
+
+\ }}}
+
+\ ---- Pre-defined symbols ---- {{{
+
+objvar symbol-table
+
+: (create-symbol) ( addr n -- symbol-obj )
+ dup 0= if
+ 2drop nil
+ else
+ 2dup drop @ character-type 2swap
+ swap 1+ swap 1-
+ recurse
+
+ cons
+ then
+;
+
+: create-symbol ( -- )
+ bl word
+ count
+
+ (create-symbol)
+ drop symbol-type
+
+ 2dup
+
+ symbol-table fetchobj
+ cons
+ symbol-table setobj
+
+ create swap , ,
+ does> dup @ swap 1+ @
+;
+
+create-symbol quote quote-symbol
+create-symbol define define-symbol
+create-symbol set! set!-symbol
+create-symbol ok ok-symbol
+create-symbol if if-symbol
+
+\ }}}
+
+\ ---- Environments ---- {{{
+
+: enclosing-env ( env -- env )
+ cdr ;
+
+: first-frame ( env -- frame )
+ car ;
+
+: make-frame ( vars vals -- frame )
+ cons ;
+
+: frame-vars ( frame -- vars )
+ car ;
+
+: frame-vals ( frame -- vals )
+ cdr ;
+
+: add-binding ( var val frame -- )
+ 2swap 2over frame-vals cons
+ 2over set-cdr!
+ 2swap 2over frame-vars cons
+ 2swap set-car!
+;
+
+: extend-env ( vars vals env -- env )
+ >R >R
+ make-frame
+ R> R>
+ cons
+;
+
+objvar vars
+objvar vals
+
+: get-vars-vals-frame ( var frame -- bool )
+ 2dup frame-vars vars setobj
+ frame-vals vals setobj
+
+ begin
+ vars fetchobj nil objeq? false =
+ while
+ 2dup vars fetchobj car objeq? if
+ 2drop true
+ exit
+ then
+
+ vars fetchobj cdr vars setobj
+ vals fetchobj cdr vals setobj
+ repeat
+
+ 2drop false
+;
+
+: get-vars-vals ( var env -- vars? vals? bool )
+
+ begin
+ 2dup nil objeq? false =
+ while
+ 2over 2over first-frame
+ get-vars-vals-frame if
+ 2drop 2drop
+ vars fetchobj vals fetchobj true
+ exit
+ then
+
+ enclosing-env
+ repeat
+
+ 2drop 2drop
+ false
+;
+
+hide vars
+hide vals
+
+: lookup-var ( var env -- val )
+ get-vars-vals if
+ 2swap 2drop car
+ else
+ bold fg red ." Tried to read unbound variable." reset-term cr abort
+ then
+;
+
+: set-var ( var val env -- )
+ >R >R 2swap R> R> ( val var env )
+ get-vars-vals if
+ 2swap 2drop ( val vals )
+ set-car!
+ else
+ bold fg red ." Tried to set unbound variable." reset-term cr abort
+ then
+;
+
+objvar env
+
+: define-var ( var val env -- )
+ env setobj
+
+ 2over env fetchobj ( var val var env )
+ get-vars-vals if
+ 2swap 2drop ( var val vals )
+ set-car!
+ 2drop
+ else
+ env fetchobj
+ first-frame ( var val frame )
+ add-binding
+ then
+;
+
+hide env
+
+objvar global-env
+nil nil nil extend-env
+global-env setobj
+
+\ }}}
+
+\ ---- Primitives ---- {{{
+
+: make-primitive ( cfa -- )
+ bl word
+ count
+
+ (create-symbol)
+ drop symbol-type
+
+ 2dup
+
+ symbol-table fetchobj
+ cons
+ symbol-table setobj
+
+ rot primitive-type ( var prim )
+ global-env fetchobj define-var
+;
+
+: add-prim ( args -- )
+ nil objeq? if
+ 0 number-type
+ else
+ 2dup cdr recurse drop
+ -rot car drop
+ + number-type
+ then
+;
+
+' add-prim make-primitive +
+
+\ }}}
+
+\ ---- Read ---- {{{
+
+defer read