add brainfuck

This commit is contained in:
The Ghost of FOSS' Past 2024-12-17 10:25:23 -06:00
parent 6626f8b124
commit b64fd18b61

62
jspp.js
View file

@ -11,6 +11,68 @@ class JSPlusPlus {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
interpretBrainfuck(code) {
const memory = new Array(30000).fill(0); // Memory tape of 30,000 cells, initialized to 0
let pointer = 0; // Data pointer
let codePointer = 0; // Code pointer
const output = [];
// Input handling: A simple function to get one character from the user
function getInput() {
const input = prompt("Enter a character (for Brainfuck input):");
return input ? input.charCodeAt(0) : 0; // Returns the ASCII value of the character, or 0 if cancelled
}
while (codePointer < code.length) {
const command = code[codePointer];
switch (command) {
case '>':
pointer++;
break;
case '<':
pointer--;
break;
case '+':
memory[pointer]++;
break;
case '-':
memory[pointer]--;
break;
case '.':
output.push(String.fromCharCode(memory[pointer])); // Output the character at the current pointer
break;
case ',':
memory[pointer] = getInput(); // Read input from the user and store it in memory[pointer]
break;
case '[':
if (memory[pointer] === 0) {
let openBrackets = 1;
while (openBrackets !== 0) {
codePointer++;
if (code[codePointer] === '[') openBrackets++;
if (code[codePointer] === ']') openBrackets--;
}
}
break;
case ']':
if (memory[pointer] !== 0) {
let closeBrackets = 1;
while (closeBrackets !== 0) {
codePointer--;
if (code[codePointer] === '[') closeBrackets--;
if (code[codePointer] === ']') closeBrackets++;
}
}
break;
}
codePointer++;
}
return output.join('');
}
async isToxic(sentences) { async isToxic(sentences) {
// Load the model. Users optionally pass in a threshold and an array of // Load the model. Users optionally pass in a threshold and an array of
// labels to include. // labels to include.