token.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package token
  2. // TokenType is the string representation of the Token types.
  3. type TokenType string
  4. // Token represents a Parser token.
  5. type Token struct {
  6. Type TokenType
  7. Literal string
  8. }
  9. // The TokenType values.
  10. const (
  11. ILLEGAL = "ILLEGAL"
  12. EOF = "EOF"
  13. // Identifiers + literals.
  14. IDENT = "IDENT" // add, foobar, x, y, ...
  15. INT = "INT" // 123456, ...
  16. // Operators.
  17. ASSIGN = "="
  18. PLUS = "+"
  19. MINUS = "-"
  20. BANG = "!"
  21. ASTERISK = "*"
  22. SLASH = "/"
  23. LT = "<"
  24. GT = ">"
  25. EQ = "=="
  26. NOT_EQ = "!="
  27. // Delimiters.
  28. COMMA = ","
  29. SEMICOLON = ";"
  30. LPAREN = "("
  31. RPAREN = ")"
  32. LBRACE = "{"
  33. RBRACE = "}"
  34. // Keywords
  35. FUNCTION = "FUNCTION"
  36. LET = "LET"
  37. TRUE = "TRUE"
  38. FALSE = "FALSE"
  39. IF = "IF"
  40. ELSE = "ELSE"
  41. RETURN = "RETURN"
  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. }