Implement 5.49
This commit is contained in:
@@ -146,7 +146,7 @@
|
|||||||
; (compile-and-go expression)
|
; (compile-and-go expression)
|
||||||
;; To test this exercise uncomment the previous line, then:
|
;; To test this exercise uncomment the previous line, then:
|
||||||
;; $ mit-scheme
|
;; $ mit-scheme
|
||||||
;; 1 ]=> (load "ex-5_45-xx")
|
;; 1 ]=> (load "ex-5_45-49")
|
||||||
;; 2 ]=> (define (g n) (* n n n))
|
;; 2 ]=> (define (g n) (* n n n))
|
||||||
;; 3 ]=> (f 3) ; calls interpreted 'g'
|
;; 3 ]=> (f 3) ; calls interpreted 'g'
|
||||||
|
|
||||||
@@ -155,20 +155,24 @@
|
|||||||
(display "\nex-5.48 - compile-and-run\n")
|
(display "\nex-5.48 - compile-and-run\n")
|
||||||
|
|
||||||
;; compile-and-go is implemented in sicp-eceval-compiler.scm
|
;; compile-and-go is implemented in sicp-eceval-compiler.scm
|
||||||
(start-eceval)
|
; (start-eceval)
|
||||||
;; To test this exercise uncomment the previous line, then:
|
;; To test this exercise uncomment the previous line, then:
|
||||||
;; $ mit-scheme
|
;; $ mit-scheme
|
||||||
;; 1 ]=> (load "ex-5_45-xx")
|
;; 1 ]=> (load "ex-5_45-49")
|
||||||
;; 2 ]=> (compile-and-run (define (f n) (if (= n 1) 1 (* n (f (- n 1))))))
|
;; 2 ]=> (compile-and-run (define (f n) (if (= n 1) 1 (* n (f (- n 1))))))
|
||||||
;; 3 ]=> (f 5)
|
;; 3 ]=> (f 5)
|
||||||
|
|
||||||
(display "[done]\n")
|
(display "[done]\n")
|
||||||
|
|
||||||
(display "\nex-5.49\n")
|
(display "\nex-5.49 - read-compile-execute-print\n")
|
||||||
|
|
||||||
(display "\nex-5.50\n")
|
; Changing the loop to read-compile-execute requies a single additional line
|
||||||
|
; within the read-eval-print-loop. If we add a compile-and-run tag to the
|
||||||
|
; expression eval-dispatch automatically dispatches to the compiler.
|
||||||
|
|
||||||
|
; (assign exp (op list) (const compile-and-run) (reg exp))
|
||||||
|
;; read-compile-execute - ex-5.49
|
||||||
|
;; Implemented in sicp-eceval-compiler.scm
|
||||||
|
|
||||||
; (display "\nex-5.51\n")
|
(display "[done]\n")
|
||||||
; (display "\nex-5.52\n")
|
|
||||||
|
|
||||||
24
ex-5_50-52.scm
Normal file
24
ex-5_50-52.scm
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
(load "shared/util")
|
||||||
|
(load "shared/sicp-load-eceval-compiler")
|
||||||
|
(load "shared/sicp-compiler")
|
||||||
|
|
||||||
|
(display "\nex-5.50 - compile-metacircular-evaluator\n")
|
||||||
|
|
||||||
|
; Exercise 5.50. Use the compiler to compile the metacircular evaluator of
|
||||||
|
; section 4.1 and run this program using the register-machine simulator. (To
|
||||||
|
; compile more than one definition at a time, you can package the definitions
|
||||||
|
; in a begin.) The resulting interpreter will run very slowly because of the
|
||||||
|
; multiple levels of interpretation, but getting all the details to work is an
|
||||||
|
; instructive exercise.
|
||||||
|
|
||||||
|
(compile-and-go
|
||||||
|
'(begin
|
||||||
|
(define (factorial n)
|
||||||
|
(if (= n 1)
|
||||||
|
1
|
||||||
|
(* (factorial (- n 1)) n)))
|
||||||
|
(factorial 10)))
|
||||||
|
|
||||||
|
(display "\nex-5.51\n")
|
||||||
|
|
||||||
|
(display "\nex-5.52\n")
|
||||||
@@ -61,6 +61,7 @@
|
|||||||
(define (compile-and-run? expression)
|
(define (compile-and-run? expression)
|
||||||
(tagged-list? expression 'compile-and-run))
|
(tagged-list? expression 'compile-and-run))
|
||||||
(define (compile-and-run expression)
|
(define (compile-and-run expression)
|
||||||
|
(display expression) (newline)
|
||||||
(let ((instructions
|
(let ((instructions
|
||||||
(assemble (statements
|
(assemble (statements
|
||||||
(compile (cadr expression) 'val 'return))
|
(compile (cadr expression) 'val 'return))
|
||||||
@@ -160,9 +161,10 @@ read-eval-print-loop
|
|||||||
(perform (op initialize-stack))
|
(perform (op initialize-stack))
|
||||||
(perform
|
(perform
|
||||||
(op prompt-for-input) (const ";;; EC-Eval input:"))
|
(op prompt-for-input) (const ";;; EC-Eval input:"))
|
||||||
(assign exp (op read))
|
|
||||||
(assign env (op get-global-environment))
|
(assign env (op get-global-environment))
|
||||||
(assign continue (label print-result))
|
(assign continue (label print-result))
|
||||||
|
(assign exp (op read))
|
||||||
|
; (assign exp (op list) (const compile-and-run) (reg exp)) ;; read-compile-execute - ex-5.49
|
||||||
(goto (label eval-dispatch))
|
(goto (label eval-dispatch))
|
||||||
print-result
|
print-result
|
||||||
;;**following instruction optional -- if use it, need monitored stack
|
;;**following instruction optional -- if use it, need monitored stack
|
||||||
|
|||||||
Reference in New Issue
Block a user