class.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package node
  2. import (
  3. "fw/cp/constant"
  4. "fw/cp/constant/enter"
  5. "fw/cp/constant/operation"
  6. "fw/cp/object"
  7. "fw/cp/statement"
  8. )
  9. const INIT constant.Class = -1
  10. type InitNode interface {
  11. Node
  12. }
  13. func New(class constant.Class) Node {
  14. switch class {
  15. case constant.ENTER:
  16. return new(enterNode)
  17. case constant.ASSIGN:
  18. return new(assignNode)
  19. case constant.VARIABLE:
  20. return new(variableNode)
  21. case constant.DYADIC:
  22. return new(dyadicNode)
  23. case constant.CONSTANT:
  24. return new(constantNode)
  25. case constant.CALL:
  26. return new(callNode)
  27. case constant.PROCEDURE:
  28. return new(procedureNode)
  29. case constant.PARAMETER:
  30. return new(parameterNode)
  31. case constant.RETURN:
  32. return new(returnNode)
  33. case constant.MONADIC:
  34. return new(monadicNode)
  35. case constant.CONDITIONAL:
  36. return new(conditionalNode)
  37. case constant.IF:
  38. return new(ifNode)
  39. case constant.REPEAT:
  40. return new(repeatNode)
  41. case constant.WHILE:
  42. return new(whileNode)
  43. case constant.EXIT:
  44. return new(exitNode)
  45. case constant.LOOP:
  46. return new(loopNode)
  47. case constant.DEREF:
  48. return new(derefNode)
  49. case constant.FIELD:
  50. return new(fieldNode)
  51. case INIT:
  52. return new(initNode)
  53. default:
  54. panic("no such class")
  55. }
  56. }
  57. type nodeFields struct {
  58. left, right, link Node
  59. obj object.Object
  60. }
  61. func (nf *nodeFields) SetLeft(n Node) { nf.left = n }
  62. func (nf *nodeFields) SetRight(n Node) { nf.right = n }
  63. func (nf *nodeFields) SetLink(n Node) { nf.link = n }
  64. func (nf *nodeFields) SetObject(o object.Object) { nf.obj = o }
  65. func (nf *nodeFields) Left() Node { return nf.left }
  66. func (nf *nodeFields) Right() Node { return nf.right }
  67. func (nf *nodeFields) Link() Node { return nf.link }
  68. func (nf *nodeFields) Object() object.Object { return nf.obj }
  69. type enterNode struct {
  70. nodeFields
  71. enter enter.Enter
  72. }
  73. func (e *enterNode) SetEnter(enter enter.Enter) { e.enter = enter }
  74. func (e *enterNode) Enter() enter.Enter { return e.enter }
  75. type constantNode struct {
  76. nodeFields
  77. typ object.Type
  78. data interface{}
  79. }
  80. func (c *constantNode) SetType(t object.Type) { c.typ = t }
  81. func (c *constantNode) SetData(data interface{}) { c.data = data }
  82. func (c *constantNode) Data() interface{} { return c.data }
  83. func (c *constantNode) Type() object.Type { return c.typ }
  84. type dyadicNode struct {
  85. nodeFields
  86. operation operation.Operation
  87. }
  88. func (d *dyadicNode) SetOperation(op operation.Operation) { d.operation = op }
  89. func (d *dyadicNode) Operation() operation.Operation { return d.operation }
  90. func (d *dyadicNode) self() DyadicNode { return d }
  91. type assignNode struct {
  92. nodeFields
  93. stat statement.Statement
  94. }
  95. func (a *assignNode) self() AssignNode { return a }
  96. func (a *assignNode) SetStatement(s statement.Statement) { a.stat = s }
  97. func (a *assignNode) Statement() statement.Statement { return a.stat }
  98. type variableNode struct {
  99. nodeFields
  100. }
  101. func (v *variableNode) self() VariableNode { return v }
  102. type callNode struct {
  103. nodeFields
  104. }
  105. func (v *callNode) self() CallNode { return v }
  106. type procedureNode struct {
  107. nodeFields
  108. }
  109. func (v *procedureNode) self() ProcedureNode { return v }
  110. type parameterNode struct {
  111. nodeFields
  112. }
  113. func (v *parameterNode) self() ParameterNode { return v }
  114. type returnNode struct {
  115. nodeFields
  116. }
  117. func (v *returnNode) self() ReturnNode { return v }
  118. type monadicNode struct {
  119. nodeFields
  120. operation operation.Operation
  121. typ object.Type
  122. }
  123. func (v *monadicNode) self() MonadicNode { return v }
  124. func (v *monadicNode) SetOperation(op operation.Operation) { v.operation = op }
  125. func (v *monadicNode) Operation() operation.Operation { return v.operation }
  126. func (v *monadicNode) SetType(t object.Type) { v.typ = t }
  127. func (v *monadicNode) Type() object.Type { return v.typ }
  128. type conditionalNode struct {
  129. nodeFields
  130. }
  131. func (v *conditionalNode) self() ConditionalNode { return v }
  132. type ifNode struct {
  133. nodeFields
  134. }
  135. func (v *ifNode) self() IfNode { return v }
  136. type whileNode struct {
  137. nodeFields
  138. }
  139. func (v *whileNode) self() WhileNode { return v }
  140. type repeatNode struct {
  141. nodeFields
  142. }
  143. func (v *repeatNode) self() RepeatNode { return v }
  144. type exitNode struct {
  145. nodeFields
  146. }
  147. func (v *exitNode) self() ExitNode { return v }
  148. type loopNode struct {
  149. nodeFields
  150. }
  151. func (v *loopNode) self() LoopNode { return v }
  152. type derefNode struct {
  153. nodeFields
  154. }
  155. func (v *derefNode) self() DerefNode { return v }
  156. type fieldNode struct {
  157. nodeFields
  158. }
  159. func (v *fieldNode) self() FieldNode { return v }
  160. type initNode struct {
  161. nodeFields
  162. }
  163. func (v *initNode) self() InitNode { return v }