token.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package token
  2. // TokenType is the string representation of the Token types.
  3. type TokenType string
  4. // The TokenType values.
  5. const (
  6. ILLEGAL = "ILLEGAL"
  7. EOF = "EOF"
  8. // Identifiers + literals.
  9. IDENT = "IDENT" // add, foobar, x, y, ...
  10. INT = "INT" // 123456, ...
  11. // Operators.
  12. ASSIGN = "="
  13. PLUS = "+"
  14. MINUS = "-"
  15. BANG = "!"
  16. ASTERISK = "*"
  17. SLASH = "/"
  18. LT = "<"
  19. GT = ">"
  20. EQ = "=="
  21. NOT_EQ = "!="
  22. // Delimiters.
  23. COMMA = ","
  24. SEMICOLON = ";"
  25. LPAREN = "("
  26. RPAREN = ")"
  27. LBRACE = "{"
  28. RBRACE = "}"
  29. // Keywords
  30. FUNCTION = "FUNCTION"
  31. LET = "LET"
  32. TRUE = "TRUE"
  33. FALSE = "FALSE"
  34. IF = "IF"
  35. ELSE = "ELSE"
  36. RETURN = "RETURN"
  37. )
  38. // Token represents a Parser token.
  39. type Token struct {
  40. Type TokenType
  41. Literal string
  42. }
  43. var keywords = map[string]TokenType{
  44. "fn": FUNCTION,
  45. "let": LET,
  46. "true": TRUE,
  47. "false": FALSE,
  48. "if": IF,
  49. "else": ELSE,
  50. "return": RETURN,
  51. }
  52. // LookupIdent checks the keywords table to see whether the given identifier is
  53. // in fact a keyword.
  54. func LookupIdent(ident string) TokenType {
  55. if tok, ok := keywords[ident]; ok {
  56. return tok
  57. }
  58. return IDENT
  59. }