Skip to content

Instantly share code, notes, and snippets.

@spacebytee
Created August 4, 2022 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spacebytee/3d75803ec44596d3f5593faf771bd664 to your computer and use it in GitHub Desktop.
Save spacebytee/3d75803ec44596d3f5593faf771bd664 to your computer and use it in GitHub Desktop.
bifuck src
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace bifuck
{
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.biff");
string readBIFC = tr.ReadToEnd();
string readBFC = "";
foreach (string s in readBIFC.Split("."))
{
switch (s.Length)
{
case 1:
readBFC += ">";
break;
case 2:
readBFC += "<";
break;
case 3:
readBFC += "+";
break;
case 4:
readBFC += "-";
break;
case 5:
readBFC += ".";
break;
case 6:
readBFC += ",";
break;
case 7:
readBFC += "[";
break;
case 8:
readBFC += "]";
break;
}
}
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