assign.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package checker
  2. import (
  3. "fmt"
  4. )
  5. func RequireExplicitType(t Type, info ExprInfo) *ExprError {
  6. if t == nil {
  7. return &ExprError {
  8. Point: info.ErrorPoint,
  9. Concrete: E_ExplicitTypeRequired {},
  10. }
  11. } else {
  12. return nil
  13. }
  14. }
  15. func AssignTo(expected Type, semi SemiExpr, ctx ExprContext) (Expr, *ExprError) {
  16. var expr, err = (func() (Expr, *ExprError) {
  17. switch semi_value := semi.Value.(type) {
  18. case TypedExpr:
  19. return AssignTypedTo(expected, Expr(semi_value), ctx, true)
  20. case UntypedLambda:
  21. return AssignLambdaTo(expected, semi_value, semi.Info, ctx)
  22. case UntypedInteger:
  23. return AssignIntegerTo(expected, semi_value, semi.Info, ctx)
  24. case SemiTypedTuple:
  25. return AssignTupleTo(expected, semi_value, semi.Info, ctx)
  26. case SemiTypedBundle:
  27. return AssignBundleTo(expected, semi_value, semi.Info, ctx)
  28. case SemiTypedArray:
  29. return AssignArrayTo(expected, semi_value, semi.Info, ctx)
  30. case SemiTypedBlock:
  31. return AssignBlockTo(expected, semi_value, semi.Info, ctx)
  32. case SemiTypedMatch:
  33. return AssignMatchTo(expected, semi_value, semi.Info, ctx)
  34. case UntypedRef:
  35. return AssignRefTo(expected, semi_value, semi.Info, ctx)
  36. case UndecidedCall:
  37. return AssignCallTo(expected, semi_value, semi.Info, ctx)
  38. default:
  39. panic("impossible branch")
  40. }
  41. })()
  42. if err != nil { return Expr{}, err }
  43. if ctx.InferTypeArgs {
  44. return Expr {
  45. Type: FillMarkedParams(expr.Type, ctx),
  46. Value: expr.Value,
  47. Info: expr.Info,
  48. }, nil
  49. } else {
  50. return expr, nil
  51. }
  52. }
  53. func AssignTypedTo(expected Type, expr Expr, ctx ExprContext, unbox bool) (Expr, *ExprError) {
  54. // TODO: update comments
  55. if expected == nil {
  56. // 1. If the expected type is not specified,
  57. // no further process is required.
  58. return expr, nil
  59. } else {
  60. // 3. Otherwise, try some implicit type conversions
  61. // 3.1. Define some inner functions
  62. // -- shortcut to produce a "not assignable" error --
  63. var throw = func(reason string) *ExprError {
  64. return &ExprError {
  65. Point: expr.Info.ErrorPoint,
  66. Concrete: E_NotAssignable {
  67. From: ctx.DescribeType(expr.Type),
  68. To: ctx.DescribeExpectedType(expected),
  69. Reason: reason,
  70. },
  71. }
  72. }
  73. // -- behavior of assigning a named type to an union type --
  74. var assign_union func(NamedType, NamedType, Union) (Expr, *ExprError)
  75. assign_union = func(exp NamedType, given NamedType, union Union) (Expr, *ExprError) {
  76. // 1. Find the given type in the list of subtypes of the union
  77. for index, subtype := range union.SubTypes {
  78. if subtype == given.Name {
  79. // 1.1. If found, check if type parameters are identical
  80. if len(exp.Args) != len(given.Args) {
  81. panic("something went wrong")
  82. }
  83. var L = len(exp.Args)
  84. for i := 0; i < L; i += 1 {
  85. var arg_exp = exp.Args[i]
  86. var arg_given = given.Args[i]
  87. if !(AreTypesEqualInSameCtx(arg_exp, arg_given)) {
  88. // 1.1.1. If not identical, throw an error.
  89. return Expr{}, throw("type parameters not matching")
  90. }
  91. }
  92. // 1.1.2. Otherwise, return a lifted value.
  93. return Expr {
  94. Type: exp,
  95. Value: Sum { Value: expr, Index: uint(index) },
  96. Info: expr.Info,
  97. }, nil
  98. }
  99. }
  100. for index, subtype := range union.SubTypes {
  101. var t = ctx.ModuleInfo.Types[subtype]
  102. var item_union, ok = t.Value.(Union)
  103. if ok {
  104. var item_expr, err = assign_union(NamedType {
  105. Name: subtype,
  106. Args: exp.Args,
  107. }, given, item_union)
  108. if err != nil { continue }
  109. return Expr {
  110. Type: exp,
  111. Value: Sum { Value: item_expr, Index: uint(index) },
  112. Info: expr.Info,
  113. }, nil
  114. }
  115. }
  116. // 1.2. Otherwise, throw an error.
  117. return Expr{}, throw("given type is not a subtype of the expected union type")
  118. }
  119. var compare_named func(NamedType, NamedType) (*ExprError)
  120. compare_named = func(E NamedType, T NamedType) (*ExprError) {
  121. if !(ctx.InferTypeArgs) { panic("something went wrong") }
  122. if E.Name != T.Name {
  123. return throw("")
  124. }
  125. if len(E.Args) != len(T.Args) {
  126. return throw("")
  127. }
  128. var L = len(T.Args)
  129. for i := 0; i < L; i += 1 {
  130. switch E_arg := E.Args[i].(type) {
  131. case ParameterType:
  132. if E_arg.BeingInferred {
  133. var inferred, exists = ctx.Inferred[E_arg.Index]
  134. if exists {
  135. if !AreTypesEqualInSameCtx(inferred, expected) {
  136. return throw(fmt.Sprintf(
  137. "cannot infer type parameter %s",
  138. ctx.InferredNames[E_arg.Index],
  139. ))
  140. }
  141. } else {
  142. ctx.Inferred[E_arg.Index] = T.Args[i]
  143. }
  144. } else {
  145. if !(AreTypesEqualInSameCtx(E.Args[i], T.Args[i])) {
  146. return throw("")
  147. }
  148. }
  149. case NamedType:
  150. switch T_arg := T.Args[i].(type) {
  151. case NamedType:
  152. var err = compare_named(E_arg, T_arg)
  153. if err != nil { return err }
  154. default:
  155. if !(AreTypesEqualInSameCtx(E.Args[i], T.Args[i])) {
  156. return throw("")
  157. }
  158. }
  159. default:
  160. if !(AreTypesEqualInSameCtx(E.Args[i], T.Args[i])) {
  161. return throw("")
  162. }
  163. }
  164. }
  165. return nil
  166. }
  167. switch E := expected.(type) {
  168. case ParameterType:
  169. if ctx.InferTypeArgs && E.BeingInferred {
  170. var inferred, exists = ctx.Inferred[E.Index]
  171. if exists {
  172. if AreTypesEqualInSameCtx(inferred, expected) {
  173. return expr, nil
  174. } else {
  175. return Expr{}, throw(fmt.Sprintf (
  176. "cannot infer type parameter %s",
  177. ctx.InferredNames[E.Index],
  178. ))
  179. }
  180. } else {
  181. ctx.Inferred[E.Index] = expr.Type
  182. return expr, nil
  183. }
  184. }
  185. case NamedType:
  186. switch T := expr.Type.(type) {
  187. case NamedType:
  188. if E.Name == T.Name && ctx.InferTypeArgs {
  189. var err = compare_named(E, T)
  190. if err != nil {
  191. return Expr{}, err
  192. } else {
  193. return expr, nil
  194. }
  195. } else {
  196. var g = ctx.ModuleInfo.Types[E.Name]
  197. var union, is_union = g.Value.(Union)
  198. if is_union {
  199. return assign_union(E, T, union)
  200. } else {
  201. // "fallthrough" to strict equal or unbox
  202. }
  203. }
  204. }
  205. }
  206. if AreTypesEqualInSameCtx(expected, expr.Type) {
  207. return expr, nil
  208. } else {
  209. if unbox {
  210. var unboxed, ok = Unbox(expr.Type, ctx).(Unboxed)
  211. if ok {
  212. var expr_unboxed = Expr {
  213. Type: unboxed.Type,
  214. Value: expr.Value,
  215. Info: expr.Info,
  216. }
  217. var expr_expected, err = AssignTypedTo (
  218. expected, expr_unboxed, ctx, false,
  219. )
  220. // if err != nil { return Expr{}, err }
  221. if err != nil { return Expr{}, throw("") }
  222. if ctx.UnboxCounted {
  223. *(ctx.UnboxCount) += 1
  224. }
  225. return expr_expected, nil
  226. }
  227. }
  228. return Expr{}, throw("")
  229. }
  230. }
  231. }