uhh
This commit is contained in:
parent
83dc103386
commit
73928a393a
1 changed files with 10 additions and 4 deletions
14
src/App.tsx
14
src/App.tsx
|
@ -23,6 +23,7 @@ export default function App() {
|
|||
onChange={(e: any) => setInput(e.target.value)}
|
||||
value={input}
|
||||
className="program"
|
||||
spellCheck="false"
|
||||
title={`\
|
||||
type Op =
|
||||
| number
|
||||
|
@ -168,7 +169,6 @@ function Stack({
|
|||
}): JSX.Element {
|
||||
let ns = [...state.stack];
|
||||
ns.reverse();
|
||||
console.log(just_popped);
|
||||
let children = (just_popped ?? []).map((just_popped, i) => (
|
||||
<StackItem
|
||||
item={just_popped}
|
||||
|
@ -328,7 +328,7 @@ function step(state: State, op: Op): State {
|
|||
ns.jumped_from = ns.ip;
|
||||
ns.just_pushed = false;
|
||||
ns.just_swapped = false;
|
||||
ns.just_popped = undefined;
|
||||
ns.just_popped = [];
|
||||
ns.ip++;
|
||||
|
||||
function binary_op(
|
||||
|
@ -399,15 +399,21 @@ function step(state: State, op: Op): State {
|
|||
if (ns.stack.length > state.stack.length) {
|
||||
ns.just_pushed = true;
|
||||
} else if (ns.stack.length < state.stack.length) {
|
||||
ns.just_popped = state.stack.slice(ns.stack.length - 1).reverse();
|
||||
if (state.stack.length - ns.stack.length == 1) {
|
||||
if (is_binary(op)) {
|
||||
ns.just_popped = state.stack.slice(-2).reverse();
|
||||
ns.just_pushed = true;
|
||||
} else {
|
||||
ns.just_popped = [state.stack[state.stack.length - 1]];
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
if (a < b) {
|
||||
return -1;
|
||||
|
|
Loading…
Reference in a new issue