Example #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();
        }
Example #2
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();
        }