+ evaluate-eproc
+;
+
+: analyze-if ( exp -- eproc )
+ 2dup if-predicate analyze
+ 2swap 2dup if-consequent analyze
+ 2swap if-alternative analyze
+
+ ['] if-executor primitive-proc-type
+ nil cons cons cons cons
+;
+
+: sequence-executor ( eproc-list env -- res )
+ 2swap
+
+ begin
+ 2dup cdr ( env elist elist-rest)
+ nil? invert
+ while
+
+ -2rot car 2over ( elist-rest env elist-head env )
+ evaluate-eproc ( elist-rest env head-res )
+ 2drop 2swap ( env elist-rest )
+ repeat
+
+ 2drop car 2swap
+ ['] evaluate-eproc goto
+;
+
+
+: (analyze-sequence) ( explist -- eproc-list )
+ nil? if exit then
+
+ 2dup car analyze
+ 2swap cdr recurse
+
+ cons
+;
+
+: analyze-sequence ( explist -- eproc )
+ (analyze-sequence)
+ ['] sequence-executor primitive-proc-type
+ nil cons cons
+;
+
+: lambda-executor ( params bproc env -- res )
+ make-procedure
+ ( Although this is packaged up as a regular compound procedure,
+ the "body" element contains an _eproc_ to be evaluated in an
+ environment resulting from extending env with the parameter
+ bindings. )
+;
+
+: analyze-lambda ( exp -- eproc )
+ 2dup lambda-parameters
+ 2swap lambda-body
+
+ nil? if
+ except-message: ." encountered lambda with an empty body." recoverable-exception throw
+ then
+
+ analyze-sequence
+
+ ['] lambda-executor primitive-proc-type
+ nil cons cons cons
+;
+
+: operand-eproc-list ( operands -- eprocs )
+ nil? invert if
+ 2dup car analyze
+ 2swap cdr recurse
+ cons
+ then
+;
+
+: evaluate-operand-eprocs ( env aprocs -- vals )
+ nil? if
+ 2swap 2drop
+ else
+ 2over 2over car 2swap evaluate-eproc ( env aprocs thisval )
+ -2rot cdr recurse ( thisval restvals )
+ cons
+ then
+;
+
+: application-executor ( operator-proc arg-procs env -- res )
+ 2rot 2over ( aprocs env fproc env )
+ evaluate-eproc ( aprocs env proc )
+
+ -2rot 2swap ( proc env aprocs )
+ evaluate-operand-eprocs ( proc vals )
+
+ 2swap ( vals proc )
+
+ dup case
+ primitive-proc-type of
+ drop execute
+ endof
+
+ compound-proc-type of
+ 2dup procedure-body ( argvals proc body )
+ -2rot 2dup procedure-params ( bproc argvals proc argnames )
+ -2rot procedure-env ( bproc argnames argvals procenv )
+
+ -2rot 2swap
+ flatten-proc-args
+ 2swap 2rot
+
+ extend-env ( bproc env )
+
+ ['] evaluate-eproc goto
+ endof
+
+ except-message: ." object '" drop print ." ' not applicable." recoverable-exception throw
+ endcase
+;
+
+: analyze-application ( exp -- eproc )
+ 2dup operator analyze
+ 2swap operands operand-eproc-list
+
+ ['] application-executor primitive-proc-type
+ nil cons cons cons
+;
+
+:noname ( exp --- eproc )
+
+ self-evaluating? if
+ analyze-self-evaluating
+ exit
+ then
+
+ quote? if
+ analyze-quoted
+ exit
+ then
+
+ variable? if
+ analyze-variable
+ exit
+ then
+
+ definition? if
+ analyze-definition
+ exit
+ then
+
+ assignment? if
+ analyze-assignment
+ exit
+ then
+
+ if? if
+ analyze-if
+ exit
+ then
+
+ lambda? if
+ analyze-lambda
+ exit
+ then
+
+ application? if
+ analyze-application
+ exit
+ then
+
+
+ except-message: ." tried to analyze unknown expression type." recoverable-exception throw
+
+; is analyze
+
+
+\ ---- Macro Expansion ---- {{{
+
+( Simply evaluates the given procedure with expbody as its argument. )
+: macro-eval ( 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
+;
+
+: expand-macro ( exp -- result )
+ pair-type istype? invert if exit then
+ 2dup car symbol-type istype? invert if 2drop exit then
+
+ lookup-macro nil? if
+ 2drop exit then
+
+ 2over cdr macro-eval
+
+ 2dup no-match-symbol objeq? if
+ 2drop exit
+ else
+ 2swap 2drop
+ then
+
+ R> drop ['] expand goto-deferred
+;
+
+: expand-definition ( exp -- result )
+ define-symbol 2swap
+
+ 2dup definition-var
+ 2swap definition-val expand
+ nil ( define var val' nil )
+
+ cons cons cons ;
+
+: expand-assignment ( exp -- result )
+ set!-symbol 2swap
+
+ 2dup assignment-var
+ 2swap assignment-val expand
+ nil ( define var val' nil )
+
+ cons cons cons ;
+
+: expand-list ( exp -- res )
+ nil? if exit then
+
+ 2dup car expand
+ 2swap cdr recurse
+
+ cons ;
+
+: macro-definition-nameparams
+ cdr car ;
+
+: expand-define-macro ( exp -- res )
+ define-macro-symbol 2swap
+ 2dup macro-definition-nameparams
+ 2swap macro-definition-body expand-list
+
+ cons cons ;
+
+: expand-lambda ( exp -- res )
+ lambda-symbol 2swap
+ 2dup lambda-parameters
+ 2swap lambda-body expand-list
+
+ cons cons ;
+
+: expand-if ( exp -- res )
+ if-symbol 2swap
+
+ 2dup if-predicate expand
+ 2swap 2dup if-consequent expand
+ 2swap if-alternative none? if
+ 2drop nil
+ else
+ expand nil cons
+ then
+
+ cons cons cons ;
+
+: expand-application ( exp -- res )
+ 2dup operator expand
+ 2swap operands expand-list
+
+ cons ;
+
+:noname ( exp -- result )
+ expand-macro
+
+ self-evaluating? if exit then
+
+ quote? if exit then
+
+ definition? if expand-definition exit then
+
+ assignment? if expand-assignment exit then
+
+ macro-definition? if expand-define-macro exit then
+
+ lambda? if expand-lambda exit then
+
+ if? if expand-if exit then
+
+ application? if expand-application exit then
+
+; is expand