Skip to content

Instantly share code, notes, and snippets.

@rekkusu
Created June 21, 2010 13:30
Show Gist options
  • Save rekkusu/446846 to your computer and use it in GitHub Desktop.
Save rekkusu/446846 to your computer and use it in GitHub Desktop.
// バグ有り注意
using System;
using System.Collections.Generic;
namespace Vuvuzela
{
class Program
{
static void Main(string[] args)
{
int length = 1024;
if (args.Length > 0){
try
{
length = int.Parse(args[0]);
if (length < 1024) length = 1024;
}
catch (FormatException) { }
}
int pos = 0;
char[] memory = new char[length];
Console.WriteLine("------------------------------");
Console.WriteLine(" Brainf**k interpreter ");
Console.WriteLine("------------------------------");
while (true)
{
try
{
Console.WriteLine();
Console.Write("> ");
string buffer = Console.ReadLine();
if (buffer.Length == 0) break;
Stack<int> stack = new Stack<int>();
for (int i = 0; i < buffer.Length; i++)
{
char c = buffer[i];
switch (c)
{
case 'ブ': pos++; break;
case 'ボ': pos--; break;
case 'ォ': memory[pos]++; break;
case 'オ': memory[pos]--; break;
case 'ァ': Console.Write(memory[pos]); break;
case 'ア': memory[pos] = Console.ReadKey(true).KeyChar; break;
case 'エ':
if (memory[pos] == 0)
{
for (int nest = 1; i < buffer.Length || nest == 0; i++)
{
if (buffer[i] == '[') nest++;
else if (buffer[i] == ']') nest--;
}
}
else stack.Push(i);
break;
case 'ェ':
if (memory[pos] != 0) i = stack.Pop() - 1;
break;
case 'c':
memory = new char[length];
pos = 0;
break;
}
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment