Consider the following macro that has a definitional macro, define-x, that stores a function definition in a structure bound to a generated name, and a macro that either calls that stored function or it does something else. [I used a generated name, x:<name>, because I need to use the original for another purpose - illustrated here by just calling the stored function.]<br>
<br>#lang racket<br><br>(struct x-info (name func))<br><br>(define-syntax (define-x stx)<br> (syntax-case stx ()<br> ((define-x (name . parameters) . body)<br> (with-syntax ((id (datum->syntax<br> (syntax define-x)<br>
(string->symbol<br> (format "x:~a" (syntax->datum #'name))))))<br> #'(define-values (id name)<br> (let ((func (lambda parameters . body)))<br>
(values (x-info 'name func) func)))))))<br><br>(define-syntax (do-x stx)<br> (syntax-case stx ()<br> ((do-x (name . arguments))<br> (with-syntax ((id (datum->syntax<br> (syntax define-x)<br>
(string->symbol<br> (format "x:~a" (syntax->datum #'name))))))<br> (if (identifier-binding #'id)<br> #'((x-info-func id) . arguments)<br>
#'(name . arguments))))))<br> <br>(provide (all-defined-out))<br><br>Now, I reference this in a separate module:<br><br>#lang racket<br><br>(require "x-2.ss")<br><br>(define (dummy a b c)<br> (printf "dummy: ~a ~a ~a~n" a b c))<br>
<br>(define-x (my-x a b c)<br> (printf "my-x: ~a ~a ~a~n" a b c))<br><br>(do-x (my-x 1 2 3))<br>(my-x 1 2 3)<br>(do-x (dummy 1 2 3))<br><br>And, when I run it I get the desired results.<br><br>my-x: 1 2 3<br>my-x: 1 2 3<br>
dummy: 1 2 3<br><br>Now, the question is whether this is the 'right' way to do this and are there any 'gotchas' that I'm not aware of? Is there something better to use than relying on identifier-binding to return #f when there is no binding? And, whatever obvious questions I don't even know enough to ask.<br>
<br>In the actually application, I am using syntax-parse to parse a rather more complicated syntax. Is there any way in syntax-parse I can define a syntax class that would recognize a class of identifier based on their bindings?<br>
<br>Doug<br>