Implement 5.49

This commit is contained in:
2021-05-01 18:30:24 -04:00
parent 363f49d434
commit 2265a4afa5
3 changed files with 38 additions and 8 deletions

View File

@ -146,7 +146,7 @@
; (compile-and-go expression)
;; To test this exercise uncomment the previous line, then:
;; $ mit-scheme
;; 1 ]=> (load "ex-5_45-xx")
;; 1 ]=> (load "ex-5_45-49")
;; 2 ]=> (define (g n) (* n n n))
;; 3 ]=> (f 3) ; calls interpreted 'g'
@ -155,20 +155,24 @@
(display "\nex-5.48 - compile-and-run\n")
;; compile-and-go is implemented in sicp-eceval-compiler.scm
(start-eceval)
; (start-eceval)
;; To test this exercise uncomment the previous line, then:
;; $ 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))))))
;; 3 ]=> (f 5)
(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 "\nex-5.52\n")
(display "[done]\n")

24
ex-5_50-52.scm Normal file
View 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")

View File

@ -61,6 +61,7 @@
(define (compile-and-run? expression)
(tagged-list? expression 'compile-and-run))
(define (compile-and-run expression)
(display expression) (newline)
(let ((instructions
(assemble (statements
(compile (cadr expression) 'val 'return))
@ -160,9 +161,10 @@ read-eval-print-loop
(perform (op initialize-stack))
(perform
(op prompt-for-input) (const ";;; EC-Eval input:"))
(assign exp (op read))
(assign env (op get-global-environment))
(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))
print-result
;;**following instruction optional -- if use it, need monitored stack