Skip to content

Commit 43184f5

Browse files
Show output live in brainfuck runner (#107)
- stream output directly to the UI on each `.` instruction without buffering or yielding ------ [Codex Task](https://chatgpt.com/codex/tasks/task_e_69596224fb948325afa60ca13ed7086d)
1 parent 15f7da7 commit 43184f5

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

brainfuck-interpreter.html

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ <h2 style="margin: 0;">Fail-safe settings</h2>
417417
return settings;
418418
}
419419

420-
function runProgram(program, inputs, failsafeSettings) {
420+
function runProgram(program, inputs, failsafeSettings, onOutput) {
421421
const commands = program.replace(/[^\>\<\+\-\.\,\[\]]/g, '');
422422
const jumpMap = buildJumpMap(commands);
423423

@@ -462,6 +462,9 @@ <h2 style="margin: 0;">Fail-safe settings</h2>
462462
break;
463463
case '.':
464464
output.push(tape[pointer]);
465+
if (onOutput) {
466+
onOutput(tape[pointer], output);
467+
}
465468
break;
466469
case ',':
467470
if (inputIndex < inputs.length) {
@@ -495,6 +498,11 @@ <h2 style="margin: 0;">Fail-safe settings</h2>
495498
outputBytes.textContent = bytes.length ? bytes.join(', ') : '(no output)';
496499
}
497500

501+
function clearOutputs() {
502+
outputAscii.textContent = '(no output)';
503+
outputBytes.textContent = '(no output)';
504+
}
505+
498506
function updateInputHelp() {
499507
if (asciiModeRadio.checked) {
500508
inputHelp.textContent = 'ASCII mode sends each character, including new lines, as its byte value.';
@@ -510,7 +518,15 @@ <h2 style="margin: 0;">Fail-safe settings</h2>
510518
const mode = asciiModeRadio.checked ? 'ascii' : 'numbers';
511519
const inputs = parseInputs(bfInput.value, mode);
512520
const program = editor.getValue();
513-
const output = runProgram(program, inputs, collectFailsafeSettingsFromInputs());
521+
clearOutputs();
522+
523+
const liveOutput = [];
524+
const onOutput = (byte) => {
525+
liveOutput.push(clampByte(byte));
526+
renderOutput(liveOutput);
527+
};
528+
529+
const output = runProgram(program, inputs, collectFailsafeSettingsFromInputs(), onOutput);
514530
renderOutput(output);
515531
updateStatus(`Program ran successfully. Output length: ${output.length} byte${output.length === 1 ? '' : 's'}.`);
516532
} catch (error) {

0 commit comments

Comments
 (0)