Com S 342 meeting -*- Outline -*- * Local Binding (5.3) ** syntax ------------------------------------------ NAMING AND LET (5.3) Concrete syntax: ::= let in ::= {;}* ::= = Examples: let x = 5; y = 6 in +(x, y) let x = 3 in let x = *(x, x) in +(x, x) Abstract syntax: ------------------------------------------ Q: What should the values of the examples be? ... (define-record let (decls body)) (define-record decl (var exp)) ** scoping Q: What should the scope rules be? Q: Where are we going to record the values of these variables? Q: Will that work with the scoping? Q: If the interpreter took both an expression and an environment, what would have to change? ------------------------------------------ INTERPRETER WITH ENVIRONMENTS ;;; Figure 5.3.1 : page 149 (define eval-exp ;; TYPE: (-> (parsed-exp Environment) ;; Expressed-Value) (lambda (exp env) (variant-case exp (lit (datum) (number->expressed datum)) (varref (var) (denoted->expressed (apply-env var))) (app (rator rands) (let ((proc (expressed->procedure (eval-exp rator ))) (args (eval-rands rands ))) (apply-proc proc args))) (if (test-exp then-exp else-exp) (if (true-value? (expressed->number (eval-exp test-exp ))) (eval-exp then-exp ) (eval-exp else-exp ))) (else (error "Invalid abstract syntax: " exp))))) ------------------------------------------ ... env fill that in everywhere ------------------------------------------ EVAL-RANDS (define eval-rands ;; TYPE: (-> ((list parsed-exp) ;; Environment) ;; (list Expressed-Value)) (lambda (rands ) (map (lambda (rand) (eval-exp rand )) rands))) ------------------------------------------ ------------------------------------------ FOR YOU TO DO Add to this interpreter the clause for let ------------------------------------------ ... (let (decls body) (let ((vars (map decl->var decls)) (args (map decl->exp decls))) (let ((new-env (extend-env vars (map expressed->denoted (eval-rands args env))))) (eval-exp body new-env)))) Q: How does your clause make the scope rules for let work out? Q: Does this result in any redundant code? yes, with procedure calls...