diff --git a/src/components/Instructions.tsx b/src/components/Instructions.tsx index c4ab2aa..d6919e2 100644 --- a/src/components/Instructions.tsx +++ b/src/components/Instructions.tsx @@ -20,7 +20,9 @@ export default function Instructions() {
  • eq
  • ne
  • gt
  • +
  • ge
  • lt
  • +
  • le
  • add
  • sub
  • +/- prefix
  • diff --git a/src/cpu.ts b/src/cpu.ts index f9eb692..acc08f8 100644 --- a/src/cpu.ts +++ b/src/cpu.ts @@ -51,6 +51,10 @@ export function parse(code: string): Instruction[] { case 'add': case 'sub': case 'drop': + case 'eq': + case 'ne': + case 'gt': + case 'lt': output.push({cond: cond as Condition, op: operator}); } } @@ -91,6 +95,30 @@ export function execute(instruction: Instruction, stack: number[]): number[] { const [a, b, ...rest] = stack; return [a - b, ...rest]; } + case 'eq': { + const [a, b, ...rest] = stack; + return [a === b ? 1 : 0, ...rest]; + } + case 'ne': { + const [a, b, ...rest] = stack; + return [a !== b ? 1 : 0, ...rest]; + } + case 'gt': { + const [a, b, ...rest] = stack; + return [a > b ? 1 : 0, ...rest]; + } + case 'ge': { + const [a, b, ...rest] = stack; + return [a >= b ? 1 : 0, ...rest]; + } + case 'lt': { + const [a, b, ...rest] = stack; + return [a < b ? 1 : 0, ...rest]; + } + case 'le': { + const [a, b, ...rest] = stack; + return [a <= b ? 1 : 0, ...rest]; + } default: return stack; }