1994-07-20 01:24:17 +04:00
|
|
|
/*
|
|
|
|
** tree.c
|
|
|
|
** TecCGraf - PUC-Rio
|
|
|
|
*/
|
|
|
|
|
1996-01-26 21:03:19 +03:00
|
|
|
char *rcs_tree="$Id: tree.c,v 1.14 1995/10/17 11:53:53 roberto Exp roberto $";
|
1994-07-20 01:24:17 +04:00
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
1994-11-16 20:39:16 +03:00
|
|
|
#include "mem.h"
|
1994-07-20 01:24:17 +04:00
|
|
|
#include "lua.h"
|
|
|
|
#include "tree.h"
|
1994-10-18 20:36:11 +03:00
|
|
|
#include "table.h"
|
1994-07-20 01:24:17 +04:00
|
|
|
|
|
|
|
|
|
|
|
#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
|
|
|
|
|
|
|
|
|
1994-11-16 21:09:11 +03:00
|
|
|
typedef struct StringNode {
|
|
|
|
struct StringNode *next;
|
1994-11-23 17:32:00 +03:00
|
|
|
TaggedString ts;
|
1994-11-16 21:09:11 +03:00
|
|
|
} StringNode;
|
|
|
|
|
|
|
|
static StringNode *string_root = NULL;
|
|
|
|
|
1994-07-20 01:24:17 +04:00
|
|
|
static TreeNode *constant_root = NULL;
|
|
|
|
|
|
|
|
/*
|
1994-11-16 21:09:11 +03:00
|
|
|
** Insert a new constant/variable at the tree.
|
1994-07-20 01:24:17 +04:00
|
|
|
*/
|
1994-11-16 21:09:11 +03:00
|
|
|
static TreeNode *tree_create (TreeNode **node, char *str)
|
1994-07-20 01:24:17 +04:00
|
|
|
{
|
|
|
|
if (*node == NULL)
|
|
|
|
{
|
1994-11-16 20:39:16 +03:00
|
|
|
*node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str));
|
1994-07-20 01:24:17 +04:00
|
|
|
(*node)->left = (*node)->right = NULL;
|
1994-11-23 17:32:00 +03:00
|
|
|
strcpy((*node)->ts.str, str);
|
|
|
|
(*node)->ts.marked = 0;
|
1994-11-25 22:27:03 +03:00
|
|
|
(*node)->ts.hash = 0;
|
1994-11-23 17:32:00 +03:00
|
|
|
(*node)->varindex = (*node)->constindex = NOT_USED;
|
1994-11-15 00:40:14 +03:00
|
|
|
return *node;
|
1994-07-20 01:24:17 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1994-11-23 17:32:00 +03:00
|
|
|
int c = lua_strcmp(str, (*node)->ts.str);
|
1994-07-20 01:24:17 +04:00
|
|
|
if (c < 0)
|
1994-11-16 21:09:11 +03:00
|
|
|
return tree_create(&(*node)->left, str);
|
1994-07-20 01:24:17 +04:00
|
|
|
else if (c > 0)
|
1994-11-16 21:09:11 +03:00
|
|
|
return tree_create(&(*node)->right, str);
|
1994-07-20 01:24:17 +04:00
|
|
|
else
|
1994-11-15 00:40:14 +03:00
|
|
|
return *node;
|
1994-07-20 01:24:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-11-23 17:32:00 +03:00
|
|
|
TaggedString *lua_createstring (char *str)
|
1994-07-20 01:24:17 +04:00
|
|
|
{
|
1994-11-17 16:58:57 +03:00
|
|
|
StringNode *newString;
|
1994-11-18 22:27:38 +03:00
|
|
|
if (str == NULL) return NULL;
|
1994-11-17 16:58:57 +03:00
|
|
|
lua_pack();
|
|
|
|
newString = (StringNode *)luaI_malloc(sizeof(StringNode)+strlen(str));
|
1994-11-23 17:32:00 +03:00
|
|
|
newString->ts.marked = 0;
|
1994-11-25 22:27:03 +03:00
|
|
|
newString->ts.hash = 0;
|
1994-11-23 17:32:00 +03:00
|
|
|
strcpy(newString->ts.str, str);
|
1994-11-16 21:09:11 +03:00
|
|
|
newString->next = string_root;
|
|
|
|
string_root = newString;
|
1994-11-23 17:32:00 +03:00
|
|
|
return &(newString->ts);
|
1994-07-20 01:24:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-11-16 21:09:11 +03:00
|
|
|
TreeNode *lua_constcreate (char *str)
|
1994-07-20 01:24:17 +04:00
|
|
|
{
|
1994-11-16 21:09:11 +03:00
|
|
|
return tree_create(&constant_root, str);
|
1994-07-20 01:24:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Garbage collection function.
|
1994-11-16 21:09:11 +03:00
|
|
|
** This function traverse the string list freeing unindexed strings
|
1994-07-20 01:24:17 +04:00
|
|
|
*/
|
1995-01-12 17:19:04 +03:00
|
|
|
Long lua_strcollector (void)
|
1994-07-20 01:24:17 +04:00
|
|
|
{
|
1994-11-16 21:09:11 +03:00
|
|
|
StringNode *curr = string_root, *prev = NULL;
|
1995-01-12 17:19:04 +03:00
|
|
|
Long counter = 0;
|
1994-11-16 21:09:11 +03:00
|
|
|
while (curr)
|
|
|
|
{
|
|
|
|
StringNode *next = curr->next;
|
1994-11-23 17:32:00 +03:00
|
|
|
if (!curr->ts.marked)
|
1994-11-16 21:09:11 +03:00
|
|
|
{
|
|
|
|
if (prev == NULL) string_root = next;
|
|
|
|
else prev->next = next;
|
|
|
|
luaI_free(curr);
|
1994-11-17 16:58:57 +03:00
|
|
|
++counter;
|
1994-11-16 21:09:11 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1994-11-23 17:32:00 +03:00
|
|
|
curr->ts.marked = 0;
|
1994-11-16 21:09:11 +03:00
|
|
|
prev = curr;
|
|
|
|
}
|
|
|
|
curr = next;
|
|
|
|
}
|
1994-11-17 16:58:57 +03:00
|
|
|
return counter;
|
1994-07-20 01:24:17 +04:00
|
|
|
}
|
|
|
|
|
1995-10-17 14:53:53 +03:00
|
|
|
|