Skip to content

Commit

Permalink
Implement the reset of the instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuth committed Jan 16, 2024
1 parent 0dd27fd commit 96f21c4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/Instructions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export default function Instructions() {
<li>eq</li>
<li>ne</li>
<li>gt</li>
<li>ge</li>
<li>lt</li>
<li>le</li>
<li>add</li>
<li>sub</li>
<li>+/- prefix</li>
Expand Down
28 changes: 28 additions & 0 deletions src/cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 96f21c4

Please sign in to comment.