public decimal Parse(string input) { Lexer lexer = new Lexer(this, input); lexer.Run(); flushWordNumBuf(); while (stack.Count != 0) { postfix.Add(stack.Pop()); } return evaluate(); }
static State lexWhitespace(Lexer l) { switch (getType(l.read())) { case CharType.Whitespace: l.discard(); return lexWhitespace; case CharType.OpenParanthesis: l.next(); l.emit(Token.OpenParanthesis); l.reset(); return lexWhitespace; case CharType.CloseParanthesis: l.next(); l.emit(Token.CloseParanthesis); l.reset(); return lexWhitespace; case CharType.Number: l.reset(); return lexNumber; case CharType.Operator: l.reset(); return lexOperator; case CharType.Letter: l.reset(); return lexWord; case CharType.EOF: return null; default: throw new Exception("Invalid input."); } }
static State lexWord(Lexer l) { switch (getType(l.read())) { case CharType.Letter: l.next(); return lexWord; case CharType.EOF: l.emit(Token.Word); return null; default: l.emit(Token.Word); l.reset(); return lexWhitespace; } }
static State lexOperator(Lexer l) { switch (getType(l.read())) { case CharType.Operator: l.next(); return lexOperator; case CharType.EOF: l.emit(Token.Operator); return null; default: l.emit(Token.Operator); l.reset(); return lexWhitespace; } }