Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Last active June 2, 2023 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboyd/063afc68dd3b52b4056d4853c12ddd8a to your computer and use it in GitHub Desktop.
Save cowboyd/063afc68dd3b52b4056d4853c12ddd8a to your computer and use it in GitHub Desktop.
A more data oriented instruction set that allows wrapping interpreters.
import type { Operation, Provide, Resolve, Reject, Result } from "./deps.ts";
export type ActionFn = (resolve: Resolve, reject: Reject) => Operation<void>;
export type Instruction =
| {
name: "spawn";
operation(): Operation<unknown>;
}
| {
name: "resource";
definition(provide: Provide<unknown>): Operation<unknown>;
}
| {
name: "action";
operation(fn: ActionFn): Operation<unknown>;
}
| {
name: "suspend";
}
| {
name: "getframe";
};
export interface InstructionCall {
// the instruction being called
instruction: Instruction;
// the frame in which this instruction is being evaluated
frame: Frame;
// the block in which this instruction is being evaluated;
block: Block;
// the nth instruction to be evaluated as part of this block.
index: number;
// a signal which will be fired if the computation is cancelled while
// the instruction is being evaluated.
signal: AbortSignal;
}
// this is the middleware, this lets you put whatever pauses, enhancements
// anything around every instruction that is executed by the runtime.
export interface Interpreter {
(call: InstructionCall, next: Interpreter): Computation<Result<unknown>>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment