reserved words are stored in main string table; "marked" field is

used to indicate its type.
Table initializations centralized by "tree.c".
This commit is contained in:
Roberto Ierusalimschy
1996-02-14 10:35:51 -03:00
parent 0f4903a5d7
commit d1608c597e
6 changed files with 79 additions and 74 deletions

31
lex.c
View File

@@ -1,4 +1,4 @@
char *rcs_lex = "$Id: lex.c,v 2.25 1996/02/12 18:32:40 roberto Exp roberto $"; char *rcs_lex = "$Id: lex.c,v 2.26 1996/02/13 17:30:39 roberto Exp roberto $";
#include <ctype.h> #include <ctype.h>
@@ -47,7 +47,6 @@ char *lua_lasttext (void)
} }
/* The reserved words must be listed in lexicographic order */
static struct static struct
{ {
char *name; char *name;
@@ -74,22 +73,14 @@ static struct
#define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0])) #define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
static int findReserved (char *name) void luaI_addReserved (void)
{ {
int l = 0; int i;
int h = RESERVEDSIZE - 1; for (i=0; i<RESERVEDSIZE; i++)
while (l <= h)
{ {
int m = (l+h)/2; TaggedString *ts = lua_createstring(reserved[i].name);
int comp = lua_strcmp(name, reserved[m].name); ts->marked = reserved[i].token; /* reserved word (always > 255) */
if (comp < 0)
h = m-1;
else if (comp == 0)
return reserved[m].token;
else
l = m+1;
} }
return 0;
} }
@@ -282,12 +273,14 @@ int luaY_lex (void)
case 'Z': case 'Z':
case '_': case '_':
{ {
Word res; TaggedString *ts;
do { save_and_next(); } while (isalnum(current) || current == '_'); do { save_and_next(); } while (isalnum(current) || current == '_');
*yytextLast = 0; *yytextLast = 0;
res = findReserved(yytext); ts = lua_createstring(yytext);
if (res) return res; if (ts->marked > 2)
luaY_lval.pTStr = luaI_createfixedstring(yytext); return ts->marked; /* reserved word */
luaY_lval.pTStr = ts;
ts->marked = 2; /* avoid GC */
return NAME; return NAME;
} }

9
lex.h
View File

@@ -1,7 +1,7 @@
/* /*
** lex.h ** lex.h
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
** $Id: $ ** $Id: lex.h,v 1.1 1996/02/13 17:30:39 roberto Exp roberto $
*/ */
#ifndef lex_h #ifndef lex_h
@@ -10,9 +10,10 @@
typedef int (*Input) (void); typedef int (*Input) (void);
void lua_setinput (Input fn); /* from "lex.c" module */ void lua_setinput (Input fn);
char *lua_lasttext (void); /* from "lex.c" module */ char *lua_lasttext (void);
int luaY_lex (void); /* from "lex.c" module */ int luaY_lex (void);
void luaI_addReserved (void);
#endif #endif

76
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.44 1996/01/26 18:03:19 roberto Exp $"; char *rcs_table="$Id: table.c,v 2.45 1996/02/12 18:32:40 roberto Exp roberto $";
/*#include <string.h>*/ /*#include <string.h>*/
@@ -37,44 +37,44 @@ static void lua_nextvar (void);
/* /*
** Initialise symbol table with internal functions ** Initialise symbol table with internal functions
*/ */
static void lua_initsymbol (void) static struct {
char *name;
lua_CFunction func;
} int_funcs[] = {
{"nextvar", lua_nextvar},
{"error", luaI_error},
{"tonumber", lua_obj2number},
{"setfallback", luaI_setfallback},
{"next", lua_next},
{"dofile", lua_internaldofile},
{"setglobal", luaI_setglobal},
{"getglobal", luaI_getglobal},
{"type", luaI_type},
{"tostring", luaI_tostring},
{"print", luaI_print},
{"dostring", lua_internaldostring},
{"assert", luaI_assert}
};
#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
void luaI_initsymbol (void)
{ {
Word n; int i;
lua_maxsymbol = BUFFER_BLOCK; lua_maxsymbol = BUFFER_BLOCK;
lua_table = newvector(lua_maxsymbol, Symbol); lua_table = newvector(lua_maxsymbol, Symbol);
n = luaI_findsymbolbyname("nextvar"); for (i=0; i<INTFUNCSIZE; i++)
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar; {
n = luaI_findsymbolbyname("error"); Word n = luaI_findsymbolbyname(int_funcs[i].name);
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error; s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
n = luaI_findsymbolbyname("tonumber"); }
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
n = luaI_findsymbolbyname("setfallback");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback;
n = luaI_findsymbolbyname("next");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
n = luaI_findsymbolbyname("dofile");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
n = luaI_findsymbolbyname("setglobal");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setglobal;
n = luaI_findsymbolbyname("getglobal");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_getglobal;
n = luaI_findsymbolbyname("type");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type;
n = luaI_findsymbolbyname("tostring");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_tostring;
n = luaI_findsymbolbyname("print");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_print;
n = luaI_findsymbolbyname("dostring");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
n = luaI_findsymbolbyname("assert");
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_assert;
} }
/* /*
** Initialise constant table with pre-defined constants ** Initialise constant table with pre-defined constants
*/ */
void lua_initconstant (void) void luaI_initconstant (void)
{ {
lua_maxconstant = BUFFER_BLOCK; lua_maxconstant = BUFFER_BLOCK;
lua_constant = newvector(lua_maxconstant, TaggedString *); lua_constant = newvector(lua_maxconstant, TaggedString *);
@@ -87,8 +87,6 @@ void lua_initconstant (void)
*/ */
Word luaI_findsymbol (TaggedString *t) Word luaI_findsymbol (TaggedString *t)
{ {
if (lua_table == NULL)
lua_initsymbol();
if (t->varindex == NOT_USED) if (t->varindex == NOT_USED)
{ {
if (lua_ntable == lua_maxsymbol) if (lua_ntable == lua_maxsymbol)
@@ -104,6 +102,8 @@ Word luaI_findsymbol (TaggedString *t)
lua_table[lua_ntable].varname = t; lua_table[lua_ntable].varname = t;
s_tag(lua_ntable) = LUA_T_NIL; s_tag(lua_ntable) = LUA_T_NIL;
lua_ntable++; lua_ntable++;
if (!t->marked)
t->marked = 2; /* avoid GC */
} }
return t->varindex; return t->varindex;
} }
@@ -111,7 +111,7 @@ Word luaI_findsymbol (TaggedString *t)
Word luaI_findsymbolbyname (char *name) Word luaI_findsymbolbyname (char *name)
{ {
return luaI_findsymbol(luaI_createfixedstring(name)); return luaI_findsymbol(lua_createstring(name));
} }
@@ -121,8 +121,6 @@ Word luaI_findsymbolbyname (char *name)
*/ */
Word luaI_findconstant (TaggedString *t) Word luaI_findconstant (TaggedString *t)
{ {
if (lua_constant == NULL)
lua_initconstant();
if (t->constindex == NOT_USED) if (t->constindex == NOT_USED)
{ {
if (lua_nconstant == lua_maxconstant) if (lua_nconstant == lua_maxconstant)
@@ -137,6 +135,8 @@ Word luaI_findconstant (TaggedString *t)
t->constindex = lua_nconstant; t->constindex = lua_nconstant;
lua_constant[lua_nconstant] = t; lua_constant[lua_nconstant] = t;
lua_nconstant++; lua_nconstant++;
if (!t->marked)
t->marked = 2; /* avoid GC */
} }
return t->constindex; return t->constindex;
} }
@@ -144,7 +144,7 @@ Word luaI_findconstant (TaggedString *t)
Word luaI_findconstantbyname (char *name) Word luaI_findconstantbyname (char *name)
{ {
return luaI_findconstant(luaI_createfixedstring(name)); return luaI_findconstant(lua_createstring(name));
} }
TaggedString *lua_constcreate(char *name) TaggedString *lua_constcreate(char *name)

View File

@@ -1,7 +1,7 @@
/* /*
** Module to control static tables ** Module to control static tables
** TeCGraf - PUC-Rio ** TeCGraf - PUC-Rio
** $Id: table.h,v 2.16 1996/02/06 16:18:21 roberto Exp roberto $ ** $Id: table.h,v 2.17 1996/02/12 18:32:40 roberto Exp roberto $
*/ */
#ifndef table_h #ifndef table_h
@@ -20,7 +20,8 @@ typedef struct
extern Symbol *lua_table; extern Symbol *lua_table;
extern TaggedString **lua_constant; extern TaggedString **lua_constant;
void lua_initconstant (void); void luaI_initsymbol (void);
void luaI_initconstant (void);
Word luaI_findsymbolbyname (char *name); Word luaI_findsymbolbyname (char *name);
Word luaI_findsymbol (TaggedString *t); Word luaI_findsymbol (TaggedString *t);
Word luaI_findconstant (TaggedString *t); Word luaI_findconstant (TaggedString *t);

27
tree.c
View File

@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_tree="$Id: tree.c,v 1.15 1996/01/26 18:03:19 roberto Exp $"; char *rcs_tree="$Id: tree.c,v 1.16 1996/02/12 18:32:40 roberto Exp roberto $";
#include <string.h> #include <string.h>
@@ -11,6 +11,7 @@ char *rcs_tree="$Id: tree.c,v 1.15 1996/01/26 18:03:19 roberto Exp $";
#include "mem.h" #include "mem.h"
#include "lua.h" #include "lua.h"
#include "tree.h" #include "tree.h"
#include "lex.h"
#include "hash.h" #include "hash.h"
#include "table.h" #include "table.h"
@@ -25,10 +26,13 @@ typedef struct {
TaggedString **hash; TaggedString **hash;
} stringtable; } stringtable;
static int initialized = 0;
static stringtable string_root[NUM_HASHS]; static stringtable string_root[NUM_HASHS];
static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 0, {0}}; static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 0, {0}};
static unsigned long hash (char *str) static unsigned long hash (char *str)
{ {
unsigned long h = 0; unsigned long h = 0;
@@ -37,6 +41,15 @@ static unsigned long hash (char *str)
return h; return h;
} }
static void initialize (void)
{
initialized = 1;
luaI_addReserved();
luaI_initsymbol();
luaI_initconstant();
}
static void grow (stringtable *tb) static void grow (stringtable *tb)
{ {
int newsize = luaI_redimension(tb->size); int newsize = luaI_redimension(tb->size);
@@ -69,7 +82,11 @@ static TaggedString *insert (char *str, stringtable *tb)
int i; int i;
int j = -1; int j = -1;
if ((Long)tb->nuse*3 >= (Long)tb->size*2) if ((Long)tb->nuse*3 >= (Long)tb->size*2)
{
if (!initialized)
initialize();
grow(tb); grow(tb);
}
i = h%tb->size; i = h%tb->size;
while (tb->hash[i]) while (tb->hash[i])
{ {
@@ -97,12 +114,6 @@ TaggedString *lua_createstring (char *str)
return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]); return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]);
} }
TaggedString *luaI_createfixedstring (char *str)
{
TaggedString *ts = lua_createstring(str);
ts->marked = 2; /* to avoid GC */
return ts;
}
/* /*
** Garbage collection function. ** Garbage collection function.
@@ -119,7 +130,7 @@ Long lua_strcollector (void)
for (j=0; j<tb->size; j++) for (j=0; j<tb->size; j++)
{ {
TaggedString *t = tb->hash[j]; TaggedString *t = tb->hash[j];
if (t != NULL && t != &EMPTY && t->marked != 2) if (t != NULL && t != &EMPTY && t->marked <= 1)
{ {
if (t->marked) if (t->marked)
t->marked = 0; t->marked = 0;

5
tree.h
View File

@@ -1,7 +1,7 @@
/* /*
** tree.h ** tree.h
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
** $Id: tree.h,v 1.11 1996/01/26 18:03:19 roberto Exp roberto $ ** $Id: tree.h,v 1.12 1996/02/12 18:32:40 roberto Exp roberto $
*/ */
#ifndef tree_h #ifndef tree_h
@@ -17,13 +17,12 @@ typedef struct TaggedString
unsigned short varindex; /* != NOT_USED if this is a symbol */ unsigned short varindex; /* != NOT_USED if this is a symbol */
unsigned short constindex; /* != NOT_USED if this is a constant */ unsigned short constindex; /* != NOT_USED if this is a constant */
unsigned long hash; /* 0 if not initialized */ unsigned long hash; /* 0 if not initialized */
char marked; /* for garbage collection; 2 means "never collect" */ int marked; /* for garbage collection; never collect (nor change) if > 1 */
char str[1]; /* \0 byte already reserved */ char str[1]; /* \0 byte already reserved */
} TaggedString; } TaggedString;
TaggedString *lua_createstring (char *str); TaggedString *lua_createstring (char *str);
TaggedString *luaI_createfixedstring (char *str);
Long lua_strcollector (void); Long lua_strcollector (void);
#endif #endif