nothing's falsity can unwind the unwary

This commit is contained in:
mehbark 2023-04-17 16:44:10 -04:00
parent 4cb13e0a16
commit 855e55bff7

View file

@ -246,7 +246,6 @@ function parse_op(op: string): Option<Op> {
case "swp":
return op;
}
console.log(op);
}
type State = {
@ -262,7 +261,7 @@ const INITIAL_STATE: State = {
};
function steps(state: State, ops: Op[], limit = 300, num_steps = 0): State[] {
if (!ops[state.ip] || num_steps > limit) {
if (typeof ops[state.ip] == "undefined" || num_steps > limit) {
return [state];
}
return cons(
@ -288,55 +287,56 @@ function step(state: State, op: Op): State {
if (typeof op == "number") {
ns.stack.push(op);
} else {
switch (op) {
case "add":
binary_op(0, 0, (a, b) => a + b);
break;
case "sub":
binary_op(0, 0, (a, b) => a - b);
break;
case "mul":
binary_op(0, 1, (a, b) => a * b);
break;
case "div":
binary_op(0, 1, (a, b) => a / b);
break;
case "put": {
let popped = ns.stack.pop();
if (typeof popped != "undefined") {
ns.output.push(popped);
}
break;
}
case "jmp":
ns.ip--;
ns.ip += ns.stack.pop() ?? 1;
break;
case "dup": {
let popped = ns.stack.pop();
if (typeof popped != "undefined") {
ns.stack.push(popped, popped);
}
break;
}
case "del":
ns.stack.pop();
break;
case "cmp": {
let [a, b] = [ns.stack.pop() ?? 0, ns.stack.pop() ?? 0];
// matter of taste
ns.stack.push(cmp(b, a));
break;
}
case "swp": {
let [a, b] = [ns.stack.pop(), ns.stack.pop()];
if (typeof a == "undefined" || typeof b == "undefined") {
break;
}
ns.stack.push(a, b);
return ns;
}
switch (op) {
case "add":
binary_op(0, 0, (a, b) => a + b);
break;
case "sub":
binary_op(0, 0, (a, b) => a - b);
break;
case "mul":
binary_op(0, 1, (a, b) => a * b);
break;
case "div":
binary_op(0, 1, (a, b) => a / b);
break;
case "put": {
let popped = ns.stack.pop();
if (typeof popped != "undefined") {
ns.output.push(popped);
}
break;
}
case "jmp":
ns.ip--;
ns.ip += ns.stack.pop() ?? 1;
break;
case "dup": {
let popped = ns.stack.pop();
if (typeof popped != "undefined") {
ns.stack.push(popped, popped);
}
break;
}
case "del":
ns.stack.pop();
break;
case "cmp": {
let [a, b] = [ns.stack.pop() ?? 0, ns.stack.pop() ?? 0];
// matter of taste
ns.stack.push(cmp(b, a));
break;
}
case "swp": {
let [a, b] = [ns.stack.pop(), ns.stack.pop()];
if (typeof a == "undefined" || typeof b == "undefined") {
break;
}
ns.stack.push(a, b);
break;
}
}