array.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package dirty
  2. import (
  3. "bufio"
  4. )
  5. type Array []Element
  6. func (Array) isElement() {}
  7. func (Array) getType() ElementType {
  8. return ElemArray
  9. }
  10. type Element interface {
  11. isElement()
  12. getType() ElementType
  13. }
  14. type ElementType int
  15. const (
  16. ElemArray ElementType = iota
  17. ElemString
  18. ElemConst
  19. ElemInt
  20. ElemFloat
  21. )
  22. func loadArray(r *bufio.Reader) ([]Element, error) {
  23. topArray := []Element{}
  24. for {
  25. t, err := nextToken(r)
  26. if err != nil {
  27. return []Element{}, err
  28. }
  29. //debugf("in LoadArray got %+v\n", t)
  30. switch t.ttype {
  31. case LBRACKET:
  32. //debugf("in LoadArray loading array\n")
  33. array, err := loadArray(r)
  34. if err != nil {
  35. return []Element{}, err
  36. }
  37. topArray = append(topArray, Array(array))
  38. case RBRACKET:
  39. //debugf("in LoadArray closing\n")
  40. return topArray, nil
  41. // todo atoms
  42. case COMMENT:
  43. continue
  44. case STRING:
  45. //debugf("in LoadArray adding string\n")
  46. topArray = append(topArray, String(t.t))
  47. case STRING_RAW:
  48. //debugf("in LoadArray adding raw string\n")
  49. topArray = append(topArray, String(t.t))
  50. case CONST:
  51. //debugf("in LoadArray adding const\n")
  52. topArray = append(topArray, NewConst(t.t))
  53. case NUMBER:
  54. //debugf("in LoadArray adding number %+v, %d\n", t, *t.i)
  55. if t.i != nil {
  56. topArray = append(topArray, Int(*t.i))
  57. }
  58. case FLOAT:
  59. //debugf("in LoadArray adding float %+v, %f\n", t, *t.f)
  60. if t.f != nil {
  61. topArray = append(topArray, Float(*t.f))
  62. }
  63. default:
  64. //debugln("loadArray")
  65. return []Element{}, NewSyntaxError(t, []token{})
  66. }
  67. }
  68. }