From d1608c597e2f45021d43c56050aff08e5d417699 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 14 Feb 1996 10:35:51 -0300 Subject: reserved words are stored in main string table; "marked" field is used to indicate its type. Table initializations centralized by "tree.c". --- lex.c | 31 +++++++++++---------------- lex.h | 9 ++++---- table.c | 76 ++++++++++++++++++++++++++++++++--------------------------------- table.h | 5 +++-- tree.c | 27 ++++++++++++++++------- tree.h | 5 ++--- 6 files changed, 79 insertions(+), 74 deletions(-) diff --git a/lex.c b/lex.c index c65d2253..d883fcb9 100644 --- a/lex.c +++ b/lex.c @@ -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 @@ -47,7 +47,6 @@ char *lua_lasttext (void) } -/* The reserved words must be listed in lexicographic order */ static struct { char *name; @@ -74,22 +73,14 @@ static struct #define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0])) -static int findReserved (char *name) +void luaI_addReserved (void) { - int l = 0; - int h = RESERVEDSIZE - 1; - while (l <= h) + int i; + for (i=0; imarked = reserved[i].token; /* reserved word (always > 255) */ } - return 0; } @@ -282,12 +273,14 @@ int luaY_lex (void) case 'Z': case '_': { - Word res; + TaggedString *ts; do { save_and_next(); } while (isalnum(current) || current == '_'); *yytextLast = 0; - res = findReserved(yytext); - if (res) return res; - luaY_lval.pTStr = luaI_createfixedstring(yytext); + ts = lua_createstring(yytext); + if (ts->marked > 2) + return ts->marked; /* reserved word */ + luaY_lval.pTStr = ts; + ts->marked = 2; /* avoid GC */ return NAME; } diff --git a/lex.h b/lex.h index e56dbf25..0c6b14d9 100644 --- a/lex.h +++ b/lex.h @@ -1,7 +1,7 @@ /* ** lex.h ** TecCGraf - PUC-Rio -** $Id: $ +** $Id: lex.h,v 1.1 1996/02/13 17:30:39 roberto Exp roberto $ */ #ifndef lex_h @@ -10,9 +10,10 @@ typedef int (*Input) (void); -void lua_setinput (Input fn); /* from "lex.c" module */ -char *lua_lasttext (void); /* from "lex.c" module */ -int luaY_lex (void); /* from "lex.c" module */ +void lua_setinput (Input fn); +char *lua_lasttext (void); +int luaY_lex (void); +void luaI_addReserved (void); #endif diff --git a/table.c b/table.c index 29d877c2..614a5c5e 100644 --- a/table.c +++ b/table.c @@ -3,7 +3,7 @@ ** 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 */ @@ -37,44 +37,44 @@ static void lua_nextvar (void); /* ** 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; - lua_maxsymbol = BUFFER_BLOCK; - lua_table = newvector(lua_maxsymbol, Symbol); - n = luaI_findsymbolbyname("nextvar"); - s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar; - n = luaI_findsymbolbyname("error"); - s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error; - 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; + int i; + lua_maxsymbol = BUFFER_BLOCK; + lua_table = newvector(lua_maxsymbol, Symbol); + for (i=0; ivarindex == NOT_USED) { if (lua_ntable == lua_maxsymbol) @@ -104,6 +102,8 @@ Word luaI_findsymbol (TaggedString *t) lua_table[lua_ntable].varname = t; s_tag(lua_ntable) = LUA_T_NIL; lua_ntable++; + if (!t->marked) + t->marked = 2; /* avoid GC */ } return t->varindex; } @@ -111,7 +111,7 @@ Word luaI_findsymbol (TaggedString *t) 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) { - if (lua_constant == NULL) - lua_initconstant(); if (t->constindex == NOT_USED) { if (lua_nconstant == lua_maxconstant) @@ -137,6 +135,8 @@ Word luaI_findconstant (TaggedString *t) t->constindex = lua_nconstant; lua_constant[lua_nconstant] = t; lua_nconstant++; + if (!t->marked) + t->marked = 2; /* avoid GC */ } return t->constindex; } @@ -144,7 +144,7 @@ Word luaI_findconstant (TaggedString *t) Word luaI_findconstantbyname (char *name) { - return luaI_findconstant(luaI_createfixedstring(name)); + return luaI_findconstant(lua_createstring(name)); } TaggedString *lua_constcreate(char *name) diff --git a/table.h b/table.h index 37085fec..6b4b9d3d 100644 --- a/table.h +++ b/table.h @@ -1,7 +1,7 @@ /* ** Module to control static tables ** 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 @@ -20,7 +20,8 @@ typedef struct extern Symbol *lua_table; extern TaggedString **lua_constant; -void lua_initconstant (void); +void luaI_initsymbol (void); +void luaI_initconstant (void); Word luaI_findsymbolbyname (char *name); Word luaI_findsymbol (TaggedString *t); Word luaI_findconstant (TaggedString *t); diff --git a/tree.c b/tree.c index 3b82dc9a..66577a78 100644 --- a/tree.c +++ b/tree.c @@ -3,7 +3,7 @@ ** 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 @@ -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 "lua.h" #include "tree.h" +#include "lex.h" #include "hash.h" #include "table.h" @@ -25,10 +26,13 @@ typedef struct { TaggedString **hash; } stringtable; +static int initialized = 0; + static stringtable string_root[NUM_HASHS]; static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 0, {0}}; + static unsigned long hash (char *str) { unsigned long h = 0; @@ -37,6 +41,15 @@ static unsigned long hash (char *str) return h; } +static void initialize (void) +{ + initialized = 1; + luaI_addReserved(); + luaI_initsymbol(); + luaI_initconstant(); +} + + static void grow (stringtable *tb) { int newsize = luaI_redimension(tb->size); @@ -69,7 +82,11 @@ static TaggedString *insert (char *str, stringtable *tb) int i; int j = -1; if ((Long)tb->nuse*3 >= (Long)tb->size*2) + { + if (!initialized) + initialize(); grow(tb); + } i = h%tb->size; while (tb->hash[i]) { @@ -97,12 +114,6 @@ TaggedString *lua_createstring (char *str) 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. @@ -119,7 +130,7 @@ Long lua_strcollector (void) for (j=0; jsize; 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) t->marked = 0; diff --git a/tree.h b/tree.h index eb347ed4..c16d0b91 100644 --- a/tree.h +++ b/tree.h @@ -1,7 +1,7 @@ /* ** tree.h ** 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 @@ -17,13 +17,12 @@ typedef struct TaggedString unsigned short varindex; /* != NOT_USED if this is a symbol */ unsigned short constindex; /* != NOT_USED if this is a constant */ 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 */ } TaggedString; TaggedString *lua_createstring (char *str); -TaggedString *luaI_createfixedstring (char *str); Long lua_strcollector (void); #endif -- cgit v1.2.3-55-g6feb