A lexer turns raw source text into a stream of tokens - the words of the language. Today you build the lexer's core and teach it the punctuation that is exactly one character, the simplest tokens there are.
Turn a string of single-character symbols into a sequence of typed tokens ending in EOF.
A token is a small tagged piece of the source: a type (what kind of thing it
is) and the literal text it came from. The lexer’s job is to walk the input
character by character and hand back one token each time you ask, so later stages
never touch raw text - they work with a clean stream of PLUS, LPAREN,
SEMICOLON, and so on.
Start with the symbols that are always exactly one character. A switch on the
current character, returning the matching token type, is the whole idea. Reaching
the end of the input is itself a token - EOF - so parsers downstream have a
definite signal to stop rather than reading off the end.
type Token struct { Type string; Literal string }// a lexer walks the input one character at a timefunc (l *Lexer) NextToken() Token {switch l.ch {case '=': return Token{ASSIGN, "="}case '+': return Token{PLUS, "+"}case '-': return Token{MINUS, "-"}// ... one case per symbol (+ - * / < > and the delimiters); 0 means end -> EOF}}