4 ms·
Proposals for how to support macros in JavaScript are very interesting. I am working on Mascara (http://www.mascaraengine.com/ http://www.mascaraengine.com/) wh
by olavk 17y ago
Proposals for how to support macros in JavaScript are very interesting. I am working on Mascara (http://www.mascaraengine.com/ http://www.mascaraengine.com/) which is basically a type-verification engine and macro expander for JavaScript. However it only supports "hardcoded"-macros in the form of transformations from ECMAScript Harmony (or whatever the next version of ECMAScript is going to be called) into plain old JavaScript.
I would love the hear ideas about how to support user-defined macros.
I notice that the submitted article uses simple string-substitution macros (like the C preprocessor). I think that is too limited and have too many pitfalls. I would prefer lisp-style macros (macros operating on the syntax tree), although I realize it is more complex in a language like JavaScript with a larger grammar.
The most powerful approach would be to allow the user to extend the language grammar however they like. But that is also complicated for the user, because you has to know the grammar of the language pretty well to avoid introducing ambiguities.
A more limited approach would be to allow macro-invocation with a predefined syntax, e.g. macroname ( arguments ) { statements } - the macro could then define transformations for the arguments and statements. This could support much of what you could want without having to mess with the actual language grammar. It would not be able to support the -> and ~> ideas in the post though, so perhaps this is to limited.