Saturday, September 17, 2011

Solution of SICP Exercise 5.24

Below is the implementation of cond as a basic form without reducing it to if.

Source code (main part only)
ev-cond
  (save continue)
  (assign argl (op cond-clauses) (reg exp))
ev-cond-loop
  (assign unev (op car) (reg argl))
  (test (op cond-else-clause?) (reg unev))
  (branch (label ev-cond-actions))
  (assign exp (op cond-predicate) (reg unev))
  (save argl)
  (save unev)
  (assign continue (label ev-cond-val))
  (goto (label eval-dispatch))
ev-cond-val
  (restore unev)
  (restore argl)
  (test (op true?) (reg val))
  (branch (label ev-cond-actions))
  (assign argl (op cdr) (reg argl))
  (goto (label ev-cond-loop))
ev-cond-actions
  (assign unev (op cond-actions) (reg unev))
  (assign continue (label ev-cond-end))
  (goto (label ev-sequence))
ev-cond-end
  (restore continue)
  (goto (reg continue))

Test result


No comments:

Post a Comment