Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created November 22, 2012 21:23
Show Gist options
  • Save francescoagati/4132970 to your computer and use it in GitHub Desktop.
Save francescoagati/4132970 to your computer and use it in GitHub Desktop.
haxe-continuation: how normal functions are converted to cps functions
import com.dongxiguo.continuation.Continuation;
@:build(com.dongxiguo.continuation.Continuation.cpsByMeta("cps"))
class Sample
{
static function delay(n:Dynamic,handler:Dynamic->Dynamic):Void {
var fn=function() {
handler(n);
}
haxe.Timer.delay(fn, 100);
}
static function sleepOneSecond(handler:Void->Void):Void {
haxe.Timer.delay(handler, 100);
}
@cps static function sum(a,b,c):Int {
return a+b+c;
}
@cps public static function async() {
trace("Start continuation.");
var lst:Array<Dynamic>=[];
for (i in 0...10)
{
sleepOneSecond().async();
trace("Run sleepOneSecond " + i + " times.");
var xx=delay(100+i).async();
lst.push(sum(i,i,xx).async());
}
trace(lst);
trace("Continuation is done.");
}
public static function main() : Void {
async(function() {});
}
}
var Sample = function() { }
Sample.__name__ = true;
Sample.delay = function(n,handler) {
var fn = function() {
handler(n);
};
haxe.Timer.delay(fn,100);
}
Sample.sleepOneSecond = function(handler) {
haxe.Timer.delay(handler,100);
}
Sample.sum = function(a,b,c,__return) {
__return(a + b + c);
}
Sample.async = function(__return) {
haxe.Log.trace("Start continuation.",{ fileName : "Sample.hx", lineNumber : 26, className : "Sample", methodName : "async"});
var lst = [];
var __iterator = null;
__iterator = new IntIter(0,10);
var __break_1 = function() {
haxe.Log.trace(lst,{ fileName : "Sample.hx", lineNumber : 37, className : "Sample", methodName : "async"});
haxe.Log.trace("Continuation is done.",{ fileName : "Sample.hx", lineNumber : 38, className : "Sample", methodName : "async"});
__return();
};
var __continue_0 = null;
var __do = function() {
var i = __iterator.next();
Sample.sleepOneSecond(function() {
haxe.Log.trace("Run sleepOneSecond " + i + " times.",{ fileName : "Sample.hx", lineNumber : 33, className : "Sample", methodName : "async"});
Sample.delay(100 + i,function(__parameter_3) {
var xx = __parameter_3;
Sample.sum(i,i,xx,function(__parameter_2) {
lst.push(__parameter_2);
__continue_0();
});
});
});
};
__continue_0 = function() {
if(__iterator.hasNext()) __do(); else __break_1();
};
__continue_0();
}
Sample.main = function() {
Sample.async(function() {
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment