コード例 #1
0
        private void ExecuteBrainfuck(int[] instructions)
        {
            unchecked
            {
                int    programCounter = 0;
                int    dataPointer    = 0;
                byte[] cells          = new byte[N_CELLS];

                while (programCounter < instructions.Length)
                {
                    int twoProgramCounterPlusOne = programCounter + 1;

                    switch (instructions[programCounter])
                    {
                    case INSTRUCTION_INCREMENT_DATA_POINTER:
                        dataPointer += instructions[twoProgramCounterPlusOne];
                        break;

                    case INSTRUCTION_DECREMENT_DATA_POINTER:
                        dataPointer -= instructions[twoProgramCounterPlusOne];
                        break;

                    case INSTRUCTION_INCREMENT_DATA:
                        cells[dataPointer] += (byte)instructions[twoProgramCounterPlusOne];
                        break;

                    case INSTRUCTION_DECREMENT_DATA:
                        cells[dataPointer] -= (byte)instructions[twoProgramCounterPlusOne];
                        break;

                    case INSTRUCTION_OUTPUT_BYTE:
                        OutputBuffer.WriteByte(cells[dataPointer]);
                        break;

                    case INSTRUCTION_INPUT_BYTE:
                        cells[dataPointer] = (byte)Console.Read();
                        break;

                    case INSTRUCTION_JUMP_FORWARD:
                        if (cells[dataPointer] == 0)
                        {
                            programCounter = instructions[twoProgramCounterPlusOne];
                        }
                        break;

                    case INSTRUCTION_JUMP_BACKWARD:
                        if (cells[dataPointer] != 0)
                        {
                            programCounter = instructions[twoProgramCounterPlusOne];
                        }
                        break;
                    }

                    programCounter += 2;
                }
            }
        }
コード例 #2
0
        private static int RunBrainfuck(string[] args)
        {
            int obIndex = Array.IndexOf(args, "-ob");

            if (args.Length > obIndex + 1)
            {
                int obTime;
                if (int.TryParse(args[obIndex + 1], out obTime))
                {
                    OutputBuffer.Open(obTime);
                }
            }

            Stopwatch time = new Stopwatch();

            try
            {
                using (StreamReader brainfuckReader = new StreamReader(args[0]))
                {
                    string brainfuck = brainfuckReader.ReadToEnd();
                    time.Start();
                    return(CreateBrainfuckRunner(args).Run(brainfuck));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                time.Stop();

                OutputBuffer.Close();
                if (Array.IndexOf(args, "-time") >= 0 || Array.IndexOf(args, "-t") >= 0)
                {
                    Console.WriteLine();
                    Console.WriteLine($"Brainfuck program executed in {time.ElapsedMilliseconds} milliseconds ({time.ElapsedTicks} ticks)");
                }
            }
            return(1);
        }
コード例 #3
0
        private static void ExecuteBrainfuck(string brainfuck)
        {
            byte[] cells          = new byte[N_CELLS];
            int    programCounter = 0;
            int    cellPointer    = 0;

            unchecked
            {
                int programLength = brainfuck.Length;

                while (programCounter < programLength)
                {
                    switch (brainfuck[programCounter])
                    {
                    case '>':
                        cellPointer++;
                        break;

                    case '<':
                        cellPointer--;
                        break;

                    case '+':
                        cells[cellPointer]++;
                        break;

                    case '-':
                        cells[cellPointer]--;
                        break;

                    case '.':
                        OutputBuffer.WriteByte(cells[cellPointer]);
                        break;

                    case ',':
                        cells[cellPointer] = (byte)Console.Read();
                        break;

                    case '[':

                        if (cells[cellPointer] == 0)
                        {
                            int bracketPosition = programCounter;
                            int nBrackets       = 1;
                            while (nBrackets > 0)
                            {
                                bracketPosition++;

                                if (brainfuck[bracketPosition] == '[')
                                {
                                    nBrackets++;
                                }
                                if (brainfuck[bracketPosition] == ']')
                                {
                                    nBrackets--;
                                }
                            }
                            programCounter = bracketPosition;
                        }
                        break;

                    case ']':
                        if (cells[cellPointer] != 0)
                        {
                            int bracketPosition = programCounter;
                            int nBrackets       = 1;
                            while (nBrackets > 0)
                            {
                                bracketPosition--;

                                if (brainfuck[bracketPosition] == ']')
                                {
                                    nBrackets++;
                                }
                                if (brainfuck[bracketPosition] == '[')
                                {
                                    nBrackets--;
                                }
                            }
                            programCounter = bracketPosition;
                        }
                        break;
                    }

                    programCounter++;
                }
            }
        }