comment.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package zygo
  2. type SexpComment struct {
  3. Comment string
  4. Block bool
  5. }
  6. func (p *SexpComment) SexpString(ps *PrintState) string {
  7. return p.Comment
  8. }
  9. func (p *SexpComment) Type() *RegisteredType {
  10. return GoStructRegistry.Registry["comment"]
  11. }
  12. // Filters return true to keep, false to drop.
  13. type Filter func(x Sexp) bool
  14. func RemoveCommentsFilter(x Sexp) bool {
  15. switch x.(type) {
  16. case *SexpComment:
  17. //P("RemoveCommentsFilter called on x= %T/val=%v. return false", x, x)
  18. return false
  19. default:
  20. //P("RemoveCommentsFilter called on x= %T/val=%v. return true", x, x)
  21. return true
  22. }
  23. }
  24. // detect SexpEnd values and return false on them to filter them out.
  25. func RemoveEndsFilter(x Sexp) bool {
  26. switch n := x.(type) {
  27. case *SexpSentinel:
  28. if n.Val == SexpEnd.Val {
  29. return false
  30. }
  31. }
  32. return true
  33. }
  34. // detect SexpComma values and return false on them to filter them out.
  35. func RemoveCommasFilter(x Sexp) bool {
  36. switch x.(type) {
  37. case *SexpComma:
  38. return false
  39. }
  40. return true
  41. }
  42. func (env *Zlisp) FilterAny(x Sexp, f Filter) (filtered Sexp, keep bool) {
  43. switch ele := x.(type) {
  44. case *SexpArray:
  45. res := &SexpArray{Val: env.FilterArray(ele.Val, f), Typ: ele.Typ, IsFuncDeclTypeArray: ele.IsFuncDeclTypeArray, Env: env}
  46. return res, true
  47. case *SexpPair:
  48. return env.FilterList(ele, f), true
  49. case *SexpHash:
  50. return env.FilterHash(ele, f), true
  51. default:
  52. keep = f(x)
  53. if keep {
  54. return x, true
  55. }
  56. return SexpNull, false
  57. }
  58. }
  59. func (env *Zlisp) FilterArray(x []Sexp, f Filter) []Sexp {
  60. //P("FilterArray: before: %d in size", len(x))
  61. //for i := range x {
  62. //P("x[i=%d] = %v", i, x[i].SexpString())
  63. //}
  64. res := []Sexp{}
  65. for i := range x {
  66. filtered, keep := env.FilterAny(x[i], f)
  67. if keep {
  68. res = append(res, filtered)
  69. }
  70. }
  71. //P("FilterArray: after: %d in size", len(res))
  72. //for i := range res {
  73. //P("x[i=%d] = %v", i, res[i].SexpString())
  74. //}
  75. return res
  76. }
  77. func (env *Zlisp) FilterHash(h *SexpHash, f Filter) *SexpHash {
  78. // should not actually need this, since hashes
  79. // don't yet exist in parsed symbols. (they are
  80. // still lists).
  81. //P("in FilterHash")
  82. return h
  83. }
  84. func (env *Zlisp) FilterList(h *SexpPair, f Filter) Sexp {
  85. //P("in FilterList")
  86. arr, err := ListToArray(h)
  87. res := []Sexp{}
  88. if err == NotAList {
  89. // don't filter pair lists
  90. return h
  91. }
  92. res = env.FilterArray(arr, f)
  93. return MakeList(res)
  94. }