array.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package checker
  2. import "kumachan/transformer/node"
  3. func (impl SemiTypedArray) SemiExprVal() {}
  4. type SemiTypedArray struct {
  5. Items [] SemiExpr
  6. }
  7. func (impl Array) ExprVal() {}
  8. type Array struct {
  9. Items [] Expr
  10. }
  11. func CheckArray(array node.Array, ctx ExprContext) (SemiExpr, *ExprError) {
  12. var info = ctx.GetExprInfo(array.Node)
  13. var L = len(array.Items)
  14. if L == 0 {
  15. return SemiExpr {
  16. Value: SemiTypedArray { make([]SemiExpr, 0) },
  17. Info: info,
  18. }, nil
  19. } else {
  20. var item_exprs = make([]SemiExpr, L)
  21. for i, item_node := range array.Items {
  22. var item, err = Check(item_node, ctx)
  23. if err != nil { return SemiExpr{}, err }
  24. item_exprs[i] = item
  25. }
  26. return SemiExpr {
  27. Value: SemiTypedArray { item_exprs },
  28. Info: info,
  29. }, nil
  30. }
  31. }
  32. func AssignArrayTo(expected Type, array SemiTypedArray, info ExprInfo, ctx ExprContext) (Expr, *ExprError) {
  33. if expected == nil {
  34. var cur_item_type Type = nil
  35. var items = make([]Expr, len(array.Items))
  36. for i, item_semi := range array.Items {
  37. var item, err = AssignTo(cur_item_type, item_semi, ctx)
  38. if err != nil { return Expr{}, err }
  39. items[i] = item
  40. }
  41. return Expr {
  42. Type: expected,
  43. Info: info,
  44. Value: Array { items },
  45. }, nil
  46. }
  47. switch E := expected.(type) {
  48. case NamedType:
  49. if E.Name == __Array {
  50. if len(E.Args) != 1 { panic("something went wrong") }
  51. var item_type = E.Args[0]
  52. var items = make([]Expr, len(array.Items))
  53. for i, item_semi := range array.Items {
  54. var item, err = AssignTo(item_type, item_semi, ctx)
  55. if err != nil { return Expr{}, err }
  56. items[i] = item
  57. }
  58. return Expr {
  59. Type: expected,
  60. Info: info,
  61. Value: Array { items },
  62. }, nil
  63. }
  64. }
  65. return Expr{}, &ExprError {
  66. Point: info.ErrorPoint,
  67. Concrete: E_ArrayAssignedToNonArrayType {},
  68. }
  69. }