正在加载图片...
6.001 Metacircular Evaluator (define (define-variable! var val env) let ((frame (first-frame env))) (define (scan vars vals) (cond ((null? vars)(add-binding-to-frame! var val frame)) ((eq? var (car vars))(set-car! vals val)) Else (scan (cdr vars)(cdr vals))))) (scan (frame-variables frame) (frame-values frame)))) Primitive procedures and the initial environment (define (primitive-procedure? proc)(tagged-list? proc 'primitive)) (define (primitive-implementation proc)(cadr proc)) (define primitive-procedures (1ist(1ist’ car car) (list ' cdr cdr) (list 'cons cons) (1ist’nul1?nu11?) (ist (list more primitives (define (primitive-procedure-names)(map car primitive-procedures)) (define (primitive-procedure-objects) (defi l (lambda (proc)(list'primitive (cadr proc)))primitive ine (apply-primitive-procedure proc args) apply (primitive-implementation proc) args)) (define (setup-environment) (let ((initial-env (extend-environment (primitive-procedure-names) (primitive-procedure-objects) the-empty-environment))) (define-variable! ,true #t initial-env) (define-variable! 'false #f initial-env) initial-env)) (define the-global-environment (setup-environment)) The Read-Eval-Print Loop (define input-prompt ";; M-Eval input: " (define output-prompt ";; M-Eval value: " (define(driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (m-eval input the-global-environment))) (announce-output output-prompt) (display output))) (driver-loop)) (define (prompt-for-input string) newline)(newline)(display string) (newline)) (newline)(display string) (newline))6.001 Metacircular Evaluator 4 (define (define-variable! var val env) (let ((frame (first-frame env))) (define (scan vars vals) (cond ((null? vars) (add-binding-to-frame! var val frame)) ((eq? var (car vars)) (set-car! vals val)) (else (scan (cdr vars) (cdr vals))))) (scan (frame-variables frame) (frame-values frame)))) Primitive Procedures and the Initial Environment (define (primitive-procedure? proc) (tagged-list? proc ’primitive)) (define (primitive-implementation proc) (cadr proc)) (define primitive-procedures (list (list ’car car) (list ’cdr cdr) (list ’cons cons) (list ’null? null?) (list ’+ +) (list ’> >) (list ’= =) (list ’* *) ; ... more primitives )) (define (primitive-procedure-names) (map car primitive-procedures)) (define (primitive-procedure-objects) (map (lambda (proc) (list ’primitive (cadr proc))) primitive-procedures)) (define (apply-primitive-procedure proc args) (apply (primitive-implementation proc) args)) (define (setup-environment) (let ((initial-env (extend-environment (primitive-procedure-names) (primitive-procedure-objects) the-empty-environment))) (define-variable! ’true #t initial-env) (define-variable! ’false #f initial-env) initial-env)) (define the-global-environment (setup-environment)) The Read-Eval-Print Loop (define input-prompt ";;; M-Eval input:") (define output-prompt ";;; M-Eval value:") (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (m-eval input the-global-environment))) (announce-output output-prompt) (display output))) (driver-loop)) (define (prompt-for-input string) (newline) (newline) (display string) (newline)) (define (announce-output string) (newline) (display string) (newline))
<<向上翻页
©2008-现在 cucdc.com 高等教育资讯网 版权所有