1
0
mirror of https://github.com/lua/lua.git synced 2025-01-28 06:03:00 +08:00

unification of symbol tree and constant tree

This commit is contained in:
Roberto Ierusalimschy 1994-11-14 19:40:14 -02:00
parent 3b7a36653b
commit 86b35cf4f6
7 changed files with 131 additions and 183 deletions

View File

@ -5,7 +5,7 @@
** Also provides some predefined lua functions. ** Also provides some predefined lua functions.
*/ */
char *rcs_inout="$Id: inout.c,v 2.9 1994/11/08 20:06:15 roberto Exp roberto $"; char *rcs_inout="$Id: inout.c,v 2.10 1994/11/09 18:09:22 roberto Exp roberto $";
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -150,12 +150,12 @@ void lua_reportbug (char *s)
{ {
sprintf (strchr(msg,0), sprintf (strchr(msg,0),
"\n\tin statement begining at line %d in function \"%s\" of file \"%s\"", "\n\tin statement begining at line %d in function \"%s\" of file \"%s\"",
lua_debugline, lua_varname(funcstack[nfuncstack-1].function), lua_debugline, lua_constant[funcstack[nfuncstack-1].function],
funcstack[nfuncstack-1].file); funcstack[nfuncstack-1].file);
sprintf (strchr(msg,0), "\n\tactive stack\n"); sprintf (strchr(msg,0), "\n\tactive stack\n");
for (i=nfuncstack-1; i>=0; i--) for (i=nfuncstack-1; i>=0; i--)
sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n", sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n",
lua_varname(funcstack[i].function), lua_constant[funcstack[i].function],
funcstack[i].file); funcstack[i].file);
} }
else else

24
lex.c
View File

@ -1,4 +1,4 @@
char *rcs_lex = "$Id: lex.c,v 2.9 1994/11/03 17:09:20 roberto Exp roberto $"; char *rcs_lex = "$Id: lex.c,v 2.10 1994/11/13 14:39:04 roberto Exp roberto $";
#include <ctype.h> #include <ctype.h>
@ -7,6 +7,8 @@ char *rcs_lex = "$Id: lex.c,v 2.9 1994/11/03 17:09:20 roberto Exp roberto $";
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "tree.h"
#include "table.h"
#include "opcode.h" #include "opcode.h"
#include "inout.h" #include "inout.h"
#include "y.tab.h" #include "y.tab.h"
@ -19,9 +21,8 @@ char *rcs_lex = "$Id: lex.c,v 2.9 1994/11/03 17:09:20 roberto Exp roberto $";
#define save_and_next() { save(current); next(); } #define save_and_next() { save(current); next(); }
static int current; static int current;
static char yytext[2][256]; static char yytext[256];
static char *yytextLast; static char *yytextLast;
static int currentText = 0;
static Input input; static Input input;
@ -34,7 +35,7 @@ void lua_setinput (Input fn)
char *lua_lasttext (void) char *lua_lasttext (void)
{ {
*yytextLast = 0; *yytextLast = 0;
return yytext[currentText]; return yytext;
} }
@ -87,10 +88,9 @@ static int findReserved (char *name)
int yylex (void) int yylex (void)
{ {
float a; float a;
currentText = !currentText;
while (1) while (1)
{ {
yytextLast = yytext[currentText]; yytextLast = yytext;
#if 0 #if 0
fprintf(stderr,"'%c' %d\n",current,current); fprintf(stderr,"'%c' %d\n",current,current);
#endif #endif
@ -110,12 +110,12 @@ int yylex (void)
while (isalnum(current) || current == '_') while (isalnum(current) || current == '_')
save_and_next(); save_and_next();
*yytextLast = 0; *yytextLast = 0;
if (lua_strcmp(yytext[currentText], "debug") == 0) if (lua_strcmp(yytext, "debug") == 0)
{ {
yylval.vInt = 1; yylval.vInt = 1;
return DEBUG; return DEBUG;
} }
else if (lua_strcmp(yytext[currentText], "nodebug") == 0) else if (lua_strcmp(yytext, "nodebug") == 0)
{ {
yylval.vInt = 0; yylval.vInt = 0;
return DEBUG; return DEBUG;
@ -179,7 +179,7 @@ int yylex (void)
} }
next(); /* skip the delimiter */ next(); /* skip the delimiter */
*yytextLast = 0; *yytextLast = 0;
yylval.pChar = yytext[currentText]; yylval.vWord = luaI_findconstant(lua_constcreate(yytext));
return STRING; return STRING;
} }
@ -200,9 +200,9 @@ int yylex (void)
int res; int res;
do { save_and_next(); } while (isalnum(current) || current == '_'); do { save_and_next(); } while (isalnum(current) || current == '_');
*yytextLast = 0; *yytextLast = 0;
res = findReserved(yytext[currentText]); res = findReserved(yytext);
if (res) return res; if (res) return res;
yylval.pChar = yytext[currentText]; yylval.pNode = lua_constcreate(yytext);
return NAME; return NAME;
} }
@ -266,7 +266,7 @@ fraction:
default: /* also end of file */ default: /* also end of file */
{ {
save_and_next(); save_and_next();
return yytext[currentText][0]; return yytext[0];
} }
} }
} }

141
lua.stx
View File

@ -1,6 +1,6 @@
%{ %{
char *rcs_luastx = "$Id: lua.stx,v 3.4 1994/11/09 18:07:38 roberto Exp roberto $"; char *rcs_luastx = "$Id: lua.stx,v 3.5 1994/11/13 14:54:18 roberto Exp roberto $";
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -9,6 +9,7 @@ char *rcs_luastx = "$Id: lua.stx,v 3.4 1994/11/09 18:07:38 roberto Exp roberto $
#include "opcode.h" #include "opcode.h"
#include "hash.h" #include "hash.h"
#include "inout.h" #include "inout.h"
#include "tree.h"
#include "table.h" #include "table.h"
#include "lua.h" #include "lua.h"
@ -40,6 +41,7 @@ static int nlocalvar=0; /* number of local variables */
static Word fields[MAXFIELDS]; /* fieldnames to be flushed */ static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
static int nfields=0; static int nfields=0;
/* Internal functions */ /* Internal functions */
static void code_byte (Byte c) static void code_byte (Byte c)
@ -164,17 +166,6 @@ static void code_number (float f)
} }
} }
static void init_function (void)
{
if (funcCode == NULL) /* first function */
{
funcCode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
if (funcCode == NULL)
lua_error("not enough memory");
maxcode = CODE_BLOCK;
}
}
%} %}
@ -187,6 +178,7 @@ static void init_function (void)
Word vWord; Word vWord;
Long vLong; Long vLong;
Byte *pByte; Byte *pByte;
TreeNode *pNode;
} }
%start functionlist %start functionlist
@ -198,8 +190,8 @@ static void init_function (void)
%token LOCAL %token LOCAL
%token FUNCTION %token FUNCTION
%token <vFloat> NUMBER %token <vFloat> NUMBER
%token <pChar> STRING %token <vWord> STRING
%token <pChar> NAME %token <pNode> NAME
%token <vInt> DEBUG %token <vInt> DEBUG
%type <vLong> PrepJump %type <vLong> PrepJump
@ -208,7 +200,7 @@ static void init_function (void)
%type <vInt> ffieldlist1 %type <vInt> ffieldlist1
%type <vInt> lfieldlist1 %type <vInt> lfieldlist1
%type <vLong> var, singlevar %type <vLong> var, singlevar
%type <pByte> body
%left AND OR %left AND OR
%left EQ NE '>' '<' LE GE %left EQ NE '>' '<' LE GE
@ -239,84 +231,54 @@ functionlist : /* empty */
function : FUNCTION NAME function : FUNCTION NAME
{ {
init_function(); init_function($2);
pc=0; basepc=funcCode; maxcurr=maxcode;
nlocalvar=0;
$<vWord>$ = lua_findsymbol($2);
} }
'(' parlist ')' body
{
if (lua_debug)
{
code_byte(SETFUNCTION);
code_code((Byte *)strdup(lua_file[lua_nfile-1]));
code_word($<vWord>3);
}
lua_codeadjust (0);
}
block
END
{ {
codereturn(); Word func = luaI_findsymbol($2);
s_tag($<vWord>3) = LUA_T_FUNCTION; s_tag(func) = LUA_T_FUNCTION;
s_bvalue($<vWord>3) = calloc (pc, sizeof(Byte)); s_bvalue(func) = $4;
if (s_bvalue($<vWord>3) == NULL)
lua_error("not enough memory");
memcpy (s_bvalue($<vWord>3), basepc, pc*sizeof(Byte));
funcCode = basepc; maxcode=maxcurr;
#if LISTING #if LISTING
PrintCode(funcCode,funcCode+pc); PrintCode(funcCode,funcCode+pc);
#endif #endif
} }
; ;
method : FUNCTION NAME { $<vWord>$ = lua_findsymbol($2); } ':' NAME method : FUNCTION NAME ':' NAME
{ {
init_function(); init_function($4);
pc=0; basepc=funcCode; maxcurr=maxcode; localvar[nlocalvar]=luaI_findsymbolbyname("self");
nlocalvar=0;
localvar[nlocalvar]=lua_findsymbol("self"); /* self param. */
add_nlocalvar(1); add_nlocalvar(1);
$<vWord>$ = lua_findconstant($5);
} }
'(' parlist ')' body
{ {
if (lua_debug)
{
code_byte(SETFUNCTION);
code_code((Byte *)strdup(lua_file[lua_nfile-1]));
code_word($<vWord>6);
}
lua_codeadjust (0);
}
block
END
{
Byte *b;
codereturn();
b = calloc (pc, sizeof(Byte));
if (b == NULL)
lua_error("not enough memory");
memcpy (b, basepc, pc*sizeof(Byte));
funcCode = basepc; maxcode=maxcurr;
#if LISTING #if LISTING
PrintCode(funcCode,funcCode+pc); PrintCode(funcCode,funcCode+pc);
#endif #endif
/* assign function to table field */ /* assign function to table field */
pc=maincode; basepc=*initcode; maxcurr=maxmain; pc=maincode; basepc=*initcode; maxcurr=maxmain;
nlocalvar=0; nlocalvar=0;
lua_pushvar(luaI_findsymbol($2)+1);
lua_pushvar($<vWord>3+1);
code_byte(PUSHSTRING); code_byte(PUSHSTRING);
code_word($<vWord>6); code_word(luaI_findconstant($4));
code_byte(PUSHFUNCTION); code_byte(PUSHFUNCTION);
code_code(b); code_code($6);
code_byte(STOREINDEXED0); code_byte(STOREINDEXED0);
maincode=pc; *initcode=basepc; maxmain=maxcurr; maincode=pc; *initcode=basepc; maxmain=maxcurr;
} }
; ;
body : '(' parlist ')' block END
{
codereturn();
$$ = calloc (pc, sizeof(Byte));
if ($$ == NULL)
lua_error("not enough memory");
memcpy ($$, basepc, pc*sizeof(Byte));
funcCode = basepc; maxcode=maxcurr;
}
;
statlist : /* empty */ statlist : /* empty */
| statlist stat sc | statlist stat sc
; ;
@ -454,7 +416,7 @@ expr : '(' expr ')' { $$ = $2; }
| STRING | STRING
{ {
code_byte(PUSHSTRING); code_byte(PUSHSTRING);
code_word(lua_findconstant($1)); code_word($1);
$$ = 1; $$ = 1;
} }
| NIL {code_byte(PUSHNIL); $$ = 1; } | NIL {code_byte(PUSHNIL); $$ = 1; }
@ -493,7 +455,7 @@ funcvalue : varexp { $$ = 0; }
| varexp ':' NAME | varexp ':' NAME
{ {
code_byte(PUSHSTRING); code_byte(PUSHSTRING);
code_word(lua_findconstant($3)); code_word(luaI_findconstant($3));
code_byte(PUSHSELF); code_byte(PUSHSELF);
$$ = 1; $$ = 1;
} }
@ -516,18 +478,18 @@ exprlist1 : expr { if ($1 == 0) $$ = -1; else $$ = 1; }
} }
; ;
parlist : /* empty */ parlist : /* empty */ { lua_codeadjust(0); }
| parlist1 | parlist1 { lua_codeadjust(0); }
; ;
parlist1 : NAME parlist1 : NAME
{ {
localvar[nlocalvar]=lua_findsymbol($1); localvar[nlocalvar]=luaI_findsymbol($1);
add_nlocalvar(1); add_nlocalvar(1);
} }
| parlist1 ',' NAME | parlist1 ',' NAME
{ {
localvar[nlocalvar]=lua_findsymbol($3); localvar[nlocalvar]=luaI_findsymbol($3);
add_nlocalvar(1); add_nlocalvar(1);
} }
; ;
@ -555,9 +517,9 @@ ffieldlist1 : ffield {$$=1;}
} }
; ;
ffield : NAME {$<vWord>$ = lua_findconstant($1);} '=' expr1 ffield : NAME '=' expr1
{ {
push_field($<vWord>2); push_field(luaI_findconstant($1));
} }
; ;
@ -591,14 +553,14 @@ var : singlevar { $$ = $1; }
| varexp '.' NAME | varexp '.' NAME
{ {
code_byte(PUSHSTRING); code_byte(PUSHSTRING);
code_word(lua_findconstant($3)); code_word(luaI_findconstant($3));
$$ = 0; /* indexed variable */ $$ = 0; /* indexed variable */
} }
; ;
singlevar : NAME singlevar : NAME
{ {
Word s = lua_findsymbol($1); Word s = luaI_findsymbol($1);
int local = lua_localname (s); int local = lua_localname (s);
if (local == -1) /* global var */ if (local == -1) /* global var */
$$ = s + 1; /* return positive value */ $$ = s + 1; /* return positive value */
@ -610,10 +572,10 @@ singlevar : NAME
varexp : var { lua_pushvar($1); } varexp : var { lua_pushvar($1); }
; ;
localdeclist : NAME {localvar[nlocalvar]=lua_findsymbol($1); $$ = 1;} localdeclist : NAME {localvar[nlocalvar]=luaI_findsymbol($1); $$ = 1;}
| localdeclist ',' NAME | localdeclist ',' NAME
{ {
localvar[nlocalvar+$1]=lua_findsymbol($3); localvar[nlocalvar+$1]=luaI_findsymbol($3);
$$ = $1+1; $$ = $1+1;
} }
; ;
@ -676,6 +638,25 @@ static void lua_codeadjust (int n)
} }
} }
static void init_function (TreeNode *func)
{
if (funcCode == NULL) /* first function */
{
funcCode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
if (funcCode == NULL)
lua_error("not enough memory");
maxcode = CODE_BLOCK;
}
pc=0; basepc=funcCode; maxcurr=maxcode;
nlocalvar=0;
if (lua_debug)
{
code_byte(SETFUNCTION);
code_code((Byte *)strdup(lua_file[lua_nfile-1]));
code_word(luaI_findconstant(func));
}
}
static void codereturn (void) static void codereturn (void)
{ {
if (lua_debug) code_byte(RESET); if (lua_debug) code_byte(RESET);

48
table.c
View File

@ -3,7 +3,7 @@
** Module to control static tables ** Module to control static tables
*/ */
char *rcs_table="$Id: table.c,v 2.15 1994/11/10 20:41:37 roberto Exp roberto $"; char *rcs_table="$Id: table.c,v 2.16 1994/11/11 14:00:08 roberto Exp roberto $";
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -51,23 +51,23 @@ static void lua_initsymbol (void)
lua_table = (Symbol *) calloc(lua_maxsymbol, sizeof(Symbol)); lua_table = (Symbol *) calloc(lua_maxsymbol, sizeof(Symbol));
if (lua_table == NULL) if (lua_table == NULL)
lua_error ("symbol table: not enough memory"); lua_error ("symbol table: not enough memory");
n = lua_findsymbol("next"); n = luaI_findsymbolbyname("next");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next; s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
n = lua_findsymbol("nextvar"); n = luaI_findsymbolbyname("nextvar");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar; s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar;
n = lua_findsymbol("type"); n = luaI_findsymbolbyname("type");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type; s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type;
n = lua_findsymbol("tonumber"); n = luaI_findsymbolbyname("tonumber");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number; s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
n = lua_findsymbol("print"); n = luaI_findsymbolbyname("print");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print; s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print;
n = lua_findsymbol("dofile"); n = luaI_findsymbolbyname("dofile");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile; s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
n = lua_findsymbol("dostring"); n = luaI_findsymbolbyname("dostring");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring; s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
n = lua_findsymbol("setfallback"); n = luaI_findsymbolbyname("setfallback");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback; s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback;
n = lua_findsymbol("error"); n = luaI_findsymbolbyname("error");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error; s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error;
} }
@ -89,13 +89,11 @@ void lua_initconstant (void)
** found, allocate it. ** found, allocate it.
** On error, return -1. ** On error, return -1.
*/ */
int lua_findsymbol (char *s) int luaI_findsymbol (TreeNode *t)
{ {
char *n;
if (lua_table == NULL) if (lua_table == NULL)
lua_initsymbol(); lua_initsymbol();
n = lua_varcreate(s); if (t->varindex == UNMARKED_STRING)
if (indexstring(n) == UNMARKED_STRING)
{ {
if (lua_ntable == lua_maxsymbol) if (lua_ntable == lua_maxsymbol)
{ {
@ -106,11 +104,17 @@ int lua_findsymbol (char *s)
if (lua_table == NULL) if (lua_table == NULL)
lua_error ("symbol table: not enough memory"); lua_error ("symbol table: not enough memory");
} }
indexstring(n) = lua_ntable; t->varindex = lua_ntable;
s_tag(lua_ntable) = LUA_T_NIL; s_tag(lua_ntable) = LUA_T_NIL;
lua_ntable++; lua_ntable++;
} }
return indexstring(n); return t->varindex;
}
int luaI_findsymbolbyname (char *name)
{
return luaI_findsymbol(lua_constcreate(name));
} }
@ -119,13 +123,11 @@ int lua_findsymbol (char *s)
** found, allocate it. ** found, allocate it.
** On error, return -1. ** On error, return -1.
*/ */
int lua_findconstant (char *s) int luaI_findconstant (TreeNode *t)
{ {
char *n;
if (lua_constant == NULL) if (lua_constant == NULL)
lua_initconstant(); lua_initconstant();
n = lua_constcreate(s); if (t->constindex == UNMARKED_STRING)
if (indexstring(n) == UNMARKED_STRING)
{ {
if (lua_nconstant == lua_maxconstant) if (lua_nconstant == lua_maxconstant)
{ {
@ -136,11 +138,11 @@ int lua_findconstant (char *s)
if (lua_constant == NULL) if (lua_constant == NULL)
lua_error ("constant table: not enough memory"); lua_error ("constant table: not enough memory");
} }
indexstring(n) = lua_nconstant; t->constindex = lua_nconstant;
lua_constant[lua_nconstant] = n; lua_constant[lua_nconstant] = t->str;
lua_nconstant++; lua_nconstant++;
} }
return indexstring(n); return t->constindex;
} }

