types.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package program
  2. import (
  3. "kumachan/standalone/ctn"
  4. "kumachan/interpreter/core"
  5. "kumachan/lang/source"
  6. "kumachan/lang/typsys"
  7. "kumachan/lang/textual/ast"
  8. )
  9. const T_Observable_Tag = core.T_Observable
  10. const T_Observable_Maybe_Tag = (core.T_Observable + "?")
  11. const T_Hook_Tag = core.T_Hook
  12. type AstType func(ast.Node)(ast.Type)
  13. func AT_Null() AstType {
  14. return makeAstType(core.T_Null)
  15. }
  16. func AT_Observable(item_t AstType) AstType {
  17. return makeAstType(core.T_Observable, item_t)
  18. }
  19. func makeAstType(item string, args ...AstType) AstType {
  20. return func(node ast.Node) ast.Type {
  21. return ast.Type {
  22. Node: node,
  23. Ref: ast.Ref {
  24. Node: node,
  25. Base: ast.RefBase {
  26. Node: node,
  27. Item: ast.String2Id(item, node),
  28. },
  29. TypeArgs: ctn.MapEach(args, func(arg AstType) ast.Type {
  30. return arg(node)
  31. }),
  32. },
  33. }
  34. }
  35. }
  36. func T_InvalidType() typsys.Type {
  37. return makeType(core.T_InvalidType)
  38. }
  39. func T_InvalidType_(t typsys.Type) bool {
  40. return matchType0(core.T_InvalidType, t)
  41. }
  42. func T_IntoReflectType(t typsys.Type) typsys.Type {
  43. return makeType(core.T_IntoReflectType, t)
  44. }
  45. func T_IntoReflectType_(t typsys.Type) (typsys.Type, bool) {
  46. return matchType1(core.T_IntoReflectType, t)
  47. }
  48. func T_IntoReflectValue(t typsys.Type) typsys.Type {
  49. return makeType(core.T_IntoReflectValue, t)
  50. }
  51. func T_IntoReflectValue_(t typsys.Type) (typsys.Type, bool) {
  52. return matchType1(core.T_IntoReflectValue, t)
  53. }
  54. func T_DummyReflectType_(t typsys.Type) bool {
  55. return matchType0(core.T_DummyReflectType, t)
  56. }
  57. func T_Null() typsys.Type {
  58. return makeType(core.T_Null)
  59. }
  60. func T_Null_(t typsys.Type) bool {
  61. return matchType0(core.T_Null, t)
  62. }
  63. func T_Bool_(t typsys.Type) bool {
  64. return matchType0(core.T_Bool, t)
  65. }
  66. func T_Maybe(t typsys.Type) typsys.Type {
  67. return makeType(core.T_Maybe, t)
  68. }
  69. func T_Maybe_(t typsys.Type) (typsys.Type, bool) {
  70. return matchType1(core.T_Maybe, t)
  71. }
  72. func T_Int() typsys.Type {
  73. return makeType(core.T_Int)
  74. }
  75. func T_Int_(t typsys.Type) bool {
  76. return matchType0(core.T_Int, t)
  77. }
  78. func T_Float() typsys.Type {
  79. return makeType(core.T_Float)
  80. }
  81. func T_Char() typsys.Type {
  82. return makeType(core.T_Char)
  83. }
  84. func T_Bytes() typsys.Type {
  85. return makeType(core.T_Bytes)
  86. }
  87. func T_String() typsys.Type {
  88. return makeType(core.T_String)
  89. }
  90. func T_RegExp() typsys.Type {
  91. return makeType(core.T_RegExp)
  92. }
  93. func T_Lambda(in typsys.Type, out typsys.Type) typsys.Type {
  94. return makeType(core.T_Lambda, in, out)
  95. }
  96. func T_Lambda_(t typsys.Type) (typsys.Type, typsys.Type, bool) {
  97. return matchType2(core.T_Lambda, t)
  98. }
  99. func T_List(t typsys.Type) typsys.Type {
  100. return makeType(core.T_List, t)
  101. }
  102. func T_List_(t typsys.Type) (typsys.Type, bool) {
  103. return matchType1(core.T_List, t)
  104. }
  105. func T_Pair(a typsys.Type, b typsys.Type) typsys.Type {
  106. return makeType(core.T_Pair, a, b)
  107. }
  108. func T_Pair_(t typsys.Type) (typsys.Type, typsys.Type, bool) {
  109. return matchType2(core.T_Pair, t)
  110. }
  111. func T_Triple(a typsys.Type, b typsys.Type, c typsys.Type) typsys.Type {
  112. return makeType(core.T_Triple, a, b, c)
  113. }
  114. func T_Triple_(t typsys.Type) (typsys.Type, typsys.Type, typsys.Type, bool) {
  115. return matchType3(core.T_Triple, t)
  116. }
  117. func T_Lens1(a typsys.Type, b typsys.Type) typsys.Type {
  118. return makeType(core.T_Lens1, a, b)
  119. }
  120. func T_Lens1_(t typsys.Type) (typsys.Type, typsys.Type, bool) {
  121. return matchType2(core.T_Lens1, t)
  122. }
  123. func T_Lens2(a typsys.Type, b typsys.Type) typsys.Type {
  124. return makeType(core.T_Lens2, a, b)
  125. }
  126. func T_Lens2_(t typsys.Type) (typsys.Type, typsys.Type, bool) {
  127. return matchType2(core.T_Lens2, t)
  128. }
  129. func T_Observable(t typsys.Type) typsys.Type {
  130. return makeType(core.T_Observable, t)
  131. }
  132. func T_Observable_(t typsys.Type) (typsys.Type, bool) {
  133. return matchType1(core.T_Observable, t)
  134. }
  135. func T_Hook(t typsys.Type) typsys.Type {
  136. return makeType(core.T_Hook, t)
  137. }
  138. func makeType(item string, args ...typsys.Type) typsys.Type {
  139. return typsys.RefType {
  140. Def: source.MakeRef("", item),
  141. Args: args,
  142. }
  143. }
  144. // TODO: makeType(variadic->slice), makeType0, makeType1, makeType2, ...
  145. func matchType(item string, t typsys.Type) ([] typsys.Type, bool) {
  146. if ref_type, ok := t.(typsys.RefType); ok {
  147. var ref = ref_type.Def
  148. var args = ref_type.Args
  149. if (ref.Namespace == "") && (ref.ItemName == item) {
  150. return args, true
  151. }
  152. }
  153. return nil, false
  154. }
  155. func matchType0(item string, t typsys.Type) bool {
  156. var args, ok = matchType(item, t)
  157. if ok {
  158. if len(args) != 0 { panic("something went wrong") }
  159. return true
  160. } else {
  161. return false
  162. }
  163. }
  164. func matchType1(item string, t typsys.Type) (typsys.Type, bool) {
  165. var args, ok = matchType(item, t)
  166. if ok {
  167. if len(args) != 1 { panic("something went wrong") }
  168. return args[0], true
  169. } else {
  170. return nil, false
  171. }
  172. }
  173. func matchType2(item string, t typsys.Type) (typsys.Type, typsys.Type, bool) {
  174. var args, ok = matchType(item, t)
  175. if ok {
  176. if len(args) != 2 { panic("something went wrong") }
  177. return args[0], args[1], true
  178. } else {
  179. return nil, nil, false
  180. }
  181. }
  182. func matchType3(item string, t typsys.Type) (typsys.Type, typsys.Type, typsys.Type, bool) {
  183. var args, ok = matchType(item, t)
  184. if ok {
  185. if len(args) != 3 { panic("something went wrong") }
  186. return args[0], args[1], args[2], true
  187. } else {
  188. return nil, nil, nil, false
  189. }
  190. }
  191. type ReflectType_ core.ReflectType
  192. func MakeReflectType_(t typsys.CertainType) ReflectType_ {
  193. return ReflectType_(core.MakeReflectType(t))
  194. }