Created
October 18, 2012 16:03
-
-
Save spion/3912802 to your computer and use it in GitHub Desktop.
Quick and dirty regularStyle-to-CPS convertor in sweet.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro asyncp { | |
case { await { $body ... } $rest ... } => { | |
defers = 0; | |
done = (function() { asyncp { $rest ... } }); | |
asyncp { $body ... } | |
} | |
case { $token } => { $token } | |
case { $token $rest ... } => { | |
$token | |
asyncp { $rest ... } | |
} | |
} | |
macro async { | |
case { $body ... } => { | |
var deferVars = {}; | |
var defers, done; | |
var defer = function() { | |
var names = arguments; | |
++defers; | |
return function() { | |
for (var k = 0; k < names.length; ++k) | |
deferVars[names[k]] = arguments[k]; | |
if (!--defers) done(); | |
}; | |
} | |
asyncp { $body ... } | |
} | |
} | |
var lolget = function(cb) { | |
setTimeout(function() { | |
cb(25); | |
}, 1000); | |
} | |
async { | |
var t = Date.now(); | |
console.log("haha"); | |
await { lolget(defer("rofl")); } | |
console.log("rofl", deferVars.rofl, Date.now() - t); | |
await { setTimeout(defer(), 1000); } | |
console.log("lol", Date.now() - t); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Output: | |
haha | |
rofl 25 1006 | |
lol 2009 | |
todo: | |
- for, if |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment