Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@followdarko
Created July 12, 2021 11:56
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 followdarko/dc5296c7905de6f7aa6a1cf40a530e57 to your computer and use it in GitHub Desktop.
Save followdarko/dc5296c7905de6f7aa6a1cf40a530e57 to your computer and use it in GitHub Desktop.
// Functions:
function a(x, y, z) {
x[y] = z;
}
function b(x, p) {
for (let i in p) a(x, i, p[i]);
}
function c() {
let x = { foo: 'bar' };
b(x, { foo: 1, bar: 2 });
}
// Stack Machine:
const OP_A = 1;
const OP_B = 2;
const OP_C = 3;
let firstOp;
let lastOp;
function op(op, obj, param, data) {
const c = { op, obj, param, data: next: undefined };
if (lastOp) lastOp.next = c;
else { firstOp = lastOp = c; run(); }
}
function run() {
while (firstOp) {
const { obj, param, data } = firstOp;
switch (firstOp.op) {
case OP_A:
obj[param] = data;
break;
case OP_B:
for (let i in param) op(OP_A, obj, i, param[i]);
break;
case OP_C: {
let obj = {};
op(OP_B, obj, { foo: 1, bar: 2 });
break;
}
}
firstOp = firstOp.next;
}
}
op(OP_C); // start!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment