From 855e55bff7a9186bbaffc99bab319f06b07dad65 Mon Sep 17 00:00:00 2001 From: mehbark Date: Mon, 17 Apr 2023 16:44:10 -0400 Subject: [PATCH] nothing's falsity can unwind the unwary --- src/App.tsx | 98 ++++++++++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 142cbf6..9315f6b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -246,7 +246,6 @@ function parse_op(op: string): Option { 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; } }