aboutsummaryrefslogtreecommitdiff
path: root/lparser.c
diff options
context:
space:
mode:
Diffstat (limited to 'lparser.c')
-rw-r--r--lparser.c47
1 files changed, 10 insertions, 37 deletions
diff --git a/lparser.c b/lparser.c
index eb61b02c..6ce0bdbf 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 2.77 2010/03/04 18:12:57 roberto Exp roberto $ 2** $Id: lparser.c,v 2.78 2010/03/08 16:55:52 roberto Exp roberto $
3** Lua Parser 3** Lua Parser
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -259,7 +259,7 @@ static void markupval (FuncState *fs, int level) {
259*/ 259*/
260static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { 260static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
261 if (fs == NULL) /* no more levels? */ 261 if (fs == NULL) /* no more levels? */
262 return VGLOBAL; /* default is global variable */ 262 return VVOID; /* default is global */
263 else { 263 else {
264 int v = searchvar(fs, n); /* look up locals at current level */ 264 int v = searchvar(fs, n); /* look up locals at current level */
265 if (v >= 0) { /* found? */ 265 if (v >= 0) { /* found? */
@@ -271,8 +271,8 @@ static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
271 else { /* not found as local at current level; try upvalues */ 271 else { /* not found as local at current level; try upvalues */
272 int idx = searchupvalue(fs, n); /* try existing upvalues */ 272 int idx = searchupvalue(fs, n); /* try existing upvalues */
273 if (idx < 0) { /* not found? */ 273 if (idx < 0) { /* not found? */
274 if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL) /* try upper levels */ 274 if (singlevaraux(fs->prev, n, var, 0) == VVOID) /* try upper levels */
275 return VGLOBAL; /* not found; is a global */ 275 return VVOID; /* not found; is a global */
276 /* else was LOCAL or UPVAL */ 276 /* else was LOCAL or UPVAL */
277 idx = newupvalue(fs, n, var); /* will be a new upvalue */ 277 idx = newupvalue(fs, n, var); /* will be a new upvalue */
278 } 278 }
@@ -286,15 +286,12 @@ static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
286static void singlevar (LexState *ls, expdesc *var) { 286static void singlevar (LexState *ls, expdesc *var) {
287 TString *varname = str_checkname(ls); 287 TString *varname = str_checkname(ls);
288 FuncState *fs = ls->fs; 288 FuncState *fs = ls->fs;
289 if (singlevaraux(fs, varname, var, 1) == VGLOBAL) { 289 if (singlevaraux(fs, varname, var, 1) == VVOID) { /* global name? */
290 if (fs->envreg == NO_REG) /* regular global? */ 290 expdesc key;
291 init_exp(var, VGLOBAL, luaK_stringK(fs, varname)); 291 singlevaraux(fs, ls->envn, var, 1); /* get _ENV variable */
292 else { /* "globals" are in current lexical environment */ 292 lua_assert(var->k == VLOCAL || var->k == VUPVAL);
293 expdesc key; 293 codestring(ls, &key, varname); /* key is variable name */
294 init_exp(var, VLOCAL, fs->envreg); /* current environment */ 294 luaK_indexed(fs, var, &key); /* env[varname] */
295 codestring(ls, &key, varname); /* key is variable name */
296 luaK_indexed(fs, var, &key); /* env[varname] */
297 }
298 } 295 }
299} 296}
300 297
@@ -363,7 +360,6 @@ static void pushclosure (LexState *ls, Proto *clp, expdesc *v) {
363 while (oldsize < f->sizep) f->p[oldsize++] = NULL; 360 while (oldsize < f->sizep) f->p[oldsize++] = NULL;
364 f->p[fs->np++] = clp; 361 f->p[fs->np++] = clp;
365 /* initial environment for new function is current lexical environment */ 362 /* initial environment for new function is current lexical environment */
366 clp->envreg = fs->envreg;
367 luaC_objbarrier(ls->L, f, clp); 363 luaC_objbarrier(ls->L, f, clp);
368 init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1)); 364 init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
369} 365}
@@ -386,7 +382,6 @@ static void open_func (LexState *ls, FuncState *fs) {
386 fs->nlocvars = 0; 382 fs->nlocvars = 0;
387 fs->nactvar = 0; 383 fs->nactvar = 0;
388 fs->firstlocal = ls->varl->nactvar; 384 fs->firstlocal = ls->varl->nactvar;
389 fs->envreg = NO_REG;
390 fs->bl = NULL; 385 fs->bl = NULL;
391 f = luaF_newproto(L); 386 f = luaF_newproto(L);
392 fs->f = f; 387 fs->f = f;
@@ -1304,24 +1299,6 @@ static void funcstat (LexState *ls, int line) {
1304} 1299}
1305 1300
1306 1301
1307static void instat (LexState *ls, int line) {
1308 /* instat -> IN exp DO block END */
1309 FuncState *fs = ls->fs;
1310 int oldenv = fs->envreg; /* save current environment */
1311 BlockCnt bl;
1312 luaX_next(ls); /* skip IN */
1313 enterblock(fs, &bl, 0); /* scope for environment variable */
1314 new_localvarliteral(ls, "(environment)");
1315 fs->envreg = exp1(ls); /* new environment */
1316 adjustlocalvars(ls, 1);
1317 checknext(ls, TK_DO);
1318 block(ls);
1319 leaveblock(fs);
1320 check_match(ls, TK_END, TK_IN, line);
1321 fs->envreg = oldenv; /* restore outer environment */
1322}
1323
1324
1325static void exprstat (LexState *ls) { 1302static void exprstat (LexState *ls) {
1326 /* stat -> func | assignment */ 1303 /* stat -> func | assignment */
1327 FuncState *fs = ls->fs; 1304 FuncState *fs = ls->fs;
@@ -1386,10 +1363,6 @@ static int statement (LexState *ls) {
1386 check_match(ls, TK_END, TK_DO, line); 1363 check_match(ls, TK_END, TK_DO, line);
1387 return 0; 1364 return 0;
1388 } 1365 }
1389 case TK_IN: {
1390 instat(ls, line);
1391 return 0;
1392 }
1393 case TK_FOR: { /* stat -> forstat */ 1366 case TK_FOR: { /* stat -> forstat */
1394 forstat(ls, line); 1367 forstat(ls, line);
1395 return 0; 1368 return 0;