+: 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
+;
+
+: 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
+;
+
+: apply ( proc args )
+ 2swap dup case
+ primitive-proc-type of
+ drop execute
+ endof
+
+ compound-proc-type of
+ 2drop 2drop
+ ." Compound procedures not yet implemented." cr
+ ok-symbol
+ 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
+ then
+
+ lambda? if
+ 2dup lambda-parameters
+ 2swap lambda-body
+ 2rot make-procedure
+ exit
+ 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 ---- {{{
+
+defer print
+
+: printnum ( numobj -- ) drop 0 .R ;
+
+: 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>" ;
+
+:noname ( obj -- )
+ fixnum-type istype? if printnum 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
+
+ bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
+ abort
+; is print
+
+\ }}}
+
+\ ---- REPL ----
+
+: repl
+ cr ." Welcome to scheme.forth.jl!" cr
+ ." Use Ctrl-D to exit." cr
+
+ empty-parse-str
+
+ begin
+ cr bold fg green ." > " reset-term
+ read
+ global-env obj@ eval
+ fg cyan ." ; " print reset-term