diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-05-17 11:11:44 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-05-17 11:11:44 -0300 |
commit | d9f40e3f6fb61650240c47d548bee69b24b07859 (patch) | |
tree | ab01022b3e3bc6bdb800423c97095a9423e0a798 /lcode.c | |
parent | 347d6961ac14213264c7176e3d125c9ba8475b01 (diff) | |
download | lua-github-d9f40e3f6fb61650240c47d548bee69b24b07859.tar.gz |
First implementation for 'const' variables
A variable can be declared const, which means it cannot be assigned to,
with the syntax 'local <const> name = exp'.
Diffstat (limited to 'lcode.c')
-rw-r--r-- | lcode.c | 36 |
1 files changed, 21 insertions, 15 deletions
@@ -678,11 +678,12 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { case VLOCAL: { /* already in a register */ + e->u.info = e->u.var.idx; e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } case VUPVAL: { /* move value to some (pending) register */ - e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); + e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.var.idx, 0); e->k = VRELOC; break; } @@ -938,12 +939,12 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { switch (var->k) { case VLOCAL: { freeexp(fs, ex); - exp2reg(fs, ex, var->u.info); /* compute 'ex' into proper place */ + exp2reg(fs, ex, var->u.var.idx); /* compute 'ex' into proper place */ return; } case VUPVAL: { int e = luaK_exp2anyreg(fs, ex); - luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0); + luaK_codeABC(fs, OP_SETUPVAL, e, var->u.var.idx, 0); break; } case VINDEXUP: { @@ -1165,25 +1166,30 @@ static int isSCnumber (expdesc *e, lua_Integer *i, int *isfloat) { ** values in registers. */ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { - lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL)); + lua_assert(!hasjumps(t) && + (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL)); if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non string? */ luaK_exp2anyreg(fs, t); /* put it in a register */ - t->u.ind.t = t->u.info; /* register or upvalue index */ if (t->k == VUPVAL) { + t->u.ind.t = t->u.var.idx; /* upvalue index */ t->u.ind.idx = k->u.info; /* literal string */ t->k = VINDEXUP; } - else if (isKstr(fs, k)) { - t->u.ind.idx = k->u.info; /* literal string */ - t->k = VINDEXSTR; - } - else if (isCint(k)) { - t->u.ind.idx = cast_int(k->u.ival); /* integer constant in proper range */ - t->k = VINDEXI; - } else { - t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */ - t->k = VINDEXED; + /* register index of the table */ + t->u.ind.t = (t->k == VLOCAL) ? t->u.var.idx: t->u.info; + if (isKstr(fs, k)) { + t->u.ind.idx = k->u.info; /* literal string */ + t->k = VINDEXSTR; + } + else if (isCint(k)) { + t->u.ind.idx = cast_int(k->u.ival); /* int. constant in proper range */ + t->k = VINDEXI; + } + else { + t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */ + t->k = VINDEXED; + } } } |