Mako/parser/token.go
2025-06-10 09:46:17 -05:00

89 lines
1.2 KiB
Go

package parser
// TokenType represents the type of a token
type TokenType int
const (
// Literals
IDENT TokenType = iota
NUMBER
STRING
TRUE
FALSE
NIL
// Operators
ASSIGN // =
PLUS // +
MINUS // -
STAR // *
SLASH // /
// Delimiters
LPAREN // (
RPAREN // )
LBRACE // {
RBRACE // }
COMMA // ,
// Keywords
VAR
IF
THEN
ELSEIF
ELSE
END
// 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,
"true": TRUE,
"false": FALSE,
"nil": NIL,
"if": IF,
"then": THEN,
"elseif": ELSEIF,
"else": ELSE,
"end": END,
}
if tok, ok := keywords[ident]; ok {
return tok
}
return IDENT
}