+: lambda? ( obj -- obj bool )
+ lambda-symbol tagged-list? ;
+
+: lambda-parameters ( obj -- params )
+ cdr car ;
+
+: lambda-body ( obj -- body )
+ cdr cdr ;
+
+: 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
+ nil? if
+ 2drop none
+ 2swap exit
+ then
+
+ begin
+ 2dup cdr ( env explist nextexplist )
+ nil? 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 ;
+
+( Ensure terminating symbol arg name is handled
+ specially to allow for variadic procedures. )
+: flatten-proc-args ( argvals argnames -- argvals' argnames' )
+ nil? if
+ 2over nil? false = if
+ bold fg red ." Too many arguments supplied to compound method. Aborting." reset-term cr
+ abort
+ else
+ 2drop
+ then
+ exit
+ then
+
+ symbol-type istype? if
+ nil cons
+ 2swap
+ nil cons
+ 2swap
+ exit
+ then
+
+ 2over
+ nil? if
+ bold fg red ." Too few arguments supplied to compound method. Aborting." reset-term cr
+ abort
+ else
+ cdr
+ then
+
+ 2over cdr
+
+ recurse ( argvals argnames argvals'' argnames'' )
+ 2rot car 2swap cons ( argvals argvals'' argnames' )
+ 2rot car 2rot cons ( argnames' argvals' )
+ 2swap
+;
+
+: apply ( proc argvals -- result )
+ 2swap dup case
+ primitive-proc-type of
+ drop execute
+ endof
+
+ compound-proc-type of
+ 2dup procedure-body ( argvals proc body )
+ -2rot 2dup procedure-params ( body argvals proc argnames )
+ -2rot procedure-env ( body argnames argvals procenv )
+
+ -2rot 2swap
+ flatten-proc-args
+ 2swap 2rot
+
+ extend-env ( body env )
+
+ eval-sequence
+
+ R> drop ['] eval goto-deferred \ Tail call optimization
+ endof
+
+ bold fg red ." Object not applicable. Aborting." reset-term cr
+ abort
+ endcase
+;
+
+: macro-expand ( proc expbody -- result )
+ 2swap
+ 2dup procedure-body ( expbody proc procbody )
+ -2rot 2dup procedure-params ( procbody expbody proc argnames )
+ -2rot procedure-env ( procbody argnames expbody procenv )
+
+ -2rot 2swap
+ flatten-proc-args
+ 2swap 2rot
+
+ extend-env eval-sequence eval
+;
+