件のコード片は以下の感じ。
(define expand-syntax
(lambda (vars template ranks p1env)
...
;; wrap the given symbol with current usage env frame.
(define (wrap-symbol sym)
(define (finish new) (add-to-transformer-env! sym new))
;; To handle this case we need to check with p1env
;; other wise mac-env is still the same as use-env
;; (define-syntax foo
;; (let ()
;; (define bar #'bzz)
;; ...
;; ))
(let* ((mac-lib (vector-ref p1env 0))
(use-lib (vector-ref use-env 0))
(g (find-binding mac-lib sym #f))
;; if the symbol is binded locally it must not be
;; wrapped with macro environment.
(lv (p1env-lookup use-env sym LEXICAL)))
;; Issue 25.
;; if the binding found in macro env, then it must be wrap with
;; macro env.
;; FIXME: it seems working but I smell something wrong with
;; this solution. The point of the issue was inside
;; of the macro it refers to the macro itself but the
;; expansion did not occure until it really called.
;; that causes library difference even though it's in
;; the macro defined library.
(if (and (identifier? lv)
(not (eq? mac-lib use-lib))
g (eq? (gloc-library g) mac-lib))
(let ((t (make-identifier sym '() mac-lib)))
(finish (make-identifier t (vector-ref mac-env 1) mac-lib)))
(let ((t (make-identifier sym '() use-lib)))
(finish (make-identifier t (vector-ref use-env 1) use-lib))))))
...
))
まぁ、見事にFIXMEなんて書いてある部分がそれにあたる。問題になったコードは以下で見える。https://github.com/marcomaggi/r6rs-sofa/blob/master/lib/sofa/compat.sagittarius.sls
同様にFIXMEと書いてある部分が問題になる。多分、問題は2つあって、
c-functionが何かしらおかしなことになっているのと、define-c-functionの展開系からはffi.int等が見えなくなる問題である。後者の問題がコメントに書いてある部分の不具合に当たる(はず)。出力されるエラーを見ると、
ffi.intがuserライブラリの識別子となっているが、これは誤りで、正しくは(sofa compat)にならなければならない。上記のコード片はその辺りの変換を行っているのである。なぜ起きるか?
もちろん書いてあるコードがおかしいので起きるのだが、シンボルから識別子に変換する際のマクロ展開時とマクロ捕捉時の環境の選別がうまく出来ていないことに起因している。上記のコードではどうも厳しすぎるみたいである。
とりあえず、現在の識別子変換の考え方を整理する。
マクロ展開時に生のシンボルが表れた際、識別子へと変換する。その際に使われる環境の選別は以下のように行われる。
- シンボルはマクロ捕捉時環境で束縛されている
- シンボルはマクロ展開時環境で未束縛である
- マクロ捕捉時とマクロ展開時ではライブラリが異なる
- 束縛されているシンボルはマクロが捕捉されたライブラリで束縛されている
ちょっと難航しそうな感があるので、0.4.2以降で直すことにする。
No comments:
Post a Comment