Laden...

Minimalistischer Brainfuck-Interpreter

Erstellt von tscherno vor 16 Jahren Letzter Beitrag vor 16 Jahren 2.802 Views
tscherno Themenstarter:in
630 Beiträge seit 2007
vor 16 Jahren
Minimalistischer Brainfuck-Interpreter

Beschreibung:

Diese Konsolenanwendung interpretiert ein Brainfuck-Programm welches als Parameter übergen werden muss.


/*
 *  bint - Brainfuck Interpreter
 *  Implements wrap-around for cells (if 255 is increased it becomes 0 and vice-versa)
 * 
 */

using System;
using System.IO;

namespace bint
{
    class Program
    {
         const short TAPE_SIZE=30000;

         static string program_code;                    //The Programcode is saved here
         static byte[] tape = new byte[TAPE_SIZE];      //Represents the Memory
         static short ptr = 0;                          //Memorypointer
         static short loop_ptr = 0;                     //looppointer
         

        static void Main(string[] args)
        {
            
            //***Programfile is passed by arg***

            //Check if any arguments where passed at all
            if (args.Length == 0)
            {
                PrintHelp();
                return;
            }
            else
            {
                //Check if passed file exists
                if (!File.Exists(args[0]))
                {
                    PrintHelp();
                    Console.WriteLine();
                    PrintError("IO_ERROR: Passed programfile not found!");
                    return;
                }
                else
                {   //Read passed programfile
                    program_code = File.ReadAllText(args[0]);

                    //Execute Program
                    //ip = instruction pointer
                    for (int ip = 0; ip < program_code.Length; ip++)
                    {
                        switch (program_code[ip])
                        {
                            case '+':  //Increase value at pointer
                                {
                                    //Implement "Wrap-Around"
                                    if (tape[ptr] == byte.MaxValue)
                                    {
                                        tape[ptr] = byte.MinValue;
                                    }
                                    else
                                    {
                                        tape[ptr]++;
                                    }
                                    break;
                                }
                            case '-': //Decrease value at pointer
                                {
                                    //Implement "Wrap-Around"
                                    if (tape[ptr] == byte.MinValue)
                                    {
                                        tape[ptr] = byte.MaxValue;
                                    }
                                    else
                                    {
                                        tape[ptr]--;
                                    }

                                    break;
                                }
                            case '>': //Increase pointer
                                {
                                    if (ptr == TAPE_SIZE)
                                    {
                                        PrintError("RUNTIME_ERROR: End of Tape.");
                                        return;
                                    }
                                    ptr++;
                                    break;
                                }
                            case '<': //Decrease pointer
                                {
                                    if (ptr == 0)
                                    {
                                        PrintError("RUNTIME_ERROR: Pointer was decreased at Position 0.");
                                        return;
                                    }
                                    
                                    ptr--;
                                    break;
                                }
                            case '[': //jump forward to the command after the corresponding ] if the byte at the pointer is zero.
                                {
                                    if (tape[ptr] == 0)
                                    {
                                        ip++;
                                        while (loop_ptr > 0 || program_code[ip] != ']')
                                        {
                                            if (program_code[ip] == '[')
                                            {
                                                loop_ptr++;
                                            }
                                            if (program_code[ip] == ']')
                                            {
                                                loop_ptr--;
                                            }
                                            ip++;
                                        }
                                    }
                                    break;
                                }
                            case ']': 
                                {
                                    ip--;
                                    while (loop_ptr > 0 || program_code[ip] != '[')
                                    {
                                        if (program_code[ip] == ']')
                                        {
                                            loop_ptr++;
                                        }
                                        if (program_code[ip] == '[')
                                        {
                                            loop_ptr--;
                                        }
                                        ip--;
                                    }
                                    ip--;

                                    break;

                                }
                            case '.': //Print value at pointer
                                {
                                  Console.Write(Convert.ToChar(tape[ptr]));
                                  Console.Out.Flush();
                                  break;
                                }
                            case ',': //Read value from Console and store it at pointer
                                {
                                    tape[ptr]=Convert.ToByte(Console.Read());
                                    break;
                                }
                            default:
                                {
                                    //Discard all other characters
                                    break;
                                }
                        }
                    }
                }
            }
            
        }



        static void PrintError(string errorMessage)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(errorMessage);
            Console.ForegroundColor = ConsoleColor.Gray;

        }

        static void PrintHelp()
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Brainfuck Interpreter");
            Console.WriteLine();
            Console.WriteLine("USAGE: bint [Programfile]");
        }
    }
}


Schlagwörter: Brainfuck Interpreter BF

To understand recursion you must first understand recursion

http://www.ilja-neumann.com
C# Gruppe bei last.fm

49.485 Beiträge seit 2005
vor 16 Jahren