; Recitation 26

; Start off by asking for "grandma" explanation of lecture.

; Highlight the progress we've made so far in the course and how
; we got to this point.

; Look at m-eval code (write a fragment of m-eval on the board).  
; Have the students think about what the assembly code would be like.  
; Review the contract for eval-dispatch.  Write beginning of 
; eval-dispatch together.

; Hand out the ec-eval.scm handout (with ev-if removed).

; Walk through ev-application together.  Talk through the code and 
; relate to m-eval.  

; Eventually, get down to ev-sequence.  Highlight how this contract is 
; a bit different from the rest of the contract (continue is already
; on the stack).  Highlight how we get tail-recursion from ev-sequence.  
; Show a different implementation of ev-sequence that does not achieve 
; tail-recursion:

ev-sequence
 (test (op no-more-exps?) (reg unev))
 (branch (label ev-sequence-end))
 (assign exp (op first-exp) (reg unev))
 (save unev)
 (save env)
 (assign continue (label ev-sequence-continue))
 (goto (label eval-dispatch))
ev-sequence-continue
 (restore env)
 (restore unev)
 (assign unev (op rest-exps) (reg unev))
 (goto (label ev-sequence))
ev-sequence-end
 (restore continue)
 (goto (reg continue))

; Now have students implement ev-if (this was purposely left off of the 
; handout).  Talk through it together.  Show the following ev-if and 
; discuss why it is not tail-recursive:

ev-if
 (save continue)
 (save exp)
 (save env)
 (assign continue (label ev-if-decide))
 (assign exp (op if-pred) (reg exp))
 (goto (label eval-dispatch))

ev-if-decide
 (restore env)
 (restore exp)
 (assign continue (label if-done))
 (test (op true?) (reg val))
 (branch (label ev-if-conseq))
 
ev-if-alt
 (assign exp (op if-alt) (reg exp))
 (goto (label eval-dispatch))

ev-if-conseq
 (assign exp (op if-conseq) (reg exp))
 (goto (label eval-dispatch))

if-done
 (restore continue)
 (goto (reg continue))

; Now walk the students through the handout exercises.

; Misc:
self-evaluating
 (test (op number?) (reg exp))
 (branch (label ev-true))
 (test (op string?) (reg exp))
 (branch (label ev-true))
 (test (op boolean?) (reg exp))
 (branch (label ev-true))

ev-false
 (assign val (const #f))
 (goto (reg continue))

ev-true
 (assign val (const #t))
 (goto (reg continue))
