+: readpair ( -- pairobj )
+ eatspaces
+
+ \ Empty lists
+ nextchar [char] ) = if
+ inc-parse-idx
+
+ delim? false = if
+ bold fg red
+ ." No delimiter following right paren. Aborting." cr
+ reset-term abort
+ then
+
+ dec-parse-idx
+
+ 0 nil-type exit
+ then
+
+ \ Read first pair element
+ read
+
+ \ Pairs
+ eatspaces
+ nextchar [char] . = if
+ inc-parse-idx
+
+ delim? false = if
+ bold fg red
+ ." No delimiter following '.'. Aborting." cr
+ reset-term abort
+ then
+
+ eatspaces read
+ else
+ recurse
+ then
+
+ eatspaces
+
+ cons
+;
+
+\ Parse a scheme expression
+:noname ( -- obj )
+
+ eatspaces
+
+ fixnum? if
+ readfixnum
+ exit
+ then
+
+ realnum? if
+ readrealnum
+ exit
+ then
+
+ boolean? if
+ readbool
+ exit
+ then
+
+ character? if
+ readchar
+ exit
+ then
+
+ string? if
+ inc-parse-idx
+
+ readstring
+ drop string-type
+
+ nextchar [char] " <> if
+ bold red ." Missing closing double-quote." reset-term cr
+ abort
+ then
+
+ inc-parse-idx
+
+ exit
+ then
+
+ pair? if
+ inc-parse-idx
+
+ eatspaces
+
+ readpair
+
+ eatspaces
+
+ nextchar [char] ) <> if
+ bold red ." Missing closing paren." reset-term cr
+ abort
+ then
+
+ inc-parse-idx
+
+ exit
+ then
+
+ nextchar [char] ' = if
+ inc-parse-idx
+ quote-symbol recurse nil cons cons exit
+ then
+
+ eof? if
+ EOF character-type
+ inc-parse-idx
+ exit
+ then
+
+ \ Anything else is parsed as a symbol
+ readsymbol charlist>symbol
+
+ \ Replace λ with lambda
+ 2dup λ-symbol objeq? if
+ 2drop lambda-symbol
+ then
+
+
+; is read
+
+\ }}}
+
+\ ---- Eval ---- {{{
+
+: self-evaluating? ( obj -- obj bool )
+ boolean-type istype? if true exit then
+ fixnum-type istype? if true exit then
+ realnum-type istype? if true exit then
+ character-type istype? if true exit then
+ string-type istype? if true exit then
+ nil-type istype? if true exit then
+ none-type istype? if true exit then
+
+ false
+;
+
+: tagged-list? ( obj tag-obj -- obj bool )
+ 2over
+ pair-type istype? false = if
+ 2drop 2drop false
+ else
+ car objeq?
+ then ;
+
+: quote? ( obj -- obj bool )
+ quote-symbol tagged-list? ;
+
+: quote-body ( quote-obj -- quote-body-obj )
+ cadr ;
+
+: variable? ( obj -- obj bool )
+ symbol-type istype? ;
+
+: definition? ( obj -- obj bool )
+ define-symbol tagged-list? ;
+
+: make-lambda ( params body -- lambda-exp )
+ lambda-symbol -2rot cons cons ;
+
+: definition-var ( obj -- var )
+ cdr car
+ symbol-type istype? false = if car then
+;
+
+: definition-val ( obj -- val )
+ 2dup cdr car symbol-type istype? if
+ 2drop
+ cdr cdr car
+ else
+ cdr 2swap cdr cdr
+ make-lambda
+ then
+;
+
+: assignment? ( obj -- obj bool )
+ set!-symbol tagged-list? ;
+
+: assignment-var ( obj -- var )
+ cdr car ;
+
+: assignment-val ( obj -- val )
+ cdr cdr car ;
+
+: eval-definition ( obj env -- res )
+ 2swap
+ 2over 2over ( env obj env obj )
+ definition-val 2swap ( env obj valexp env )
+ eval ( env obj val )
+
+ 2swap definition-var 2swap ( env var val )
+
+ 2rot ( var val env )
+ define-var
+
+ ok-symbol
+;
+
+: eval-assignment ( obj env -- res )
+ 2swap
+ 2over 2over ( env obj env obj )
+ assignment-val 2swap ( env obj valexp env )
+ eval ( env obj val )
+
+ 2swap assignment-var 2swap ( env var val )
+
+ 2rot ( var val env )
+ set-var
+
+ ok-symbol
+;
+
+: if? ( obj -- obj bool )
+ if-symbol tagged-list? ;
+
+: if-predicate ( ifobj -- pred )
+ cdr car ;
+
+: if-consequent ( ifobj -- conseq )
+ cdr cdr car ;
+
+: if-alternative ( ifobj -- alt|false )
+ cdr cdr cdr
+ 2dup nil objeq? if
+ 2drop false
+ else
+ car
+ then ;
+
+: false? ( boolobj -- boolean )
+ boolean-type istype? if
+ false boolean-type objeq?
+ else
+ 2drop false
+ then
+;
+
+: true? ( boolobj -- bool )
+ false? invert ;
+
+: lambda? ( obj -- obj bool )
+ lambda-symbol tagged-list? ;
+
+: lambda-parameters ( obj -- params )
+ cdr car ;
+
+: lambda-body ( obj -- body )
+ cdr cdr ;
+
+: make-procedure ( params body env -- proc )
+ nil
+ cons cons cons
+ drop compound-proc-type
+;
+
+: begin? ( obj -- obj bool )
+ begin-symbol tagged-list? ;
+
+: begin-actions ( obj -- actions )
+ cdr ;
+
+: eval-sequence ( explist env -- finalexp env )
+ ( Evaluates all bar the final expressions in
+ an an expression list. The final expression
+ is returned to allow for tail optimization. )
+
+ 2swap ( env explist )
+
+ \ Abort on empty list
+ 2dup nil objeq? if
+ 2drop none
+ 2swap exit
+ then
+
+ begin
+ 2dup cdr ( env explist nextexplist )
+ 2dup nil objeq? false =
+ while
+ -2rot car 2over ( nextexplist env exp env )
+ eval
+ 2drop \ discard result
+ 2swap ( env nextexplist )
+ repeat
+
+ 2drop car 2swap ( finalexp env )
+;
+
+: application? ( obj -- obj bool)
+ pair-type istype? ;
+
+: operator ( obj -- operator )
+ car ;
+
+: operands ( obj -- operands )
+ cdr ;
+
+: nooperands? ( operands -- bool )
+ nil objeq? ;
+
+: first-operand ( operands -- operand )
+ car ;
+
+: rest-operands ( operands -- other-operands )
+ cdr ;
+
+: list-of-vals ( args env -- vals )
+ 2swap
+
+ 2dup nooperands? if
+ 2swap 2drop
+ else
+ 2over 2over first-operand 2swap eval
+ -2rot rest-operands 2swap recurse
+ cons
+ then
+;
+
+: procedure-params ( proc -- params )
+ drop pair-type car ;
+
+: procedure-body ( proc -- body )
+ drop pair-type cdr car ;
+
+: procedure-env ( proc -- body )
+ drop pair-type cdr cdr car ;
+
+: apply ( proc args )
+ 2swap dup case
+ primitive-proc-type of
+ drop execute
+ endof
+
+ compound-proc-type of
+ 2dup procedure-body ( args proc body )
+ -2rot 2dup procedure-params ( body args proc params )
+ -2rot procedure-env ( body params args procenv )
+
+ extend-env ( body env )
+
+ eval-sequence
+
+ R> drop ['] eval goto-deferred \ Tail call optimization
+ endof
+
+ bold fg red ." Object not applicable. Aboring." reset-term cr
+ abort
+ endcase
+;
+
+:noname ( obj env -- result )
+ 2swap
+
+ self-evaluating? if
+ 2swap 2drop
+ exit
+ then
+
+ quote? if
+ quote-body
+ 2swap 2drop
+ exit
+ then
+
+ variable? if
+ 2swap lookup-var
+ exit
+ then
+
+ definition? if
+ 2swap eval-definition
+ exit
+ then
+
+ assignment? if
+ 2swap eval-assignment
+ exit
+ then
+
+ if? if
+ 2over 2over
+ if-predicate
+ 2swap eval
+
+ true? if
+ if-consequent
+ else
+ if-alternative
+ then
+
+ 2swap
+ ['] eval goto-deferred
+ then
+
+ lambda? if
+ 2dup lambda-parameters
+ 2swap lambda-body
+ 2rot make-procedure
+ exit
+ then
+
+ begin? if
+ begin-actions 2swap
+ eval-sequence
+ ['] eval goto-deferred
+ then
+
+ application? if
+ 2over 2over
+ operator 2swap eval
+ -2rot
+ operands 2swap list-of-vals
+
+ apply
+ exit
+ then
+
+ bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
+ abort
+; is eval
+
+\ }}}
+
+\ ---- Print ---- {{{
+
+: printfixnum ( fixnumobj -- ) drop 0 .R ;
+
+: printrealnum ( realnumobj -- ) drop float-print ;
+
+: printbool ( numobj -- )
+ drop if
+ ." #t"
+ else
+ ." #f"
+ then
+;
+
+: printchar ( charobj -- )
+ drop
+ case
+ 9 of ." #\tab" endof
+ bl of ." #\space" endof
+ '\n' of ." #\newline" endof
+
+ dup ." #\" emit
+ endcase
+;
+
+: (printstring) ( stringobj -- )
+ nil-type istype? if 2drop exit then
+
+ 2dup car drop dup
+ case
+ '\n' of ." \n" drop endof
+ [char] \ of ." \\" drop endof
+ [char] " of [char] \ emit [char] " emit drop endof
+ emit
+ endcase
+
+ cdr recurse
+;
+: printstring ( stringobj -- )
+ [char] " emit
+ (printstring)
+ [char] " emit ;
+
+: printsymbol ( symbolobj -- )
+ nil-type istype? if 2drop exit then
+
+ 2dup car drop emit
+ cdr recurse
+;
+
+: printnil ( nilobj -- )
+ 2drop ." ()" ;
+
+: printpair ( pairobj -- )
+ 2dup
+ car print
+ cdr
+ nil-type istype? if 2drop exit then
+ pair-type istype? if space recurse exit then
+ ." . " print
+;
+
+: printprim ( primobj -- )
+ 2drop ." <primitive procedure>" ;
+
+: printcomp ( primobj -- )
+ 2drop ." <compound procedure>" ;
+
+: printnone ( noneobj -- )
+ 2drop ." Unspecified return value" ;
+
+:noname ( obj -- )
+ fixnum-type istype? if printfixnum exit then
+ realnum-type istype? if printrealnum exit then
+ boolean-type istype? if printbool exit then
+ character-type istype? if printchar exit then
+ string-type istype? if printstring exit then
+ symbol-type istype? if printsymbol exit then
+ nil-type istype? if printnil exit then
+ pair-type istype? if ." (" printpair ." )" exit then
+ primitive-proc-type istype? if printprim exit then
+ compound-proc-type istype? if printcomp exit then
+ none-type istype? if printnone exit then
+
+ bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
+ abort
+; is print
+
+\ }}}
+
+\ ---- Garbage Collection ---- {{{
+
+variable gc-enabled
+false gc-enabled !
+
+variable gc-stack-depth
+
+: enable-gc
+ depth gc-stack-depth !
+ true gc-enabled ! ;
+
+: disable-gc
+ false gc-enabled ! ;
+
+: gc-enabled?
+ gc-enabled @ ;
+
+: pairlike? ( obj -- obj bool )
+ pair-type istype? if true exit then
+ string-type istype? if true exit then
+ symbol-type istype? if true exit then
+ compound-proc-type istype? if true exit then
+
+ false
+;
+
+: pairlike-marked? ( obj -- obj bool )
+ over nextfrees + @ 0=
+;
+
+: mark-pairlike ( obj -- obj )
+ over nextfrees + 0 swap !
+;
+
+: gc-unmark ( -- )
+ scheme-memsize 0 do
+ 1 nextfrees i + !
+ loop
+;
+
+: gc-mark-obj ( obj -- )
+
+ pairlike? invert if 2drop exit then
+ pairlike-marked? if 2drop exit then
+
+ mark-pairlike
+
+ drop pair-type 2dup
+
+ car recurse
+ cdr recurse
+;
+
+: gc-sweep
+ scheme-memsize nextfree !
+ 0 scheme-memsize 1- do
+ nextfrees i + @ 0<> if
+ nextfree @ nextfrees i + !
+ i nextfree !
+ then
+ -1 +loop
+;
+
+\ Following a GC, this gives the amount of free memory
+: gc-count-marked
+ 0
+ scheme-memsize 0 do
+ nextfrees i + @ 0= if 1+ then
+ loop
+;
+
+\ Debugging word - helps spot memory that is retained
+: gc-zero-unmarked
+ scheme-memsize 0 do
+ nextfrees i + @ 0<> if
+ 0 car-cells i + !
+ 0 cdr-cells i + !
+ then
+ loop
+;
+
+:noname
+ \ ." GC! "
+
+ gc-unmark
+
+ symbol-table obj@ gc-mark-obj
+ global-env obj@ gc-mark-obj
+
+ depth gc-stack-depth @ do
+ PSP0 i + 1 + @
+ PSP0 i + 2 + @
+
+ gc-mark-obj
+ 2 +loop
+
+ gc-sweep
+
+ \ ." (" gc-count-marked . ." pairs marked as used.)" cr
+; is collect-garbage
+
+\ }}}
+
+\ ---- Primitives ---- {{{
+
+: make-primitive ( cfa -- )
+ bl word
+ count
+
+ \ 2dup ." Defining primitive " type ." ..." cr
+
+ cstr>charlist
+ drop symbol-type
+
+ 2dup
+
+ symbol-table obj@
+ cons
+ symbol-table obj!
+
+ 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 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
+
+\ }}}
+
+\ ---- Loading files ---- {{{
+
+: charlist>cstr ( charlist addr -- n )
+
+ dup 2swap ( origaddr addr charlist )
+
+ begin
+ 2dup nil objeq? false =
+ while
+ 2dup cdr 2swap car
+ drop ( origaddr addr charlist char )
+ -rot 2swap ( origaddr charlist addr char )
+ over !
+ 1+ -rot ( origaddr nextaddr charlist )
+ repeat
+
+ 2drop ( origaddr finaladdr )
+ swap -
+;
+
+: load ( addr n -- finalResult )
+ open-input-file
+
+ empty-parse-str
+
+ ok-symbol ( port res )
+
+ begin
+ 2over read-port ( port res obj )
+
+ 2dup EOF character-type objeq? if
+ 2drop 2swap close-port
+ exit
+ then
+
+ 2swap 2drop ( port obj )
+
+ global-env obj@ eval ( port res )
+ again
+;
+
+:noname ( args -- finalResult )
+ 2dup 1 ensure-arg-count
+ car string-type ensure-arg-type
+
+ drop pair-type
+ pad charlist>cstr
+ pad swap load
+; make-primitive load
+
+\ }}}
+
+\ ---- REPL ----
+
+: repl