Exercise 5.49. As an alternative to using the explicit-control evaluator's read-eval-print loop, design a register machine that performs a read-compile-execute-print loop. That is, the machine should run a loop that reads an expression, compiles it, assembles and executes the resulting code, and prints the result. This is easy to run in our simulated setup, since we can arrange to call the procedures compile and assemble as ``register-machine operations.'' ———————————————————————————————————————————————————————————————————————— $ scheme --load 5.49-load-eceval-compiler.scm 1 ]=> (load "ch5-compiler") 1 ]=> (start-eceval) ;;; EC-Eval input: (define (fib n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) ;;; EC-Eval value: ok ;;; EC-Eval input: (fib 3) (total-pushes = 22 maximum-depth = 8) ;;; EC-Eval value: 2 ;;; EC-Eval input: (fib 6) (total-pushes = 122 maximum-depth = 17) ;;; EC-Eval value: 8 ;;; EC-Eval input: (fib 10) (total-pushes = 882 maximum-depth = 29) ;;; EC-Eval value: 55 This is a straightforward modification of the solution to the previous exercise. The support code from 5.48 is used unchanged. compile-and-keep-going is the same as in 5.48. The only interesting differences are in the REPL itself, which is changed as follows, similarly to the changes in the previous exercise: @@ -142,21 +152,22 @@ (assign compapp (label compound-apply)) ;;*next instruction supports entry from compiler (from section 5.5.7) (branch (label external-entry)) -read-eval-print-loop +read-compile-execute-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)) + (perform (op compile-and-keep-going) (reg exp)) (assign continue (label print-result)) - (goto (label eval-dispatch)) + (goto (reg val)) print-result ;;**following instruction optional -- if use it, need monitored stack (perform (op print-stack-statistics)) (perform (op announce-output) (const ";;; EC-Eval value:")) (perform (op user-print) (reg val)) - (goto (label read-eval-print-loop)) + (goto (label read-compile-execute-print-loop)) ;;*support for entry from compiler (from section 5.5.7) external-entry