[Chicken-users] combining syntax-rules and er/ir-macro
I'm working on a macro, |enum|, that accepts a prefix and a set of values, that expand in to a basic symbolic enumeration: (enum my foo bar) = (define my '(foo bar)) (define my-foo 'foo) (define my-bar 'bar) The reason for the above is immaterial, I really need help with the error I'm seeing trying to implement it. I am using a combination of syntax-rules and er/ir macros, and I'm getting an error message I don't understand (warning, untested code): ++ file.scm (define-syntax $%map (syntax-rules () ((_ fn () (r ...)) (r ...)) ((_ fn (x y ...) (r ...)) ($%map fn (y ...) (r ... (fn x)) ; (map foo x y z) = ((foo x) (foo y) (foo z)) ; (define-syntax %map (syntax-rules () ((_ fn l ...) ($%map fn (l ...) () (define-syntax enum (ir-macro-transformer (lambda (form inject compare?) `(define-values (,(cadr form) ,@(map (lambda (x) (symbol-append ,(cadr form) '- x)) (cddr form))) (values ,@(%map quote (cdr form))) (enum my foo bar) (pretty-print `(,my ,my-foo ,my-bar)) -- $ csi -n file.scm Error: during expansion of (enum ...) - unbound variable: %map Why is %map not visible inside enum? -a -- my personal website: http://c0redump.org/ ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] combining syntax-rules and er/ir-macro
Hi Alyn, On 2014-01-29 18:07, .alyn.post. wrote: Why is %map not visible inside enum? In order to make `%map` available to the enum transformer, it needs to be defined at expansion time, e.g. (this is one way, there may be others): (begin-for-syntax (define-syntax %map (syntax.rules () ...))) Also note that because `enum` is an ir transformer, the form provided to it will already have been renamed, so as it stands it will most likely define something like my-foo123 rather than my-foo -- you'll probably have to `inject` those parts of the form in order to have the correct identifiers bound as a result /possibly unwelcome observation. Cheers, Evan ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] combining syntax-rules and er/ir-macro
Thank you Evan, that helps explain the error message I was seeing. When I make your proposed changes, I get the following: ++ my-file.scm (begin-for-syntax (define-syntax $%map (syntax-rules () ((_ fn () (r ...)) (r ...)) ((_ fn (x y ...) (r ...)) ($%map fn (y ...) (r ... (fn x)) (define-syntax %map (syntax-rules () ((_ fn l ...) ($%map fn (l ...) ()) (define-syntax enum (ir-macro-transformer (lambda (form inject compare?) `(define-values (,(cadr form) ,@(map (lambda (x) (symbol-append (cadr form) '- x)) (cddr form))) (values ,@((inject '%map) (inject 'quote) (cdr form))) (enum my foo bar) (pretty-print `(,my ,my-foo ,my-bar)) -- $ csi -n my-file.scm Error: during expansion of (enum ...) - call of non-procedure: %map128 I might assume the problem here is that %map isn't a procedure, it's syntax, and that the symbol is being properly injected, but then how do I can a macro from a macro? I'm not even certain I'm diagnosing the problem correctly. -a On Thu, Jan 30, 2014 at 03:16:57PM +1300, Evan Hanson wrote: Hi Alyn, On 2014-01-29 18:07, .alyn.post. wrote: Why is %map not visible inside enum? In order to make `%map` available to the enum transformer, it needs to be defined at expansion time, e.g. (this is one way, there may be others): (begin-for-syntax (define-syntax %map (syntax.rules () ...))) Also note that because `enum` is an ir transformer, the form provided to it will already have been renamed, so as it stands it will most likely define something like my-foo123 rather than my-foo -- you'll probably have to `inject` those parts of the form in order to have the correct identifiers bound as a result /possibly unwelcome observation. Cheers, Evan ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users -- my personal website: http://c0redump.org/ ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users