class.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package node
  2. import (
  3. "fw/cp/constant"
  4. "fw/cp/constant/enter"
  5. "fw/cp/constant/operation"
  6. "fw/cp/constant/statement"
  7. "fw/cp/object"
  8. "ypk/assert"
  9. )
  10. const INIT constant.Class = -1
  11. const COMPOUND constant.Class = -2
  12. type InitNode interface {
  13. Node
  14. self() InitNode
  15. }
  16. type CompNode interface {
  17. Node
  18. self() CompNode
  19. }
  20. func New(class constant.Class, id int) (ret Node) {
  21. switch class {
  22. case constant.ENTER:
  23. ret = new(enterNode)
  24. case constant.ASSIGN:
  25. ret = new(assignNode)
  26. case constant.VARIABLE:
  27. ret = new(variableNode)
  28. case constant.DYADIC:
  29. ret = new(dyadicNode)
  30. case constant.CONSTANT:
  31. ret = new(constantNode)
  32. case constant.CALL:
  33. ret = new(callNode)
  34. case constant.PROCEDURE:
  35. ret = new(procedureNode)
  36. case constant.PARAMETER:
  37. ret = new(parameterNode)
  38. case constant.RETURN:
  39. ret = new(returnNode)
  40. case constant.MONADIC:
  41. ret = new(monadicNode)
  42. case constant.CONDITIONAL:
  43. ret = new(conditionalNode)
  44. case constant.IF:
  45. ret = new(ifNode)
  46. case constant.REPEAT:
  47. ret = new(repeatNode)
  48. case constant.WHILE:
  49. ret = new(whileNode)
  50. case constant.EXIT:
  51. ret = new(exitNode)
  52. case constant.LOOP:
  53. ret = new(loopNode)
  54. case constant.DEREF:
  55. ret = new(derefNode)
  56. case constant.FIELD:
  57. ret = new(fieldNode)
  58. case INIT:
  59. ret = new(initNode)
  60. case constant.INDEX:
  61. ret = new(indexNode)
  62. case constant.TRAP:
  63. ret = new(trapNode)
  64. case constant.WITH:
  65. ret = new(withNode)
  66. case constant.GUARD:
  67. ret = new(guardNode)
  68. case constant.CASE:
  69. ret = new(caseNode)
  70. case constant.ELSE:
  71. ret = new(elseNode)
  72. case constant.DO:
  73. ret = new(doNode)
  74. case constant.RANGE:
  75. ret = new(rangeNode)
  76. case COMPOUND:
  77. ret = new(compNode)
  78. default:
  79. panic("no such class")
  80. }
  81. ret.Adr(id)
  82. return ret
  83. }
  84. type nodeFields struct {
  85. left, right, link Node
  86. obj object.Object
  87. adr int
  88. }
  89. func (nf *nodeFields) Adr(a ...int) int {
  90. assert.For(len(a) <= 1, 20)
  91. if len(a) == 1 {
  92. nf.adr = a[0]
  93. }
  94. return nf.adr
  95. }
  96. func (nf *nodeFields) SetLeft(n Node) { nf.left = n }
  97. func (nf *nodeFields) SetRight(n Node) { nf.right = n }
  98. func (nf *nodeFields) SetLink(n Node) { nf.link = n }
  99. func (nf *nodeFields) SetObject(o object.Object) { nf.obj = o; o.SetRef(nf) }
  100. func (nf *nodeFields) Left() Node { return nf.left }
  101. func (nf *nodeFields) Right() Node { return nf.right }
  102. func (nf *nodeFields) Link() Node { return nf.link }
  103. func (nf *nodeFields) Object() object.Object { return nf.obj }
  104. type enterNode struct {
  105. nodeFields
  106. enter enter.Enter
  107. }
  108. func (e *enterNode) SetEnter(enter enter.Enter) { e.enter = enter }
  109. func (e *enterNode) Enter() enter.Enter { return e.enter }
  110. type constantNode struct {
  111. nodeFields
  112. typ object.Type
  113. data interface{}
  114. min, max *int
  115. }
  116. func (c *constantNode) SetType(t object.Type) { c.typ = t }
  117. func (c *constantNode) SetData(data interface{}) { c.data = data }
  118. func (c *constantNode) Data() interface{} { return c.data }
  119. func (c *constantNode) Type() object.Type { return c.typ }
  120. func (c *constantNode) SetMax(x int) { c.max = new(int); *c.max = x }
  121. func (c *constantNode) Max() *int { return c.max }
  122. func (c *constantNode) SetMin(x int) { c.min = new(int); *c.min = x }
  123. func (c *constantNode) Min() *int { return c.min }
  124. type dyadicNode struct {
  125. nodeFields
  126. operation operation.Operation
  127. }
  128. func (d *dyadicNode) SetOperation(op operation.Operation) { d.operation = op }
  129. func (d *dyadicNode) Operation() operation.Operation { return d.operation }
  130. func (d *dyadicNode) self() DyadicNode { return d }
  131. type assignNode struct {
  132. nodeFields
  133. stat statement.Statement
  134. }
  135. func (a *assignNode) self() AssignNode { return a }
  136. func (a *assignNode) SetStatement(s statement.Statement) { a.stat = s }
  137. func (a *assignNode) Statement() statement.Statement { return a.stat }
  138. type variableNode struct {
  139. nodeFields
  140. }
  141. func (v *variableNode) self() VariableNode { return v }
  142. type callNode struct {
  143. nodeFields
  144. }
  145. func (v *callNode) self() CallNode { return v }
  146. type procedureNode struct {
  147. nodeFields
  148. super bool
  149. }
  150. func (v *procedureNode) self() ProcedureNode { return v }
  151. func (v *procedureNode) Super(x ...string) bool {
  152. if len(x) > 0 {
  153. v.super = x[0] == "super"
  154. }
  155. return v.super
  156. }
  157. type parameterNode struct {
  158. nodeFields
  159. }
  160. func (v *parameterNode) self() ParameterNode { return v }
  161. type returnNode struct {
  162. nodeFields
  163. }
  164. func (v *returnNode) self() ReturnNode { return v }
  165. type monadicNode struct {
  166. nodeFields
  167. operation operation.Operation
  168. typ object.Type
  169. }
  170. func (v *monadicNode) self() MonadicNode { return v }
  171. func (v *monadicNode) SetOperation(op operation.Operation) { v.operation = op }
  172. func (v *monadicNode) Operation() operation.Operation { return v.operation }
  173. func (v *monadicNode) SetType(t object.Type) { v.typ = t }
  174. func (v *monadicNode) Type() object.Type { return v.typ }
  175. type conditionalNode struct {
  176. nodeFields
  177. }
  178. func (v *conditionalNode) self() ConditionalNode { return v }
  179. type ifNode struct {
  180. nodeFields
  181. }
  182. func (v *ifNode) self() IfNode { return v }
  183. type whileNode struct {
  184. nodeFields
  185. }
  186. func (v *whileNode) self() WhileNode { return v }
  187. type repeatNode struct {
  188. nodeFields
  189. }
  190. func (v *repeatNode) self() RepeatNode { return v }
  191. type exitNode struct {
  192. nodeFields
  193. }
  194. func (v *exitNode) self() ExitNode { return v }
  195. type loopNode struct {
  196. nodeFields
  197. }
  198. func (v *loopNode) self() LoopNode { return v }
  199. type derefNode struct {
  200. nodeFields
  201. ptr bool
  202. }
  203. func (v *derefNode) self() DerefNode { return v }
  204. func (c *derefNode) Ptr(x ...string) bool {
  205. if len(x) > 0 {
  206. c.ptr = x[0] == "ptr"
  207. }
  208. return c.ptr
  209. }
  210. type fieldNode struct {
  211. nodeFields
  212. }
  213. func (v *fieldNode) self() FieldNode { return v }
  214. type initNode struct {
  215. nodeFields
  216. }
  217. func (v *initNode) self() InitNode { return v }
  218. type compNode struct {
  219. nodeFields
  220. }
  221. func (v *compNode) self() CompNode { return v }
  222. type indexNode struct {
  223. nodeFields
  224. }
  225. func (v *indexNode) self() IndexNode { return v }
  226. type trapNode struct {
  227. nodeFields
  228. }
  229. func (v *trapNode) self() TrapNode { return v }
  230. type withNode struct {
  231. nodeFields
  232. }
  233. func (v *withNode) self() WithNode { return v }
  234. type guardNode struct {
  235. nodeFields
  236. typ object.ComplexType
  237. }
  238. func (v *guardNode) self() GuardNode { return v }
  239. func (v *guardNode) SetType(t object.ComplexType) { v.typ = t }
  240. func (v *guardNode) Type() object.ComplexType { return v.typ }
  241. type caseNode struct {
  242. nodeFields
  243. }
  244. func (v *caseNode) self() CaseNode { return v }
  245. type elseNode struct {
  246. nodeFields
  247. min, max int
  248. }
  249. func (v *elseNode) Min(x ...int) int {
  250. if len(x) > 0 {
  251. v.min = x[0]
  252. }
  253. return v.min
  254. }
  255. //func (c *elseNode) SetMax(x int) { c.max = x }
  256. func (c *elseNode) Max(x ...int) int {
  257. if len(x) > 0 {
  258. c.max = x[0]
  259. }
  260. return c.max
  261. }
  262. //func (c *elseNode) SetMin(x int) { c.min = x }
  263. type doNode struct {
  264. nodeFields
  265. }
  266. func (v *doNode) self() DoNode { return v }
  267. type rangeNode struct {
  268. nodeFields
  269. }
  270. func (v *rangeNode) self() RangeNode { return v }