About ifstat

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

About ifstat

CHU Run-min
Hi everyone,
Would you give me some more detailed information about the following function?
Especially, I can't finger out the meaning of luaK_concat and luaK_patchtohere.

static void ifstat (LexState *ls, int line) {
 /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
 FuncState *fs = ls->fs;
 int flist;
 int escapelist = NO_JUMP;
 flist = test_then_block(ls);  /* IF cond THEN block */
 while (ls->t.token == TK_ELSEIF) {
   luaK_concat(fs, &escapelist, luaK_jump(fs));
   luaK_patchtohere(fs, flist);
   flist = test_then_block(ls);  /* ELSEIF cond THEN block */
 }
 if (ls->t.token == TK_ELSE) {
   luaK_concat(fs, &escapelist, luaK_jump(fs));
   luaK_patchtohere(fs, flist);
   luaX_next(ls);  /* skip ELSE (after patch, for correct line info) */
   block(ls);  /* `else' part */
 }
 else
   luaK_concat(fs, &escapelist, flist);
 luaK_patchtohere(fs, escapelist);
 check_match(ls, TK_END, TK_IF, line);
}
Reply | Threaded
Open this post in threaded view
|

Re: About ifstat

Roberto Ierusalimschy
> Hi everyone,
> Would you give me some more detailed information about the following
> function?
> Especially, I can't finger out the meaning of luaK_concat and
> luaK_patchtohere.

luaK_concat concatenates two lists of pending jumps. So, the call

>   luaK_concat(fs, &escapelist, luaK_jump(fs));

will create a new jump instruction (luaK_jump) and add it to the
'escapelist'.  The luaK_patchtohere function patches all jump
instructions in the given list to jump to the current instruction
("here").  So, the call

> luaK_patchtohere(fs, escapelist);

in the end of the ifstat will make all jumps in the 'escapelist' to
jump to the end of the statement.

-- Roberto