70 lines
984 B
Go
70 lines
984 B
Go
package parser
|
|
|
|
// TokenType represents the type of a token
|
|
type TokenType int
|
|
|
|
const (
|
|
// Literals
|
|
IDENT TokenType = iota
|
|
NUMBER
|
|
STRING
|
|
|
|
// Operators
|
|
ASSIGN // =
|
|
PLUS // +
|
|
MINUS // -
|
|
STAR // *
|
|
SLASH // /
|
|
|
|
// Delimiters
|
|
LPAREN // (
|
|
RPAREN // )
|
|
|
|
// Keywords
|
|
VAR
|
|
|
|
// Special
|
|
EOF
|
|
ILLEGAL
|
|
)
|
|
|
|
// Token represents a single token
|
|
type Token struct {
|
|
Type TokenType
|
|
Literal string
|
|
Line int
|
|
Column int
|
|
}
|
|
|
|
// Precedence levels for Pratt parsing
|
|
type Precedence int
|
|
|
|
const (
|
|
_ Precedence = iota
|
|
LOWEST
|
|
SUM // +
|
|
PRODUCT // *
|
|
PREFIX // -x, !x
|
|
CALL // function()
|
|
)
|
|
|
|
// precedences maps token types to their precedence levels
|
|
var precedences = map[TokenType]Precedence{
|
|
PLUS: SUM,
|
|
MINUS: SUM,
|
|
SLASH: PRODUCT,
|
|
STAR: PRODUCT,
|
|
}
|
|
|
|
// lookupIdent checks if an identifier is a keyword
|
|
func lookupIdent(ident string) TokenType {
|
|
keywords := map[string]TokenType{
|
|
"var": VAR,
|
|
}
|
|
|
|
if tok, ok := keywords[ident]; ok {
|
|
return tok
|
|
}
|
|
return IDENT
|
|
}
|