diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-03-07 12:55:38 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-03-07 12:55:38 -0300 |
| commit | 4a1612ff9b968fe446bc4dd20460bfaccabeb3b3 (patch) | |
| tree | aef8f8c476e0814c357ead6248a614fb0ca07eac /lparser.c | |
| parent | 464658b16a1a539fd590e1696bfcfb572a77fe13 (diff) | |
| download | lua-4a1612ff9b968fe446bc4dd20460bfaccabeb3b3.tar.gz lua-4a1612ff9b968fe446bc4dd20460bfaccabeb3b3.tar.bz2 lua-4a1612ff9b968fe446bc4dd20460bfaccabeb3b3.zip | |
new experimental syntax using reserved word 'undef'
Diffstat (limited to 'lparser.c')
| -rw-r--r-- | lparser.c | 29 |
1 files changed, 18 insertions, 11 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lparser.c,v 2.177 2018/02/09 15:16:06 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 2.178 2018/02/17 19:20:00 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 | */ |
| @@ -65,13 +65,6 @@ static void statement (LexState *ls); | |||
| 65 | static void expr (LexState *ls, expdesc *v); | 65 | static void expr (LexState *ls, expdesc *v); |
| 66 | 66 | ||
| 67 | 67 | ||
| 68 | /* semantic error */ | ||
| 69 | static l_noret semerror (LexState *ls, const char *msg) { | ||
| 70 | ls->t.token = 0; /* remove "near <token>" from final message */ | ||
| 71 | luaX_syntaxerror(ls, msg); | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | static l_noret error_expected (LexState *ls, int token) { | 68 | static l_noret error_expected (LexState *ls, int token) { |
| 76 | luaX_syntaxerror(ls, | 69 | luaX_syntaxerror(ls, |
| 77 | luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); | 70 | luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); |
| @@ -347,7 +340,7 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) { | |||
| 347 | const char *msg = luaO_pushfstring(ls->L, | 340 | const char *msg = luaO_pushfstring(ls->L, |
| 348 | "<goto %s> at line %d jumps into the scope of local '%s'", | 341 | "<goto %s> at line %d jumps into the scope of local '%s'", |
| 349 | getstr(gt->name), gt->line, getstr(vname)); | 342 | getstr(gt->name), gt->line, getstr(vname)); |
| 350 | semerror(ls, msg); | 343 | luaK_semerror(ls, msg); |
| 351 | } | 344 | } |
| 352 | luaK_patchgoto(fs, gt->pc, label->pc, 1); | 345 | luaK_patchgoto(fs, gt->pc, label->pc, 1); |
| 353 | /* remove goto from pending list */ | 346 | /* remove goto from pending list */ |
| @@ -477,7 +470,7 @@ static void fixbreaks (FuncState *fs, BlockCnt *bl) { | |||
| 477 | static l_noret undefgoto (LexState *ls, Labeldesc *gt) { | 470 | static l_noret undefgoto (LexState *ls, Labeldesc *gt) { |
| 478 | const char *msg = "no visible label '%s' for <goto> at line %d"; | 471 | const char *msg = "no visible label '%s' for <goto> at line %d"; |
| 479 | msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); | 472 | msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); |
| 480 | semerror(ls, msg); | 473 | luaK_semerror(ls, msg); |
| 481 | } | 474 | } |
| 482 | 475 | ||
| 483 | 476 | ||
| @@ -900,6 +893,11 @@ static void primaryexp (LexState *ls, expdesc *v) { | |||
| 900 | singlevar(ls, v); | 893 | singlevar(ls, v); |
| 901 | return; | 894 | return; |
| 902 | } | 895 | } |
| 896 | case TK_UNDEF: { | ||
| 897 | luaX_next(ls); | ||
| 898 | init_exp(v, VUNDEF, 0); | ||
| 899 | return; | ||
| 900 | } | ||
| 903 | default: { | 901 | default: { |
| 904 | luaX_syntaxerror(ls, "unexpected symbol"); | 902 | luaX_syntaxerror(ls, "unexpected symbol"); |
| 905 | } | 903 | } |
| @@ -1185,6 +1183,10 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { | |||
| 1185 | else { /* assignment -> '=' explist */ | 1183 | else { /* assignment -> '=' explist */ |
| 1186 | int nexps; | 1184 | int nexps; |
| 1187 | checknext(ls, '='); | 1185 | checknext(ls, '='); |
| 1186 | if (nvars == 1 && testnext(ls, TK_UNDEF)) { | ||
| 1187 | luaK_codeundef(ls->fs, &lh->v); | ||
| 1188 | return; | ||
| 1189 | } | ||
| 1188 | nexps = explist(ls, &e); | 1190 | nexps = explist(ls, &e); |
| 1189 | if (nexps != nvars) | 1191 | if (nexps != nvars) |
| 1190 | adjust_assign(ls, nvars, nexps, &e); | 1192 | adjust_assign(ls, nvars, nexps, &e); |
| @@ -1237,7 +1239,7 @@ static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { | |||
| 1237 | const char *msg = luaO_pushfstring(fs->ls->L, | 1239 | const char *msg = luaO_pushfstring(fs->ls->L, |
| 1238 | "label '%s' already defined on line %d", | 1240 | "label '%s' already defined on line %d", |
| 1239 | getstr(label), ll->arr[i].line); | 1241 | getstr(label), ll->arr[i].line); |
| 1240 | semerror(fs->ls, msg); | 1242 | luaK_semerror(fs->ls, msg); |
| 1241 | } | 1243 | } |
| 1242 | } | 1244 | } |
| 1243 | } | 1245 | } |
| @@ -1650,6 +1652,11 @@ static void statement (LexState *ls) { | |||
| 1650 | luaX_next(ls); /* skip LOCAL */ | 1652 | luaX_next(ls); /* skip LOCAL */ |
| 1651 | if (testnext(ls, TK_FUNCTION)) /* local function? */ | 1653 | if (testnext(ls, TK_FUNCTION)) /* local function? */ |
| 1652 | localfunc(ls); | 1654 | localfunc(ls); |
| 1655 | else if (testnext(ls, TK_UNDEF)) | ||
| 1656 | (void)0; /* ignore */ | ||
| 1657 | /* old versions may need to declare 'local undef' | ||
| 1658 | when using 'undef' with no environment; so this | ||
| 1659 | version accepts (and ignores) these declarations */ | ||
| 1653 | else | 1660 | else |
| 1654 | localstat(ls); | 1661 | localstat(ls); |
| 1655 | break; | 1662 | break; |
