tree.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. ** tree.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_tree="$Id: tree.c,v 1.7 1994/11/16 18:09:11 roberto Exp roberto $";
  6. #include <string.h>
  7. #include "mem.h"
  8. #include "lua.h"
  9. #include "tree.h"
  10. #include "table.h"
  11. #define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
  12. typedef struct StringNode {
  13. struct StringNode *next;
  14. Word mark;
  15. char str[1];
  16. } StringNode;
  17. static StringNode *string_root = NULL;
  18. static TreeNode *constant_root = NULL;
  19. /*
  20. ** Insert a new constant/variable at the tree.
  21. */
  22. static TreeNode *tree_create (TreeNode **node, char *str)
  23. {
  24. if (*node == NULL)
  25. {
  26. *node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str));
  27. (*node)->left = (*node)->right = NULL;
  28. strcpy((*node)->str, str);
  29. (*node)->varindex = (*node)->constindex = UNMARKED_STRING;
  30. return *node;
  31. }
  32. else
  33. {
  34. int c = lua_strcmp(str, (*node)->str);
  35. if (c < 0)
  36. return tree_create(&(*node)->left, str);
  37. else if (c > 0)
  38. return tree_create(&(*node)->right, str);
  39. else
  40. return *node;
  41. }
  42. }
  43. char *lua_strcreate (char *str)
  44. {
  45. StringNode *newString;
  46. lua_pack();
  47. newString = (StringNode *)luaI_malloc(sizeof(StringNode)+strlen(str));
  48. newString->mark = UNMARKED_STRING;
  49. strcpy(newString->str, str);
  50. newString->next = string_root;
  51. string_root = newString;
  52. return newString->str;
  53. }
  54. TreeNode *lua_constcreate (char *str)
  55. {
  56. return tree_create(&constant_root, str);
  57. }
  58. /*
  59. ** Garbage collection function.
  60. ** This function traverse the string list freeing unindexed strings
  61. */
  62. int lua_strcollector (void)
  63. {
  64. StringNode *curr = string_root, *prev = NULL;
  65. int counter = 0;
  66. while (curr)
  67. {
  68. StringNode *next = curr->next;
  69. if (curr->mark == UNMARKED_STRING)
  70. {
  71. if (prev == NULL) string_root = next;
  72. else prev->next = next;
  73. luaI_free(curr);
  74. ++counter;
  75. }
  76. else
  77. {
  78. curr->mark = UNMARKED_STRING;
  79. prev = curr;
  80. }
  81. curr = next;
  82. }
  83. return counter;
  84. }
  85. /*
  86. ** Return next variable.
  87. */
  88. static TreeNode *tree_next (TreeNode *node, char *str)
  89. {
  90. if (node == NULL) return NULL;
  91. else if (str == NULL) return node;
  92. else
  93. {
  94. int c = lua_strcmp(str, node->str);
  95. if (c == 0)
  96. return node->left != NULL ? node->left : node->right;
  97. else if (c < 0)
  98. {
  99. TreeNode *result = tree_next(node->left, str);
  100. return result != NULL ? result : node->right;
  101. }
  102. else
  103. return tree_next(node->right, str);
  104. }
  105. }
  106. TreeNode *lua_varnext (char *n)
  107. {
  108. TreeNode *result;
  109. char *name = n;
  110. while (1)
  111. { /* repeat until a valid (non nil) variable */
  112. result = tree_next(constant_root, name);
  113. if (result == NULL) return NULL;
  114. if (result->varindex != UNMARKED_STRING &&
  115. s_tag(result->varindex) != LUA_T_NIL)
  116. return result;
  117. name = result->str;
  118. }
  119. }