eval(readFile('Compiled_NamespaceJS_Compiler')) continuation_lib = ' NS = { end_normally : false, workinglist : [new Object()], current : new Object(), destination : {}, action : "", propagate : "", what : "", parameter : "", in_progress : false, debugMode : true, // predefined namespaces userNs : new Object() } $globals = { continuation: {in_progress: false, k: []}, top_level_continuation: null, top_level_namespace: new Object(), k_type: {call_cc: new Object(), call_comp: new Object(), normal_stack: new Object()} } function enable_delimited_continuations(next){ $globals.top_level_continuation = new Continuation(); var result; try [$globals.top_level_namespace] { result = next(); } catch (e) { result = e; } return result; } function invoke_continuation(k, val){ if (k.type == $globals.k_type.call_cc){ $globals.continuation.k = []; $globals.continuation.k.push(k); } else if (k.type == $globals.k_type.call_comp){ var restoreK = new Continuation(); //if we are not in the process of invoking a continuation if (!$globals.continuation.in_progress) { restoreK.type = $globals.k_type.normal_stack; $globals.continuation.k = [restoreK]; } else { //replace the last continuation with the current one var last = $globals.continuation.k.length-1; restoreK.type = $globals.continuation.k[last].type; restoreK.target = $globals.continuation.k[last].target; $globals.continuation.k.pop(); $globals.continuation.k.push(restoreK); } } $globals.continuation.in_progress = true; k(val); } function call_cc(tag, next){ var k = new Continuation(); k.target = tag; k.type = $globals.k_type.call_cc; return next(k); } function call_comp(tag, next){ var k = new Continuation(); k.target = tag; k.type = $globals.k_type.call_comp; return next(k); } function prompt(tag, abort_handler, next){ print("entering prompt " + tag); try [tag] { var result = next(); //if this has to be delimited if ($globals.continuation.in_progress){ //get the continuation from the end of the list var k = $globals.continuation.k[$globals.continuation.k.length-1]; if (k.target === tag) { if (k.type == $globals.k_type.call_cc) { $globals.continuation.pop(); throw [$globals.top_level_namespace] result; } else if (k.type == $globals.k_type.call_comp){ //pop and invoke the next continuation from the end of the list $globals.continuation.k.pop(); if ($globals.continuation.k[$globals.continuation.k.length-1].type == $globals.k_type.normal_stack) { //if this of type normal_stack, then it is the last continuation to invoke, and it will not be delimited by a prompt either $globals.continuation.k.pop()(result); } else { //this is either a composable or non-composable continuation $globals.continuation.k[$globals.continuation.k.length-1](result); } } else{ print("we should not reach here"); } } } } catch (e) { result = abort_handler(e) } return result; } function abort(tag, val){ throw [tag] val; } '