token.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. MINUS = "-"
  17. BANG = "!"
  18. ASTERISK = "*"
  19. SLASH = "/"
  20. LT = "<"
  21. GT = ">"
  22. EQ = "=="
  23. NOT_EQ = "!="
  24. // Delimiters.
  25. COMMA = ","
  26. SEMICOLON = ";"
  27. LPAREN = "("
  28. RPAREN = ")"
  29. LBRACE = "{"
  30. RBRACE = "}"
  31. // Keywords
  32. FUNCTION = "FUNCTION"
  33. LET = "LET"
  34. TRUE = "TRUE"
  35. FALSE = "FALSE"
  36. IF = "IF"
  37. ELSE = "ELSE"
  38. RETURN = "RETURN"
  39. )
  40. var keywords = map[string]TokenType{
  41. "fn": FUNCTION,
  42. "let": LET,
  43. "true": TRUE,
  44. "false": FALSE,
  45. "if": IF,
  46. "else": ELSE,
  47. "return": RETURN,
  48. }
  49. // LookupIdent checks the keywords table to see whether the given identifier is
  50. // in fact a keyword.
  51. func LookupIdent(ident string) TokenType {
  52. if tok, ok := keywords[ident]; ok {
  53. return tok
  54. }
  55. return IDENT
  56. }