node.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. * Author: Ceriel J.H. Jacobs
  6. */
  7. /* N O D E O F A N A B S T R A C T P A R S E T R E E */
  8. /* $Id$ */
  9. #include "debug.h"
  10. #include <em_label.h>
  11. #include <em_arith.h>
  12. #include <alloc.h>
  13. #include <system.h>
  14. #include "LLlex.h"
  15. #include "def.h"
  16. #include "type.h"
  17. #include "node.h"
  18. #include "main.h"
  19. static int nsubnodes[] = {
  20. 0,
  21. 2,
  22. 2,
  23. 2,
  24. 2,
  25. 2,
  26. 1,
  27. 1,
  28. 2,
  29. 1,
  30. 2,
  31. 1,
  32. 2
  33. };
  34. t_node *
  35. getnode(class)
  36. {
  37. register t_node *nd = new_node();
  38. if (options['R']) nd->nd_flags |= ROPTION;
  39. if (options['A']) nd->nd_flags |= AOPTION;
  40. nd->nd_class = class;
  41. return nd;
  42. }
  43. t_node *
  44. dot2node(class, left, right)
  45. t_node *left, *right;
  46. {
  47. register t_node *nd = getnode(class);
  48. nd->nd_symb = dot.tk_symb;
  49. nd->nd_lineno = dot.tk_lineno;
  50. nd->nd_LEFT = left;
  51. nd->nd_RIGHT = right;
  52. return nd;
  53. }
  54. t_node *
  55. dot2leaf(class)
  56. {
  57. register t_node *nd = getnode(class);
  58. nd->nd_token = dot;
  59. switch(nsubnodes[class]) {
  60. case 1:
  61. nd->nd_NEXT = 0;
  62. break;
  63. case 2:
  64. nd->nd_LEFT = 0;
  65. nd->nd_RIGHT = 0;
  66. break;
  67. }
  68. return nd;
  69. }
  70. FreeNode(nd)
  71. register t_node *nd;
  72. {
  73. /* Put nodes that are no longer needed back onto the free
  74. list
  75. */
  76. if (!nd) return;
  77. switch(nsubnodes[nd->nd_class]) {
  78. case 2:
  79. FreeNode(nd->nd_LEFT);
  80. FreeNode(nd->nd_RIGHT);
  81. break;
  82. case 1:
  83. FreeNode(nd->nd_NEXT);
  84. break;
  85. }
  86. free_node(nd);
  87. }
  88. /*ARGSUSED*/
  89. NodeCrash(expp)
  90. t_node *expp;
  91. {
  92. crash("(NodeCrash) Illegal node");
  93. }
  94. /*ARGSUSED*/
  95. PNodeCrash(expp)
  96. t_node **expp;
  97. {
  98. crash("(PNodeCrash) Illegal node");
  99. }
  100. #ifdef DEBUG
  101. extern char *symbol2str();
  102. indnt(lvl)
  103. {
  104. while (lvl--) {
  105. print(" ");
  106. }
  107. }
  108. printnode(nd, lvl)
  109. register t_node *nd;
  110. {
  111. indnt(lvl);
  112. print("Class: %d; Symbol: %s; Flags: %d\n", nd->nd_class, symbol2str(nd->nd_symb), nd->nd_flags);
  113. if (nd->nd_type) {
  114. indnt(lvl);
  115. print("Type: ");
  116. DumpType(nd->nd_type);
  117. print("\n");
  118. }
  119. }
  120. PrNode(nd, lvl)
  121. register t_node *nd;
  122. {
  123. if (! nd) {
  124. indnt(lvl); print("<nilnode>\n");
  125. return;
  126. }
  127. printnode(nd, lvl);
  128. switch(nsubnodes[nd->nd_class]) {
  129. case 1:
  130. PrNode(nd->nd_LEFT, lvl + 1);
  131. PrNode(nd->nd_RIGHT, lvl + 1);
  132. break;
  133. case 2:
  134. PrNode(nd->nd_NEXT, lvl + 1);
  135. break;
  136. }
  137. }
  138. #endif /* DEBUG */