4

このソリューションを作成しました:

; use like this:
; (/* content ... */ <default-return>)
; or
; (/* content ... */) => #f
(define-syntax /*
  (syntax-rules (*/)
    ((/* body ... */) #f)
    ((/* body ... */ r) r)))

しかし、それは本当に最善または最も簡単な方法でしょうか?

4

2 に答える 2

7

このようにすることはできません。多くのコンテキストでは機能しません。動作しない例を次に示します。

(+ (/* foo */) 1 2)

(define (foo a (/* b */) c) ...)

(/* foo; bar */)

(/*x*/)

(let ((x (/* 1 */) 2))
  ...)

(let ((/* (x 1) */)
      (x 2))
  ...)

(car '((/* foo */) 1 2 3))

R5RS までの Scheme レポートには標準的な複数行のコメントはありませんが、R6RS はとにかく広く使用されていた構文を追加しました: #|...|#.

しかし、もしあなたが本当にしたいのなら...

コメントで私が話していたことは次のとおりです。コード全体をマクロでラップする場合、マクロは本文全体を処理でき、より多くのコンテキストで効果的です。上記のセミコロンの例のような構文的に無効なものや、終了していない文字列をコメントアウトしようとすることを除いて、それらのほとんどすべて。本当に努力する価値があるかどうかは、自分で判断できます...

(個人的には、私はそのようなゲームを楽しんでいますが、それでも無意味だと思います。しかし、これらのゲームを本当に楽しん、役に立つと思う場合は、以下の宿題のセクションを参照してください...)

(define-syntax prog (syntax-rules () [(_ x ...) (prog~ (begin x ...))]))
(define-syntax prog~
  (syntax-rules (/* */)
    [(prog~ (/* x ...) b ...)
     ;; comment start => mark it (possibly nested on top of a previous mark)
     (prog~ (x ...) /* b ...)]
    [(prog~ (*/ x ...) /* b ...)
     ;; finished eliminating a comment => continue
     (prog~ (x ...) b ...)]
    [(prog~ (*/ x ...) b ...)
     ;; a comment terminator without a marker => error
     (unexpected-comment-closing)]
    [(prog~ (x0 x ...) /* b ...)
     ;; some expression inside a comment => throw it out
     (prog~ (x ...) /* b ...)]
    [(prog~ ((y . ys) x ...) b ...)
     ;; nested expression start => save the context
     (prog~ (y . ys) prog~ ((x ...) (b ...)))]
    [(prog~ (x0 x ...) b ...)
     ;; atomic element => add it to the body
     (prog~ (x ...) b ... x0)]
    [(prog~ () prog~ ((x ...) (b ...)) nested ...)
     ;; nested expression done => restore context
     (prog~ (x ...) b ... (nested ...))]
    [(prog~ () /* b ...)
     ;; input done with an active marker => error
     (unterminated-comment-error)]
    [(prog~ () b ...)
     ;; all done, no markers, not nested => time for the burp.
     (b ...)]))

そして例:

(prog

 (define x 1)

 (display (+ x 2)) (newline)

 /*
 (display (+ x 10))
 /* nested comment! */
 (/ 5 0)
 */

 (define (show label /* a label to show in the output, before x */
               x /* display this (and a newline), then returns it */)
   (display label)
   (display x)
   (newline)
   x
   /* this comment doesn't prevent the function from returning x */)

 (let ([x 1] /* some comment here */ [y 2])
   (show "result = " /* now display the result of show... */
         (show "list = " (list x /* blah blah */ y)))
   'done /* just a value to return from the `let' expression */)

 (show "and ... " '(even works /* boo! */ inside a quote))

 )

宿題

余分なクレジットが必要な場合は、それを拡張して、バランスの取れていない括弧をコメントアウトできるようにします。たとえば、次のようにします。

(prog
 blah blah /* junk ( junk */ blah blah /* junk ) junk */ blah blah.
 )

明らかに、入力は全体としてバランスの取れた括弧を持つ必要があります。これは、この種の拡張を実装する意味があまりないことを意味します。それがなくても、アンバランスな括弧をコメントアウトする意味は何ですか?

でも、せっかくここまで来たのなら、こんな自虐を楽しまなきゃ…でしょ?

于 2010-09-17T02:11:42.887 に答える