Created
August 4, 2022 20:27
-
-
Save spacebytee/af9e6c6e92c85e7109ba50fb87cecf89 to your computer and use it in GitHub Desktop.
brainfork
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; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
namespace brainfork | |
{ | |
class Program | |
{ | |
static string bfc = "+[----->+++<]>+.+."; | |
static int pos = 0; | |
static int length = 30000; | |
static List<int> values = new List<int>(); | |
static void Main(string[] args) | |
{ | |
TextReader tr = new StreamReader(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\code.bf"); | |
string readBIFC = tr.ReadToEnd(); | |
string readBFC = readBIFC; | |
if (readBFC != null) { bfc = readBFC; } | |
string readLength = tr.ReadLine(); | |
if (readLength != null) { length = int.Parse(readLength); } | |
for (int i = 0; i < length; i++) | |
{ | |
values.Add(0); | |
} | |
string[] code = Regex.Split(bfc, string.Empty); | |
int ci = 0; | |
int ignorenext = 0; | |
for (int i = 0; i <= code.Length; i++) | |
{ | |
string s = code[i]; | |
if (ignorenext == 0) | |
{ | |
ci += 1; | |
switch (s) | |
{ | |
case ">": | |
pos += 1; | |
break; | |
case "<": | |
pos -= 1; | |
break; | |
case "+": | |
values[pos] += 1; | |
if (values[pos] > 127) { values[pos] = -128; } | |
break; | |
case "-": | |
values[pos] -= 1; | |
if (values[pos] < -128) { values[pos] = 127; } | |
break; | |
case ".": | |
Console.Write(Encoding.ASCII.GetString(new byte[] { (byte)(values[pos]) })); | |
break; | |
case ",": | |
values[pos] = (int)Encoding.ASCII.GetBytes(Console.ReadLine().Substring(0, 1))[0]; | |
break; | |
case "[": | |
string lc = ""; | |
int tempi = ci; | |
int length = 0; | |
while (code[tempi] != "]") | |
{ | |
lc += code[tempi]; | |
tempi += 1; | |
length += 1; | |
} | |
ignorenext += length; | |
//int ranpos = pos; | |
while (values[pos] != 0) | |
{ | |
RunBF(lc); | |
} | |
break; | |
} | |
} | |
else | |
{ | |
ignorenext -= 1; | |
} | |
} | |
Console.WriteLine(" "); | |
Console.WriteLine("This Brainfork application has abruptly stopped. This is likely not an error, but if you believe it is, contact the software vendor."); | |
Over(); | |
} | |
static void RunBF(string rbfc) | |
{ | |
string[] code = Regex.Split(rbfc, string.Empty); | |
int ci = 0; | |
foreach (String s in code) | |
{ | |
ci += 1; | |
switch (s) | |
{ | |
case ">": | |
pos += 1; | |
break; | |
case "<": | |
pos -= 1; | |
break; | |
case "+": | |
values[pos] += 1; | |
if (values[pos] > 127) { values[pos] = -128; } | |
break; | |
case "-": | |
values[pos] -= 1; | |
if (values[pos] < -128) { values[pos] = 127; } | |
break; | |
case ".": | |
Console.Write(Encoding.ASCII.GetString(new byte[] { (byte)(values[pos]) })); | |
break; | |
case ",": | |
values[pos] = (int)Encoding.ASCII.GetBytes(Console.ReadLine().Substring(0, 1))[0]; | |
break; | |
case "[": | |
string lc = ""; | |
int tempi = ci; | |
while (code[tempi] != "]") | |
{ | |
lc += code[tempi]; | |
tempi += 1; | |
} | |
//int ranpos = pos; | |
while (values[pos] != 0) | |
{ | |
RunBF(lc); | |
} | |
break; | |
} | |
} | |
} | |
static void Over() | |
{ | |
Console.ReadLine(); | |
Over(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment