public IParseNode Parse(TokenWalker walker) { Debug.Assert(TermParser != null); IParseNode result; if (walker.NextIs <MinusToken>()) { var lexem = walker.Pop(); result = new UnaryMinusOperator(TermParser.Parse(walker), lexem.Position); } else { result = TermParser.Parse(walker); } while (walker.NextIsLineOperator()) { var lexem = walker.Pop(); result = lexem.Token switch { MinusToken _ => new MinusOperator(result, TermParser.Parse(walker), lexem.Position), PlusToken _ => new PlusOperator(result, TermParser.Parse(walker), lexem.Position), _ => result }; } return(result); } }
public void AfterGettingLastThereAreNoMoreIsSet() { Token firstToken = new MinusToken(); var tokens = new List <Token>() { firstToken }; var walker = new TokensWalker(tokens); walker.GetNext(); Assert.IsFalse(walker.ThereAreMoreTokens); }
public void CanGetExisting() { Token firstToken = new MinusToken(); var tokens = new List <Token>() { firstToken }; var walker = new TokensWalker(tokens); Assert.IsTrue(walker.ThereAreMoreTokens); var first = walker.GetNext(); Assert.AreEqual(firstToken, first); }
public IEnumerable <BrainfuckToken> Parse(TextReader streamReader) { var ret = new List <BrainfuckToken>(); var leftBracketStack = new Stack <LeftBracketToken>(); BrainfuckToken prev = null; var index = -1; while (0 <= streamReader.Peek()) { ++index; var c = (char)streamReader.Read(); BrainfuckToken current = null; switch (c) { case '>': current = new RightShiftToken(); break; case '<': current = new LeftShiftToken(); break; case '+': current = new PlusToken(); break; case '-': current = new MinusToken(); break; case '.': current = new DotToken(); break; case ',': current = new SemicolonToken(); break; case '[': { var leftBracket = new LeftBracketToken(); leftBracketStack.Push(leftBracket); current = leftBracket; } break; case ']': { if (leftBracketStack.Count == 0) { throw new InvalidOperationException(); } var leftBracket = leftBracketStack.Pop(); var rightBracket = new RightBracketToken(); leftBracket.RightBracketToken = rightBracket; rightBracket.LeftBracketToken = leftBracket; current = rightBracket; } break; } if (current == null) { continue; } current.Index = index; prev?.SetNextIndexToken(current); prev = current; ret.Add(current); } if (leftBracketStack.Count != 0) { throw new InvalidOperationException(); } return(ret); }
public BrainfuckTokenSequence Parse(TextReader streamReader, BrainfuckMemoryTape memoryTape = null) { memoryTape = memoryTape ?? new BrainfuckMemoryTape(); var leftBracketStack = new Stack <LeftBracketToken>(); BrainfuckToken first = null; BrainfuckToken prev = null; var index = -1; while (0 <= streamReader.Peek()) { ++index; var c = (char)streamReader.Read(); BrainfuckToken current = null; switch (c) { case '>': current = new RightShiftToken(); break; case '<': current = new LeftShiftToken(); break; case '+': current = new PlusToken(); break; case '-': current = new MinusToken(); break; case '.': current = new DotToken(); break; case ',': current = new SemicolonToken(); break; case '[': { var leftBracket = new LeftBracketToken(); leftBracketStack.Push(leftBracket); current = leftBracket; } break; case ']': { if (leftBracketStack.Count == 0) { throw new InvalidOperationException(); } var leftBracket = leftBracketStack.Pop(); var rightBracket = new RightBracketToken(); leftBracket.RightBracketToken = rightBracket; rightBracket.LeftBracketToken = leftBracket; current = rightBracket; } break; default: break; } if (current == null) { continue; } current.MemoryTape = memoryTape; current.Index = index; if (prev != null && prev.Next == null) { prev.SetNextIndexToken(current); } prev = current; if (first != null) { continue; } first = current; } if (leftBracketStack.Count != 0) { throw new InvalidOperationException(); } return(new BrainfuckTokenSequence(first)); }