diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-11-07 10:03:05 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-11-07 10:03:05 -0200 |
commit | b8fed93215a23a3f443c5b0126f0de1725771b44 (patch) | |
tree | 1d7d29388b8a20fb7d0920d94b02a8040612314d /lparser.c | |
parent | 5e76a4fd313a8690d300085c4e8fcb9dca50c01a (diff) | |
download | lua-b8fed93215a23a3f443c5b0126f0de1725771b44.tar.gz lua-b8fed93215a23a3f443c5b0126f0de1725771b44.tar.bz2 lua-b8fed93215a23a3f443c5b0126f0de1725771b44.zip |
New syntax for to-be-closed variables
The new syntax is <local *toclose x = f()>. The mark '*' allows other
attributes to be added later without the need of new keywords; it
also allows better error messages. The API function was also renamed
('lua_tobeclosed' -> 'lua_toclose').
Diffstat (limited to 'lparser.c')
-rw-r--r-- | lparser.c | 25 |
1 files changed, 13 insertions, 12 deletions
@@ -1546,16 +1546,15 @@ static void localfunc (LexState *ls) { | |||
1546 | } | 1546 | } |
1547 | 1547 | ||
1548 | 1548 | ||
1549 | static void commonlocalstat (LexState *ls, TString *firstvar) { | 1549 | static void commonlocalstat (LexState *ls) { |
1550 | /* stat -> LOCAL NAME {',' NAME} ['=' explist] */ | 1550 | /* stat -> LOCAL NAME {',' NAME} ['=' explist] */ |
1551 | int nvars = 1; | 1551 | int nvars = 0; |
1552 | int nexps; | 1552 | int nexps; |
1553 | expdesc e; | 1553 | expdesc e; |
1554 | new_localvar(ls, firstvar); | 1554 | do { |
1555 | while (testnext(ls, ',')) { | ||
1556 | new_localvar(ls, str_checkname(ls)); | 1555 | new_localvar(ls, str_checkname(ls)); |
1557 | nvars++; | 1556 | nvars++; |
1558 | } | 1557 | } while (testnext(ls, ',')); |
1559 | if (testnext(ls, '=')) | 1558 | if (testnext(ls, '=')) |
1560 | nexps = explist(ls, &e); | 1559 | nexps = explist(ls, &e); |
1561 | else { | 1560 | else { |
@@ -1567,8 +1566,12 @@ static void commonlocalstat (LexState *ls, TString *firstvar) { | |||
1567 | } | 1566 | } |
1568 | 1567 | ||
1569 | 1568 | ||
1570 | static void scopedlocalstat (LexState *ls) { | 1569 | static void tocloselocalstat (LexState *ls) { |
1571 | FuncState *fs = ls->fs; | 1570 | FuncState *fs = ls->fs; |
1571 | TString *attr = str_checkname(ls); | ||
1572 | if (strcmp(getstr(attr), "toclose") != 0) | ||
1573 | luaK_semerror(ls, | ||
1574 | luaO_pushfstring(ls->L, "unknown attribute '%s'", getstr(attr))); | ||
1572 | new_localvar(ls, str_checkname(ls)); | 1575 | new_localvar(ls, str_checkname(ls)); |
1573 | checknext(ls, '='); | 1576 | checknext(ls, '='); |
1574 | exp1(ls, 0); | 1577 | exp1(ls, 0); |
@@ -1580,13 +1583,11 @@ static void scopedlocalstat (LexState *ls) { | |||
1580 | 1583 | ||
1581 | static void localstat (LexState *ls) { | 1584 | static void localstat (LexState *ls) { |
1582 | /* stat -> LOCAL NAME {',' NAME} ['=' explist] | 1585 | /* stat -> LOCAL NAME {',' NAME} ['=' explist] |
1583 | | LOCAL SCOPED NAME '=' exp */ | 1586 | | LOCAL *toclose NAME '=' exp */ |
1584 | TString *firstvar = str_checkname(ls); | 1587 | if (testnext(ls, '*')) |
1585 | if (ls->t.token == TK_NAME && | 1588 | tocloselocalstat(ls); |
1586 | eqshrstr(firstvar, luaS_newliteral(ls->L, "scoped"))) | ||
1587 | scopedlocalstat(ls); | ||
1588 | else | 1589 | else |
1589 | commonlocalstat(ls, firstvar); | 1590 | commonlocalstat(ls); |
1590 | } | 1591 | } |
1591 | 1592 | ||
1592 | 1593 | ||