SICP 読み (334) 5.5 翻訳系

問題 5.38

d.について検討、なんですが具体的にという部分でどうしたものやら。
今は compile-plus とか mul とかが、というよりは spread-arguments が引数二つ、にしか対応してないので例えば

(+ 1 2 3)

な式は

(+ 1 (+ 2 3))

に無理矢理変換して compile に再度流した方が良いのかなぁ。(cond->if 方式)

(define (fold-exp l)
  (define (fold-exp-iter operator operands)
    (if (null? (cdr operands))
	(car operands)
	(list operator 
	      (car operands) 
	      (fold-exp-iter operator (cdr operands)))))
  (fold-exp-iter (car l) (cdr l)))

で、compile なソレを

	((eq? (car exp) '+) (compile-plus (fold-exp exp) target linkage))

だとどうかな。てか、fold-exp って名前的に微妙。一応タタンでる事になるのかどうか。一応動作はできてる模様。

gosh> (fold-exp '(+ 1 2 3 4))
(+ 1 (+ 2 (+ 3 4)))
gosh> 

評価器に吸わせてみました。整形済みのナニを以下に

gosh> (compile '(+ 1 2 3 4) 'val 'return)
(() (arg1 arg2 val) 
 ((assign arg1 (const 1)) 
  (save arg1) 
  (assign arg1 (const 2)) 
  (save arg1)
  (assign arg1 (const 3)) 
  (assign arg2 (const 4)) 
  (assign arg2 (op +) (reg arg1) (reg arg2))
  (restore arg1) 
  (assign arg2 (op +) (reg arg1) (reg arg2)) 
  (restore arg1)
  (assign val (op +) (reg arg1) (reg arg2))))
gosh> 

なんか微妙なカンジがするんですが気のせいでしょうか。(何