Skip to content

Instantly share code, notes, and snippets.

@disnet
disnet / gist:6171843
Created August 7, 2013 07:06
thinking about sweet.js and code completion

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:

@disnet
disnet / gist:6735679
Created September 27, 2013 21:47
hygiene
var random = function(seed) { /* ... */ }
let m = macro {
rule {()} => {
var n = random(42); // ...
}
}
macro = {
rule { > { $body ... } } => { function foo() { $body ...} }
}
=> { return 42; }
@disnet
disnet / let_async.js
Last active December 31, 2015 04:28
sweet.js: async macro
let let = macro {
rule { async $vars ... = $fname ... ($params ...); $rest ...} => {
$fname ... ($params ..., function (err, $vars ...) {
if (err) throw err;
$rest ...
})
}
}
var buffer = new Buffer(1024);
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;
letstx $name ... = [name1, name2, name3];
letstx $type ... ... = [[type1, type2], [type3, type4]]
@disnet
disnet / getlocation.js
Created March 21, 2016 21:14
handle url parsing in IE
// the web is crazy
// http://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript
function getLocation(href) {
var location = document.createElement("a");
location.href = href;
// IE doesn't populate all link properties when setting .href with a relative URL,
// however .href will return an absolute URL which then can be used on itself
// to populate these additional fields.
if (location.host == "") {
location.href = location.href;
operator >>= left 1 = (left, right) => {
return #`${left}.then(${right})`;
}
fetch('/foo.json') >>= resp => { return resp.json() }
>>= json => { return processJson(json) }
fetch("/foo.json").then(resp => {
return resp.json();
}).then(json => {
return processJson(json);
});
// foo.js
'lang sweet.js';
export syntax m = // ...
// main.js
'lang sweet.js';
import { m } from './foo';
m // ...