;;;;SIMULATION OF ECEVAL MACHINE OPERATIONS -- ;;;;loaded by load-eceval.scm and by load-eceval-compiler.scm ;;;;FIRST A LOT FROM 4.1.2-4.1.4 ;(load "ch5-syntax.scm"); ;section 4.1.2 syntax procedures (load "misc/sicp-eceval-syntax.scm") ;section 4.1.2 syntax procedures (load "misc/sicp-leval.scm") ; for lazy evaluation in eceval ;;;SECTION 4.1.3 ;;; operations used by compiled code and eceval except as noted (define (true? x) (not (eq? x false))) ;;* not used by eceval itself -- used by compiled code when that ;; is run in the eceval machine (define (false? x) (eq? x false)) ;;following compound-procedure operations not used by compiled code (define (make-procedure parameters body env) (list 'procedure parameters body env)) (define (compound-procedure? p) (tagged-list? p 'procedure)) (define (procedure-parameters p) (cadr p)) (define (procedure-body p) (caddr p)) (define (procedure-environment p) (cadddr p)) ;;(end of compound procedures) (define (enclosing-environment env) (cdr env)) (define (first-frame env) (car env)) (define the-empty-environment '()) (define (make-frame variables values) (cons variables values)) (define (frame-variables frame) (car frame)) (define (frame-values frame) (cdr frame)) (define (add-binding-to-frame! var val frame) (set-car! frame (cons var (car frame))) (set-cdr! frame (cons val (cdr frame)))) (define (extend-environment vars vals base-env) (if (= (length vars) (length vals)) (cons (make-frame vars vals) base-env) (if (< (length vars) (length vals)) (error "Too many arguments supplied" vars vals) (error "Too few arguments supplied" vars vals)))) (define (lookup-variable-value var env) (define (env-loop env) (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (car vals)) (else (scan (cdr vars) (cdr vals))))) (if (eq? env the-empty-environment) 'unbound-variable-error (let ((frame (first-frame env))) (scan (frame-variables frame) (frame-values frame))))) (env-loop env)) (define (unbound-variable? var) (eq? var 'unbound-variable-error)) (define (set-variable-value! var val env) (define (env-loop env) (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (set-car! vals val)) (else (scan (cdr vars) (cdr vals))))) (if (eq? env the-empty-environment) (error "Unbound variable -- SET!" var) (let ((frame (first-frame env))) (scan (frame-variables frame) (frame-values frame))))) (env-loop env)) (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)))) ;;; cond-support (define (cond? exp) (tagged-list? exp 'cond)) (define (cond-clauses exp) (cdr exp)) (define (cond-first-clause exp) (car exp)) (define (cond-else-clause? clause) (eq? (cond-predicate clause) 'else)) (define (cond-predicate clause) (car clause)) (define (cond-actions clause) (cdr clause)) (define (cond->if exp) (expand-clauses (cond-clauses exp))) (define (expand-clauses clauses) (if (null? clauses) 'false ; no else clause (let ((first (car clauses)) (rest (cdr clauses))) (if (cond-else-clause? first) (if (null? rest) (sequence->exp (cond-actions first)) (error "ELSE clause isn't last -- COND->IF" clauses)) (make-if (cond-predicate first) (sequence->exp (cond-actions first)) (expand-clauses rest)))))) ;;; lambda-support ;;; let-support (define (let? exp) (tagged-list? exp 'let)) (define (let-bindings exp) (cadr exp)) (define (let-body exp) (cddr exp)) (define (let-binding-var binding) (car binding)) (define (let-binding-exp binding) (cadr binding)) (define (let-vars exp) (map let-binding-var (let-bindings exp))) (define (let-exps exp) (map let-binding-exp (let-bindings exp))) (define (make-lambda parameters body) (cons 'lambda (cons parameters body))) (define (let->combination exp) (let ((let-variables (let-vars exp)) (let-expressions (let-exps exp))) (cons (make-lambda let-variables (let-body exp)) let-expressions))) ;;;SECTION 4.1.4 (define (setup-environment) (let ((initial-env (extend-environment (primitive-procedure-names) (primitive-procedure-objects) the-empty-environment))) (define-variable! 'true true initial-env) (define-variable! 'false false initial-env) initial-env)) (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?) ;;above from book -- here are some more (list '+ +) (list '- -) (list '* *) (list '= =) (list '/ /) (list '> >) (list '< <) )) (define (primitive-procedure-names) (map car primitive-procedures)) (define (primitive-procedure-objects) (map (lambda (proc) (list 'primitive (cadr proc))) primitive-procedures)) (define apply-in-underlying-scheme apply) (define (check-apply-primitive proc args) (cond ((eq? proc /) (check-apply-division args)) ((eq? proc car) (check-apply-car args)) (else 'ok))) (define (apply-primitive-error? val) (eq? (car val) 'error)) (define (apply-primitive-result val) (cadr val)) (define (apply-primitive-procedure-safe proc args) (let ((code (check-apply-primitive (primitive-implementation proc) args))) (if (eq? code 'ok) (list 'ok (apply-in-underlying-scheme (primitive-implementation proc) args)) (list 'error code)))) (define (apply-primitive-procedure proc args) (apply-in-underlying-scheme (primitive-implementation proc) args)) (define (prompt-for-input string) (newline) (newline) (display string) (newline)) (define (announce-output string) (newline) (display string) (newline)) (define (user-print object) (if (compound-procedure? object) (display (list 'compound-procedure (procedure-parameters object) (procedure-body object) ')) (display object))) ;;; Simulation of new machine operations needed by ;;; eceval machine (not used by compiled code) ;;; From section 5.4.1 footnote (define (empty-arglist) '()) (define (adjoin-arg arg arglist) (append arglist (list arg))) (define (last-operand? ops) (null? (cdr ops))) ;;; From section 5.4.2 footnote, for non-tail-recursive sequences (define (no-more-exps? seq) (null? seq)) ;;; From section 5.4.4 footnote (define (get-global-environment) the-global-environment) ;; will do following when ready to run, not when load this file ;;(define the-global-environment (setup-environment)) ;;; Simulation of new machine operations needed for compiled code ;;; and eceval/compiler interface (not used by plain eceval machine) ;;; From section 5.5.2 footnote (define (make-compiled-procedure entry env) (list 'compiled-procedure entry env)) (define (compiled-procedure? proc) (tagged-list? proc 'compiled-procedure)) (define (compiled-procedure-entry c-proc) (cadr c-proc)) (define (compiled-procedure-env c-proc) (caddr c-proc)) (define eceval-operations (list ;;primitive Scheme operations (list 'display display) (list 'newline newline) (list 'read read) ;;operations in syntax.scm (list 'application? application?) (list 'assignment-value assignment-value) (list 'assignment-variable assignment-variable) (list 'assignment? assignment?) (list 'begin-actions begin-actions) (list 'begin? begin?) (list 'definition-value definition-value) (list 'definition-variable definition-variable) (list 'definition? definition?) (list 'first-exp first-exp) (list 'first-operand first-operand) (list 'if-alternative if-alternative) (list 'if-consequent if-consequent) (list 'if-predicate if-predicate) (list 'if? if?) (list 'lambda-body lambda-body) (list 'lambda-parameters lambda-parameters) (list 'lambda? lambda?) (list 'last-exp? last-exp?) (list 'no-operands? no-operands?) (list 'operands operands) (list 'operator operator) (list 'quoted? quoted?) (list 'rest-exps rest-exps) (list 'rest-operands rest-operands) (list 'self-evaluating? self-evaluating?) (list 'text-of-quotation text-of-quotation) (list 'variable? variable?) ;;operations in eceval-support.scm (list 'adjoin-arg adjoin-arg) (list 'announce-output announce-output) (list 'apply-primitive-procedure apply-primitive-procedure) (list 'compound-procedure? compound-procedure?) (list 'define-variable! define-variable!) (list 'empty-arglist empty-arglist) (list 'extend-environment extend-environment) (list 'get-global-environment get-global-environment) (list 'last-operand? last-operand?) (list 'lookup-variable-value lookup-variable-value) (list 'make-procedure make-procedure) (list 'no-more-exps? no-more-exps?) ;for non-tail-recursive machine (list 'primitive-procedure? primitive-procedure?) (list 'procedure-body procedure-body) (list 'procedure-environment procedure-environment) (list 'procedure-parameters procedure-parameters) (list 'prompt-for-input prompt-for-input) (list 'set-variable-value! set-variable-value!) (list 'true? true?) (list 'user-print user-print) ;;5.23 (list 'cond->if cond->if) (list 'cond-actions cond-actions) (list 'cond-clauses cond-clauses) (list 'cond-else-clause? cond-else-clause?) (list 'cond-first-clause cond-first-clause) (list 'cond-predicate cond-predicate) (list 'cond? cond?) (list 'let->combination let->combination) (list 'let? let?) ;;5.25 (list 'delay-it delay-it) (list 'thunk? thunk?) (list 'thunk-exp thunk-exp) (list 'thunk-env thunk-env) ;;5.30 (list 'unbound-variable? unbound-variable?) (list 'apply-primitive-procedure-safe apply-primitive-procedure-safe) (list 'apply-primitive-result apply-primitive-result) (list 'apply-primitive-error? apply-primitive-error?) ))