hay guyz
I just wrote a horrible
brainfuck interpreter in C#:
/* Quick and Dirty C# Brainfuck interpreter, by Spectere.
*
* Written in Microsoft Visual C# 2008 Express Edition (lawl).
* Designed to target .NET Framework v2.0...should work with v1.0/1.1 without much modification (most likely).
*
* By the way, you probably shouldn't be looking at this terrible code, let alone thinking about compiling it.
* I'm dead serious.
* This isn't robust, so don't do anything silly.
* ...
* Yes, that means you, Bobbias.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace BFSharp {
class Program {
const int MAX_RAM = 10000;
const int MAX_LOOP = 1000;
static void Main(string[] args) {
if(args.Length < 1) {
Console.WriteLine("usage: bfsharp.exe <filename>");
return;
}
// We'll just use a string to hold the entire program...
string bfProgram = null;
// BrainRAM
byte[] ramData = new byte[MAX_RAM];
int ramPtr = 0;
// loop stack
int[] loopPos = new int[MAX_LOOP];
int loopPtr = -1;
int skipNum = 0;
// instruction pointer and current instruction
int bfIP = -1; // since you never know when you're going to run into a 2GB BF program!
char inst = '\0'; // I don't feel like messing with character conversions, so here ya go -- more blatant inefficiency
// file stream GET
try {
FileStream fstream = new FileStream(args[0], FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader sreader = new StreamReader(fstream);
// YA GOTTA OPERATE DA E-Z WAY
bfProgram = sreader.ReadToEnd();
// clean up file handles
sreader.Close();
fstream.Close();
} catch {
Console.WriteLine("Error opening file! :(");
return;
};
// parse dat shit
while(bfIP < bfProgram.Length-1) {
inst = Convert.ToChar(bfProgram.Substring(++bfIP, 1));
switch(inst) {
case '+':
if(ramData[ramPtr]==255) ramData[ramPtr]=0;
else ramData[ramPtr]++;
break;
case '-':
if(ramData[ramPtr]==0) ramData[ramPtr]=255;
else ramData[ramPtr]--;
break;
case '>':
if(++ramPtr==MAX_RAM-1) ramPtr=0;
break;
case '<':
if(--ramPtr==-1) ramPtr=MAX_RAM-1;
break;
case '.':
if(ramData[ramPtr]==10) Console.Write("\n");
else Console.Write((char)ramData[ramPtr]);
break;
case ',':
ramData[ramPtr]=(byte)Console.ReadKey(true).KeyChar;
break;
case '[':
if(ramData[ramPtr]!=0) loopPos[++loopPtr]=bfIP;
else { skipNum++;
while(skipNum>0) {
switch(bfProgram.Substring(++bfIP,1)) {
case "[":
skipNum++;
break;
case "]":
skipNum--;
break;
}
}
}
break;
case ']':
if(ramData[ramPtr]!=0) bfIP=loopPos[loopPtr];
else loopPtr--;
break;
}
}
}
}
}
I consider it proof that you can write some pretty nasty shit in C#. I think my personal favorite statement is this: "
switch(bfProgram.Substring(++bfIP,1))". There's so much wrong with that I don't even know where to begin.
This should work without a hitch (well, aside from the code being a horrible abomination) in Visual Studio 2003 and above. I wrote it with VC# 2008 Express Edition, so it should work on just about anything. It'll probably compile on .NET 1.0/1.1 but I dunno; I don't feel like installing VS.NET 2002 to find out.
But enough about that...enjoy!