開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- MIT/GNU Scheme (処理系)
計算機プログラムの構造と解釈(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.49.を解いてみる。
その他参考書籍
問題 5.49.
追加、修正箇所。
コード(BBEdit)
;; … (define (get-register-machine) eceval) ;; … (define eceval-operations ;; … (list 'compile compile) (list 'assemble assemble) (list 'statements statements) (list 'get-register-machine get-register-machine))) (define eceval (make-machine '(exp env val proc argl continue unev compapp machine) eceval-operations '( (assign machine (op get-register-machine)) ;; ループ read-eval-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)) (assign continue (label print-result)) (goto (label compile)) ;; 翻訳 compile (assign exp (op compile) (reg exp) (const val) (const return)) (goto (label assemble)) ;; アセンブリ assemble (assign exp (op statements) (reg exp)) (assign val (op assemble) (reg exp) (reg machine)) (goto (reg val)) ;; 結果を印字 print-result (perform (op print-stack-statistics)) (op announce-output) (const ";;; EC-Eval value:")) (perform (op user-print) (reg val)) (goto (label read-eval-print-loop)) )))
入出力結果(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: 10 (total-pushes = 0 maximum-depth = 0) ;;; EC-Eval value: 10 ;;; EC-Eval input: (+ 1 2) (total-pushes = 0 maximum-depth = 0) ;;; EC-Eval value: 3 ;;; EC-Eval input: (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 = 56 maximum-depth = 29) ;;; EC-Eval value: 3628800 ;;; EC-Eval input: (factorial 100) (total-pushes = 596 maximum-depth = 299) ;;; EC-Eval value: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 ;;; EC-Eval input: End of input stream reached. Moriturus te saluto. $
0 コメント:
コメントを投稿