Sweet.js – Hygienic Macros for JavaScript
sweetjs.org
sweetjs.org
// C program to illustrate a situation known as
// accidental capture of identifiers - an
// undesirable result caused by unhygienic macros
#define INCI(i) do { int x = 0; ++i; } while(0)
int main(void) { int x = 4, y = 8;
// macro called first time
INCI(x);
// macro called second time
INCI(y);
printf("x = %d, b = %d\n", x, y);
return 0;
}That can't be right.
The first case doesn't have any side effects. The second case does.
> Currently, Sweet should be considered experimental and under heavy development (re-development more like). As such, the API will be undergoing a bit of churn until probably the end of the year. So, probably best not to try Sweet in production systems just yet. If you're interested in helping out though we'd love to have you!
Project last updated 2 years ago. I think this is dead.
Besides the one I've linked, they have lots of interesting papers around the macro system: the handling of 'stages', the modularity, etc. Basically just google "Racket [something]" and go from there :)
[0] Probably more just a general 'extensible languages' type thing?
[1] http://www.ccs.neu.edu/home/stchang/pubs/ckg-popl2017.pdf
Yes, it is built using macros and I assume cleverly uses whatever macro system features Racket provides so there is possibly something novel there as well.
However, the fact that it can be done or has been done on its own is not quite so surprising to me. If you have the ability to inspect syntax you can obviously do something like that.
I could also never really figure it out post-rewrite. Pre-rewrite, I had made some cool macros just out of intuition after reading some of its examples. Now, I can't get many of its own examples to compile in its live editor.
Too bad. It's about 1000x simpler than, say, implementing low-level Babel transformations.
https://babeljs.io/blog/2017/09/11/zero-config-with-babel-ma...
https://github.com/kentcdodds/babel-plugin-macros
https://github.com/jgierer12/awesome-babel-macros
These "macros" take a different approach than Sweet.js: they're not syntax extensions, but rather code transformers that you import and use explicitly.
Would this qualify as a "hygienic macro"?
The two things I'd quite like to transform are
In a es2015 Class function if base identifier foo is a class field (in this or ancestor class) it becomes this.foo . Only for fields defined in the Class definition.
for (x of y) becomes for (let x of y)
I mean, I've seen languages like Rust using macros excessively, but why would you do so in JS?