class.go 6.0 KB

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