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
|
|
|
|
|
|
|
|
|
|
;;;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)
|
|
|
|
|
(error "Unbound variable" var)
|
|
|
|
|
(let ((frame (first-frame env)))
|
|
|
|
|
(scan (frame-variables frame)
|
|
|
|
|
(frame-values frame)))))
|
|
|
|
|
(env-loop env))
|
|
|
|
|
|
|
|
|
|
(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))
|
|
|
|
|
|