mike.castillo.jr
2019-4-3 22:54:55

Hi! I’m trying to create a line function using the source line of a syntax object, and put it into a syntax variable, for substitution into my #%module-begin template.

Here are some of the things I’ve tried (this is derived from Beautiful Racket).

(with-syntax*
    ([((_ LINE-NUM _ ...) ...) #'(LINE ...)]
     [(LINE-ID ...)     (prefix-id "line-" #'(LINE-NUM ...))])
; ***prefix-id is a variant of format-id***

This works just fine, but I have to manually add the line numbers into my program.

[(LINE-NUM ...)    (syntax-line #'(LINE ...))]
[(LINE-ID ...)     (prefix-id "line-" #'(LINE-NUM ...))]

This gives me the line of the template itself (not of the contents of LINE), since syntax-line is not a macro.

[(LINE-NUM ...)    #'((syntax-line #'LINE) ...)]
[(LINE-ID ...)     (prefix-id "line-" #'(LINE-NUM ...))]

This is a compilation error. If I could somehow splice the contents of the #’(LINE-NUM …) template here, I think it would work.

[(LINE-NUM ...)    #'((syntax-line #'LINE) ...)]
[(LINE-ID ...)     #'((prefix-id "line-" #`#,LINE-NUM) ...)]

This gives me the correct line number, but turns LINE-ID into a syntax object, not a function id.

[(LINE-NUM ...)    #'((syntax-line #'LINE) ...)]
[(LINE-ID ...)     #'((format-symbol "line-~a" LINE-NUM) ...)]

This turns LINE-ID into a symbol. I couldn’t get either a syntax object or a symbol to run as code using eval / eval-syntax, apparently because they don’t inherit the original function identifier bindings.

From what I can tell, I need to substitute the raw function names into my #%module-begin (not syntax or symbols) in order for it to work. I just can’t figure out how.