santoshrajan / lispyscript

A javascript with Lispy syntax and macros
MIT License
572 stars 57 forks source link

Strange macro behavior #24

Closed sjl closed 11 years ago

sjl commented 11 years ago

I'm trying to make a simple macro to save myself some typing:

(macro onload (body...)
  ($ (function ()
       ~@body...
       null)))

(onload
  (alert (+ 1 2))
  (alert (+ 2 3)))

($ (function ()
     (alert (+ 1 2))
     (alert (+ 2 3))
     null))

I expect the onload form to expand into something like the last form there, and then get compiled. But when I compile this I get the following javascript:

// Generated by LispyScript v0.2.9
$(function() {
    alert;
    (1 + 2);
    return null;
});
$(function() {
    alert((1 + 2));
    alert((2 + 3));
    return null;
});

I have no idea what the heck is happening here since Lispyscript macros are a templating language instead of real Lisp macros (and aren't really documented (I think the ~@ char splices?)). Is this a bug or am I doing something wrong?

santoshrajan commented 11 years ago

use rest... and ~rest... to dereference. rest... is a special case that will have the rest of the expressions bound to it. No need to use @ to dereference it.

sjl commented 11 years ago

Oh god, I figured it out. The name rest... is somehow magical. If I rename body... to rest... it works. Could you maybe document that somewhere?

santoshrajan commented 11 years ago

It is documented in the let macro example in the macro docs. But yes I need to improve the docs though. Maybe not very clear. Thanks.

sjl commented 11 years ago

Happening to use the name "rest..." in an example is not the same as saying "the name rest... is treated specially". I read the example but just thought you picked "rest" because you felt like it.

On Nov 17, 2012, at 1:44 AM, Santosh Rajan notifications@github.com wrote:

It is documented in the let macro example in the macro docs. But yes I need to improve the docs though. Maybe not very clear. Thanks.

— Reply to this email directly or view it on GitHub.

santoshrajan commented 11 years ago

Yes I will make it explicit in the docs. Thanks.