+include term-colours.4th
+include defer-is.4th
+include float.4th
+
+include debugging.4th
+
+defer read
+defer eval
+defer print
+
+defer collect-garbage
+
+\ ------ Types ------
+
+variable nexttype
+0 nexttype !
+: make-type
+ create nexttype @ ,
+ 1 nexttype +!
+ does> @ ;
+
+make-type fixnum-type
+make-type realnum-type
+make-type boolean-type
+make-type character-type
+make-type string-type
+make-type nil-type
+make-type none-type
+make-type pair-type
+make-type symbol-type
+make-type primitive-proc-type
+make-type compound-proc-type
+make-type fileport-type
+: istype? ( obj type -- obj bool )
+ over = ;
+
+\ ---- List-structured memory ---- {{{
+
+10000 constant scheme-memsize
+
+create car-cells scheme-memsize allot
+create car-type-cells scheme-memsize allot
+create cdr-cells scheme-memsize allot
+create cdr-type-cells scheme-memsize allot
+
+create nextfrees scheme-memsize allot
+:noname
+ scheme-memsize 0 do
+ i 1+ nextfrees i + !
+ loop
+; execute
+
+variable nextfree
+0 nextfree !
+
+: inc-nextfree
+ nextfrees nextfree @ + @
+ nextfree !
+
+ nextfree @ scheme-memsize >= if
+ collect-garbage
+ then
+
+ nextfree @ scheme-memsize >= if
+ fg red bold
+ ." Out of memory! Aborting."
+ reset-term abort
+ then
+;
+
+: cons ( car-obj cdr-obj -- pair-obj )
+ cdr-type-cells nextfree @ + !
+ cdr-cells nextfree @ + !
+ car-type-cells nextfree @ + !
+ car-cells nextfree @ + !
+
+ nextfree @ pair-type
+ inc-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? ;
+
+: none 0 none-type ;
+: none? none-type istype? ;
+
+: objvar create nil swap , , ;
+
+: value@ ( objvar -- val ) @ ;
+: type@ ( objvar -- type ) 1+ @ ;
+: value! ( newval objvar -- ) ! ;
+: type! ( newtype objvar -- ) 1+ ! ;
+: obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
+: obj@ ( 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>
+;
+
+\ }}}
+
+\ ---- Port I/O ---- {{{
+
+: fileport>fid ( fileport -- fid )
+ drop ;
+
+: fid>fileport ( fid -- fileport )
+ fileport-type ;
+
+: open-input-file ( addr n -- fileport )
+ r/o open-file drop fid>fileport
+;
+
+: close-port ( fileport -- )
+ fileport>fid close-file drop
+;
+
+objvar console-i/o-port
+0 fileport-type console-i/o-port obj!
+
+objvar current-input-port
+console-i/o-port obj@ current-input-port obj!
+
+: read-port ( fileport -- obj )
+ current-input-port obj!
+ read ;
+
+: read-console ( -- obj )
+ console-i/o-port obj@ read-port ;
+
+\ }}}
+
+\ ---- Pre-defined symbols ---- {{{
+
+objvar symbol-table
+
+: duplicate-charlist ( charlist -- copy )
+ nil? 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 obj@
+
+ 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 obj@ cons
+ symbol-table obj!
+;
+
+
+: cstr>charlist ( addr n -- charlist )
+ dup 0= if
+ 2drop nil
+ else
+ 2dup drop @ character-type 2swap
+ swap 1+ swap 1-
+ recurse
+
+ cons
+ then
+;
+
+: create-symbol ( -- )
+ bl word
+ count
+
+ cstr>charlist
+ charlist>symbol
+
+ create swap , ,
+ does> dup @ swap 1+ @
+;
+
+create-symbol quote quote-symbol
+create-symbol define define-symbol
+create-symbol define-macro define-macro-symbol
+create-symbol set! set!-symbol
+create-symbol ok ok-symbol
+create-symbol if if-symbol
+create-symbol lambda lambda-symbol
+create-symbol λ λ-symbol
+create-symbol begin begin-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 obj!
+ frame-vals vals obj!
+
+ begin
+ vars obj@ nil objeq? false =
+ while
+ 2dup vars obj@ car objeq? if
+ 2drop true
+ exit
+ then
+
+ vars obj@ cdr vars obj!
+ vals obj@ cdr vals obj!
+ repeat
+
+ 2drop false
+;
+
+: get-vars-vals ( var env -- vars? vals? bool )
+
+ begin
+ nil? false =
+ while
+ 2over 2over first-frame
+ get-vars-vals-frame if
+ 2drop 2drop
+ vars obj@ vals obj@ 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 obj!
+
+ 2over env obj@ ( var val var env )
+ get-vars-vals if
+ 2swap 2drop ( var val vals )
+ set-car!
+ 2drop
+ else
+ env obj@
+ first-frame ( var val frame )
+ add-binding
+ then
+;
+
+hide env
+
+: make-procedure ( params body env -- proc )
+ nil
+ cons cons cons
+ drop compound-proc-type
+;
+
+objvar global-env
+nil nil nil extend-env
+global-env obj!
+
+\ }}}
+
+\ ---- Primitives ---- {{{
+
+: make-primitive ( cfa -- )
+ bl word
+ count
+
+ \ 2dup ." Defining primitive " type ." ..." cr
+
+ cstr>charlist
+ charlist>symbol
+
+ rot primitive-proc-type ( var prim )
+ global-env obj@ 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 nil? 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
+;
+
+
+\ }}}
+
+\ ---- Macros ---- {{{
+
+objvar macro-table
+
+( Look up macro in macro table. Returns nil if
+ no macro is found. )
+: lookup-macro ( name_symbol -- proc )
+ macro-table obj@
+
+ begin
+ nil? false =
+ while
+ 2over 2over
+ car car objeq? if
+ 2swap 2drop
+ car cdr
+ exit
+ then
+
+ cdr
+ repeat
+
+ 2swap 2drop
+;
+
+: make-macro ( name_symbol params body env -- )
+ make-procedure
+
+ 2swap ( proc name_symbol )
+
+ macro-table obj@
+
+ begin
+ nil? false =
+ while
+ 2over 2over ( proc name table name table )
+ car car objeq? if
+ 2swap 2drop ( proc table )
+ car ( proc entry )
+ set-cdr!
+ exit
+ then
+
+ cdr
+ repeat
+
+ 2drop
+
+ 2swap cons
+ macro-table obj@ cons
+ macro-table obj!
+;
+
+\ }}}
+
+\ ---- Read ---- {{{
+
+variable parse-idx
+variable stored-parse-idx
+create parse-str 161 allot
+variable parse-str-span
+
+create parse-idx-stack 10 allot
+variable parse-idx-sp
+parse-idx-stack parse-idx-sp !
+
+: push-parse-idx
+ parse-idx @ parse-idx-sp @ !
+ 1 parse-idx-sp +!
+;
+
+: pop-parse-idx
+ parse-idx-sp @ parse-idx-stack <= abort" Parse index stack underflow."
+
+ 1 parse-idx-sp -!
+
+ parse-idx-sp @ @ parse-idx ! ;
+
+
+: append-newline
+ '\n' parse-str parse-str-span @ + !
+ 1 parse-str-span +! ;
+
+: append-eof
+ 4 parse-str parse-str-span @ + !
+ 1 parse-str-span +! ;
+
+: empty-parse-str
+ 0 parse-str-span !
+ 0 parse-idx ! ;
+
+: getline
+ current-input-port obj@ console-i/o-port obj@ objeq? if
+ parse-str 160 expect cr
+ span @ parse-str-span !
+ else
+ parse-str 160 current-input-port obj@ fileport>fid read-line
+ drop swap parse-str-span !
+
+ parse-str-span @ 0= and if append-eof then
+ then
+ append-newline
+ 0 parse-idx ! ;
+
+: inc-parse-idx
+ 1 parse-idx +! ;
+
+: dec-parse-idx
+ 1 parse-idx -! ;
+
+: charavailable? ( -- bool )
+ parse-str-span @ parse-idx @ > ;
+
+: nextchar ( -- char )
+ charavailable? false = if getline then
+ parse-str parse-idx @ + @ ;
+
+: '\t' 9 ;
+: whitespace? ( -- bool )
+ nextchar BL =
+ nextchar '\n' =
+ nextchar '\t' =
+ or or ;
+
+: EOF 4 ;
+: eof? ( -- bool )
+ nextchar EOF = ;
+
+: delim? ( -- bool )
+ whitespace?
+ nextchar [char] ( = or
+ nextchar [char] ) = or
+;
+
+: commentstart? ( -- bool )
+ nextchar [char] ; = ;
+
+: eatspaces
+
+ false \ Indicates whether or not we're eating a comment
+
+ begin
+ dup whitespace? or commentstart? or
+ while
+ dup nextchar '\n' = and if
+ invert \ Stop eating comment
+ else
+ dup false = commentstart? and if
+ invert \ Begin eating comment
+ then
+ then
+
+ inc-parse-idx
+ repeat
+ drop
+;
+
+: digit? ( -- bool )
+ nextchar [char] 0 >=
+ nextchar [char] 9 <=
+ and ;
+
+: minus? ( -- bool )
+ nextchar [char] - = ;
+
+: plus? ( -- bool )
+ nextchar [char] + = ;
+
+: fixnum? ( -- bool )
+ minus? plus? or if
+ inc-parse-idx
+
+ delim? if
+ dec-parse-idx
+ false exit
+ else
+ dec-parse-idx
+ then
+ else
+ digit? false = if
+ false exit
+ then
+ then
+
+ push-parse-idx
+ inc-parse-idx
+
+ begin digit? while
+ inc-parse-idx
+ repeat
+
+ delim? pop-parse-idx
+;
+
+: realnum? ( -- bool )
+ push-parse-idx
+
+ minus? plus? or if
+ inc-parse-idx
+ then
+
+ \ Record starting parse idx:
+ \ Want to detect whether any characters (following +/-) were eaten.
+ parse-idx @
+
+ begin digit? while
+ inc-parse-idx
+ repeat
+
+ [char] . nextchar = if
+ inc-parse-idx
+ begin digit? while
+ inc-parse-idx
+ repeat
+ then
+
+ [char] e nextchar = [char] E nextchar = or if
+ inc-parse-idx
+
+ minus? plus? or if
+ inc-parse-idx
+ then
+
+ digit? invert if
+ drop pop-parse-idx false exit
+ then
+
+ begin digit? while
+ inc-parse-idx
+ repeat
+ then
+
+ \ This is a real number if characters were
+ \ eaten and the next characer is a delimiter.
+ parse-idx @ < delim? and
+
+ pop-parse-idx
+;
+
+: boolean? ( -- bool )
+ nextchar [char] # <> if false exit then
+
+ push-parse-idx
+ inc-parse-idx
+
+ nextchar [char] t <>
+ nextchar [char] f <>
+ and if pop-parse-idx false exit then
+
+ inc-parse-idx
+ delim? if
+ pop-parse-idx
+ true
+ else
+ pop-parse-idx
+ false
+ then
+;
+
+: str-equiv? ( str -- bool )
+
+ push-parse-idx
+
+ true -rot
+
+ swap dup rot + swap
+
+ do
+ i @ nextchar <> if
+ drop false
+ leave
+ then
+
+ inc-parse-idx
+ loop
+
+ delim? false = if drop false then
+
+ pop-parse-idx
+;
+
+: character? ( -- bool )
+ nextchar [char] # <> if false exit then
+
+ push-parse-idx
+ inc-parse-idx
+
+ nextchar [char] \ <> if pop-parse-idx false exit then
+
+ inc-parse-idx
+
+ S" newline" str-equiv? if pop-parse-idx true exit then
+ S" space" str-equiv? if pop-parse-idx true exit then
+ S" tab" str-equiv? if pop-parse-idx true exit then
+
+ charavailable? false = if pop-parse-idx false exit then
+
+ pop-parse-idx true
+;
+
+: pair? ( -- bool )
+ nextchar [char] ( = ;
+
+: string? ( -- bool )
+ nextchar [char] " = ;
+
+: readfixnum ( -- num-atom )
+ plus? minus? or if
+ minus?
+ inc-parse-idx
+ else
+ false
+ then
+
+ 0
+
+ begin digit? while
+ 10 * nextchar [char] 0 - +
+ inc-parse-idx
+ repeat
+
+ swap if negate then
+
+ fixnum-type
+;
+
+: readrealnum ( -- realnum )
+
+ \ Remember that at this point we're guaranteed to
+ \ have a parsable real on this line.
+
+ parse-str parse-idx @ +
+
+ begin delim? false = while
+ inc-parse-idx
+ repeat
+
+ parse-str parse-idx @ + over -
+
+ float-parse
+
+ realnum-type
+;
+
+: readbool ( -- bool-obj )
+ inc-parse-idx
+
+ nextchar [char] f = if
+ false
+ else
+ true
+ then
+
+ inc-parse-idx
+
+ boolean-type
+;
+
+: readchar ( -- char-obj )
+ inc-parse-idx
+ inc-parse-idx
+
+ S" newline" str-equiv? if 7 parse-idx +! '\n' character-type exit then
+ S" space" str-equiv? if 5 parse-idx +! bl character-type exit then
+ S" tab" str-equiv? if 3 parse-idx +! 9 character-type exit then
+
+ nextchar character-type
+
+ inc-parse-idx
+;
+
+: readstring ( -- charlist )
+ nextchar [char] " = if
+ inc-parse-idx
+
+ delim? false = if
+ bold fg red
+ ." No delimiter following right double quote. Aborting." cr
+ reset-term abort
+ then
+
+ dec-parse-idx
+
+ 0 nil-type exit
+ then
+
+ nextchar [char] \ = if
+ inc-parse-idx
+ nextchar case
+ [char] n of '\n' endof
+ [char] " of [char] " endof
+ [char] \
+ endcase
+ else
+ nextchar
+ then
+ inc-parse-idx character-type