From 4a1612ff9b968fe446bc4dd20460bfaccabeb3b3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Mar 2018 12:55:38 -0300 Subject: new experimental syntax using reserved word 'undef' --- lparser.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'lparser.c') diff --git a/lparser.c b/lparser.c index da27c472..9bf5485e 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.177 2018/02/09 15:16:06 roberto Exp roberto $ +** $Id: lparser.c,v 2.178 2018/02/17 19:20:00 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -65,13 +65,6 @@ static void statement (LexState *ls); static void expr (LexState *ls, expdesc *v); -/* semantic error */ -static l_noret semerror (LexState *ls, const char *msg) { - ls->t.token = 0; /* remove "near " from final message */ - luaX_syntaxerror(ls, msg); -} - - static l_noret error_expected (LexState *ls, int token) { luaX_syntaxerror(ls, luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); @@ -347,7 +340,7 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) { const char *msg = luaO_pushfstring(ls->L, " at line %d jumps into the scope of local '%s'", getstr(gt->name), gt->line, getstr(vname)); - semerror(ls, msg); + luaK_semerror(ls, msg); } luaK_patchgoto(fs, gt->pc, label->pc, 1); /* remove goto from pending list */ @@ -477,7 +470,7 @@ static void fixbreaks (FuncState *fs, BlockCnt *bl) { static l_noret undefgoto (LexState *ls, Labeldesc *gt) { const char *msg = "no visible label '%s' for at line %d"; msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); - semerror(ls, msg); + luaK_semerror(ls, msg); } @@ -900,6 +893,11 @@ static void primaryexp (LexState *ls, expdesc *v) { singlevar(ls, v); return; } + case TK_UNDEF: { + luaX_next(ls); + init_exp(v, VUNDEF, 0); + return; + } default: { luaX_syntaxerror(ls, "unexpected symbol"); } @@ -1185,6 +1183,10 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { else { /* assignment -> '=' explist */ int nexps; checknext(ls, '='); + if (nvars == 1 && testnext(ls, TK_UNDEF)) { + luaK_codeundef(ls->fs, &lh->v); + return; + } nexps = explist(ls, &e); if (nexps != nvars) adjust_assign(ls, nvars, nexps, &e); @@ -1237,7 +1239,7 @@ static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { const char *msg = luaO_pushfstring(fs->ls->L, "label '%s' already defined on line %d", getstr(label), ll->arr[i].line); - semerror(fs->ls, msg); + luaK_semerror(fs->ls, msg); } } } @@ -1650,6 +1652,11 @@ static void statement (LexState *ls) { luaX_next(ls); /* skip LOCAL */ if (testnext(ls, TK_FUNCTION)) /* local function? */ localfunc(ls); + else if (testnext(ls, TK_UNDEF)) + (void)0; /* ignore */ + /* old versions may need to declare 'local undef' + when using 'undef' with no environment; so this + version accepts (and ignores) these declarations */ else localstat(ls); break; -- cgit v1.2.3-55-g6feb