Beispiel #1
0
        public byte GetSumFromSeries(CharReader cr, Char lookupChar)
        {
            _hits++;
            var startPosition = cr.Position;
            var cachedCount   = _seriesCache[startPosition];

            if (cachedCount != 0)
            {
                cr.Position += cachedCount - 1;
                if (cachedCount > 10)
                {
                    _cacheHits++;
                }
                return(cachedCount);
            }

            byte count = 1;

            cr.Forward();
            while (cr.GetChar() == lookupChar)
            {
                count += 1; cr.Forward();
            }
            cr.Back();

            _seriesCache[startPosition] = count;

            return(count);
        }
        private int CountSeries(CharReader cr)
        {
            var count       = 0;
            var currentChar = cr.GetChar();

            while (cr.HasCharacters() && cr.GetChar() == currentChar)
            {
                count++;
                cr.Forward();
            }
            cr.Back();
            return(count);
        }
        private Instruction GetComboInstruction(CharReader cr)
        {
            var newInstruction = new Instruction(InstructionType.Combo);

            while (cr.HasCharacters())
            {
                var c = cr.GetChar();

                if (c == '[' || c == ']' || c == '.' || c == ',')
                {
                    break;
                }

                if (c == '+')
                {
                    newInstruction.Add();
                }
                if (c == '-')
                {
                    newInstruction.Subtract();
                }
                if (c == '<')
                {
                    newInstruction.ShiftLeft();
                }
                if (c == '>')
                {
                    newInstruction.ShiftRight();
                }

                cr.Forward();
            }

            cr.Back();

            newInstruction.Complete();

            return(newInstruction);
        }
        public void Run(FileStream source)
        {
            var pointer = 0;
            var memory  = new byte[1024 * 1024];
            var cr      = new CharReader(source);

            while (cr.HasCharacters())
            {
                var c = cr.GetChar();
                switch (c)
                {
                case '>':
                    pointer++;
                    break;

                case '<':
                    pointer--;
                    break;

                case '+':
                    memory[pointer]++;
                    break;

                case '-':
                    memory[pointer]--;
                    break;

                case '.':
                    Console.Write((char)memory[pointer]);
                    break;

                case ',':
                    var newChar = Console.Read();
                    memory[pointer] = (byte)newChar;
                    break;

                case '[':
                    var nesting = 0;
                    if (memory[pointer] == 0)
                    {
                        while (cr.HasCharacters())
                        {
                            cr.Forward();
                            var tempChar = cr.GetChar();
                            if (tempChar == ']' && nesting == 0)
                            {
                                break;
                            }
                            if (tempChar == '[')
                            {
                                nesting++;
                            }
                            if (tempChar == ']')
                            {
                                nesting--;
                            }
                        }
                    }
                    break;

                case ']':
                    nesting = 0;
                    if (memory[pointer] != 0)
                    {
                        while (true)
                        {
                            cr.Back();
                            var tempChar = cr.GetChar();
                            if (tempChar == '[' && nesting == 0)
                            {
                                cr.GetChar(); break;
                            }
                            if (tempChar == ']')
                            {
                                nesting++;
                            }
                            if (tempChar == '[')
                            {
                                nesting--;
                            }
                        }
                    }
                    break;
                }
                cr.Forward();
            }
        }