This commit is contained in:
mehbark 2023-04-17 20:40:55 -04:00
parent 83dc103386
commit 73928a393a

View file

@ -23,6 +23,7 @@ export default function App() {
onChange={(e: any) => setInput(e.target.value)} onChange={(e: any) => setInput(e.target.value)}
value={input} value={input}
className="program" className="program"
spellCheck="false"
title={`\ title={`\
type Op = type Op =
| number | number
@ -168,7 +169,6 @@ function Stack({
}): JSX.Element { }): JSX.Element {
let ns = [...state.stack]; let ns = [...state.stack];
ns.reverse(); ns.reverse();
console.log(just_popped);
let children = (just_popped ?? []).map((just_popped, i) => ( let children = (just_popped ?? []).map((just_popped, i) => (
<StackItem <StackItem
item={just_popped} item={just_popped}
@ -328,7 +328,7 @@ function step(state: State, op: Op): State {
ns.jumped_from = ns.ip; ns.jumped_from = ns.ip;
ns.just_pushed = false; ns.just_pushed = false;
ns.just_swapped = false; ns.just_swapped = false;
ns.just_popped = undefined; ns.just_popped = [];
ns.ip++; ns.ip++;
function binary_op( function binary_op(
@ -399,15 +399,21 @@ function step(state: State, op: Op): State {
if (ns.stack.length > state.stack.length) { if (ns.stack.length > state.stack.length) {
ns.just_pushed = true; ns.just_pushed = true;
} else if (ns.stack.length < state.stack.length) { } else if (ns.stack.length < state.stack.length) {
ns.just_popped = state.stack.slice(ns.stack.length - 1).reverse(); if (is_binary(op)) {
if (state.stack.length - ns.stack.length == 1) { ns.just_popped = state.stack.slice(-2).reverse();
ns.just_pushed = true; ns.just_pushed = true;
} else {
ns.just_popped = [state.stack[state.stack.length - 1]];
} }
} }
return ns; return ns;
} }
function is_binary(op: Op): op is "add" | "sub" | "div" | "mul" | "cmp" {
return ["add", "sub", "div", "mul", "cmp"].indexOf(`${op}`) >= 0;
}
function cmp(a: number, b: number): number { function cmp(a: number, b: number): number {
if (a < b) { if (a < b) {
return -1; return -1;