6 char *rcs_tree
="$Id: tree.c,v 1.13 1995/01/12 14:19:04 roberto Exp $";
17 #define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
20 typedef struct StringNode
{
21 struct StringNode
*next
;
25 static StringNode
*string_root
= NULL
;
27 static TreeNode
*constant_root
= NULL
;
30 ** Insert a new constant/variable at the tree.
32 static TreeNode
*tree_create (TreeNode
**node
, char *str
)
36 *node
= (TreeNode
*) luaI_malloc(sizeof(TreeNode
)+strlen(str
));
37 (*node
)->left
= (*node
)->right
= NULL
;
38 strcpy((*node
)->ts
.str
, str
);
39 (*node
)->ts
.marked
= 0;
41 (*node
)->varindex
= (*node
)->constindex
= NOT_USED
;
46 int c
= lua_strcmp(str
, (*node
)->ts
.str
);
48 return tree_create(&(*node
)->left
, str
);
50 return tree_create(&(*node
)->right
, str
);
56 TaggedString
*lua_createstring (char *str
)
58 StringNode
*newString
;
59 if (str
== NULL
) return NULL
;
61 newString
= (StringNode
*)luaI_malloc(sizeof(StringNode
)+strlen(str
));
62 newString
->ts
.marked
= 0;
63 newString
->ts
.hash
= 0;
64 strcpy(newString
->ts
.str
, str
);
65 newString
->next
= string_root
;
66 string_root
= newString
;
67 return &(newString
->ts
);
71 TreeNode
*lua_constcreate (char *str
)
73 return tree_create(&constant_root
, str
);
78 ** Garbage collection function.
79 ** This function traverse the string list freeing unindexed strings
81 Long
lua_strcollector (void)
83 StringNode
*curr
= string_root
, *prev
= NULL
;
87 StringNode
*next
= curr
->next
;
90 if (prev
== NULL
) string_root
= next
;
91 else prev
->next
= next
;
106 ** Return next variable.
108 static TreeNode
*tree_next (TreeNode
*node
, char *str
)
110 if (node
== NULL
) return NULL
;
111 else if (str
== NULL
) return node
;
114 int c
= lua_strcmp(str
, node
->ts
.str
);
116 return node
->left
!= NULL
? node
->left
: node
->right
;
119 TreeNode
*result
= tree_next(node
->left
, str
);
120 return result
!= NULL
? result
: node
->right
;
123 return tree_next(node
->right
, str
);
127 TreeNode
*lua_varnext (char *n
)
132 { /* repeat until a valid (non nil) variable */
133 result
= tree_next(constant_root
, name
);
134 if (result
== NULL
) return NULL
;
135 if (result
->varindex
!= NOT_USED
&&
136 s_tag(result
->varindex
) != LUA_T_NIL
)
138 name
= result
->ts
.str
;