public
Last active

West London Hack Night - Reworked Brainfuck interpreter in C#

  • Download Gist
Program.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
namespace BrainFuckContinued
{
using System;
using System.Collections.Generic;
 
class Program
{
static void Main(string[] args)
{
var p = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.";
int ip=-1,dp=0;var f=false;var d=new char[30000];var s=new Stack<int>();
var u=new Dictionary<char,Action>{
{'>',()=>dp++},{'<',()=>dp--},{'+',()=>d[dp]++},{'-',()=>d[dp]--},
{'.',()=>Console.Write(d[dp])},{',',()=>d[dp]=Console.ReadKey().KeyChar},
{'[',()=>{if(d[dp]>0)s.Push(ip);else f=true;}},
{']',()=>{if(d[dp]>0)ip=s.Peek();else s.Pop();}}};
while (ip++<p.Length-1){if(f){f=p[ip]!=']';continue;}u[p[ip]]();}
Console.ReadKey();
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.