From ee2cc40968524e6b5d40f1d85e6b50387c7d3e01 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Mon, 26 Apr 2021 15:57:47 -0400 Subject: [PATCH] Make compile and go work and answer 5.45 --- ex-5_45-xx.scm | 44 ++++++++++++++++++++++++++-- shared/sicp-load-eceval-compiler.scm | 20 +++++++++++++ shared/sicp-regsim.scm | 4 +-- 3 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 shared/sicp-load-eceval-compiler.scm diff --git a/ex-5_45-xx.scm b/ex-5_45-xx.scm index 1efc026..6ab39b5 100644 --- a/ex-5_45-xx.scm +++ b/ex-5_45-xx.scm @@ -1,3 +1,43 @@ -(load "shared/util.scm") +(load "shared/util") +(load "shared/sicp-load-eceval-compiler") +(load "shared/sicp-compiler") -(display "\nex-5.45\n") + +(display "\nex-5.45 - factorial-stack-usage-comparison\n") + +;(compile-and-go +; '(begin +; (define (factorial n) +; (if (= n 1) +; 1 +; (* (factorial (- n 1)) n))) +; (factorial 10))) + +; a. + +; | (factorial 10) | pushes | depth | +; | -------------- | ------ | ------ | +; | machine | 18 | 18 | +; | compiled | 56 | 29 | +; | evaluator | 310 | 35 | + +; | (factorial 20) | pushes | depth | +; | -------------- | ------ | ------ | +; | machine | 38 | 38 | +; | compiled | 116 | 59 | +; | evaluator | 630 | 65 | + +; pushes(compiled/machine) = 3 +; depth(compiled/machine) = 1.5 + +; pushes(evaluator/compiled) = 5.5 +; depth(evaluator/compiled) = 1.2 + +; b. Use special handling for primitive procedures from exercise 5.38. + +(display "[answered]\n") + +(display "\nex-5.46 - fibo-stack-usage-comparison\n") + + +(display "\nex-5.47\n") diff --git a/shared/sicp-load-eceval-compiler.scm b/shared/sicp-load-eceval-compiler.scm new file mode 100644 index 0000000..55ad39b --- /dev/null +++ b/shared/sicp-load-eceval-compiler.scm @@ -0,0 +1,20 @@ +;;;; 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. + +;;;;This is like load-eceval.scm except that it loads the version +;;;; of eceval that interfaces with compiled code +;;;;It doesn't load the compiler itself -- loading the compiler is up to you. + +;;;; **NB** The actual "load" calls are implementation dependent. + +(load "shared/sicp-regsim") ;reg machine simulator + +;; **NB** next file contains another "load" +(load "shared/sicp-eceval-support") ;simulation of machine operations + +;;**NB** eceval-compiler *must* be loaded after eceval-support, +;; so that the version of user-print in eceval-compiler will override +;; the version in eceval-support +(load "shared/sicp-eceval-compiler") ;eceval itself + ;and interface to compiled code diff --git a/shared/sicp-regsim.scm b/shared/sicp-regsim.scm index f3322fc..0a75f9d 100644 --- a/shared/sicp-regsim.scm +++ b/shared/sicp-regsim.scm @@ -291,7 +291,7 @@ ((eq? (car inst) 'restore) (make-restore inst machine stack pc)) ((eq? (car inst) 'label) - (make-label pc)) + (make-pseudo-label pc)) ((eq? (car inst) 'perform) (make-perform inst machine labels ops pc)) ((eq? (car inst) 'inc) @@ -299,7 +299,7 @@ (else (error "Unknown instruction type -- ASSEMBLE" inst)))) -(define (make-label pc) +(define (make-pseudo-label pc) (lambda () (advance-pc pc))) (define (make-assign inst machine labels operations pc)