Let's say you want to implement TypeScript with macros:
function foo(str: string) {
return str.toUpperCase()
// ^
// want to provide completion here
}
So we define the function macro:
| // test.js | |
| #lang "./macros/stxcase.js"; | |
| import { m } from "./mod.js"; | |
| m(10, 100) | |
| // mod.js | |
| #lang "./macros/stxcase.js"; |
| macro m { | |
| rule {} => { | |
| (function() { | |
| return | |
| { | |
| foo: "bar" | |
| } | |
| }) | |
| } | |
| } |
| letstx $name ... = [name1, name2, name3]; | |
| letstx $type ... ... = [[type1, type2], [type3, type4]] |
| let function = macro { | |
| case { _ * $id ($args ...) { $body ... } } => { | |
| var body = #{ $body ... }; | |
| var isAwaitGenerator = false; | |
| for (var i = 0; i < body.length; i++) { | |
| if (body[i].token.type === parser.Token.Identifier && | |
| body[i].token.value === 'await') { | |
| var expr = getExpr(body.slice(i)); | |
| if (expr.success) { | |
| isAwaitGenerator = true; |
| let let = macro { | |
| rule { async $vars ... = $fname ... ($params ...); $rest ...} => { | |
| $fname ... ($params ..., function (err, $vars ...) { | |
| if (err) throw err; | |
| $rest ... | |
| }) | |
| } | |
| } | |
| var buffer = new Buffer(1024); |
| macro = { | |
| rule { > { $body ... } } => { function foo() { $body ...} } | |
| } | |
| => { return 42; } |
| var random = function(seed) { /* ... */ } | |
| let m = macro { | |
| rule {()} => { | |
| var n = random(42); // ... | |
| } | |
| } |
Let's say you want to implement TypeScript with macros:
function foo(str: string) {
return str.toUpperCase()
// ^
// want to provide completion here
}
So we define the function macro:
| macro forThing { | |
| case $val ($a:expr, $b:expr) => {(function($val){forEach.call($b, this)}).bind($a)} | |
| case $val ($a:expr) => {(function($val){forEach.call(val,this)}).bind($a)} | |
| } | |
| function makeMutationObserver(tag){ | |
| var queue= tag.queue, | |
| put= queue.put.bind(queue), | |
| mutationAddObserve= forThing val (put, val.getElementsByTagName(this.tag)), | |
| mutationObserver= forThing val (mutationAddObserve, val.addedNodes), | |
| mutationsObserver= forThing val (mutationObserver) |
| macro _arms { | |
| case (default => $value:expr) => { | |
| else { | |
| return $value; | |
| } | |
| } | |
| case (case $cond:expr => $value:expr) => { | |
| if($cond) { | |
| return $value; | |
| } |