const.go 831 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package dirty
  2. import (
  3. "fmt"
  4. )
  5. type Const int
  6. const (
  7. TRUE Const = iota
  8. FALSE
  9. NULL
  10. )
  11. func NewConst(s string) Const {
  12. if s == "true" {
  13. return TRUE
  14. }
  15. if s == "false" {
  16. return FALSE
  17. }
  18. if s == "null" {
  19. return NULL
  20. }
  21. panic("invalid const " + s)
  22. }
  23. func (Const) isElement() {}
  24. func (Const) getType() ElementType {
  25. return ElemConst
  26. }
  27. func (c Const) String() string {
  28. if c == TRUE {
  29. return "true"
  30. }
  31. if c == FALSE {
  32. return "false"
  33. }
  34. if c == NULL {
  35. return "null"
  36. }
  37. panic(fmt.Sprintf("invalid const %d", c))
  38. }
  39. func (c Const) Bool() bool {
  40. if c == TRUE {
  41. return true
  42. } else if c == FALSE {
  43. return false
  44. } else {
  45. panic("Const is not bool")
  46. }
  47. }
  48. func parseConst(t token) (token, error) {
  49. if t.t != "true" && t.t != "false" && t.t != "null" {
  50. return token{}, NewConstError(t.t)
  51. }
  52. return t, nil
  53. }