2014年1月12日日曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の5(レジスタ計算機での計算)、5.5(翻訳系)、翻訳系の概観、5.5.7(翻訳したコードと評価器のインターフェース)、解釈と翻訳、問題 5.48.を解いてみる。

その他参考書籍

問題 5.48.

追加、修正箇所。

コード(BBEdit)

;; …
(define (compile-and-run? exp) (tagged-list? exp 'compile-and-run))

(define (compile-and-run-code exp) (cadr exp))
;; …
   (list 'compile-and-run compile-and-run)
   (list 'compile-and-run? compile-and-run?)
;; …
(define (compile-and-run exp)
  (assemble (statements
             (compile (compile-and-run-code exp) 'val 'return))
            eceval))
;; …

eval-dispatch
  (test (op compile-and-run?) (reg exp))
  (branch (label ev-compile-and-run))
;; …
ev-compile-and-run
  (assign val (op text-of-quotation) (reg exp))
  (assign val (op compile-and-run) (reg val))
  (goto (reg val))
;; …

入出力結果(Terminal, REPL(Read, Eval, Print, Loop))

$ scheme
MIT/GNU Scheme running under MacOSX
Type `^C' (control-C) followed by `H' to obtain information about interrupts.

Copyright (C) 2011 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Image saved on Saturday October 26, 2013 at 11:02:50 PM
  Release 9.1.1 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/C 4.118
  Edwin 3.116

1 ]=> (load "./eceval.scm")

;Loading "./eceval.scm"... done
;Value: start-eceval

1 ]=> (start-eceval)


;;; EC-Eval input:
(define (factorial n)
  (if (= n 1)
      1
      (* (factorial (- n 1)) n)))

(total-pushes = 3 maximum-depth = 3)
;;; EC-Eval value:
ok

;;; EC-Eval input:
(factorial 10)

(total-pushes = 304 maximum-depth = 53)
;;; EC-Eval value:
3628800

;;; EC-Eval input:
(compile-and-run
 '(define (factorial n)
    (if (= n 1)
        1
        (* (factorial (- n 1)) n))))

(total-pushes = 0 maximum-depth = 0)
;;; EC-Eval value:
ok

;;; EC-Eval input:
(factorial 10)

(total-pushes = 61 maximum-depth = 29)
;;; EC-Eval value:
3628800

;;; EC-Eval input:
(define (factorial n)
  (if (= n 1)
      1
      (* (factorial (- n 1)) n)))

(total-pushes = 3 maximum-depth = 3)
;;; EC-Eval value:
ok

;;; EC-Eval input:
(factorial 100)

(total-pushes = 3184 maximum-depth = 503)
;;; EC-Eval value:
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

;;; EC-Eval input:
(compile-and-run
 '(define (factorial n)
    (if (= n 1)
        1
        (* (factorial (- n 1)) n))))

(total-pushes = 0 maximum-depth = 0)
;;; EC-Eval value:
ok

;;; EC-Eval input:
(factorial 100)

(total-pushes = 601 maximum-depth = 299)
;;; EC-Eval value:
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

;;; EC-Eval input:
(factorial 10)

(total-pushes = 61 maximum-depth = 29)
;;; EC-Eval value:
3628800

;;; EC-Eval input:
(define (factorial n)
  (if (= n 1)
      1
      (* (factorial (- n 1)) n)))

(total-pushes = 3 maximum-depth = 3)
;;; EC-Eval value:
ok

;;; EC-Eval input:
(factorial 5)

(total-pushes = 144 maximum-depth = 28)
;;; EC-Eval value:
120

;;; EC-Eval input:
(factorial 10)

(total-pushes = 304 maximum-depth = 53)
;;; EC-Eval value:
3628800

;;; EC-Eval input:
End of input stream reached.
Moriturus te saluto.
$

積極制御評価器で翻訳系を呼び出すことが出来るようになったことを確認できた。(翻訳した階乗手続き、解釈した階乗手続きのスタック演算の違いが確認できた。)

0 コメント:

コメントを投稿