2014年12月30日火曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の5(レジスタ計算機での計算)、5.5(翻訳系)、5.5.5(翻訳したコードの例)、問題 5.35.を解いてみる。

その他参考書籍

問題 5.35.

コード(BBEdit, Emacs)

sample35.scm

#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-

(load "./compiler.scm")   
   (print  (compile
            '(define (f x)
               (+ x (g (+ x 2))))
            'val
            'next))

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

$ ./sample35.scm
((env val) (val) ((assign val (op make-compiled-procedure) (label entry1) (reg env)) (goto (label after-lambda2)) entry1 (assign env (op compiled-procedure-env) (reg proc)) (assign env (op extend-environment) (const (x)) (reg argl) (reg env)) (assign proc (op lookup-variable-value) (const +) (reg env)) (save continue) (save proc) (save env) (assign proc (op lookup-variable-value) (const g) (reg env)) (save proc) (assign proc (op lookup-variable-value) (const +) (reg env)) (assign val (const 2)) (assign argl (op list) (reg val)) (assign val (op lookup-variable-value) (const x) (reg env)) (assign argl (op cons) (reg val) (reg argl)) (test (op primitive-procedure?) (reg proc)) (branch (label primitive-branch3)) compiled-branch4 (assign continue (label after-call5)) (assign val (op compiled-procedure-entry) (reg proc)) (goto (reg val)) primitive-branch3 (assign val (op apply-primitive-procedure) (reg proc) (reg argl)) after-call5 (assign argl (op list) (reg val)) (restore proc) (test (op primitive-procedure?) (reg proc)) (branch (label primitive-branch6)) compiled-branch7 (assign continue (label after-call8)) (assign val (op compiled-procedure-entry) (reg proc)) (goto (reg val)) primitive-branch6 (assign val (op apply-primitive-procedure) (reg proc) (reg argl)) after-call8 (assign argl (op list) (reg val)) (restore env) (assign val (op lookup-variable-value) (const x) (reg env)) (assign argl (op cons) (reg val) (reg argl)) (restore proc) (restore continue) (test (op primitive-procedure?) (reg proc)) (branch (label primitive-branch9)) compiled-branch10 (assign val (op compiled-procedure-entry) (reg proc)) (goto (reg val)) primitive-branch9 (assign val (op apply-primitive-procedure) (reg proc) (reg argl)) (goto (reg continue)) after-call11 after-lambda2 (perform (op define-variable!) (const f) (reg val) (reg env)) (assign val (const ok))))
$

見やすくするため、改行を挿入して修正。

レジスタ計算機。

(assign val (op make-compiled-procedure) (label entry1) (reg env)) 
(goto (label after-lambda2))
entry1 
(assign env (op compiled-procedure-env) (reg proc)) 
(assign env (op extend-environment) (const (x)) (reg argl) (reg env)) 
(assign proc (op lookup-variable-value) (const +) (reg env)) 
(save continue) 
(save proc) 
(save env) 
(assign proc (op lookup-variable-value) (const g) (reg env)) 
(save proc) 
(assign proc (op lookup-variable-value) (const +) (reg env)) 
(assign val (const 2)) 
(assign argl (op list) (reg val)) 
(assign val (op lookup-variable-value) (const x) (reg env)) 
(assign argl (op cons) (reg val) (reg argl)) 
(test (op primitive-procedure?) (reg proc)) 
(branch (label primitive-branch3))
compiled-branch4 
(assign continue (label after-call5)) 
(assign val (op compiled-procedure-entry) (reg proc)) 
(goto (reg val))
primitive-branch3 
(assign val (op apply-primitive-procedure) (reg proc) (reg argl))
after-call5 
(assign argl (op list) (reg val)) 
(restore proc) 
(test (op primitive-procedure?) (reg proc)) 
(branch (label primitive-branch6))
compiled-branch7 
(assign continue (label after-call8)) 
(assign val (op compiled-procedure-entry) (reg proc)) 
(goto (reg val))
primitive-branch6 
(assign val (op apply-primitive-procedure) (reg proc) (reg argl))
after-call8 
(assign argl (op list) (reg val)) 
(restore env) 
(assign val (op lookup-variable-value) (const x) (reg env)) 
(assign argl (op cons) (reg val) (reg argl)) 
(restore proc) 
(restore continue) 
(test (op primitive-procedure?) (reg proc)) 
(branch (label primitive-branch9))
compiled-branch10 
(assign val (op compiled-procedure-entry) (reg proc)) 
(goto (reg val))
primitive-branch9 
(assign val (op apply-primitive-procedure) (reg proc) (reg argl)) 
(goto (reg continue))
after-call11
after-lambda2 
(perform (op define-variable!) (const f) (reg val) (reg env)) 
(assign val (const ok))

0 コメント:

コメントを投稿