ast.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Abstract Syntax tree for the MUESLI interpreter
  2. package muesli
  3. import "fmt"
  4. /* AST node kind */
  5. type Ast struct {
  6. Parent * Ast
  7. Child * Ast
  8. Before * Ast
  9. After * Ast
  10. AstKind
  11. *Token
  12. }
  13. func NewAst(kind AstKind, parent * Ast, token * Token) *Ast {
  14. child := &Ast{parent, nil, nil, nil, kind, token}
  15. return child
  16. }
  17. func (ast * Ast) LastSibling() * Ast {
  18. res := ast
  19. for res != nil && res.After != nil {
  20. res = res.After
  21. }
  22. return res
  23. }
  24. func (ast * Ast) LastChild() * Ast {
  25. return ast.Child.LastSibling()
  26. }
  27. /* Detaches, I.E removes this node and all it's children from the parent tree. */
  28. func (ast * Ast) Remove() * Ast {
  29. parent := ast.Parent
  30. before := ast.Before
  31. after := ast.After
  32. if before != nil {
  33. before.After = after
  34. }
  35. if after != nil {
  36. after.Before = before
  37. }
  38. if parent != nil {
  39. /* Special case if ast is the first child of it's parent. */
  40. if ast == parent.Child {
  41. parent.Child = after
  42. }
  43. }
  44. ast.Parent = nil
  45. return ast
  46. }
  47. func (ast * Ast) InsertSibling(sibling * Ast) * Ast {
  48. after := ast.After
  49. ast.After = sibling
  50. sibling.Before = ast
  51. sibling.After = after
  52. if after != nil {
  53. after.Before = sibling
  54. }
  55. sibling.Parent = ast.Parent
  56. return sibling
  57. }
  58. func (ast * Ast) AppendSibling(sibling * Ast) * Ast {
  59. return ast.LastSibling().InsertSibling(sibling)
  60. }
  61. func (ast * Ast) AppendChild(child * Ast) * Ast {
  62. child.Parent = ast
  63. if ast.Child == nil {
  64. ast.Child = child
  65. } else {
  66. ast.Child.AppendSibling(child)
  67. }
  68. return child
  69. }
  70. func (ast * Ast) NewSibling(kind AstKind, token * Token) * Ast {
  71. sibling := NewAst(kind, ast.Parent, token)
  72. return ast.AppendSibling(sibling)
  73. }
  74. func (ast * Ast) NewChild(kind AstKind, token * Token) * Ast {
  75. sibling := NewAst(kind, ast.Parent, token)
  76. return ast.AppendChild(sibling)
  77. }
  78. func (ast * Ast) Walk(walker func(node * Ast) * Ast) * Ast {
  79. if found := walker(ast); found != nil {
  80. return found
  81. }
  82. if ast.Child != nil {
  83. if found := ast.Child.Walk(walker); found != nil {
  84. return found
  85. }
  86. }
  87. if ast.After != nil {
  88. if found := ast.After.Walk(walker); found != nil {
  89. return found
  90. }
  91. }
  92. return nil
  93. }
  94. func (ast * Ast) String() string {
  95. return fmt.Sprintf("Ast %d %v", ast.AstKind, ast.Token)
  96. }
  97. func (ast * Ast) Display() {
  98. ast.Walk(func(node * Ast) * Ast {
  99. fmt.Printf("Tree: %s\n", node)
  100. return nil
  101. })
  102. }
  103. func (ast * Ast) Depth() int {
  104. var depth int = 0;
  105. parent := ast.Parent
  106. for parent != nil {
  107. depth ++;
  108. parent = parent.Parent
  109. }
  110. return depth
  111. }
  112. func (ast * Ast) CountChildren() int {
  113. var count int = 0;
  114. child := ast.Child
  115. for child != nil {
  116. count ++;
  117. child = child.After
  118. }
  119. return count;
  120. }