token.go 741 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package token
  2. type TokenType string
  3. type Token struct {
  4. Type TokenType
  5. Literal string
  6. }
  7. const (
  8. ILLEGAL = "ILLEGAL"
  9. EOF = "EOF"
  10. // Identifiers + literals.
  11. IDENT = "IDENT" // add, foobar, x, y, ...
  12. INT = "INT" // 123456, ...
  13. // Operators.
  14. ASSIGN = "="
  15. PLUS = "+"
  16. // Delimiters.
  17. COMMA = ","
  18. SEMICOLON = ";"
  19. LPAREN = "("
  20. RPAREN = ")"
  21. LBRACE = "{"
  22. RBRACE = "}"
  23. // Keywords
  24. FUNCTION = "FUNCTION"
  25. LET = "LET"
  26. )
  27. var keywords = map[string]TokenType{
  28. "fn": FUNCTION,
  29. "let": LET,
  30. }
  31. // LookupIdent checks the keywords table to see whether the given identifier is
  32. // in fact a keyword.
  33. func LookupIdent(ident string) TokenType {
  34. if tok, ok := keywords[ident]; ok {
  35. return tok
  36. }
  37. return IDENT
  38. }