sicp-ex-5.49



<< Previous exercise (5.48) | Index | Next exercise (5.50) >>


ypeels

(load "ch5-regsim.scm") ; for (make-machine) and (assemble)
(load "ch5-compiler.scm") ; for (compile)
(load "ch5-eceval-support.scm") ; for syntax and utility procedures
(load "ch5-eceval-compiler.scm") ; for operation list, needed by assembler

; return linkage makes compiled code return to print-result below
(define (compile-and-assemble expr)
    (assemble
        (statements (compile expr 'val 'return))
        ec-comp-exec))

; compiler expects same register set as eceval
(define ec-comp-exec-registers
    '(expr env val proc argl continue unev))
    
(define ec-comp-exec-operations 
    (append 
        eceval-operations ; from ch5-eceval-compiler.scm
        (list (list 'compile-and-assemble compile-and-assemble))))    
    
    
    
(define ec-comp-exec-controller-text '(

; main loop same as eceval
read-compile-exec-print-loop
    (perform (op initialize-stack)) 
    (perform (op prompt-for-input) (const ";;; EC-Comp-Exec input:"))
    (assign expr (op read))
    (assign env (op get-global-environment))
    (assign continue (label print-result)) 
    (goto (label compile-and-execute)) ; ** label name changed
print-result
    (perform (op print-stack-statistics))
    (perform (op announce-output) (const ";;; EC-Comp-Exec value:"))
    (perform (op user-print) (reg val))
    (goto (label read-compile-exec-print-loop))

    
; the entirety of the new machine! as per the problem statement, 
; all complexity is deferred to "primitives" (compile) and (assemble)
compile-and-execute
    (assign val (op compile-and-assemble) (reg expr))
    (goto (reg val))
    
))



(define ec-comp-exec (make-machine
    ec-comp-exec-registers
    ec-comp-exec-operations 
    ec-comp-exec-controller-text))

(define (start-ec-comp-exec)
    (set! the-global-environment (setup-environment))
    (ec-comp-exec 'start)
)

(start-ec-comp-exec)