;;;; LOADS THE EXPLICIT-CONTROL EVALUATOR FROM SECTION 5.4 OF ;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS, WITH ;;;; ALL THE SUPPORTING CODE IT NEEDS IN ORDER TO RUN. ;(load "syntax") ;section 4.1.2 syntax procedures ;(load "compiler") ;compiler ;(load "regsim") ;reg machine simulator ;(load "ecevsup") ;simulation of machine operations ;(load "ecevcomp") ;eceval itself ;and interface to compiled code (define the-global-environment (setup-environment)) (define standard-primitives `((+ ,+) (- ,-) (* ,*) (/ ,/) (inc ,inc) (dec ,dec) (= ,=) (< ,<) (> ,>) (zero? ,zero?) (not ,not) (true ,true) (false ,false) (nil ,nil) (cons ,cons) (car ,car) (cdr ,cdr) (pair? ,pair?) (null? ,null?) (list ,list) (eq? ,eq?) (symbol? ,symbol?) (write-line ,write-line))) (define (compile-and-display expression) (fluid-let ((*unparser-list-breadth-limit* #f) (*unparse-uninterned-symbols-by-name?* #T)) (newline) (for-each (lambda (statement) (newline) (if (pair? statement) (display " ")) (display statement)) (statements (compile expression 'val 'return))))) (define (continue-eceval) (set-register-contents! eceval 'flag false) (start eceval)) ;;;; To start, can use compile-and-go as in section 5.5.7 ;;;; or start-eceval as in the section 5.5.7 footnote. ;;;; To resume the machine without reinitializing the global environment ;;;; if you have somehow interrupted out of the machine back to Scheme, do ;;;; (set-register-contents! eceval 'flag false) ;;;; (start eceval) ;;; Now the following things should work: ;; (define gcd-machine ;; (make-machine ;; '(a b t) ;; (list (list 'rem remainder) (list '= =)) ;; '(test-b ;; (test (op =) (reg b) (const 0)) ;; (branch (label gcd-done)) ;; (assign t (op rem) (reg a) (reg b)) ;; (assign a (reg b)) ;; (assign b (reg t)) ;; (goto (label test-b)) ;; gcd-done))) ;; ;; (set-register-contents! gcd-machine 'a 206) ;; ;; (set-register-contents! gcd-machine 'b 40) ;; ;; (start gcd-machine) ;; ;; (get-register-contents gcd-machine 'a) ;; ... should return 2 ;; (start-eceval) ;; ;; ;;; EC-Eval input: ;; (define (append x y) ;; (if (null? x) ;; y ;; (cons (car x) ;; (append (cdr x) y)))) ;; (total-pushes = 3 maximum-depth = 3) ;;; EC-Eval value: ;; ok ;; ;; ;;; EC-Eval input: ;; (append '(a b c) '(d e f)) ;; (total-pushes = 118 maximum-depth = 17) ;; ;;; EC-Eval value: ;; (a b c d e f)