Got the stack display and stack top wrong. Looks like everything else is in the right order but I'll have to test more later.

master
Zed A. Shaw 4 years ago
parent c0456f84c3
commit fd71f8a45e
  1. 8
      src/node_modules/buttons.js
  2. 24
      src/routes/index.svelte

8
src/node_modules/buttons.js generated vendored

@ -39,7 +39,7 @@ class ButtonMachine {
} }
get stack_top() { get stack_top() {
return this.stack[0]; return this.stack[this.stack.length - 1];
} }
get cur_op() { get cur_op() {
@ -142,14 +142,16 @@ class ButtonMachine {
} }
op_STOR(reg) { op_STOR(reg) {
if(!this.assert(!reg.includes(reg), `Register ${reg} is not valid. Use ${ButtonMachine.registers_names()}`)) return; const reg_names = ButtonMachine.register_names();
if(!this.assert(reg_names.includes(reg), `Register "${reg}" is not valid. Use ${reg_names}`)) return;
this.registers[reg] = this.stack_top; this.registers[reg] = this.stack_top;
this.next(); this.next();
} }
op_RSTOR(reg) { op_RSTOR(reg) {
if(!this.assert(!reg.includes(reg), `Register ${reg} is not valid. Use ${ButtonMachine.registers_names()}`)) return; const reg_names = ButtonMachine.register_names();
if(!this.assert(reg_names.includes(reg), `Register "${reg}" is not valid. Use ${reg_names}`)) return;
let val = this.registers[reg]; let val = this.registers[reg];
this.assert(val !== undefined, `Invalid register ${reg}`); this.assert(val !== undefined, `Invalid register ${reg}`);

@ -11,6 +11,7 @@
let code = [ let code = [
['PUSH', -10], // start at -10 ['PUSH', -10], // start at -10
['PUSH', 1], // increment by 1 ['PUSH', 1], // increment by 1
['STOR', 'AX'],
['ADD'], ['ADD'],
['JZ', 5], // if we're at 0 jumps to the end ['JZ', 5], // if we're at 0 jumps to the end
['JUMP', 1] // the previous test fails so it jumps to loop again ['JUMP', 1] // the previous test fails so it jumps to loop again
@ -25,6 +26,7 @@
const data_ops = new Set(['PUSH', 'JZ', 'JNZ', 'JUMP', 'STOR', 'RSTOR', 'HALT']); const data_ops = new Set(['PUSH', 'JZ', 'JNZ', 'JUMP', 'STOR', 'RSTOR', 'HALT']);
const ops = ButtonMachine.operations(); const ops = ButtonMachine.operations();
const registers = ButtonMachine.register_names();
const change_op = (i, count) => { const change_op = (i, count) => {
let cur_op = code[i][0]; let cur_op = code[i][0];
@ -87,6 +89,16 @@
transition: unset; transition: unset;
padding: 5px; padding: 5px;
} }
#reg-select {
background-color: $gray;
width: 100%;
}
#reg-select select {
background-color: $gray;
width: 100%;
}
</style> </style>
<div class="container grid-lg" id="content" data-testid="buttons-page"> <div class="container grid-lg" id="content" data-testid="buttons-page">
@ -122,7 +134,15 @@
class="btn btn-primary input-group-btn">{op}</button> class="btn btn-primary input-group-btn">{op}</button>
{#if op_has_data(op)} {#if op_has_data(op)}
{#if op === 'STOR' || op === 'RSTOR' || op == 'HALT'} {#if op === 'STOR' || op === 'RSTOR'}
<div class="form-group" id="reg-select">
<select class="form-select" bind:value={data}>
{#each registers as reg}
<option>{reg}</option>
{/each}
</select>
</div>
{:else if op == 'HALT'}
<input class:active-line={ machine.ip == i } <input class:active-line={ machine.ip == i }
type="text" class="form-input" placeholder="register" bind:value={data}> type="text" class="form-input" placeholder="register" bind:value={data}>
{:else} {:else}
@ -170,7 +190,7 @@
</tr> </tr>
</thead> </thead>
{#each machine.stack as datum, i} {#each [...machine.stack].reverse() as datum, i}
<tr> <tr>
<td>{datum}</td> <td>{datum}</td>
</tr> </tr>

Loading…
Cancel
Save