1;;; interpret.ss 2;;; Copyright (C) 1996 R. Kent Dybvig 3;;; from "The Scheme Programming Language, 2ed" by R. Kent Dybvig 4 5;;; Permission is hereby granted, free of charge, to any person obtaining a 6;;; copy of this software and associated documentation files (the "Software"), 7;;; to deal in the Software without restriction, including without limitation 8;;; the rights to use, copy, modify, merge, publish, distribute, sublicense, 9;;; and/or sell copies of the Software, and to permit persons to whom the 10;;; Software is furnished to do so, subject to the following conditions: 11;;; 12;;; The above copyright notice and this permission notice shall be included in 13;;; all copies or substantial portions of the Software. 14;;; 15;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18;;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21;;; DEALINGS IN THE SOFTWARE. 22 23(define interpret #f) 24(let () 25 ;; primitive-environment is an environment containing a small 26 ;; number of primitive procedures; it can be extended easily 27 ;; to include additional primitives. 28 (define primitive-environment 29 (list (cons 'apply apply) 30 (cons 'assq assq) 31 (cons 'call/cc call/cc) 32 (cons 'car car) 33 (cons 'cadr cadr) 34 (cons 'caddr caddr) 35 (cons 'cadddr cadddr) 36 (cons 'cddr cddr) 37 (cons 'cdr cdr) 38 (cons 'cons cons) 39 (cons 'eq? eq?) 40 (cons 'list list) 41 (cons 'map map) 42 (cons 'memv memv) 43 (cons 'null? null?) 44 (cons 'pair? pair?) 45 (cons 'read read) 46 (cons 'set-car! set-car!) 47 (cons 'set-cdr! set-cdr!) 48 (cons 'symbol? symbol?))) 49 50 ;; new-env returns a new environment from a formal parameter 51 ;; specification, a list of actual parameters, and an outer 52 ;; environment. The symbol? test identifies "improper" 53 ;; argument lists. Environments are association lists, 54 ;; associating variables with values. 55 (define new-env 56 (lambda (formals actuals env) 57 (cond 58 ((null? formals) env) 59 ((symbol? formals) (cons (cons formals actuals) env)) 60 (else 61 (cons (cons (car formals) (car actuals)) 62 (new-env (cdr formals) (cdr actuals) env)))))) 63 64 ;; lookup finds the value of the variable var in the environment 65 ;; env, using assq. Assumes var is bound in env. 66 (define lookup 67 (lambda (var env) 68 (cdr (assq var env)))) 69 70 ;; assign is similar to lookup but alters the binding of the 71 ;; variable var in the environment env by changing the cdr of 72 ;; association pair 73 (define assign 74 (lambda (var val env) 75 (set-cdr! (assq var env) val))) 76 77 ;; exec evaluates the expression, recognizing all core forms. 78 (define exec 79 (lambda (exp env) 80 (cond 81 ((symbol? exp) (lookup exp env)) 82 ((pair? exp) 83 (case (car exp) 84 ((quote) (cadr exp)) 85 ((lambda) 86 (lambda vals 87 (let ((env (new-env (cadr exp) vals env))) 88 (let loop ((exps (cddr exp))) 89 (if (null? (cdr exps)) 90 (exec (car exps) env) 91 (begin 92 (exec (car exps) env) 93 (loop (cdr exps)))))))) 94 ((if) 95 (if (exec (cadr exp) env) 96 (exec (caddr exp) env) 97 (exec (cadddr exp) env))) 98 ((set!) 99 (assign (cadr exp) 100 (exec (caddr exp) env) 101 env)) 102 (else 103 (apply (exec (car exp) env) 104 (map (lambda (x) (exec x env)) 105 (cdr exp)))))) 106 (else exp)))) 107 108 ;; interpret starts execution with the primitive environment. 109 (set! interpret 110 (lambda (exp) 111 (exec exp primitive-environment)))) 112