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); }
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 Instruction[] BuildInstructions(Stream source) { var instructions = new List <Instruction>(); var reader = new CharReader(source); var jumpTable = new Stack <int>(); while (reader.HasCharacters()) { if (IsSetToZero(reader)) { instructions.Add(new Instruction(InstructionType.SetToZero)); } var c = reader.GetChar(); switch (c) { case '+': instructions.Add(GetComboInstruction(reader)); break; case '-': instructions.Add(GetComboInstruction(reader)); break; case '>': instructions.Add(GetComboInstruction(reader)); break; case '<': instructions.Add(GetComboInstruction(reader)); break; case '.': instructions.Add(new Instruction(InstructionType.Print)); break; case ',': instructions.Add(new Instruction(InstructionType.Read)); break; case '[': instructions.Add(new Instruction(InstructionType.BeginLoop)); jumpTable.Push(instructions.Count - 1); break; case ']': var beginPosition = jumpTable.Pop(); var beginInstruction = instructions[beginPosition]; instructions.Add(new Instruction(InstructionType.EndLoop, beginPosition)); beginInstruction.Parameter = instructions.Count - 1; break; } reader.Forward(); } return(instructions.ToArray()); }
private byte[] BuildSeriesCache(CharReader cr) { var result = new byte[cr.Length]; var previous = (char)0; byte sameCount = 0; while (cr.HasCharacters()) { var currentCharacter = cr.GetChar(); if (currentCharacter == '+' || currentCharacter == '-' || currentCharacter == '[' || currentCharacter == ']' || currentCharacter == '<' || currentCharacter == '>' || currentCharacter == '.' || currentCharacter == ',') { if (previous == 0) { previous = currentCharacter; sameCount = 1; cr.Forward(); continue; } if (previous == currentCharacter) { sameCount++; } else { result[cr.Position - sameCount] = sameCount; sameCount = 1; previous = currentCharacter; } } cr.Forward(); } cr.Position = 0; return(result); }
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); _seriesCache = new byte[cr.Length]; var jumpTable = new Stack <int>(); while (cr.HasCharacters()) { var c = cr.GetChar(); switch (c) { case '>': pointer++; break; case '<': pointer--; break; case '+': memory[pointer] += GetSumFromSeries(cr, '+'); break; case '-': memory[pointer] -= GetSumFromSeries(cr, '-'); 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--; } } } else { jumpTable.Push(cr.Position); } break; case ']': nesting = 0; if (memory[pointer] != 0) { cr.Position = jumpTable.Peek(); } else { jumpTable.Pop(); } break; } cr.Forward(); } Console.WriteLine("Cache hits: " + _cacheHits); Console.WriteLine("Cache misses: " + (_hits - _cacheHits)); }
public void Run(FileStream source) { var pointer = 0; var memory = new byte[1024 * 1024]; var cr = new CharReader(source); _jumpTableForward = new int[cr.Length]; var jumpTable = new Stack <int>(); 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) { var startPosition = cr.Position; if (_jumpTableForward[startPosition] > 0) { cr.Position = _jumpTableForward[startPosition] - 1; } else { while (cr.HasCharacters()) { cr.Forward(); var tempChar = cr.GetChar(); if (tempChar == ']' && nesting == 0) { _jumpTableForward[startPosition] = cr.Position + 1; break; } if (tempChar == '[') { nesting++; } if (tempChar == ']') { nesting--; } } } } else { jumpTable.Push(cr.Position); } break; case ']': nesting = 0; if (memory[pointer] != 0) { cr.Position = jumpTable.Peek(); } else { jumpTable.Pop(); } break; } cr.Forward(); } }
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(); } }