aboutsummaryrefslogtreecommitdiff
path: root/lparser.c
diff options
context:
space:
mode:
Diffstat (limited to 'lparser.c')
-rw-r--r--lparser.c34
1 files changed, 22 insertions, 12 deletions
diff --git a/lparser.c b/lparser.c
index 3c68f2d7..875f7d04 100644
--- a/lparser.c
+++ b/lparser.c
@@ -206,6 +206,7 @@ static Vardesc *new_localvar (LexState *ls, TString *name) {
206 var = &dyd->actvar.arr[dyd->actvar.n++]; 206 var = &dyd->actvar.arr[dyd->actvar.n++];
207 var->idx = cast(short, reg); 207 var->idx = cast(short, reg);
208 var->ro = 0; 208 var->ro = 0;
209 setnilvalue(&var->val);
209 return var; 210 return var;
210} 211}
211 212
@@ -238,7 +239,7 @@ static LocVar *getlocvar (FuncState *fs, int i) {
238** where that variable was defined. Return NULL if expression 239** where that variable was defined. Return NULL if expression
239** is neither a local variable nor an upvalue. 240** is neither a local variable nor an upvalue.
240*/ 241*/
241static Vardesc *getvardesc (FuncState **fs, expdesc *e) { 242Vardesc *luaY_getvardesc (FuncState **fs, const expdesc *e) {
242 if (e->k == VLOCAL) 243 if (e->k == VLOCAL)
243 return getlocalvardesc(*fs, e->u.var.idx); 244 return getlocalvardesc(*fs, e->u.var.idx);
244 else if (e->k != VUPVAL) 245 else if (e->k != VUPVAL)
@@ -261,7 +262,7 @@ static Vardesc *getvardesc (FuncState **fs, expdesc *e) {
261 262
262static void check_readonly (LexState *ls, expdesc *e) { 263static void check_readonly (LexState *ls, expdesc *e) {
263 FuncState *fs = ls->fs; 264 FuncState *fs = ls->fs;
264 Vardesc *vardesc = getvardesc(&fs, e); 265 Vardesc *vardesc = luaY_getvardesc(&fs, e);
265 if (vardesc && vardesc->ro) { /* is variable local and const? */ 266 if (vardesc && vardesc->ro) { /* is variable local and const? */
266 const char *msg = luaO_pushfstring(ls->L, 267 const char *msg = luaO_pushfstring(ls->L,
267 "attempt to assign to const variable '%s'", 268 "attempt to assign to const variable '%s'",
@@ -1678,20 +1679,13 @@ static void commonlocalstat (LexState *ls) {
1678static void tocloselocalstat (LexState *ls, Vardesc *var) { 1679static void tocloselocalstat (LexState *ls, Vardesc *var) {
1679 FuncState *fs = ls->fs; 1680 FuncState *fs = ls->fs;
1680 var->ro = 1; /* to-be-closed variables are always read-only */ 1681 var->ro = 1; /* to-be-closed variables are always read-only */
1681 markupval(fs, fs->nactvar); 1682 markupval(fs, fs->nactvar + 1);
1682 fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ 1683 fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */
1683 luaK_codeABC(fs, OP_TBC, fs->nactvar - 1, 0, 0); 1684 luaK_codeABC(fs, OP_TBC, fs->nactvar, 0, 0);
1684} 1685}
1685 1686
1686 1687
1687static void attriblocalstat (LexState *ls) { 1688static void checkattrib (LexState *ls, TString *attr, Vardesc *var) {
1688 Vardesc *var;
1689 TString *attr = str_checkname(ls);
1690 testnext(ls, '>');
1691 var = new_localvar(ls, str_checkname(ls));
1692 checknext(ls, '=');
1693 exp1(ls);
1694 adjustlocalvars(ls, 1);
1695 if (strcmp(getstr(attr), "const") == 0) 1689 if (strcmp(getstr(attr), "const") == 0)
1696 var->ro = 1; /* set variable as read-only */ 1690 var->ro = 1; /* set variable as read-only */
1697 else if (strcmp(getstr(attr), "toclose") == 0) 1691 else if (strcmp(getstr(attr), "toclose") == 0)
@@ -1702,6 +1696,22 @@ static void attriblocalstat (LexState *ls) {
1702} 1696}
1703 1697
1704 1698
1699static void attriblocalstat (LexState *ls) {
1700 FuncState *fs = ls->fs;
1701 Vardesc *var;
1702 expdesc e;
1703 TString *attr = str_checkname(ls);
1704 testnext(ls, '>');
1705 var = new_localvar(ls, str_checkname(ls));
1706 checknext(ls, '=');
1707 expr(ls, &e);
1708 checkattrib(ls, attr, var);
1709 luaK_tonumeral(fs, &e, &var->val);
1710 luaK_exp2nextreg(fs, &e);
1711 adjustlocalvars(ls, 1);
1712}
1713
1714
1705static void localstat (LexState *ls) { 1715static void localstat (LexState *ls) {
1706 /* stat -> LOCAL NAME {',' NAME} ['=' explist] 1716 /* stat -> LOCAL NAME {',' NAME} ['=' explist]
1707 | LOCAL *toclose NAME '=' exp */ 1717 | LOCAL *toclose NAME '=' exp */