View File

@ -1,12 +1,14 @@
/* /*
** Module to control static tables ** Module to control static tables
** TeCGraf - PUC-Rio ** TeCGraf - PUC-Rio
** $Id: table.h,v 2.3 1994/10/17 19:03:23 celes Exp roberto $ ** $Id: table.h,v 2.4 1994/11/03 21:48:36 roberto Exp roberto $
*/ */
#ifndef table_h #ifndef table_h
#define table_h #define table_h
#include "tree.h"
extern Symbol *lua_table; extern Symbol *lua_table;
extern char **lua_constant; extern char **lua_constant;
@ -19,8 +21,9 @@ extern Word lua_recovered;
void lua_initconstant (void); void lua_initconstant (void);
int lua_findsymbol (char *s); int luaI_findsymbolbyname (char *name);
int lua_findconstant (char *s); int luaI_findsymbol (TreeNode *t);
int luaI_findconstant (TreeNode *t);
void lua_travsymbol (void (*fn)(Object *)); void lua_travsymbol (void (*fn)(Object *));
void lua_markobject (Object *o); void lua_markobject (Object *o);
void lua_pack (void); void lua_pack (void);

69
tree.c
View File

@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_tree="$Id: tree.c,v 1.2 1994/10/18 17:36:11 celes Exp roberto $"; char *rcs_tree="$Id: tree.c,v 1.3 1994/11/10 20:41:37 roberto Exp roberto $";
#include <stdlib.h> #include <stdlib.h>
@ -17,22 +17,13 @@ char *rcs_tree="$Id: tree.c,v 1.2 1994/10/18 17:36:11 celes Exp roberto $";
#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b))) #define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
typedef struct TreeNode
{
struct TreeNode *right;
struct TreeNode *left;
Word index;
char str[1]; /* \0 byte already reserved */
} TreeNode;
static TreeNode *string_root = NULL; static TreeNode *string_root = NULL;
static TreeNode *constant_root = NULL; static TreeNode *constant_root = NULL;
static TreeNode *variable_root = NULL;
/* /*
** Insert a new string/constant/variable at the tree. ** Insert a new string/constant/variable at the tree.
*/ */
static char *tree_create (TreeNode **node, char *str, int *created) static TreeNode *tree_create (TreeNode **node, char *str, int *created)
{ {
if (*node == NULL) if (*node == NULL)
{ {
@ -41,9 +32,9 @@ static char *tree_create (TreeNode **node, char *str, int *created)
lua_error("not enough memory"); lua_error("not enough memory");
(*node)->left = (*node)->right = NULL; (*node)->left = (*node)->right = NULL;
strcpy((*node)->str, str); strcpy((*node)->str, str);
(*node)->index = UNMARKED_STRING; (*node)->varindex = (*node)->constindex = UNMARKED_STRING;
*created = 1; *created = 1;
return (*node)->str; return *node;
} }
else else
{ {
@ -53,35 +44,28 @@ static char *tree_create (TreeNode **node, char *str, int *created)
else if (c > 0) else if (c > 0)
return tree_create(&(*node)->right, str, created); return tree_create(&(*node)->right, str, created);
else else
return (*node)->str; return *node;
} }
} }
char *lua_strcreate (char *str) char *lua_strcreate (char *str)
{ {
int created=0; int created=0;
char *s = tree_create(&string_root, str, &created); TreeNode *t = tree_create(&string_root, str, &created);
if (created) if (created)
{ {
if (lua_nentity == lua_block) lua_pack (); if (lua_nentity == lua_block) lua_pack ();
lua_nentity++; lua_nentity++;
} }
return s; return t->str;
} }
char *lua_constcreate (char *str) TreeNode *lua_constcreate (char *str)
{ {
int created; int created;
return tree_create(&constant_root, str, &created); return tree_create(&constant_root, str, &created);
} }
char *lua_varcreate (char *str)
{
int created;
return tree_create(&variable_root, str, &created);
}
/* /*
** Free a node of the tree ** Free a node of the tree
@ -141,14 +125,14 @@ static TreeNode *lua_travcollector (TreeNode *r)
if (r == NULL) return NULL; if (r == NULL) return NULL;
r->right = lua_travcollector(r->right); r->right = lua_travcollector(r->right);
r->left = lua_travcollector(r->left); r->left = lua_travcollector(r->left);
if (r->index == UNMARKED_STRING) if (r->constindex == UNMARKED_STRING)
{ {
++lua_recovered; ++lua_recovered;
return lua_strfree(r); return lua_strfree(r);
} }
else else
{ {
r->index = UNMARKED_STRING; r->constindex = UNMARKED_STRING;
return r; return r;
} }
} }
@ -168,18 +152,6 @@ void lua_strcollector (void)
*/ */
static TreeNode *tree_next (TreeNode *node, char *str) static TreeNode *tree_next (TreeNode *node, char *str)
{ {
#if 0
if (node == NULL) return NULL;
if (str == NULL || lua_strcmp(str, node->str) < 0)
{
TreeNode *result = tree_next(node->left, str);
return result == NULL ? node : result;
}
else
{
return tree_next(node->right, str);
}
#else
if (node == NULL) return NULL; if (node == NULL) return NULL;
else if (str == NULL) return node; else if (str == NULL) return node;
else else
@ -195,30 +167,11 @@ static TreeNode *tree_next (TreeNode *node, char *str)
else else
return tree_next(node->right, str); return tree_next(node->right, str);
} }
#endif
} }
char *lua_varnext (char *n) char *lua_varnext (char *n)
{ {
TreeNode *result = tree_next(variable_root, n); TreeNode *result = tree_next(constant_root, n);
return result != NULL ? result->str : NULL; return result != NULL ? result->str : NULL;
} }
/*
** Given an id, find the string with exaustive search
*/
static char *tree_name (TreeNode *node, Word index)
{
if (node == NULL) return NULL;
if (node->index == index) return node->str;
else
{
char *result = tree_name(node->left, index);
return result != NULL ? result : tree_name(node->right, index);
}
}
char *lua_varname (Word index)
{
return tree_name(variable_root, index);
}

17
tree.h
View File

@ -1,7 +1,7 @@
/* /*
** tree.h ** tree.h
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
** $Id: $ ** $Id: tree.h,v 1.1 1994/07/19 21:24:17 celes Exp roberto $
*/ */
#ifndef tree_h #ifndef tree_h
@ -14,14 +14,23 @@
#define MARKED_STRING 0xFFFE #define MARKED_STRING 0xFFFE
#define MAX_WORD 0xFFFD #define MAX_WORD 0xFFFD
typedef struct TreeNode
{
struct TreeNode *right;
struct TreeNode *left;
Word varindex; /* if this is a symbol */
Word constindex; /* if this is a constant; also used for garbage collection */
char str[1]; /* \0 byte already reserved */
} TreeNode;
#define indexstring(s) (*(((Word *)s)-1)) #define indexstring(s) (*(((Word *)s)-1))
char *lua_strcreate (char *str); char *lua_strcreate (char *str);
char *lua_constcreate (char *str); TreeNode *lua_constcreate (char *str);
char *lua_varcreate (char *str);
void lua_strcollector (void); void lua_strcollector (void);
char *lua_varnext (char *n); char *lua_varnext (char *n);
char *lua_varname (Word index);
#endif #endif