expr4.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package compiler
  2. import (
  3. "kumachan/standalone/ctn"
  4. "kumachan/lang/source"
  5. "kumachan/lang/typsys"
  6. "kumachan/lang/textual/ast"
  7. "kumachan/interpreter/program"
  8. )
  9. func checkIf(I ast.If, cc *exprCheckContext) (*program.Expr, *source.Error) {
  10. var return_type = cc.expected
  11. var branches = make([] program.IfBranch, 0)
  12. var add = func(c ([] ast.Cond), b ast.Block) *source.Error {
  13. { var cc = cc.withBlockScope()
  14. var conds = make([] program.Cond, len(c))
  15. for i, c := range c {
  16. var value, err = cc.checkChildExpr(nil, c.Expr)
  17. if err != nil { return err }
  18. var inner_t, kind, ok = getCondInnerType(value.Type)
  19. if !(ok) {
  20. return source.MakeError(c.Expr.Location,
  21. E_InvalidCondType {
  22. TypeDesc: typsys.DescribeCertain(value.Type),
  23. })
  24. }
  25. if kind == program.CK_Bool {
  26. if pattern, ok := c.Pattern.(ast.VariousPattern); ok {
  27. return source.MakeError(pattern.Location,
  28. E_InvalidCondPattern {})
  29. }}
  30. { var match, err = cc.match(c.Pattern, inner_t)
  31. if err != nil { return err }
  32. conds[i] = program.Cond {
  33. Kind: kind,
  34. Match: match,
  35. Value: value,
  36. }}
  37. }
  38. var b = ast.WrapTermAsExpr(ast.VariousTerm { Node: b.Node, Term: b })
  39. var value, err = cc.checkChildExpr(return_type, b)
  40. if err != nil { return err }
  41. if return_type == nil {
  42. return_type = value.Type.Type
  43. }
  44. branches = append(branches, program.IfBranch {
  45. Conds: conds,
  46. Value: value,
  47. })
  48. return nil }
  49. }
  50. var err = add(I.Conds, I.Yes)
  51. if err != nil { return nil, err }
  52. for _, elif := range I.ElIfs {
  53. var err = add(elif.Conds, elif.Yes)
  54. if err != nil { return nil, err }
  55. }
  56. { var err = add(nil, I.No)
  57. if err != nil { return nil, err } }
  58. var t, ok = cc.getCertainOrInferred(return_type)
  59. if !(ok) { panic("something went wrong") }
  60. var loc = I.Location
  61. return cc.assign(t, loc,
  62. program.If {
  63. Branches: branches,
  64. })
  65. }
  66. func checkWhen(W ast.When, cc *exprCheckContext) (*program.Expr, *source.Error) {
  67. var ctx = cc.context
  68. var operand, err = cc.checkChildExpr(nil, W.Operand)
  69. if err != nil { return nil, err }
  70. var operand_t = operand.Type
  71. var return_type = cc.expected
  72. if f, d, a, k, ok := getUnionOrEnumFields(operand_t.Type, ctx); ok {
  73. var branches = make([] *program.WhenBranch, len(f.FieldList))
  74. for _, c := range W.Cases {
  75. for _, n := range c.Names {
  76. if c.Off {
  77. break
  78. }
  79. var in = c.InputPattern
  80. var out = c.OutputExpr
  81. var key = ast.Id2String(n)
  82. var not_default = (key != Underscore)
  83. var cc, match, index, err = (func() (*exprCheckContext, program.PatternMatching, int, *source.Error) {
  84. if not_default {
  85. var index, exists = f.FieldIndexMap[key]
  86. if !(exists) {
  87. var loc = n.Location
  88. return nil, nil, -1, source.MakeError(loc,
  89. E_NoSuchCase {
  90. CaseName: key,
  91. })
  92. }
  93. if branches[index] != nil {
  94. var loc = n.Location
  95. return nil, nil, -1, source.MakeError(loc,
  96. E_DuplicateCase {
  97. CaseName: key,
  98. })
  99. }
  100. if k == program.UE_Union {
  101. var field = f.FieldList[index]
  102. var in_t_ = inflateFieldType(field, d, a)
  103. var in_t = typsys.CertainType { Type: in_t_ }
  104. var cc = cc.withBlockScope()
  105. var match, err = cc.match(in, in_t)
  106. if err != nil { return nil, nil, -1, err }
  107. return cc, match, index, nil
  108. } else {
  109. if pattern, ok := in.(ast.VariousPattern); ok {
  110. var loc = pattern.Location
  111. return nil, nil, -1, source.MakeError(loc,
  112. E_InvalidCasePattern {})
  113. }
  114. return cc, nil, index, nil
  115. }
  116. } else {
  117. if pattern, ok := in.(ast.VariousPattern); ok {
  118. var loc = pattern.Location
  119. return nil, nil, -1, source.MakeError(loc,
  120. E_InvalidCasePattern {})
  121. }
  122. return cc, nil, -1, nil
  123. }
  124. })()
  125. if err != nil { return nil, err }
  126. { var value, err = cc.checkChildExpr(return_type, out)
  127. if err != nil { return nil, err }
  128. if return_type == nil {
  129. return_type = value.Type.Type
  130. }
  131. var branch = &program.WhenBranch {
  132. Match: match,
  133. Value: value,
  134. }
  135. if not_default {
  136. branches[index] = branch
  137. } else {
  138. var ok = false
  139. for i := range branches {
  140. if branches[i] == nil {
  141. branches[i] = branch
  142. ok = true
  143. }
  144. }
  145. if !(ok) {
  146. var loc = n.Location
  147. return cc.error(loc, E_SuperfluousDefaultCase {})
  148. }
  149. }}
  150. }}
  151. for i := range branches {
  152. if branches[i] == nil {
  153. var missing = f.FieldList[i].Name
  154. var loc = W.Location
  155. return cc.error(loc,
  156. E_MissingCase {
  157. CaseName: missing,
  158. })
  159. }
  160. }
  161. var t, ok = cc.getCertainOrInferred(return_type)
  162. if !(ok) { panic("something went wrong") }
  163. var operand = program.WhenOperand {
  164. Kind: k,
  165. Value: operand,
  166. }
  167. var loc = W.Location
  168. return cc.assign(t, loc,
  169. program.When {
  170. Operand: operand,
  171. Branches: branches,
  172. })
  173. }
  174. var loc = W.Operand.Location
  175. return cc.error(loc,
  176. E_InvalidWhenOperand {
  177. TypeDesc: typsys.DescribeCertain(operand_t),
  178. })
  179. }
  180. func checkEach(E ast.Each, cc *exprCheckContext) (*program.Expr, *source.Error) {
  181. var ctx = cc.context
  182. var operand_t, err = ctx.makeType(E.Operand)
  183. if err != nil { return nil, err }
  184. var item_type = (func() typsys.Type {
  185. if cc.expected == nil {
  186. return nil
  187. }
  188. if item_type, ok := program.T_List_(cc.expected); ok {
  189. return item_type
  190. }
  191. return nil
  192. })()
  193. if f, d, a, k, ok := getUnionOrEnumFields(operand_t.Type, ctx); ok {
  194. var values = make([] program.EachValue, 0)
  195. var occurred = make(map[int] struct{})
  196. for _, c := range E.Cases {
  197. for _, n := range c.Names {
  198. if c.Off {
  199. break
  200. }
  201. var in = c.InputPattern
  202. var out = c.OutputExpr
  203. var key = ast.Id2String(n)
  204. var is_default = (key == Underscore)
  205. if is_default {
  206. var loc = n.Location
  207. return cc.error(loc, E_SuperfluousDefaultCase {})
  208. }
  209. var cc, match, index, err = (func() (*exprCheckContext, program.PatternMatching, int, *source.Error) {
  210. var index, exists = f.FieldIndexMap[key]
  211. if !(exists) {
  212. var loc = n.Location
  213. return nil, nil, -1, source.MakeError(loc,
  214. E_NoSuchCase {
  215. CaseName: key,
  216. })
  217. }
  218. var _, duplicate = occurred[index]
  219. occurred[index] = struct{}{}
  220. if duplicate {
  221. var loc = n.Location
  222. return nil, nil, -1, source.MakeError(loc,
  223. E_DuplicateCase {
  224. CaseName: key,
  225. })
  226. }
  227. if k == program.UE_Union {
  228. var field = f.FieldList[index]
  229. var field_t_ = inflateFieldType(field, d, a)
  230. var union_t_ = operand_t.Type
  231. var in_t_ = program.T_Lambda(field_t_, union_t_)
  232. var in_t = typsys.CertainType { Type: in_t_ }
  233. var cc = cc.withBlockScope()
  234. var match, err = cc.match(in, in_t)
  235. if err != nil { return nil, nil, -1, err }
  236. return cc, match, index, nil
  237. } else {
  238. var enum_t = operand_t
  239. var in_t = enum_t
  240. var cc = cc.withBlockScope()
  241. var match, err = cc.match(in, in_t)
  242. if err != nil { return nil, nil, -1, err }
  243. return cc, match, index, nil
  244. }
  245. })()
  246. if err != nil { return nil, err }
  247. { var value, err = cc.checkChildExpr(item_type, out)
  248. if err != nil { return nil, err }
  249. if item_type == nil {
  250. item_type = value.Type.Type
  251. }
  252. { var value = program.EachValue {
  253. Kind: k,
  254. Index: index,
  255. Match: match,
  256. Value: value,
  257. }
  258. values = append(values, value) }}
  259. }}
  260. for i, field := range f.FieldList {
  261. var index = i
  262. var _, ok = occurred[index]
  263. if !(ok) {
  264. var missing = field.Name
  265. var loc = E.Location
  266. return cc.error(loc,
  267. E_MissingCase {
  268. CaseName: missing,
  269. })
  270. }
  271. }
  272. var item_t, ok = cc.getCertainOrInferred(item_type)
  273. if !(ok) { panic("something went wrong") }
  274. var t_ = program.T_List(item_t.Type)
  275. var t = typsys.CertainType { Type: t_ }
  276. var items = ctn.MapEach(values, func(value program.EachValue) *program.Expr {
  277. return &program.Expr {
  278. Type: item_t,
  279. Info: value.Value.Info,
  280. Content: value,
  281. }
  282. })
  283. var loc = E.Location
  284. return cc.assign(t, loc,
  285. program.List {
  286. Items: items,
  287. })
  288. }
  289. var loc = E.Operand.Location
  290. return cc.error(loc,
  291. E_InvalidEachOperand {
  292. TypeDesc: typsys.DescribeCertain(operand_t),
  293. })
  294. }
  295. func getCondInnerType(t typsys.CertainType) (typsys.CertainType, program.CondKind, bool) {
  296. var nil_t typsys.CertainType
  297. if program.T_Bool_(t.Type) {
  298. return nil_t, program.CK_Bool, true
  299. }
  300. if inner, ok := program.T_Maybe_(t.Type); ok {
  301. var inner_t = typsys.CertainType { Type: inner }
  302. return inner_t, program.CK_Maybe, true
  303. }
  304. if _, inner, ok := program.T_Lens2_(t.Type); ok {
  305. var inner_t = typsys.CertainType { Type: inner }
  306. return inner_t, program.CK_Lens2, true
  307. }
  308. return nil_t, program.CondKind(-1), false
  309. }
  310. func getUnionOrEnumFields(t typsys.Type, ctx *exprContext) (*typsys.Fields, *typsys.TypeDef, ([] typsys.Type), program.UnionOrEnum, bool) {
  311. if u, d, a, ok := getUnion(t, ctx); ok {
  312. return u.Fields, d, a, program.UE_Union, true
  313. }
  314. if e, d, ok := getEnum(t, ctx); ok {
  315. return e.Fields, d, nil, program.UE_Enum, true
  316. }
  317. return nil, nil, nil, program.UnionOrEnum(-1), false
  318. }