node.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* 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 */
  2. #include "debug.h"
  3. #include <alloc.h>
  4. #include <em_arith.h>
  5. #include <em_label.h>
  6. #include <system.h>
  7. #include "LLlex.h"
  8. #include "node.h"
  9. #include "type.h"
  10. struct node *
  11. MkNode(class, left, right, token)
  12. struct node *left, *right;
  13. struct token *token;
  14. {
  15. /* Create a node and initialize it with the given parameters
  16. */
  17. register struct node *nd = new_node();
  18. nd->nd_left = left;
  19. nd->nd_right = right;
  20. nd->nd_token = *token;
  21. nd->nd_class = class;
  22. nd->nd_type = error_type;
  23. return nd;
  24. }
  25. struct node *
  26. MkLeaf(class, token)
  27. struct token *token;
  28. {
  29. register struct node *nd = new_node();
  30. nd->nd_left = nd->nd_right = NULLNODE;
  31. nd->nd_token = *token;
  32. nd->nd_type = error_type;
  33. nd->nd_class = class;
  34. return nd;
  35. }
  36. FreeNode(nd)
  37. register struct node *nd;
  38. {
  39. /* Put nodes that are no longer needed back onto the free list
  40. */
  41. if( !nd ) return;
  42. FreeNode(nd->nd_left);
  43. FreeNode(nd->nd_right);
  44. free_node(nd);
  45. }
  46. NodeCrash(expp)
  47. struct node *expp;
  48. {
  49. crash("Illegal node %d", expp->nd_class);
  50. }
  51. #ifdef DEBUG
  52. extern char *symbol2str();
  53. indnt(lvl)
  54. {
  55. while( lvl-- )
  56. print(" ");
  57. }
  58. printnode(nd, lvl)
  59. register struct node *nd;
  60. {
  61. indnt(lvl);
  62. print("Class: %d; Symbol: %s\n", nd->nd_class, symbol2str(nd->nd_symb));
  63. if( nd->nd_type ) {
  64. indnt(lvl);
  65. print("Type: ");
  66. DumpType(nd->nd_type);
  67. print("\n");
  68. }
  69. }
  70. PrNode(nd, lvl)
  71. register struct node *nd;
  72. {
  73. if( !nd ) {
  74. indnt(lvl); print("<nilnode>\n");
  75. return;
  76. }
  77. PrNode(nd->nd_left, lvl + 1);
  78. printnode(nd, lvl);
  79. PrNode(nd->nd_right, lvl + 1);
  80. }
  81. #endif