diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-05-16 14:51:07 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-05-16 14:51:07 -0300 |
commit | ded2ad2d86f44424c6b6e12bf1b75836cfa9e502 (patch) | |
tree | 825ca888400928a3912687a8c4af793ad6b1f7b5 | |
parent | 3fb7a77731e6140674a6b13b73979256bfb95ce3 (diff) | |
download | lua-ded2ad2d86f44424c6b6e12bf1b75836cfa9e502.tar.gz lua-ded2ad2d86f44424c6b6e12bf1b75836cfa9e502.tar.bz2 lua-ded2ad2d86f44424c6b6e12bf1b75836cfa9e502.zip |
Slightly faster way to check for "global"
-rw-r--r-- | llex.c | 20 | ||||
-rw-r--r-- | llex.h | 1 | ||||
-rw-r--r-- | lparser.c | 4 |
3 files changed, 13 insertions, 12 deletions
@@ -40,16 +40,11 @@ | |||
40 | 40 | ||
41 | #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') | 41 | #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') |
42 | 42 | ||
43 | #if defined(LUA_COMPAT_GLOBAL) | ||
44 | #define GLOBALLEX ".g" /* anything not recognizable as a name */ | ||
45 | #else | ||
46 | #define GLOBALLEX "global" | ||
47 | #endif | ||
48 | 43 | ||
49 | /* ORDER RESERVED */ | 44 | /* ORDER RESERVED */ |
50 | static const char *const luaX_tokens [] = { | 45 | static const char *const luaX_tokens [] = { |
51 | "and", "break", "do", "else", "elseif", | 46 | "and", "break", "do", "else", "elseif", |
52 | "end", "false", "for", "function", GLOBALLEX, "goto", "if", | 47 | "end", "false", "for", "function", "global", "goto", "if", |
53 | "in", "local", "nil", "not", "or", "repeat", | 48 | "in", "local", "nil", "not", "or", "repeat", |
54 | "return", "then", "true", "until", "while", | 49 | "return", "then", "true", "until", "while", |
55 | "//", "..", "...", "==", ">=", "<=", "~=", | 50 | "//", "..", "...", "==", ">=", "<=", "~=", |
@@ -189,10 +184,15 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, | |||
189 | ls->linenumber = 1; | 184 | ls->linenumber = 1; |
190 | ls->lastline = 1; | 185 | ls->lastline = 1; |
191 | ls->source = source; | 186 | ls->source = source; |
192 | ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */ | 187 | /* all three strings here ("_ENV", "break", "global") were fixed, |
193 | ls->brkn = luaS_newliteral(L, "break"); /* get "break" name */ | 188 | so they cannot be collected */ |
194 | /* "break" cannot be collected, as it is a reserved word" */ | 189 | ls->envn = luaS_newliteral(L, LUA_ENV); /* get env string */ |
195 | lua_assert(isreserved(ls->brkn)); | 190 | ls->brkn = luaS_newliteral(L, "break"); /* get "break" string */ |
191 | #if defined(LUA_COMPAT_GLOBAL) | ||
192 | /* compatibility mode: "global" is not a reserved word */ | ||
193 | ls->glbn = luaS_newliteral(L, "global"); /* get "global" string */ | ||
194 | ls->glbn->extra = 0; /* mark it as not reserved */ | ||
195 | #endif | ||
196 | luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ | 196 | luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ |
197 | } | 197 | } |
198 | 198 | ||
@@ -76,6 +76,7 @@ typedef struct LexState { | |||
76 | TString *source; /* current source name */ | 76 | TString *source; /* current source name */ |
77 | TString *envn; /* environment variable name */ | 77 | TString *envn; /* environment variable name */ |
78 | TString *brkn; /* "break" name (used as a label) */ | 78 | TString *brkn; /* "break" name (used as a label) */ |
79 | TString *glbn; /* "global" name (when not a reserved word) */ | ||
79 | } LexState; | 80 | } LexState; |
80 | 81 | ||
81 | 82 | ||
@@ -2001,10 +2001,10 @@ static void statement (LexState *ls) { | |||
2001 | case TK_NAME: { | 2001 | case TK_NAME: { |
2002 | /* compatibility code to parse global keyword when "global" | 2002 | /* compatibility code to parse global keyword when "global" |
2003 | is not reserved */ | 2003 | is not reserved */ |
2004 | if (strcmp(getstr(ls->t.seminfo.ts), "global") == 0) { | 2004 | if (ls->t.seminfo.ts == ls->glbn) { /* current = "global"? */ |
2005 | int lk = luaX_lookahead(ls); | 2005 | int lk = luaX_lookahead(ls); |
2006 | if (lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) { | 2006 | if (lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) { |
2007 | /* 'global <name>' or 'global *' or 'global function' */ | 2007 | /* 'global name' or 'global *' or 'global function' */ |
2008 | globalstatfunc(ls, line); | 2008 | globalstatfunc(ls, line); |
2009 | break; | 2009 | break; |
2010 | } | 2010 | } |