diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-14 10:35:51 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-14 10:35:51 -0300 |
commit | d1608c597e2f45021d43c56050aff08e5d417699 (patch) | |
tree | e6b0a07e68384cd9707e7dda6864dd8664e2ee99 | |
parent | 0f4903a5d79fb594115c5603072d0dce77b2b84e (diff) | |
download | lua-d1608c597e2f45021d43c56050aff08e5d417699.tar.gz lua-d1608c597e2f45021d43c56050aff08e5d417699.tar.bz2 lua-d1608c597e2f45021d43c56050aff08e5d417699.zip |
reserved words are stored in main string table; "marked" field is
used to indicate its type.
Table initializations centralized by "tree.c".
-rw-r--r-- | lex.c | 31 | ||||
-rw-r--r-- | lex.h | 9 | ||||
-rw-r--r-- | table.c | 76 | ||||
-rw-r--r-- | table.h | 5 | ||||
-rw-r--r-- | tree.c | 27 | ||||
-rw-r--r-- | tree.h | 5 |
6 files changed, 79 insertions, 74 deletions
@@ -1,4 +1,4 @@ | |||
1 | char *rcs_lex = "$Id: lex.c,v 2.25 1996/02/12 18:32:40 roberto Exp roberto $"; | 1 | char *rcs_lex = "$Id: lex.c,v 2.26 1996/02/13 17:30:39 roberto Exp roberto $"; |
2 | 2 | ||
3 | 3 | ||
4 | #include <ctype.h> | 4 | #include <ctype.h> |
@@ -47,7 +47,6 @@ char *lua_lasttext (void) | |||
47 | } | 47 | } |
48 | 48 | ||
49 | 49 | ||
50 | /* The reserved words must be listed in lexicographic order */ | ||
51 | static struct | 50 | static struct |
52 | { | 51 | { |
53 | char *name; | 52 | char *name; |
@@ -74,22 +73,14 @@ static struct | |||
74 | #define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0])) | 73 | #define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0])) |
75 | 74 | ||
76 | 75 | ||
77 | static int findReserved (char *name) | 76 | void luaI_addReserved (void) |
78 | { | 77 | { |
79 | int l = 0; | 78 | int i; |
80 | int h = RESERVEDSIZE - 1; | 79 | for (i=0; i<RESERVEDSIZE; i++) |
81 | while (l <= h) | ||
82 | { | 80 | { |
83 | int m = (l+h)/2; | 81 | TaggedString *ts = lua_createstring(reserved[i].name); |
84 | int comp = lua_strcmp(name, reserved[m].name); | 82 | ts->marked = reserved[i].token; /* reserved word (always > 255) */ |
85 | if (comp < 0) | ||
86 | h = m-1; | ||
87 | else if (comp == 0) | ||
88 | return reserved[m].token; | ||
89 | else | ||
90 | l = m+1; | ||
91 | } | 83 | } |
92 | return 0; | ||
93 | } | 84 | } |
94 | 85 | ||
95 | 86 | ||
@@ -282,12 +273,14 @@ int luaY_lex (void) | |||
282 | case 'Z': | 273 | case 'Z': |
283 | case '_': | 274 | case '_': |
284 | { | 275 | { |
285 | Word res; | 276 | TaggedString *ts; |
286 | do { save_and_next(); } while (isalnum(current) || current == '_'); | 277 | do { save_and_next(); } while (isalnum(current) || current == '_'); |
287 | *yytextLast = 0; | 278 | *yytextLast = 0; |
288 | res = findReserved(yytext); | 279 | ts = lua_createstring(yytext); |
289 | if (res) return res; | 280 | if (ts->marked > 2) |
290 | luaY_lval.pTStr = luaI_createfixedstring(yytext); | 281 | return ts->marked; /* reserved word */ |
282 | luaY_lval.pTStr = ts; | ||
283 | ts->marked = 2; /* avoid GC */ | ||
291 | return NAME; | 284 | return NAME; |
292 | } | 285 | } |
293 | 286 | ||
@@ -1,7 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | ** lex.h | 2 | ** lex.h |
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | ** $Id: $ | 4 | ** $Id: lex.h,v 1.1 1996/02/13 17:30:39 roberto Exp roberto $ |
5 | */ | 5 | */ |
6 | 6 | ||
7 | #ifndef lex_h | 7 | #ifndef lex_h |
@@ -10,9 +10,10 @@ | |||
10 | 10 | ||
11 | typedef int (*Input) (void); | 11 | typedef int (*Input) (void); |
12 | 12 | ||
13 | void lua_setinput (Input fn); /* from "lex.c" module */ | 13 | void lua_setinput (Input fn); |
14 | char *lua_lasttext (void); /* from "lex.c" module */ | 14 | char *lua_lasttext (void); |
15 | int luaY_lex (void); /* from "lex.c" module */ | 15 | int luaY_lex (void); |
16 | void luaI_addReserved (void); | ||
16 | 17 | ||
17 | 18 | ||
18 | #endif | 19 | #endif |
@@ -3,7 +3,7 @@ | |||
3 | ** Module to control static tables | 3 | ** Module to control static tables |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_table="$Id: table.c,v 2.44 1996/01/26 18:03:19 roberto Exp $"; | 6 | char *rcs_table="$Id: table.c,v 2.45 1996/02/12 18:32:40 roberto Exp roberto $"; |
7 | 7 | ||
8 | /*#include <string.h>*/ | 8 | /*#include <string.h>*/ |
9 | 9 | ||
@@ -37,44 +37,44 @@ static void lua_nextvar (void); | |||
37 | /* | 37 | /* |
38 | ** Initialise symbol table with internal functions | 38 | ** Initialise symbol table with internal functions |
39 | */ | 39 | */ |
40 | static void lua_initsymbol (void) | 40 | static struct { |
41 | char *name; | ||
42 | lua_CFunction func; | ||
43 | } int_funcs[] = { | ||
44 | {"nextvar", lua_nextvar}, | ||
45 | {"error", luaI_error}, | ||
46 | {"tonumber", lua_obj2number}, | ||
47 | {"setfallback", luaI_setfallback}, | ||
48 | {"next", lua_next}, | ||
49 | {"dofile", lua_internaldofile}, | ||
50 | {"setglobal", luaI_setglobal}, | ||
51 | {"getglobal", luaI_getglobal}, | ||
52 | {"type", luaI_type}, | ||
53 | {"tostring", luaI_tostring}, | ||
54 | {"print", luaI_print}, | ||
55 | {"dostring", lua_internaldostring}, | ||
56 | {"assert", luaI_assert} | ||
57 | }; | ||
58 | |||
59 | #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0])) | ||
60 | |||
61 | void luaI_initsymbol (void) | ||
41 | { | 62 | { |
42 | Word n; | 63 | int i; |
43 | lua_maxsymbol = BUFFER_BLOCK; | 64 | lua_maxsymbol = BUFFER_BLOCK; |
44 | lua_table = newvector(lua_maxsymbol, Symbol); | 65 | lua_table = newvector(lua_maxsymbol, Symbol); |
45 | n = luaI_findsymbolbyname("nextvar"); | 66 | for (i=0; i<INTFUNCSIZE; i++) |
46 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar; | 67 | { |
47 | n = luaI_findsymbolbyname("error"); | 68 | Word n = luaI_findsymbolbyname(int_funcs[i].name); |
48 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error; | 69 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func; |
49 | n = luaI_findsymbolbyname("tonumber"); | 70 | } |
50 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number; | ||
51 | n = luaI_findsymbolbyname("setfallback"); | ||
52 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback; | ||
53 | n = luaI_findsymbolbyname("next"); | ||
54 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next; | ||
55 | n = luaI_findsymbolbyname("dofile"); | ||
56 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile; | ||
57 | n = luaI_findsymbolbyname("setglobal"); | ||
58 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setglobal; | ||
59 | n = luaI_findsymbolbyname("getglobal"); | ||
60 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_getglobal; | ||
61 | n = luaI_findsymbolbyname("type"); | ||
62 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type; | ||
63 | n = luaI_findsymbolbyname("tostring"); | ||
64 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_tostring; | ||
65 | n = luaI_findsymbolbyname("print"); | ||
66 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_print; | ||
67 | n = luaI_findsymbolbyname("dostring"); | ||
68 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring; | ||
69 | n = luaI_findsymbolbyname("assert"); | ||
70 | s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_assert; | ||
71 | } | 71 | } |
72 | 72 | ||
73 | 73 | ||
74 | /* | 74 | /* |
75 | ** Initialise constant table with pre-defined constants | 75 | ** Initialise constant table with pre-defined constants |
76 | */ | 76 | */ |
77 | void lua_initconstant (void) | 77 | void luaI_initconstant (void) |
78 | { | 78 | { |
79 | lua_maxconstant = BUFFER_BLOCK; | 79 | lua_maxconstant = BUFFER_BLOCK; |
80 | lua_constant = newvector(lua_maxconstant, TaggedString *); | 80 | lua_constant = newvector(lua_maxconstant, TaggedString *); |
@@ -87,8 +87,6 @@ void lua_initconstant (void) | |||
87 | */ | 87 | */ |
88 | Word luaI_findsymbol (TaggedString *t) | 88 | Word luaI_findsymbol (TaggedString *t) |
89 | { | 89 | { |
90 | if (lua_table == NULL) | ||
91 | lua_initsymbol(); | ||
92 | if (t->varindex == NOT_USED) | 90 | if (t->varindex == NOT_USED) |
93 | { | 91 | { |
94 | if (lua_ntable == lua_maxsymbol) | 92 | if (lua_ntable == lua_maxsymbol) |
@@ -104,6 +102,8 @@ Word luaI_findsymbol (TaggedString *t) | |||
104 | lua_table[lua_ntable].varname = t; | 102 | lua_table[lua_ntable].varname = t; |
105 | s_tag(lua_ntable) = LUA_T_NIL; | 103 | s_tag(lua_ntable) = LUA_T_NIL; |
106 | lua_ntable++; | 104 | lua_ntable++; |
105 | if (!t->marked) | ||
106 | t->marked = 2; /* avoid GC */ | ||
107 | } | 107 | } |
108 | return t->varindex; | 108 | return t->varindex; |
109 | } | 109 | } |
@@ -111,7 +111,7 @@ Word luaI_findsymbol (TaggedString *t) | |||
111 | 111 | ||
112 | Word luaI_findsymbolbyname (char *name) | 112 | Word luaI_findsymbolbyname (char *name) |
113 | { | 113 | { |
114 | return luaI_findsymbol(luaI_createfixedstring(name)); | 114 | return luaI_findsymbol(lua_createstring(name)); |
115 | } | 115 | } |
116 | 116 | ||
117 | 117 | ||
@@ -121,8 +121,6 @@ Word luaI_findsymbolbyname (char *name) | |||
121 | */ | 121 | */ |
122 | Word luaI_findconstant (TaggedString *t) | 122 | Word luaI_findconstant (TaggedString *t) |
123 | { | 123 | { |
124 | if (lua_constant == NULL) | ||
125 | lua_initconstant(); | ||
126 | if (t->constindex == NOT_USED) | 124 | if (t->constindex == NOT_USED) |
127 | { | 125 | { |
128 | if (lua_nconstant == lua_maxconstant) | 126 | if (lua_nconstant == lua_maxconstant) |
@@ -137,6 +135,8 @@ Word luaI_findconstant (TaggedString *t) | |||
137 | t->constindex = lua_nconstant; | 135 | t->constindex = lua_nconstant; |
138 | lua_constant[lua_nconstant] = t; | 136 | lua_constant[lua_nconstant] = t; |
139 | lua_nconstant++; | 137 | lua_nconstant++; |
138 | if (!t->marked) | ||
139 | t->marked = 2; /* avoid GC */ | ||
140 | } | 140 | } |
141 | return t->constindex; | 141 | return t->constindex; |
142 | } | 142 | } |
@@ -144,7 +144,7 @@ Word luaI_findconstant (TaggedString *t) | |||
144 | 144 | ||
145 | Word luaI_findconstantbyname (char *name) | 145 | Word luaI_findconstantbyname (char *name) |
146 | { | 146 | { |
147 | return luaI_findconstant(luaI_createfixedstring(name)); | 147 | return luaI_findconstant(lua_createstring(name)); |
148 | } | 148 | } |
149 | 149 | ||
150 | TaggedString *lua_constcreate(char *name) | 150 | TaggedString *lua_constcreate(char *name) |
@@ -1,7 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | ** Module to control static tables | 2 | ** Module to control static tables |
3 | ** TeCGraf - PUC-Rio | 3 | ** TeCGraf - PUC-Rio |
4 | ** $Id: table.h,v 2.16 1996/02/06 16:18:21 roberto Exp roberto $ | 4 | ** $Id: table.h,v 2.17 1996/02/12 18:32:40 roberto Exp roberto $ |
5 | */ | 5 | */ |
6 | 6 | ||
7 | #ifndef table_h | 7 | #ifndef table_h |
@@ -20,7 +20,8 @@ typedef struct | |||
20 | extern Symbol *lua_table; | 20 | extern Symbol *lua_table; |
21 | extern TaggedString **lua_constant; | 21 | extern TaggedString **lua_constant; |
22 | 22 | ||
23 | void lua_initconstant (void); | 23 | void luaI_initsymbol (void); |
24 | void luaI_initconstant (void); | ||
24 | Word luaI_findsymbolbyname (char *name); | 25 | Word luaI_findsymbolbyname (char *name); |
25 | Word luaI_findsymbol (TaggedString *t); | 26 | Word luaI_findsymbol (TaggedString *t); |
26 | Word luaI_findconstant (TaggedString *t); | 27 | Word luaI_findconstant (TaggedString *t); |
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_tree="$Id: tree.c,v 1.15 1996/01/26 18:03:19 roberto Exp $"; | 6 | char *rcs_tree="$Id: tree.c,v 1.16 1996/02/12 18:32:40 roberto Exp roberto $"; |
7 | 7 | ||
8 | 8 | ||
9 | #include <string.h> | 9 | #include <string.h> |
@@ -11,6 +11,7 @@ char *rcs_tree="$Id: tree.c,v 1.15 1996/01/26 18:03:19 roberto Exp $"; | |||
11 | #include "mem.h" | 11 | #include "mem.h" |
12 | #include "lua.h" | 12 | #include "lua.h" |
13 | #include "tree.h" | 13 | #include "tree.h" |
14 | #include "lex.h" | ||
14 | #include "hash.h" | 15 | #include "hash.h" |
15 | #include "table.h" | 16 | #include "table.h" |
16 | 17 | ||
@@ -25,10 +26,13 @@ typedef struct { | |||
25 | TaggedString **hash; | 26 | TaggedString **hash; |
26 | } stringtable; | 27 | } stringtable; |
27 | 28 | ||
29 | static int initialized = 0; | ||
30 | |||
28 | static stringtable string_root[NUM_HASHS]; | 31 | static stringtable string_root[NUM_HASHS]; |
29 | 32 | ||
30 | static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 0, {0}}; | 33 | static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 0, {0}}; |
31 | 34 | ||
35 | |||
32 | static unsigned long hash (char *str) | 36 | static unsigned long hash (char *str) |
33 | { | 37 | { |
34 | unsigned long h = 0; | 38 | unsigned long h = 0; |
@@ -37,6 +41,15 @@ static unsigned long hash (char *str) | |||
37 | return h; | 41 | return h; |
38 | } | 42 | } |
39 | 43 | ||
44 | static void initialize (void) | ||
45 | { | ||
46 | initialized = 1; | ||
47 | luaI_addReserved(); | ||
48 | luaI_initsymbol(); | ||
49 | luaI_initconstant(); | ||
50 | } | ||
51 | |||
52 | |||
40 | static void grow (stringtable *tb) | 53 | static void grow (stringtable *tb) |
41 | { | 54 | { |
42 | int newsize = luaI_redimension(tb->size); | 55 | int newsize = luaI_redimension(tb->size); |
@@ -69,7 +82,11 @@ static TaggedString *insert (char *str, stringtable *tb) | |||
69 | int i; | 82 | int i; |
70 | int j = -1; | 83 | int j = -1; |
71 | if ((Long)tb->nuse*3 >= (Long)tb->size*2) | 84 | if ((Long)tb->nuse*3 >= (Long)tb->size*2) |
85 | { | ||
86 | if (!initialized) | ||
87 | initialize(); | ||
72 | grow(tb); | 88 | grow(tb); |
89 | } | ||
73 | i = h%tb->size; | 90 | i = h%tb->size; |
74 | while (tb->hash[i]) | 91 | while (tb->hash[i]) |
75 | { | 92 | { |
@@ -97,12 +114,6 @@ TaggedString *lua_createstring (char *str) | |||
97 | return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]); | 114 | return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]); |
98 | } | 115 | } |
99 | 116 | ||
100 | TaggedString *luaI_createfixedstring (char *str) | ||
101 | { | ||
102 | TaggedString *ts = lua_createstring(str); | ||
103 | ts->marked = 2; /* to avoid GC */ | ||
104 | return ts; | ||
105 | } | ||
106 | 117 | ||
107 | /* | 118 | /* |
108 | ** Garbage collection function. | 119 | ** Garbage collection function. |
@@ -119,7 +130,7 @@ Long lua_strcollector (void) | |||
119 | for (j=0; j<tb->size; j++) | 130 | for (j=0; j<tb->size; j++) |
120 | { | 131 | { |
121 | TaggedString *t = tb->hash[j]; | 132 | TaggedString *t = tb->hash[j]; |
122 | if (t != NULL && t != &EMPTY && t->marked != 2) | 133 | if (t != NULL && t != &EMPTY && t->marked <= 1) |
123 | { | 134 | { |
124 | if (t->marked) | 135 | if (t->marked) |
125 | t->marked = 0; | 136 | t->marked = 0; |
@@ -1,7 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | ** tree.h | 2 | ** tree.h |
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | ** $Id: tree.h,v 1.11 1996/01/26 18:03:19 roberto Exp roberto $ | 4 | ** $Id: tree.h,v 1.12 1996/02/12 18:32:40 roberto Exp roberto $ |
5 | */ | 5 | */ |
6 | 6 | ||
7 | #ifndef tree_h | 7 | #ifndef tree_h |
@@ -17,13 +17,12 @@ typedef struct TaggedString | |||
17 | unsigned short varindex; /* != NOT_USED if this is a symbol */ | 17 | unsigned short varindex; /* != NOT_USED if this is a symbol */ |
18 | unsigned short constindex; /* != NOT_USED if this is a constant */ | 18 | unsigned short constindex; /* != NOT_USED if this is a constant */ |
19 | unsigned long hash; /* 0 if not initialized */ | 19 | unsigned long hash; /* 0 if not initialized */ |
20 | char marked; /* for garbage collection; 2 means "never collect" */ | 20 | int marked; /* for garbage collection; never collect (nor change) if > 1 */ |
21 | char str[1]; /* \0 byte already reserved */ | 21 | char str[1]; /* \0 byte already reserved */ |
22 | } TaggedString; | 22 | } TaggedString; |
23 | 23 | ||
24 | 24 | ||
25 | TaggedString *lua_createstring (char *str); | 25 | TaggedString *lua_createstring (char *str); |
26 | TaggedString *luaI_createfixedstring (char *str); | ||
27 | Long lua_strcollector (void); | 26 | Long lua_strcollector (void); |
28 | 27 | ||
29 | #endif | 28 | #endif |