Evaluation


(define *environment* '())
(define (m-top-eval s) (m-eval s *environment*))
(define (m-eval s env)  ; Eval expr S in ENV
  (cond ((or (eq? s #t) (eq? s #f) (null? s)
             (number? s) (string? s) (char? s))
           s)  ; constants evaluate to themselves
        ((symbol? s)           (m-value s env))
        ((eq? (car s) 'quote)  (cadr s))
        ((eq? (car s) 'if)     (m-if s env))
        ((eq? (car s) 'let)    (m-let s env))
        ((eq? (car s) 'set!)   (m-set! s env))
        ((eq? (car s) 'begin)  (m-begin (cdr s)
                                        env #f))
        ((eq? (car s) 'define) (m-define s env))
        ((m-getdef (car s) env)  ; interpreted fn
          (m-apply (m-getdef (car s) env)
                   (map (lambda (x) (m-eval x env))
                        (cdr s))
                   env))
        ((m-compiled? (car s))  ; compiled fn
          (apply (eval (car s))
                 (map (lambda (x) (m-eval x env))
                      (cdr s))))
        (t (m-error "Undefined fn " (car s)))))

Contents    Page-10    Prev    Next    Page+10    Index