expr.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package checked
  2. import (
  3. "sort"
  4. "reflect"
  5. "kumachan/interpreter/lang/common/name"
  6. "kumachan/interpreter/lang/common/source"
  7. "kumachan/interpreter/lang/common/limits"
  8. "kumachan/interpreter/compiler/checker/typsys"
  9. )
  10. type Expr struct {
  11. Type typsys.Type // change to CertainType when generics available
  12. Info ExprInfo
  13. Content ExprContent
  14. }
  15. type ExprInfo struct {
  16. Location source.Location
  17. }
  18. func ExprInfoFrom(loc source.Location) ExprInfo {
  19. return ExprInfo { Location: loc }
  20. }
  21. type ExprContent interface { implExpr() }
  22. func (UnitValue) implExpr() {}
  23. type UnitValue struct {}
  24. func (Argument) implExpr() {}
  25. type Argument struct {}
  26. func (Wrapper) implExpr() {}
  27. type Wrapper struct {
  28. Inner *Expr
  29. }
  30. func (FuncRef) implExpr() {}
  31. type FuncRef struct {
  32. Name name.FunctionName
  33. Implicit [] *Expr
  34. }
  35. func (LocalRef) implExpr() {}
  36. type LocalRef struct {
  37. Binding *LocalBinding
  38. }
  39. func (Call) implExpr() {}
  40. type Call struct {
  41. Callee *Expr
  42. Argument *Expr
  43. }
  44. func (Interface) implExpr() {}
  45. type Interface struct {
  46. ConcreteValue *Expr
  47. DispatchTable *DispatchTable
  48. }
  49. func (InterfaceTransform) implExpr() {}
  50. type InterfaceTransform struct {
  51. Argument *Expr
  52. Path InterfaceTransformPath
  53. }
  54. type InterfaceTransformPath ([] uint)
  55. func (Enum) implExpr() {}
  56. type Enum struct {
  57. Index uint
  58. Value *Expr
  59. }
  60. func (Tuple) implExpr() {}
  61. type Tuple struct {
  62. Parallel bool
  63. Elements [] *Expr
  64. }
  65. func (TupleUpdate) implExpr() {}
  66. type TupleUpdate struct {
  67. Tuple *Expr
  68. Replaced [] TupleUpdateElement
  69. }
  70. type TupleUpdateElement struct {
  71. Index uint
  72. Value *Expr
  73. }
  74. func (TupleGet) implExpr() {}
  75. type TupleGet struct {
  76. Tuple *Expr
  77. Index uint
  78. }
  79. func (InterfaceMethod) implExpr() {}
  80. type InterfaceMethod struct {
  81. Interface *Expr
  82. Index uint
  83. }
  84. func (List) implExpr() {}
  85. type List struct {
  86. Items [] *Expr
  87. Info ListInfo
  88. }
  89. type ListInfo struct {
  90. Compact bool
  91. ItemType reflect.Type
  92. }
  93. func (InteriorRef) implExpr() {}
  94. type InteriorRef struct {
  95. Base *Expr
  96. Index uint
  97. Table *DispatchTable // optional, used in dynamic cast
  98. Kind InteriorRefKind
  99. Operand InteriorRefOperand
  100. }
  101. type InteriorRefKind int
  102. const (
  103. RK_Proj InteriorRefKind = iota
  104. RK_Case
  105. RK_InterfaceCase
  106. )
  107. type InteriorRefOperand int
  108. const (
  109. RO_Record InteriorRefOperand = iota
  110. RO_EnumOrInterface
  111. RO_ProjRef
  112. RO_CaseRef
  113. )
  114. func (NumericLiteral) implExpr() {}
  115. type NumericLiteral struct {
  116. Value interface {}
  117. }
  118. func (StringLiteral) implExpr() {}
  119. type StringLiteral struct {
  120. Value string
  121. }
  122. func (StringFormatter) implExpr() {}
  123. type StringFormatter struct {
  124. Template string
  125. Parameters [] uint
  126. }
  127. func (Lambda) implExpr() {}
  128. type Lambda struct {
  129. In ProductPatternInfo
  130. Out *Expr
  131. Self *LocalBinding
  132. }
  133. func (Block) implExpr() {}
  134. type Block struct {
  135. LetList [] Let
  136. Return *Expr
  137. }
  138. type Let struct {
  139. Pattern ProductPatternInfo
  140. Input *Expr
  141. }
  142. func NestedLambdaCalls(block Block, info ExprInfo) *Expr {
  143. var L = len(block.LetList)
  144. if L == 0 { return block.Return }
  145. var expr = block.Return
  146. for i := range block.LetList {
  147. var r = ((L - 1) - i)
  148. var let = block.LetList[r]
  149. var lambda = &Expr {
  150. Type: &typsys.NestedType { Content:
  151. typsys.Lambda {
  152. Input: let.Input.Type,
  153. Output: expr.Type,
  154. } },
  155. Info: expr.Info,
  156. Content: Lambda {
  157. In: let.Pattern,
  158. Out: expr,
  159. },
  160. }
  161. var call = &Expr {
  162. Type: expr.Type,
  163. Info: let.Input.Info,
  164. Content: Call {
  165. Callee: lambda,
  166. Argument: let.Input,
  167. },
  168. }
  169. expr = call
  170. }
  171. return &Expr {
  172. Type: expr.Type,
  173. Info: info,
  174. Content: expr.Content,
  175. }
  176. }
  177. func (Switch) implExpr() {}
  178. type Switch struct {
  179. Argument *Expr
  180. HasDefault bool
  181. Default Branch
  182. Branches map[uint] Branch
  183. }
  184. func (s *Switch) SortedBranchKeys() ([] uint) {
  185. var int_keys = make([] int, 0)
  186. for key := range s.Branches {
  187. int_keys = append(int_keys, int(key))
  188. }
  189. sort.Ints(int_keys)
  190. var uint_keys = make([] uint, len(int_keys))
  191. for i, k := range int_keys {
  192. uint_keys[i] = uint(k)
  193. }
  194. return uint_keys
  195. }
  196. func (Select) implExpr() {}
  197. type Select struct {
  198. Arguments [] *Expr
  199. HasDefault bool
  200. Default Branch
  201. Branches map[SelectBranchKey] Branch
  202. }
  203. func (s *Select) SortedBranchKeys() ([] SelectBranchKey) {
  204. var keys = make(SelectBranchKeys, 0)
  205. for key := range s.Branches {
  206. keys = append(keys, key)
  207. }
  208. sort.Sort(keys)
  209. return keys
  210. }
  211. type SelectBranchKey ([limits.MaxSelectArity] uint)
  212. type SelectBranchKeys ([] SelectBranchKey)
  213. func (keys SelectBranchKeys) Len() int {
  214. return len(keys)
  215. }
  216. func (keys SelectBranchKeys) Less(u, v int) bool {
  217. for i := 0; i < limits.MaxSelectArity; i += 1 {
  218. var x = keys[u][i]
  219. var y = keys[v][i]
  220. if x < y {
  221. return true
  222. } else if x == y {
  223. continue
  224. } else {
  225. return false
  226. }
  227. }
  228. return false
  229. }
  230. func (keys SelectBranchKeys) Swap(u, v int) {
  231. var t = keys[u]
  232. keys[u] = keys[v]
  233. keys[v] = t
  234. }
  235. type Branch struct {
  236. Lambda
  237. }
  238. func (b *Branch) ToExpr() *Expr {
  239. return &Expr {
  240. Type: b.Out.Type,
  241. Info: b.Out.Info,
  242. Content: *b, // note: *Branch will also implement ExprContent
  243. }
  244. }