lua-users home
lua-l archive

Re: Say No to global-by-default (summary of the discussion)

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


If you really want to play with this idea, here is a patch for Lua 5.3.5:
diff llex.c noglobal/llex.c
547a548
> #include "proxy.c"
diff lparser.c noglobal/lparser.c
284c284,286
< if (var->k == VVOID) /* not found? */
---
> if (var->k == VVOID) /* not found? */ {
> luaX_syntaxerror(fs->ls,
> luaO_pushfstring(fs->ls->L,"undeclared global " LUA_QL("%s"),getstr(n)));
285a288
> }
% cat test.lua
\a=3
\print(\a)
\print(a)
% cat proxy.c
/*
* proxy.c
* lexer proxy for Lua parser -- implements '\' -> '_ENV.'
* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
* This code is hereby placed in the public domain.
* Add <<#include "proxy.c">> just before the definition of luaX_next in llex.c
*/
static int nexttoken(LexState *ls, SemInfo *seminfo)
{
 static int state=0;
 int t;
 if (state==0) {
 t=llex(ls,seminfo);
 if (t=='\\') {
 state=1;
 seminfo->ts=ls->envn;
 t=TK_NAME;
 }
 return t;
 } else {
 state=0;
 return '.';
 }
 return t;
}
#define llex nexttoken

AltStyle によって変換されたページ (->オリジナル) /