santoshrajan / lispyscript

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

How to create more complicated macros? #21

Closed sjl closed 11 years ago

sjl commented 11 years ago

Right now it seems like macro is just kind of a templating engine instead of a full, Lispy macro implementation. For example, what if I wanted a macro that would let me insert some logging between each line of a function? Ideally I'd do something like this (in pseudocode):

(macro traced-function (args rest...)
  (var logs (map (function (n) `(console.log ~n))
                 (range (length rest...))))
  (var interleaved-lines (interleave logs rest...))
  `(function ~args ~@interleaved-lines))

(traced-function (foo)
  (var foo 5)
  (var bar 10)
  (+ foo bar))

Which would macroexpand to:

(function (foo)
  (console.log 1)
  (var foo 5)
  (console.log 2)
  (var bar 10)
  (console.log 3)
  (+ foo bar))

Is this simply not going to happen, and that's what "LispyScript is not a dialect of Lisp. There is no list processing in LispyScript." means? I haven't looked at the implementation, so I don't know how homoiconic LispyScript is...

santoshrajan commented 11 years ago

"LispyScript is not a dialect of Lisp. There is no list processing in LispyScript." means that List Processing is not available in LispyScript, and you may not be able to do some things the Lisp way. However you can do similar things in other ways. Problems similar to yours will be addressed in the future versions.

sjl commented 11 years ago

You don't necessarily need lists to be homoiconic. You could in principle use native Javascript arrays. Or you could use objects like Julia does. If you're not going to do that that's fine, but just saying it doesn't process lists doesn't really make it clear that homoiconicity (and all the power that you get with it) isn't on the agenda.

santoshrajan commented 11 years ago

In LispyScript code and data is distinguished by context. The third argument in a macro expression is treated as data. Thats the code the macro will expand to after dereferencing is done.

santoshrajan commented 11 years ago

The latest source now contains support for recursive macros and homoiconic expressions, which work specifically inside macro definitions. I haven't documented these yet, but that is how the "cond" macro and "->" (method chaining macro) are implemented. Have a look at the macros.ls for now.