small optimizations

This commit is contained in:
Roberto Ierusalimschy 2005-08-22 15:54:32 -03:00
parent 027e9e99ca
commit 43ad0637ca

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 2.31 2005/07/11 14:01:37 roberto Exp roberto $ ** $Id: lparser.c,v 2.32 2005/08/17 18:32:09 roberto Exp roberto $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -239,35 +239,35 @@ static void markupval (FuncState *fs, int level) {
} }
static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
if (fs == NULL) /* no more levels? */ if (fs == NULL) { /* no more levels? */
init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ init_exp(var, VGLOBAL, NO_REG); /* default is global variable */
return VGLOBAL;
}
else { else {
int v = searchvar(fs, n); /* look up at current level */ int v = searchvar(fs, n); /* look up at current level */
if (v >= 0) { if (v >= 0) {
init_exp(var, VLOCAL, v); init_exp(var, VLOCAL, v);
if (!base) if (!base)
markupval(fs, v); /* local will be used as an upval */ markupval(fs, v); /* local will be used as an upval */
return VLOCAL;
} }
else { /* not found at current level; try upper one */ else { /* not found at current level; try upper one */
singlevaraux(fs->prev, n, var, 0); if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL)
if (var->k == VGLOBAL) { return VGLOBAL;
if (base) var->info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */
var->info = luaK_stringK(fs, n); /* info points to global name */ var->k = VUPVAL; /* upvalue in this level */
} return VUPVAL;
else { /* LOCAL or UPVAL */
var->info = indexupvalue(fs, n, var);
var->k = VUPVAL; /* upvalue in this level */
}
} }
} }
} }
static TString *singlevar (LexState *ls, expdesc *var, int base) { static void singlevar (LexState *ls, expdesc *var) {
TString *varname = str_checkname(ls); TString *varname = str_checkname(ls);
singlevaraux(ls->fs, varname, var, base); FuncState *fs = ls->fs;
return varname; if (singlevaraux(fs, varname, var, 1) == VGLOBAL)
var->info = luaK_stringK(fs, varname); /* info points to global name */
} }
@ -694,7 +694,7 @@ static void prefixexp (LexState *ls, expdesc *v) {
return; return;
} }
case TK_NAME: { case TK_NAME: {
singlevar(ls, v, 1); singlevar(ls, v);
return; return;
} }
default: { default: {
@ -1214,7 +1214,7 @@ static void localstat (LexState *ls) {
static int funcname (LexState *ls, expdesc *v) { static int funcname (LexState *ls, expdesc *v) {
/* funcname -> NAME {field} [`:' NAME] */ /* funcname -> NAME {field} [`:' NAME] */
int needself = 0; int needself = 0;
singlevar(ls, v, 1); singlevar(ls, v);
while (ls->t.token == '.') while (ls->t.token == '.')
field(ls, v); field(ls, v);
if (ls->t.token == ':') { if (ls->t.token == ':') {