+: duplicate-charlist ( charlist -- copy )
+ 2dup nil objeq? false = if
+ 2dup car 2swap cdr recurse cons
+ then ;
+
+: charlist-equiv ( charlist charlist -- bool )
+
+ 2over 2over
+
+ \ One or both nil
+ nil? -rot 2drop
+ if
+ nil? -rot 2drop
+ if
+ 2drop 2drop true exit
+ else
+ 2drop 2drop false exit
+ then
+ else
+ nil? -rot 2drop
+ if
+ 2drop 2drop false exit
+ then
+ then
+
+ 2over 2over
+
+ \ Neither nil
+ car drop -rot car drop = if
+ cdr 2swap cdr recurse
+ else
+ 2drop 2drop false
+ then
+;
+
+: charlist>symbol ( charlist -- symbol-obj )
+
+ symbol-table fetchobj
+
+ begin
+ nil? false =
+ while
+ 2over 2over
+ car drop pair-type
+ charlist-equiv if
+ 2swap 2drop
+ car
+ exit
+ else
+ cdr
+ then
+ repeat
+
+ 2drop
+ drop symbol-type 2dup
+ symbol-table fetchobj cons
+ symbol-table setobj
+;
+
+
+: (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
+;
+
+: arg-count-error
+ bold fg red ." Incorrect argument count." reset-term cr
+ abort
+;
+
+: ensure-arg-count ( args n -- )
+ dup 0= if
+ drop nil objeq? false = if
+ arg-count-error
+ then
+ else
+ -rot 2dup nil objeq? if
+ arg-count-error
+ then
+
+ cdr rot 1- recurse
+ then
+;
+
+: arg-type-error
+ bold fg red ." Incorrect argument type." reset-term cr
+ abort
+;
+
+: ensure-arg-type ( arg type -- arg )
+ istype? false = if
+ arg-type-error
+ then
+;
+
+include scheme-primitives.4th
+
+\ }}}
+
+\ ---- Read ---- {{{
+
+defer read