js-slang
Version:
Javascript-based implementations of Source, written in Typescript
80 lines • 2.46 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeDummyContCallExpression = exports.Continuation = exports.isCallWithCurrentContinuation = exports.call_with_current_continuation = exports.Call_cc = void 0;
/**
* A dummy function used to detect for the call/cc function object.
* If the interpreter sees this specific function, a continuation at the current
* point of evaluation is executed instead of a regular function call.
*/
class Call_cc extends Function {
constructor() {
super();
}
static get() {
return Call_cc.instance;
}
toString() {
return 'call/cc';
}
}
exports.Call_cc = Call_cc;
Call_cc.instance = new Call_cc();
exports.call_with_current_continuation = Call_cc.get();
function isCallWithCurrentContinuation(value) {
return value === exports.call_with_current_continuation;
}
exports.isCallWithCurrentContinuation = isCallWithCurrentContinuation;
/**
* An object representing a continuation of the CSE machine.
* When instantiated, it copies the control stack, and
* current environment at the point of capture.
*
* Continuations and functions are treated as the same by
* the typechecker so that they can be first-class values.
*/
class Continuation extends Function {
constructor(control, stash, env) {
super();
this.control = control.copy();
this.stash = stash.copy();
this.env = [...env];
}
// As the continuation needs to be immutable (we can call it several times)
// we need to copy its elements whenever we access them
getControl() {
return this.control.copy();
}
getStash() {
return this.stash.copy();
}
getEnv() {
return [...this.env];
}
toString() {
return 'continuation';
}
}
exports.Continuation = Continuation;
/**
* Provides an adequate representation of what calling
* call/cc or continuations looks like, to give to the
* APPLICATION instruction.
*/
function makeDummyContCallExpression(callee, argument) {
return {
type: 'CallExpression',
optional: false,
callee: {
type: 'Identifier',
name: callee
},
arguments: [
{
type: 'Identifier',
name: argument
}
]
};
}
exports.makeDummyContCallExpression = makeDummyContCallExpression;
//# sourceMappingURL=continuations.js.map
;