new experimental syntax using reserved word 'undef'

This commit is contained in:
Roberto Ierusalimschy
2018-03-07 12:55:38 -03:00
parent 464658b16a
commit 4a1612ff9b
11 changed files with 113 additions and 43 deletions

View File

@@ -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 <token>" 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,
"<goto %s> 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 <goto> 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;