typeutils.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package zygo
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. func IsArray(expr Sexp) bool {
  7. switch expr.(type) {
  8. case *SexpArray:
  9. return true
  10. }
  11. return false
  12. }
  13. func IsList(expr Sexp) bool {
  14. if expr == SexpNull {
  15. return true
  16. }
  17. switch list := expr.(type) {
  18. case *SexpPair:
  19. return IsList(list.Tail)
  20. }
  21. return false
  22. }
  23. func IsAssignmentList(expr Sexp, pos int) (bool, int) {
  24. if expr == SexpNull {
  25. return false, -1
  26. }
  27. switch list := expr.(type) {
  28. case *SexpPair:
  29. sym, isSym := list.Head.(*SexpSymbol)
  30. if !isSym {
  31. return IsAssignmentList(list.Tail, pos+1)
  32. }
  33. if sym.name == "=" || sym.name == ":=" {
  34. return true, pos
  35. }
  36. return IsAssignmentList(list.Tail, pos+1)
  37. }
  38. return false, -1
  39. }
  40. func IsFloat(expr Sexp) bool {
  41. switch expr.(type) {
  42. case *SexpFloat:
  43. return true
  44. }
  45. return false
  46. }
  47. func IsInt(expr Sexp) bool {
  48. switch expr.(type) {
  49. case *SexpInt:
  50. return true
  51. }
  52. return false
  53. }
  54. func IsString(expr Sexp) bool {
  55. switch expr.(type) {
  56. case *SexpStr:
  57. return true
  58. }
  59. return false
  60. }
  61. func IsChar(expr Sexp) bool {
  62. switch expr.(type) {
  63. case *SexpChar:
  64. return true
  65. }
  66. return false
  67. }
  68. func IsNumber(expr Sexp) bool {
  69. switch expr.(type) {
  70. case *SexpFloat:
  71. return true
  72. case *SexpInt:
  73. return true
  74. case *SexpChar:
  75. return true
  76. }
  77. return false
  78. }
  79. func IsSymbol(expr Sexp) bool {
  80. switch expr.(type) {
  81. case *SexpSymbol:
  82. return true
  83. }
  84. return false
  85. }
  86. func IsHash(expr Sexp) bool {
  87. switch expr.(type) {
  88. case *SexpHash:
  89. return true
  90. }
  91. return false
  92. }
  93. func IsZero(expr Sexp) bool {
  94. switch e := expr.(type) {
  95. case *SexpInt:
  96. return int(e.Val) == 0
  97. case *SexpChar:
  98. return int(e.Val) == 0
  99. case *SexpFloat:
  100. return float64(e.Val) == 0.0
  101. }
  102. return false
  103. }
  104. func IsEmpty(expr Sexp) bool {
  105. if expr == SexpNull {
  106. return true
  107. }
  108. switch e := expr.(type) {
  109. case *SexpArray:
  110. return len(e.Val) == 0
  111. case *SexpHash:
  112. return HashIsEmpty(e)
  113. }
  114. return false
  115. }
  116. func IsFunc(expr Sexp) bool {
  117. switch expr.(type) {
  118. case *SexpFunction:
  119. return true
  120. }
  121. return false
  122. }
  123. func TypeOf(expr Sexp) *SexpStr {
  124. v := ""
  125. switch e := expr.(type) {
  126. case *SexpRaw:
  127. v = "raw"
  128. case *SexpBool:
  129. v = "bool"
  130. case *SexpArray:
  131. v = "array"
  132. case *SexpInt:
  133. v = "int64"
  134. case *SexpUint64:
  135. v = "uint64"
  136. case *SexpStr:
  137. v = "string"
  138. case *SexpChar:
  139. v = "char"
  140. case *SexpFloat:
  141. v = "float64"
  142. case *SexpHash:
  143. v = e.TypeName
  144. case *SexpPair:
  145. v = "list"
  146. case *SexpSymbol:
  147. v = "symbol"
  148. case *SexpFunction:
  149. v = "func"
  150. case *SexpSentinel:
  151. v = "nil"
  152. case *SexpTime:
  153. v = "time.Time"
  154. case *RegisteredType:
  155. v = "regtype"
  156. case *SexpPointer:
  157. v = e.MyType.RegisteredName
  158. case *SexpArraySelector:
  159. v = "arraySelector"
  160. case *SexpHashSelector:
  161. v = "hashSelector"
  162. case *SexpReflect:
  163. rt := expr.Type()
  164. if rt != nil {
  165. return &SexpStr{S: rt.RegisteredName}
  166. }
  167. //v = reflect.Value(e).Type().Name()
  168. //if v == "Ptr" {
  169. // v = reflect.Value(e).Type().Elem().Kind().String()
  170. //}
  171. kind := reflect.Value(e.Val).Type().Kind()
  172. if kind == reflect.Ptr {
  173. v = reflect.Value(e.Val).Elem().Type().Name()
  174. } else {
  175. P("kind = %v", kind)
  176. v = "reflect.Value"
  177. }
  178. case *Stack:
  179. if e.IsPackage {
  180. v = "package"
  181. } else {
  182. v = "stack"
  183. }
  184. default:
  185. fmt.Printf("\n error: unknown type: %T in '%#v'\n", e, e)
  186. }
  187. return &SexpStr{S: v}
  188. }