Skip to content

Instantly share code, notes, and snippets.

@pvdz
Created June 11, 2014 18:21
Show Gist options
  • Save pvdz/1b75c28f3a5adfca11a5 to your computer and use it in GitHub Desktop.
Save pvdz/1b75c28f3a5adfca11a5 to your computer and use it in GitHub Desktop.
Silly example to add numbers to 5, after processing, beautified (ugly artifacts left in intentionally because, well, idc)
var yielding = false;
function this_makeFive(n){
var step = 1;
var v1, v0;
var f1, f0;
return function(){
while (true) {
switch (step) {
case 1:
if (n === 3);
else { step = 3; continue; }
case 2: step = 2;
case 5: step = 5;
v0 = (f0 = f0 || this_addOne(n))();
if (yielding) return;
case 6: step = 6;
return v0+1;
step = 4;
continue;
case 3: step = 3;
if (n === 1);
else { step = 8; continue; }
case 7: step = 7;
case 10: step = 10;
v1 = (f1 = f1 || this_addThree(n))();
if (yielding) return;
case 11: step = 11;
return v1+1;
step = 9;
continue;
case 8: step = 8;
throw 'invalid input, can only hand 1 and 3';
case 9: step = 9;
case 4: step = 4;
return;
}
}
};
}
function this_addThree(n){
var step = 1;
var v0;
var f0;
return function(){
while (true) {
switch (step) {
case 1:
case 2: step = 2;
v0 = (f0 = f0 || this_addOne(n))();
if (yielding) return;
case 3: step = 3;
return v0 + 2;
return;
}
}
};
}
function this_addOne(n){
var step = 1;
return function(){
while (true) {
switch (step) {
case 1:
step = 2;
return void (yielding = true);
case 2:
;
return n+1;
return;
}
}
};
}
function get(n, callback) {
var f = this_makeFive(n);
var tmp = f();
if (yielding) {
setTimeout(function again(){
yielding = false;
tmp = f();
if (yielding) {
setTimeout(again, 100);
} else {
callback(tmp);
}
}, 100);
}
}
get(3, function(n){ console.log(3,':',n); });
get(1, function(n){ console.log(1,':',n); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment