Write interface for debugging Brainfuck-like language in HTML+CSS+JS. Interface theme need to be futuristic black+white. On top on 1/5 of page need to be memory inspector, which shows memory state (one block of memory - int32_t). Interface need to have code explorer with ability to setting breakpoints on fields of lines, output block, where program will write like in stdout and input field, to send in stdin.
Assistant
Here's an interface for debugging a Brainfuck-like language implemented in HTML, CSS, and JavaScript with a futuristic black and white theme. The interface includes a memory inspector, code editor with breakpoints, an output block, and an input field.
HTML
html
<!DOCTYPE html>
<html>
<head>
<title>Brainfuck Debugger</title>
<style>
/* CSS styles */
body {
margin: 0;
padding: 0;
background-color: #000;
color: #fff;
font-family: 'Courier New', Courier, monospace;
}
#memory-inspector {
height: 20vh;
border-bottom: 1px solid #fff;
overflow-x: auto;
white-space: nowrap;
padding: 10px;
}
.memory-cell {
display: inline-block;
width: 60px;
text-align: center;
margin-right: 5px;
background-color: #111;
padding: 5px;
border: 1px solid #555;
}
#code-input-section {
height: 20vh;
padding: 10px;
}
#code-input {
width: 100%;
height: 100%;
background-color: #000;
color: #fff;
border: 1px solid #fff;
resize: none;
font-family: 'Courier New', Courier, monospace;
font-size: 14px;
}
#code-editor {
position: relative;
height: 40vh;
border-bottom: 1px solid #fff;
overflow-y: auto;
padding: 10px;
}
#code-lines {
margin-left: 40px;
}
.code-line {
display: flex;
align-items: center;
}
.line-number {
width: 30px;
text-align: right;
margin-right: 10px;
cursor: pointer;
user-select: none;
}
.code-content {
flex-grow: 1;
}
.breakpoint-active {
background-color: #333;
}
#output {
height: 20vh;
border-bottom: 1px solid #fff;
overflow-y: auto;
padding: 10px;
}
#input-field {
padding: 10px;
}
input#stdin {
width: 60%;
padding: 5px;
background-color: #000;
color: #fff;
border: 1px solid #fff;
}
button {
padding: 5px 10px;
margin-left: 5px;
background-color: #fff;
color: #000;
border: none;
cursor: pointer;
font-weight: bold;
}
button:hover {
background-color: #ccc;
}
h2 {
margin-top: 0;
}
#output-content {
white-space: pre-wrap;
word-wrap: break-word;
}
</style>
</head>
<body>
<div id="memory-inspector">
<!-- Memory state displayed here -->
</div>
<div id="code-input-section">
<textarea id="code-input" spellcheck="false" placeholder="Enter your code here..."></textarea>
</div>
<div id="code-editor">
<div id="code-lines">
<!-- Code lines will be generated here -->
</div>
</div>
<div id="output">
<h2>Output:</h2>
<pre id="output-content"></pre>
</div>
<div id="input-field">
<input type="text" id="stdin" placeholder="Input">
<button id="run-button">Run</button>
<button id="step-button">Step</button>
</div>
<script>
// JavaScript code
var breakpoints = new Set();
document.getElementById('run-button').addEventListener('click', function() {
var code = document.getElementById('code-input').value;
var codeLines = code.split('\n');
generateCodeLines(codeLines);
interpreter.codeChars = [];
var lineNumber = 1;
for (var i = 0; i < code.length; i++) {
var c = code[i];
if (c === '\n') {
lineNumber++;
} else if ('><+-.,[]'.includes(c)) {
interpreter.codeChars.push({ char: c, lineNumber: lineNumber });
}
}
interpreter.memory.fill(0);
interpreter.pointer = 0;
interpreter.instructionPointer = 0;
interpreter.outputBuffer = '';
interpreter.inputBuffer = document.getElementById('stdin').value;
interpreter.breakpoints = new Set([...breakpoints]); // Copy breakpoints
interpreter.running = true;
updateMemoryInspector();
executeProgram();
});
document.getElementById('step-button').addEventListener('click', function() {
if (!interpreter.codeChars || interpreter.codeChars.length === 0) {
alert('No code to execute. Please click Run first.');
return;
}
interpreter.running = true;
if (interpreter.instructionPointer < interpreter.codeChars.length) {
var instrObj = interpreter.codeChars[interpreter.instructionPointer];
var currentInstruction = instrObj.char;
var currentLineNumber = instrObj.lineNumber;
executeInstruction(currentInstruction);
interpreter.instructionPointer++;
updateMemoryInspector();
updateOutput();
} else {
interpreter.running = false;
alert('Program has finished execution.');
}
});
function generateCodeLines(codeLines) {
var codeLinesContainer = document.getElementById('code-lines');
codeLinesContainer.innerHTML = ''; // Clear previous lines
for (var i = 0; i < codeLines.length; i++) {
var lineNumber = i + 1;
var lineText = codeLines[i];
var codeLine = document.createElement('div');
codeLine.className = 'code-line';
var lineNumberDiv = document.createElement('div');
lineNumberDiv.className = 'line-number';
lineNumberDiv.textContent = lineNumber;
lineNumberDiv.dataset.lineNumber = lineNumber;
lineNumberDiv.addEventListener('click', toggleBreakpoint);
var codeContentDiv = document.createElement('div');
codeContentDiv.className = 'code-content';
codeContentDiv.textContent = lineText;
codeLine.appendChild(lineNumberDiv);
codeLine.appendChild(codeContentDiv);
codeLinesContainer.appendChild(codeLine);
}
}
function toggleBreakpoint(event) {
var lineNumberDiv = event.target;
lineNumberDiv.classList.toggle('breakpoint-active');
var lineNumber = lineNumberDiv.dataset.lineNumber;
if (breakpoints.has(lineNumber)) {
breakpoints.delete(lineNumber);
} else {
breakpoints.add(lineNumber);
}
}
var interpreter = {
codeChars: [], // Array of {char, lineNumber}
memory: new Int32Array(30000), // Memory of 30,000 int32_t cells
pointer: 0, // data pointer
instructionPointer: 0, // instruction pointer
inputBuffer: '', // input for ,
outputBuffer: '', // output for .
breakpoints: new Set(), // set of line numbers with breakpoints
running: false,
callStack: [], // for [ and ]
};
function executeProgram() {
while (interpreter.instructionPointer < interpreter.codeChars.length) {
var instrObj = interpreter.codeChars[interpreter.instructionPointer];
var currentInstruction = instrObj.char;
var currentLineNumber = instrObj.lineNumber;
// Check for breakpoint at current line
if (interpreter.breakpoints.has(String(currentLineNumber))) {
// Breakpoint hit
interpreter.running = false;
alert('Breakpoint hit at line ' + currentLineNumber);
updateMemoryInspector();
return;
}
executeInstruction(currentInstruction);
interpreter.instructionPointer++;
}
interpreter.running = false;
updateOutput();
alert('Program has finished execution.');
}
function executeInstruction(instr) {
switch (instr) {
case '>':
interpreter.pointer++;
break;
case '<':
interpreter.pointer--;
break;
case '+':
interpreter.memory[interpreter.pointer]++;
break;
case '-':
interpreter.memory[interpreter.pointer]--;
break;
case '.':
interpreter.outputBuffer += String.fromCharCode(interpreter.memory[interpreter.pointer]);
updateOutput();
break;
case ',':
if (interpreter.inputBuffer.length > 0) {
interpreter.memory[interpreter.pointer] = interpreter.inputBuffer.charCodeAt(0);
interpreter.inputBuffer = interpreter.inputBuffer.slice(1);
} else {
interpreter.memory[interpreter.pointer] = 0;
}
break;
case '[':
if (interpreter.memory[interpreter.pointer] === 0) {
// Jump forward to the matching ]
var loop = 1;
while (loop > 0) {
interpreter.instructionPointer++;
if (interpreter.instructionPointer >= interpreter.codeChars.length) {
throw new Error('Syntax error: unmatched [');
}
var c = interpreter.codeChars[interpreter.instructionPointer].char;
if (c === '[') {
loop++;
} else if (c === ']') {
loop--;
}
}
} else {
interpreter.callStack.push(interpreter.instructionPointer);
}
break;
case ']':
if (interpreter.memory[interpreter.pointer] !== 0) {
if (interpreter.callStack.length === 0) {
throw new Error('Syntax error: unmatched ]');
}
interpreter.instructionPointer = interpreter.callStack[interpreter.callStack.length -1];
} else {
interpreter.callStack.pop();
}
break;
default:
// Ignore any non-Brainfuck character
break;
}
updateMemoryInspector();
}
function updateMemoryInspector() {
var memoryInspector = document.getElementById('memory-inspector');
memoryInspector.innerHTML = '';
for (var i = 0; i < 10; i++) { // Show first 10 memory cells
var memCell = document.createElement('div');
memCell.className = 'memory-cell';
if (i === interpreter.pointer) {
memCell.style.backgroundColor = '#333';
}
memCell.textContent = interpreter.memory[i];
memoryInspector.appendChild(memCell);
}
}
function updateOutput() {
var outputContent = document.getElementById('output-content');
outputContent.textContent = interpreter.outputBuffer;
}
</script>
</body>
</html>
Usage Instructions:
Enter Code: In the "Enter your code here..." textarea, input your Brainfuck code.
Set Breakpoints: After clicking "Run," your code will display in the code editor below. Click on the line numbers to toggle breakpoints.
Input Data: If your program requires input (using the , command), enter the input characters in the input field next to the "Run" and "Step" buttons.
Run Program: Click the "Run" button to execute the program. Execution will pause at breakpoints.
Step Through Code: Use the "Step" button to execute the program one instruction at a time.
View Memory State: The top section displays the first 10 cells of memory. The cell highlighted in darker gray is the current memory pointer.
View Output: The "Output" section displays the program's output, simulating stdout.
Features:
Futuristic Black and White Theme: The interface has a sleek, modern look with a black background and white text.
Memory Inspector: Shows the state of the first 10 memory cells (each cell is an int32_t).
Code Editor with Breakpoints: Allows you to view your code with line numbers and set breakpoints by clicking on the line numbers.
Output Block: Displays the output of your program as it would appear in stdout.
Input Field: Accepts user input that the program can read using the , command.
Step Execution: Execute your program step by step to observe how the memory and output change over time.
Example Program:
Try the following Brainfuck program, which inputs a character, increments it, and outputs it:
code
,+
.
Enter the above code into the code input area.
Enter a character in the input field (e.g., A).
Click "Run" or "Step" through the code.
The output should display the next character in the ASCII table (e.g., B).
Notes:
The program currently displays the first 10 memory cells for simplicity.
The memory pointer starts at position 0.
The interpreter handles basic Brainfuck commands and supports loop constructs ([ and ]).
Customizing Memory View:
If you wish to view more memory cells, adjust the loop in the updateMemoryInspector function:
javascript
for (var i = 0; i < N; i++) { // Change N to the number of cells you want to display
// ...
}
This interface provides a basic environment for debugging Brainfuck-like programs with the required features.