From b64fd18b610734b41f30886d3fb2522f6e91bfc6 Mon Sep 17 00:00:00 2001 From: sneedgroup-holder Date: Tue, 17 Dec 2024 10:25:23 -0600 Subject: [PATCH] add brainfuck --- jspp.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/jspp.js b/jspp.js index 00407d5..4843c7b 100644 --- a/jspp.js +++ b/jspp.js @@ -11,6 +11,68 @@ class JSPlusPlus { 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) { // Load the model. Users optionally pass in a threshold and an array of // labels to include.