node.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package node
  2. import (
  3. "fw/cp/constant"
  4. "fw/cp/object"
  5. )
  6. type Node interface {
  7. SetLeft(n Node)
  8. SetRight(n Node)
  9. SetLink(n Node)
  10. SetObject(o object.Object)
  11. Left() Node
  12. Right() Node
  13. Link() Node
  14. Object() object.Object
  15. }
  16. func New(class constant.Class) Node {
  17. switch class {
  18. case constant.ENTER:
  19. return new(enterNode)
  20. case constant.ASSIGN:
  21. return new(assignNode)
  22. case constant.VARIABLE:
  23. return new(variableNode)
  24. case constant.DYADIC:
  25. return new(dyadicNode)
  26. case constant.CONSTANT:
  27. return new(constantNode)
  28. case constant.CALL:
  29. return new(callNode)
  30. case constant.PROCEDURE:
  31. return new(procedureNode)
  32. case constant.PARAMETER:
  33. return new(parameterNode)
  34. case constant.RETURN:
  35. return new(returnNode)
  36. case constant.MONADIC:
  37. return new(monadicNode)
  38. case constant.CONDITIONAL:
  39. return new(conditionalNode)
  40. case constant.IF:
  41. return new(ifNode)
  42. case constant.WHILE:
  43. return new(whileNode)
  44. default:
  45. panic("no such class")
  46. }
  47. }
  48. type nodeFields struct {
  49. left, right, link Node
  50. obj object.Object
  51. }
  52. func (nf *nodeFields) SetLeft(n Node) { nf.left = n }
  53. func (nf *nodeFields) SetRight(n Node) { nf.right = n }
  54. func (nf *nodeFields) SetLink(n Node) { nf.link = n }
  55. func (nf *nodeFields) SetObject(o object.Object) { nf.obj = o }
  56. func (nf *nodeFields) Left() Node { return nf.left }
  57. func (nf *nodeFields) Right() Node { return nf.right }
  58. func (nf *nodeFields) Link() Node { return nf.link }
  59. func (nf *nodeFields) Object() object.Object { return nf.obj }