chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] struggling with macros


From: Peter Bex
Subject: Re: [Chicken-users] struggling with macros
Date: Sun, 11 Nov 2012 13:30:59 +0100
User-agent: Mutt/1.4.2.3i

On Sun, Nov 11, 2012 at 01:23:13PM +0100, Răzvan Rotaru wrote:
> Hi,

Hi!

> I'm trying to write a simple javascript DSL, and got stuck in the macros
> :). (I'm coming from lisp macros)  Take for example this one:
> 
> (define-syntax js
>   (ir-macro-transformer
>     (lambda (expr inject compare)
>       (let ((body (cdr expr)) (next (cadr expr)))
>       (printf "next=~a~n" next)
>         (cond
>           [(string? next) (string-append "\"" next "\"")]
>           [(number? next) (number->string next)]
>           [(null? next) ""]
>           [(list? next) `(string-append (js ,(car next)) "("  ")")]
>           )))))
> 
> It is supposed to handle numbers and function calls, without building the
> parameter list in the function calls.

It's doing this correctly, is it?

> However, when trying to build the parameter list I get and error which I
> don't understand:
> 
> (define-syntax js
>   (ir-macro-transformer
>     (lambda (expr inject compare)
>       (let ((body (cdr expr)) (next (cadr expr)))
>       (printf "next=~a~n" next)
>         (cond
>           [(string? next) (string-append "\"" next "\"")]
>           [(number? next) (number->string next)]
>           [(null? next) ""]
>           [(list? next) `(string-append (js ,(car next)) "(" (apply
> string-append (map js ,(cdr next))) ")")]
>           )))))
> 
> CSI> (js (1 2 3))
> next=(1 2 3)
> next=1
> Error: unbound variable: js

You're trying to use MAP on a macro.  That's not possible because
macros are not first-class (this is true in Common Lisp as well).

> 1/ Why is "js" not bound in the second example? I also tried to use
> letrec-syntax but with same result.

Because of map.

> 2/ Are there other ways to achieve what I want?

I don't understand what you're trying to do here.

> 3/ I also tried to use syntax-rules, but from what I understood this is not
> possible, because you can't execute arbitrary expressions (in this case a
> cond) at macroexpansion time. Am I right here?

Indeed.  You can expand to a cond, but that would be less efficient.
On the other hand, this macro will only work on string literals.  Perhaps
the user would like to be able to use variables containing strings, which
would be impossible in this case.

Cheers,
Peter
-- 
http://sjamaan.ath.cx
--
"The process of preparing programs for a digital computer
 is especially attractive, not only because it can be economically
 and scientifically rewarding, but also because it can be an aesthetic
 experience much like composing poetry or music."
                                                        -- Donald Knuth



reply via email to

[Prev in Thread] Current Thread [Next in Thread]