[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Ternary operator patch
- From: JKB <bcs@...>
- Date: Sat, 8 Jun 2019 20:22:45 -0700 (MST)
Ryota,
With respect to your mods for if/then/else in an expression I appear to have
found a bug:
b = 1
local x, y = {aa=1, bb=2}, {aa=3.14, bb=2.718}
a = if b == 1 then x else y
print(a.aa)
prints 3.14 instead of 1. It produces the code:
16 [8] EQ 0 2 -2 ; - 1
17 [8] JMP 1 ; to 19
18 [8] JMP 0 ; to 19
19 [8] SETGLOBAL 1 -11 ; a
When I changed in ifexpr(...) two lines of luaK_exp2anyreg(...) to
luaK_exp2nextreg(...) it worked - as shown below:
static void ifexpr (LexState *ls, expdesc *v) {
/* ifexpr -> IF cond THEN expr ELSE expr */
FuncState *fs = ls->fs;
int condexit;
int escapelist = NO_JUMP;
luaX_next(ls); /* skip IF */
condexit = cond(ls); /* check cond */
checknext(ls, TK_THEN); /* skip THEN */
expr(ls, v); /* eval THEN part */
luaK_exp2nextreg(fs, v); /* set result to reg. */
//!!!!!!!!!!!!!!!!!!!!!
luaK_freeexp(fs, v); /* free reg. bound to v */
luaK_concat(fs, &escapelist, luaK_jump(fs)); /* JMP to last */
luaK_patchtohere(fs, condexit); /* label to JMP when cond is false */
checknext(ls, TK_ELSE); /* skip ELSE */
expr(ls, v); /* eval ELSE part */
luaK_exp2nextreg(fs, v); /* set result to reg. */
//!!!!!!!!!!!!!!!!!!!!!
luaK_patchtohere(fs, escapelist); /* label to JMP when cond is true */
}
Do you see any problems?
JK Berkeley
--
Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html