Skip to content

Instantly share code, notes, and snippets.

@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;
}
@disnet
disnet / gist:6024833
Last active Dec 19, 2015
simple object structure case matching macro
View gist:6024833
macro _match_cond {
case $o ($field) => {
(typeof $o.$field !== 'undefined')
}
case $o ($field $rest ...) => {
_match_cond $o ($field) && _match_cond $o ($rest ...)
}
}
macro _match_var {
View gist:5267782
macro class {
case $className { constructor $constParam $constBody $rest ... } => {
function $className $constParam $constBody
class $className { $rest ... }
}
case $className { private_function $pMethodName $pMethodParam $pMethodBody $rest ...} => {
function $pMethodName $pMethodParam $pMethodBody
class $className { $rest ... }
@disnet
disnet / scheme.sjs
Created Oct 8, 2012
start of scheme in sweet.js (modified from http://pastebin.com/0nPBg8LY)
View scheme.sjs
macro sexp {
case () => {
;
}
case ($p) => {
$p
}
case ($x $y) => {
$x($y);
}
@disnet
disnet / gist:4165526
Created Nov 28, 2012
javascript - sweet.js macro checking for null
View gist:4165526
macro null_helper {
case ($processed ...) ($rest) => {
$processed (.) ... . $rest
}
case ($processed ...) ($rest_head $rest ...) => {
$processed (.) ... . $rest_head
&& null_helper ($processed ... $rest_head) ($rest ...)
}
}
You can’t perform that action at this time.