call.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package checker
  2. import (
  3. "kumachan/parser/scanner"
  4. "kumachan/transformer/node"
  5. )
  6. func (impl UndecidedCall) SemiExprVal() {}
  7. type UndecidedCall struct {
  8. Options [] AvailableCall
  9. FuncName string
  10. }
  11. type AvailableCall struct {
  12. Expr Expr
  13. UnboxCount uint
  14. }
  15. func (impl Call) ExprVal() {}
  16. type Call struct {
  17. Function Expr
  18. Argument Expr
  19. }
  20. func CheckCall(call node.Call, ctx ExprContext) (SemiExpr, *ExprError) {
  21. var arg_node, has_arg = call.Arg.(node.Call)
  22. if has_arg {
  23. var f, err1 = CheckTerm(call.Func, ctx)
  24. if err1 != nil { return SemiExpr{}, err1 }
  25. var arg, err2 = CheckCall(arg_node, ctx)
  26. if err2 != nil { return SemiExpr{}, err2 }
  27. var info = ctx.GetExprInfo(call.Node)
  28. return CheckSingleCall(f, arg, info, ctx)
  29. } else {
  30. return CheckTerm(call.Func, ctx)
  31. }
  32. }
  33. func CheckSingleCall(f SemiExpr, arg SemiExpr, info ExprInfo, ctx ExprContext) (SemiExpr, *ExprError) {
  34. switch f_concrete := f.Value.(type) {
  35. case TypedExpr:
  36. var t = f_concrete.Type
  37. switch T := t.(type) {
  38. case AnonymousType:
  39. switch r := T.Repr.(type) {
  40. case Func:
  41. var arg_typed, err = AssignTo(r.Input, arg, ctx)
  42. if err != nil { return SemiExpr{}, err }
  43. return LiftTyped(Expr {
  44. Type: r.Output,
  45. Value: Call {
  46. Function: Expr(f_concrete),
  47. Argument: arg_typed,
  48. },
  49. Info: f.Info,
  50. }), nil
  51. }
  52. }
  53. return SemiExpr{}, &ExprError {
  54. Point: f.Info.ErrorPoint,
  55. Concrete: E_ExprTypeNotCallable {
  56. Type: ctx.DescribeType(t),
  57. },
  58. }
  59. case UntypedLambda:
  60. return CallUntypedLambda(arg, f_concrete, f.Info, info, ctx)
  61. case UntypedRef:
  62. return CallUntypedRef(arg, f_concrete, f.Info, info, ctx)
  63. case SemiTypedMatch,
  64. SemiTypedBlock,
  65. UndecidedCall:
  66. return SemiExpr{}, &ExprError {
  67. Point: f.Info.ErrorPoint,
  68. Concrete: E_ExplicitTypeRequired {},
  69. }
  70. default:
  71. return SemiExpr{}, &ExprError {
  72. Point: f.Info.ErrorPoint,
  73. Concrete: E_ExprNotCallable {},
  74. }
  75. }
  76. }
  77. func CheckInfix(infix node.Infix, ctx ExprContext) (SemiExpr, *ExprError) {
  78. return CheckCall(DesugarInfix(infix), ctx)
  79. }
  80. func AssignCallTo(expected Type, call UndecidedCall, info ExprInfo, ctx ExprContext) (Expr, *ExprError) {
  81. var err = RequireExplicitType(expected, info)
  82. if err != nil { return Expr{}, err }
  83. var types_desc = make([]string, 0)
  84. for _, option := range call.Options {
  85. var expr, err = AssignTypedTo(expected, option.Expr, ctx, false)
  86. if err != nil {
  87. types_desc = append (
  88. types_desc,
  89. ctx.DescribeType(option.Expr.Type),
  90. )
  91. continue
  92. } else {
  93. return expr, nil
  94. }
  95. }
  96. return Expr{}, &ExprError {
  97. Point: info.ErrorPoint,
  98. Concrete: E_NoneOfTypesAssignable {
  99. From: types_desc,
  100. To: ctx.DescribeExpectedType(expected),
  101. },
  102. }
  103. }
  104. func DesugarExpr(expr node.Expr) node.Call {
  105. return DesugarPipeline(expr.Call, expr.Pipeline)
  106. }
  107. func DesugarPipeline(left node.Call, p node.MaybePipeline) node.Call {
  108. var pipeline, ok = p.(node.Pipeline)
  109. if !ok {
  110. return left
  111. }
  112. var f = pipeline.Func
  113. var maybe_right = pipeline.Arg
  114. var right, exists = maybe_right.(node.Call)
  115. var arg node.Tuple
  116. var current_node node.Node
  117. if exists {
  118. arg = node.Tuple {
  119. Node: pipeline.Operator.Node,
  120. Elements: []node.Expr {
  121. node.Expr {
  122. Node: left.Node,
  123. Call: left,
  124. Pipeline: nil,
  125. },
  126. node.Expr {
  127. Node: right.Node,
  128. Call: right,
  129. Pipeline: nil,
  130. },
  131. },
  132. }
  133. current_node = node.Node {
  134. Point: pipeline.Node.Point,
  135. Span: scanner.Span {
  136. Start: pipeline.Node.Span.Start,
  137. End: right.Span.End,
  138. },
  139. UID: 0,
  140. }
  141. } else {
  142. arg = node.Tuple {
  143. Node: left.Node,
  144. Elements: []node.Expr { {
  145. Node: left.Node,
  146. Call: left,
  147. Pipeline: nil,
  148. } },
  149. }
  150. current_node = node.Node {
  151. Point: pipeline.Node.Point,
  152. Span: scanner.Span {
  153. Start: pipeline.Node.Span.Start,
  154. End: pipeline.Func.Span.End,
  155. },
  156. UID: 0,
  157. }
  158. }
  159. var current = node.Call {
  160. Node: current_node,
  161. Func: f,
  162. Arg: node.Call {
  163. Node: arg.Node,
  164. Func: node.VariousTerm {
  165. Node: arg.Node,
  166. Term: arg,
  167. },
  168. Arg: nil,
  169. },
  170. }
  171. return DesugarPipeline(current, pipeline.Next)
  172. }
  173. func DesugarInfix(infix node.Infix) node.Call {
  174. return node.Call {
  175. Node: infix.Node,
  176. Func: infix.Operator,
  177. Arg: node.Call {
  178. Node: infix.Node,
  179. Func: node.VariousTerm {
  180. Node: infix.Node,
  181. Term: node.Tuple {
  182. Node: infix.Node,
  183. Elements: []node.Expr {
  184. node.Expr {
  185. Node: infix.Operand1.Node,
  186. Call: node.Call {
  187. Node: infix.Operand1.Node,
  188. Func: infix.Operand1,
  189. Arg: nil,
  190. },
  191. Pipeline: nil,
  192. },
  193. node.Expr {
  194. Node: infix.Operand2.Node,
  195. Call: node.Call {
  196. Node: infix.Operand2.Node,
  197. Func: infix.Operand2,
  198. Arg: nil,
  199. },
  200. Pipeline: nil,
  201. },
  202. },
  203. },
  204. },
  205. Arg: nil,
  206. },
  207. }
  208. }