proj4b - CS 61A project 4 part II solutions Problem A5...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 (define (handle-infix value line-obj env) (if (ask line-obj 'empty?) value (let ((token (ask line-obj 'next))) (if (memq token '(+ - * / = < >)) (handle-infix ((text (lookup-procedure (de-infix token))) value (eval-prefix line-obj env) ) line-obj env) (begin (ask line-obj 'put-back token) value))))) (define (eval-definition line-obj) (define (parse-formal token) (if (eq? (first token) ':) (bf token) (error "Bad input name format in TO" token))) (define (get-formals) (if (ask line-obj 'empty?) '() (let ((token (ask line-obj 'next))) (cons (parse-formal token) (get-formals))))) (define (get-body) (prompt "-> ") (let ((line (logo-read))) (if (equal? line '(end)) '() (cons line (get-body))))) (let ((name (ask line-obj 'next))) (let ((formals (get-formals))) (set! the-procedures (cons (list name 'compound (length formals) (cons formals (get-body))) the-procedures)))) '=no-value=)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Sheet1 Page 2 (define (eval-sequence exps env) (if (null? exps) '=no-value= (let ((value (eval-line (make-line-obj (car exps)) env))) (cond ((eq? value '=stop=) '=no-value=) ((and (pair? value) (eq? (car value) '=output=)) (cdr value)) ((not (eq? value '=no-value=)) (error "You don't say what to do with" value)) (else (eval-sequence (cdr exps) env)))))) The difference between lexical scope (Scheme) and dynamic scope (Logo) is that in lexical scope, calling a procedure creates a new environment by extending *the environment in which the procedure was created* scope, calling a procedure creates a new environment by extending *the current environment*. Where is this extending of environments done? In APPLY: (define (mc-apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (EXTEND-ENVIRONMENT (PROCEDURE-PARAMETERS PROCEDURE) ARGUMENTS (PROCEDURE-ENVIRONMENT PROCEDURE)))) (else (error "Unknown procedure type -- APPLY" procedure)))) We are going to do the same thing, except that the third argument to EXTEND-ENVIRONMENT will be the current environment. Where do we find it? In Scheme, APPLY doesn't know anything about the current environment! So in the Logo interpreter, we're going to have to pass it as an additional argument: (define (logo-apply procedure arguments ENV) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (parameters procedure) arguments ENV)))
Background image of page 2
Sheet1 Page 3 (else (error "Unknown procedure type -- LOGO-APPLY" procedure)))) We also have to modify the places where LOGO-APPLY is called, to provide the extra argument: (define (eval-prefix line-obj env) (define (eval-helper paren-flag) (let ((token (ask line-obj 'next))) (cond ((self-evaluating? token) token) ... (else
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 28

proj4b - CS 61A project 4 part II solutions Problem A5...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online