Skip to content

Instantly share code, notes, and snippets.

@disnet
disnet / import.js
Last active Aug 29, 2015
import macros
View import.js
// test.js
#lang "./macros/stxcase.js";
import { m } from "./mod.js";
m(10, 100)
// mod.js
#lang "./macros/stxcase.js";
View gist:44f8572b863ed398a69a
macro m {
rule {} => {
(function() {
return
{
foo: "bar"
}
})
}
}
View test.js
letstx $name ... = [name1, name2, name3];
letstx $type ... ... = [[type1, type2], [type3, type4]]
View gist:8609245
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;
@disnet
disnet / let_async.js
Last active Dec 31, 2015
sweet.js: async macro
View let_async.js
let let = macro {
rule { async $vars ... = $fname ... ($params ...); $rest ...} => {
$fname ... ($params ..., function (err, $vars ...) {
if (err) throw err;
$rest ...
})
}
}
var buffer = new Buffer(1024);
View gist:7781497
macro = {
rule { > { $body ... } } => { function foo() { $body ...} }
}
=> { return 42; }
View gist:6735679
var random = function(seed) { /* ... */ }
let m = macro {
rule {()} => {
var n = random(42); // ...
}
}
@disnet
disnet / gist:6171843
Created Aug 7, 2013
thinking about sweet.js and code completion
View gist:6171843

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:

View gist:6055864
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)
View gist:6024991
macro _arms {
case (default => $value:expr) => {
else {
return $value;
}
}
case (case $cond:expr => $value:expr) => {
if($cond) {
return $value;
}
You can’t perform that action at this time.