traversal.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package typsys
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func Equal(t1 Type, t2 Type) bool {
  7. switch T1 := t1.(type) {
  8. case InferringType:
  9. if T2, ok := t2.(InferringType); ok {
  10. return (T1.Id == T2.Id)
  11. }
  12. case ParameterType:
  13. if T2, ok := t2.(ParameterType); ok {
  14. return (T1.Name == T2.Name)
  15. }
  16. case RefType:
  17. if T2, ok := t2.(RefType); ok {
  18. if T1.Def == T2.Def {
  19. if len(T1.Args) == len(T2.Args) {
  20. var n = len(T1.Args)
  21. var all_equal = true
  22. for i := 0; i < n; i += 1 {
  23. var equal = Equal(T1.Args[i], T2.Args[i])
  24. if !(equal) {
  25. all_equal = false
  26. break
  27. }
  28. }
  29. return all_equal
  30. }
  31. }
  32. }
  33. }
  34. return false
  35. }
  36. func Transform(t Type, f func(t Type)(Type,bool)) Type {
  37. switch T := t.(type) {
  38. case RefType:
  39. var mapped_args = make([] Type, len(T.Args))
  40. for i, arg := range T.Args {
  41. mapped_args[i] = Transform(arg, f)
  42. }
  43. var t = RefType {
  44. Def: T.Def,
  45. Args: mapped_args,
  46. }
  47. if u, ok := f(t); ok {
  48. return u
  49. } else {
  50. return t
  51. }
  52. default:
  53. if u, ok := f(t); ok {
  54. return u
  55. } else {
  56. return t
  57. }
  58. }
  59. }
  60. func Describe(t Type) string {
  61. switch T := t.(type) {
  62. case InferringType:
  63. return ("(" + T.Id + ")")
  64. case ParameterType:
  65. return T.Name
  66. case RefType:
  67. if len(T.Args) == 0 {
  68. return T.Def.String()
  69. } else {
  70. var name_desc = T.Def.String()
  71. var n = len(T.Args)
  72. var arg_desc = make([] string, n)
  73. for i := 0; i < n; i += 1 {
  74. arg_desc[i] = Describe(T.Args[i])
  75. }
  76. var args_desc = strings.Join(arg_desc, ",")
  77. return fmt.Sprintf("%s[%s]", name_desc, args_desc)
  78. }
  79. default:
  80. panic("impossible branch")
  81. }
  82. }
  83. func Inflate(t Type, params ([] string), args ([] Type)) Type {
  84. return Transform(t, func(t Type) (Type, bool) {
  85. switch T := t.(type) {
  86. case InferringType:
  87. panic("invalid argument")
  88. case ParameterType:
  89. for i := range params {
  90. if params[i] == T.Name {
  91. if i < len(args) {
  92. return args[i], true
  93. }
  94. }
  95. }
  96. }
  97. return nil, false
  98. })
  99. }
  100. func DescribeCertain(t CertainType) string {
  101. return Describe(t.Type)
  102. }
  103. func DescribeWithInferringState(t Type, s *InferringState) string {
  104. if s == nil {
  105. return Describe(t)
  106. } else {
  107. return Describe(Transform(t, func(t Type) (Type, bool) {
  108. switch T := t.(type) {
  109. case InferringType:
  110. var current, has_current = s.getInferred(T.Id)
  111. if has_current {
  112. return current, true
  113. }
  114. }
  115. return nil, false
  116. }))
  117. }
  118. }