예제 #1
0
        public static void Run(string program, Func <int> read, Action <char> write)
        {
            var runner = new ProgramRunner(program);

            runner.RegisterCommand('<', b => b.StatePointer--);
            runner.RegisterCommand('>', b => b.StatePointer++);
            runner.RegisterCommand('+', b => b.State[b.StatePointer]++);
            runner.RegisterCommand('-', b => b.State[b.StatePointer]--);
            runner.RegisterCommand('.', b => write((char)b.State[b.StatePointer]));
            runner.RegisterCommand(',', b => b.State[b.StatePointer] = (byte)read());

            var pairBrackets = FindPairBrackets(program);
            Action <ProgramRunner, Func <bool> > jumpToPairBracket = (b, predicate) =>
            {
                if (predicate())
                {
                    b.ProgramPointer = pairBrackets[b.ProgramPointer];
                }
            };

            runner.RegisterCommand('[', b => jumpToPairBracket(b, () => b.State[b.StatePointer] == 0));
            runner.RegisterCommand(']', b => jumpToPairBracket(b, () => b.State[b.StatePointer] != 0));

            runner.Run();
        }
예제 #2
0
 static void GetConstans(ProgramRunner runner)
 {
     for (char i = '0'; i <= '9'; i++)
     {
         var j = i;
         runner.RegisterCommand(j, b => b.Memory[b.MemoryPointer] = (byte)j);
     }
     for (char i = 'A'; i <= 'Z'; i++)
     {
         var j = i;
         runner.RegisterCommand(j, b => b.Memory[b.MemoryPointer] = (byte)j);
         runner.RegisterCommand(char.ToLower(j), b => b.Memory[b.MemoryPointer] = (byte)char.ToLower(j));
     }
 }
예제 #3
0
        public static void Run(string program, Func <int> read, Action <char> write)
        {
            var runner = new ProgramRunner(program);

            runner.RegisterCommand('<', b =>
            {
                if (b.MemoryPointer == 0)
                {
                    b.MemoryPointer = 29999;
                }
                else
                {
                    b.MemoryPointer--;
                }
            });

            runner.RegisterCommand('>', b =>
            {
                if (b.MemoryPointer == 29999)
                {
                    b.MemoryPointer = 0;
                }
                else
                {
                    b.MemoryPointer++;
                }
            });

            runner.RegisterCommand('+', b => b.Memory[b.MemoryPointer]++);
            runner.RegisterCommand('-', b => b.Memory[b.MemoryPointer]--);
            runner.RegisterCommand('.', b => write((char)b.Memory[b.MemoryPointer]));
            runner.RegisterCommand(',', b => b.Memory[b.MemoryPointer] = (byte)read());

            Cycles(runner);

            GetConstans(runner);

            runner.Run();
        }
예제 #4
0
        static void Cycles(ProgramRunner runner)
        {
            var openBrackets  = new int[runner.Instructions.Length];
            var closeBrackets = new int[runner.Instructions.Length];
            var bracketsStack = new Stack <int>();

            for (int i = 0; i < runner.Instructions.Length; i++)
            {
                if (runner.Instructions[i] == '[')
                {
                    bracketsStack.Push(i);
                }
                if (runner.Instructions[i] == ']')
                {
                    var openBracket = bracketsStack.Pop();
                    closeBrackets[openBracket] = i;
                    openBrackets[i]            = openBracket;
                }
            }

            runner.RegisterCommand('[', b =>
            {
                if (b.Memory[b.MemoryPointer] == 0)
                {
                    b.InstructionPointer = closeBrackets[b.InstructionPointer];
                }
            });

            runner.RegisterCommand(']', b =>
            {
                if (b.Memory[b.MemoryPointer] != 0)
                {
                    b.InstructionPointer = openBrackets[b.InstructionPointer];
                }
            });
        }