2010-10-24 68 views
0

所以我用一个简单的文档字符串系统玩弄如在方案热身,其想法是,你可以这样做:绑定函数名作为参数的内部宏观

(def-with-doc (foo a b) 
    (desc  "Takes two parameters and sums them") 
    (param 'a "First parameter") 
    (param 'b "Second parameter") 
    (return "Sum of arguments") 
    (+ a b) 

这将是开启成:

(begin 
    (begin 
    (desc 'foo "Takes two parameters and sums them") 
    (param 'foo 'a "First parameter") 
    (param 'foo 'b "Second parameter") 
    (return 'foo "Sum of arguments")) 
    (begin 
    (define (foo a b) 
     (+ a b)))) 

我写的宏:

(define doc-symbol-list '(param desc return)) 

(define-macro (def-with-doc arg-list #!rest body) 
    ;; Loop over body, splitting into doc calls and everything else 
    (let loop ((remaining body) (docs '()) (main '())) 
    (if (null? remaining) 
     ; Reverse accumulation order of docs and main 
     ; And build re-ordered begin tree 
     (let ((docs (cons 'begin (reverse docs))) 
     (main (cons 'begin (reverse main)))) 
      (cons 'begin `(,docs ,`(define ,arg-list ,main)))) 

     ; Accumulate into docs list if expression is reserved 
     ; Otherwise into the body list 
     (let ((sexp (car remaining)) (rest (cdr remaining))) 
     (if (member (car sexp) doc-symbol-list) 
     (loop rest (cons sexp docs) main) 
     (loop rest docs (cons sexp main))))))) 

注意到定义,移动帕拉姆/递减/换货政... rn调用begin语句中的顶层并重新构造函数的主体,这样,文档字符串调用只在文件加载时执行一次,而不是在每次调用函数时执行。我知道我可以手动将文档字符串的东西放在顶层,但我试图模拟Python的文档字符串。

无论如何,最后认为我需要做的是将函数名称(上面的foo)绑定到doc-string调用中,以便(param'a“第一个参数”)变成(param'foo'a“第一个参数“),以便每个调用所关联的函数是已知的。这是我遇到麻烦的地方,我所做的每一次尝试都没有做到我想做的。

+0

你可以发布一个前后的例子吗? (你已经有了之前的版本,但是你的最终结果应该是什么并不明显。) – erjiang 2010-10-25 17:02:52

+0

用示例显示了我的目标是什么。 – 2010-10-26 12:36:48

回答

1

我建议使用define-syntax,因为它很卫生,它的syntax-rules很容易理解。 syntax-rules处于模式到结果的格式;如果你能理解cond,你可以了解syntax-rules

我认为这是做你想做的,通过片段前后来判断。

(define-syntax def-with-doc 
    (syntax-rules() 
     ;; this pattern 
     [(_ (func params ...) 
      (tag attributes ...) 
      ... 
      code) 
     ;; is converted into 
     (begin 
     (tag (quote func) attributes ...) 
     ... 
     (define (func params ...) 
      code))])) 

原谅我的术语,因为我从来没有使用过文档字符串。 基本上,这匹配任何遵循该模式的函数+ params def,具有属性的0个或更多标记以及代码语句。

然后,它只是重新排列一切。

+0

我会给这个镜头,谢谢! – 2010-10-27 12:39:24