2013年12月15日日曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の5(レジスタ計算機での計算)、5.4(積極制御評価器)、5.4.4(評価の実行)、評価器の性能監視、問題 5.30-a.を解いてみる。

その他参考書籍

問題 5.30-a.

修正、追加箇所。

コード(BBEdit)

eval.scm

;;
(define (lookup-variable-value var env)
  (define (env-loop env)
    (define (scan vars vals)
      (cond ((null? vars)
             (env-loop (enclosing-environment env)))
            ((eq? var (car vars))
             (cons 'bounded (car vals)))
            (else (scan (cdr vars) (cdr vals)))))
    (if (eq? env the-empty-environment)
        (cons 'unbounded '())
        (let ((frame (first-frame env)))
          (scan (frame-variables frame)
                (frame-values frame)))))
  (env-loop env))

(define (unbounded? var)
  (and (pair? var) (eq? (car var) 'unbounded)))

(define (bounded-value var)
  (cdr var))
;;

コード(BBEdit)

eceval.scm

;;
   (list 'unbounded? unbounded?)
   (list 'bounded-value bounded-value)
;;
ev-variable
  (assign val (op lookup-variable-value) (reg exp) (reg env))
  (test (op unbounded?) (reg val))
  (branch (label ev-unbounded))
  (assign val (op bounded-value) (reg val))
  (goto (reg continue))
ev-unbounded
  (assign val (const unbound-variable-error))
  (goto (label signal-error))
;;

コード(BBEdit)

sample.scm

(load "./eval.scm")
(load "./register.scm")
(load "./eceval.scm")
(start eceval)

入出力結果(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 "./sample.scm")

;Loading "./sample.scm"...
;  Loading "eval.scm"... done
;  Loading "register.scm"... done
;  Loading "eceval.scm"... done


;;; EC-Eval input:
a
unbound-variable-error

;;; EC-Eval input:
(define a 10)

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

;;; EC-Eval input:
a

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

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

0 コメント:

コメントを投稿