SICP/misc/sicp-eceval-support.scm

317 lines
9.8 KiB
Scheme
Raw Normal View History

2021-04-03 16:27:19 +02:00
;;;;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
2021-04-03 16:27:19 +02:00
;;;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
2021-04-03 16:27:19 +02:00
(let ((frame (first-frame env)))
(scan (frame-variables frame)
(frame-values frame)))))
(env-loop env))
(define (unbound-variable? var)
(eq? var 'unbound-variable-error))
2021-04-03 16:27:19 +02:00
(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))))
2021-04-04 17:46:15 +02:00
;;; cond-support
(define (cond? exp) (tagged-list? exp 'cond))
(define (cond-clauses exp) (cdr exp))
2021-04-05 17:09:03 +02:00
(define (cond-first-clause exp) (car exp))
2021-04-04 17:46:15 +02:00
(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)))
2021-04-03 16:27:19 +02:00
;;;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 (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)
'<procedure-env>))
(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?)
))