Example #1
0
        static void Execute(String source)
        {
            ArrayList src        = Parse(source);
            const int memorySize = 3000;

            char[] memory = new char[memorySize];
            for (int i = 0; i < memorySize - 1; i++)
            {
                memory[i] = '\0';
            }

            int   memoryPointer = 0;
            int   codePointer   = 0;
            Stack braces        = new Stack();

            while (codePointer < src.Count)
            {
                BrainFuck token = (BrainFuck)src[codePointer];
                switch (token)
                {
                case BrainFuck.INC:
                    memory[memoryPointer]++;
                    break;

                case BrainFuck.DEC:
                    memory[memoryPointer]--;
                    break;

                case BrainFuck.SLF:
                    memoryPointer--;
                    break;

                case BrainFuck.SRT:
                    memoryPointer++;
                    if (memoryPointer > memorySize)
                    {
                        memoryPointer = 0;
                    }

                    break;

                case BrainFuck.IN:
                    memory[memoryPointer] = Input();
                    break;

                case BrainFuck.OUT:
                    Console.WriteLine((int)memory[memoryPointer]);
                    break;

                case BrainFuck.SJP:
                    braces.Push(codePointer - 1);
                    break;

                case BrainFuck.JNZ:
                    if (memory[memoryPointer] != 0)
                    {
                        codePointer = (int)braces.Pop();
                    }
                    else
                    {
                        braces.Pop();
                    }
                    break;
                }

                codePointer++;
            }
        }
Example #2
0
        static void Execute(String source, String inputs)
        {
            ArrayList src          = Parse(source);
            ArrayList input        = ParseCSV(inputs);
            int       inputPointer = 0;
            const int memorySize   = 3000;

            char[] memory = new char[memorySize];
            for (int i = 0; i < memorySize - 1; i++)
            {
                memory[i] = '\0';
            }

            int   memoryPointer = 0;
            int   codePointer   = 0;
            Stack braces        = new Stack();

            while (codePointer < src.Count)
            {
                BrainFuck token = (BrainFuck)src[codePointer];
                switch (token)
                {
                case BrainFuck.INC:
                    memory[memoryPointer]++;
                    break;

                case BrainFuck.DEC:
                    memory[memoryPointer]--;
                    break;

                case BrainFuck.SLF:
                    memoryPointer--;
                    break;

                case BrainFuck.SRT:
                    memoryPointer++;
                    if (memoryPointer > memorySize)
                    {
                        memoryPointer = 0;
                    }

                    break;

                case BrainFuck.IN:
                    try
                    {
                        memory[memoryPointer] = (char)input[inputPointer];
                        Console.WriteLine(input[0]);
                        inputPointer++;
                    }
                    catch (Exception exception)
                    {
                        Console.Error.Write("Error: Not enough inputs provided in csv, falling back to on-demand input");
                        memory[memoryPointer] = Input();
                    }
                    break;

                case BrainFuck.OUT:
                    Console.WriteLine((int)memory[memoryPointer]);
                    break;

                case BrainFuck.SJP:
                    braces.Push(codePointer - 1);
                    break;

                case BrainFuck.JNZ:
                    if (memory[memoryPointer] != 0)
                    {
                        codePointer = (int)braces.Pop();
                    }
                    else
                    {
                        braces.Pop();
                    }
                    break;
                }

                codePointer++;
            }
        }