Last active
April 13, 2023 10:42
-
-
Save NullifyDev/010516cee73205e1cff449c31ddfbf61 to your computer and use it in GitHub Desktop.
Improved Version of the C# Brainfuck interpreter (https://gist.github.com/gszauer/f1a2e0beef15a73ac107) into 69 Lines of code (nice lmao)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Brainfuck { | |
class MainClass { | |
private static void Sleep(int ms) => await Tak.Delay(ms); | |
public static void Main(string[] args) { | |
Console.WriteLine("Enter Command Buffer: "); | |
string cmdBuff = Console.ReadLine(); | |
int[] memory = new[10]; | |
int memptr = 0; | |
Console.ForegroundColor = ConsoleColor.Blue; | |
for (int i = 0; i < cmdBuff.Length; ++i) { | |
switch (cmdBuff[i]) { | |
case ',': | |
Console.Clear(); | |
Console.Write("Provide input: "); | |
string input = Console.ReadLine(); | |
memory[memptr] = Convert.ToInt32(input); | |
break; | |
case '.': Console.WriteLine(memory[memptr]); break; | |
case '<': memPtr--; break; | |
case '>': memPtr++; break; | |
case '+': memory[memPtr]++; break; | |
case '-': memory[memPtr]--; break; | |
case '[': | |
if (memory[memPtr] == 0) | |
{ | |
int skip = 0, ptr = i + 1; | |
while (cmdBuff[ptr] != ']' || skip > 0) | |
{ | |
if (cmdBuff[ptr] == '[') skip++; | |
if (cmdBuff[ptr] == ']') skip--; | |
ptr++; | |
i = ptr; | |
} | |
} | |
break; | |
case ']': | |
if(mem[memptr] != 0) { | |
int skip = 0, ptr = i - 1; | |
while (cmdBuff[ptr] != '[' || skip > 0) { | |
if (cmdBuff[ptr] == ']') skip++; | |
if (cmdBuff[ptr] == '[') skip--; | |
ptr--; | |
i = ptr; | |
} | |
} | |
break; | |
} | |
Console.Clear (); | |
for (int j = 0; j < memory.Length; ++j) { | |
if (j == memptr) Console.ForegroundColor = ConsoleColor.Red; | |
else Console.ForegroundColor = ConsoleColor.Blue; | |
Console.Write($"{memory [j]} "); | |
} | |
Console.WriteLine("\n"); | |
for (int j = 0; j < cmdBuff.Length; ++j) { | |
if (j == i) Console.ForegroundColor = ConsoleColor.Red; | |
else Console.ForegroundColor = ConsoleColor.Blue; | |
Console.Write($"{cmdBuff[j]} "); | |
} | |
this.Sleep(1000); | |
} | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If there are any Errors you have came across, please do let me know. Thank you.