static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { expdesc e; check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, "syntax error"); if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */ struct LHS_assign nv; nv.prev = lh; primaryexp(ls, &nv.v); if (nv.v.k == VLOCAL) check_conflict(ls, lh, &nv.v); assignment(ls, &nv, nvars+1); } else { /* assignment -> `=' explist1 */ int nexps; check(ls, '='); nexps = explist1(ls, &e); if (nexps != nvars) { adjust_assign(ls, nvars, nexps, &e); if (nexps > nvars) ls->fs->freereg -= nexps - nvars; /* remove extra values */ } else { luaK_setcallreturns(ls->fs, &e, 1); /* close last expression */ luaK_storevar(ls->fs, &lh->v, &e); return; /* avoid default */ } } init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ luaK_storevar(ls->fs, &lh->v, &e); }
static void retstat (LexState *ls) { /* stat -> RETURN explist */ FuncState *fs = ls->fs; expdesc e; int first, nret; /* registers with returned values */ next(ls); /* skip RETURN */ if (block_follow(ls->t.token) || ls->t.token == ';') first = nret = 0; /* return no values */ else { nret = explist1(ls, &e); /* optional return values */ if (e.k == VCALL) { luaK_setcallreturns(fs, &e, LUA_MULTRET); if (nret == 1) { /* tail call? */ SET_OPCODE(getcode(fs,&e), OP_TAILCALL); lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar); } first = fs->nactvar; nret = LUA_MULTRET; /* return all values */ } else { if (nret == 1) /* only one single value? */ first = luaK_exp2anyreg(fs, &e); else { luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */ first = fs->nactvar; /* return all `active' values */ lua_assert(nret == fs->freereg - first); } } } luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); }
void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { FuncState *fs = ls->fs; if (!discharge(fs, v)) { /* `v' is an expression? */ OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]); if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) { /* expression has no jumps */ if (onlyone) luaK_setcallreturns(fs, 1); /* call must return 1 value */ } else { /* expression has jumps */ int final; /* position after whole expression */ int j = NO_JUMP; /* eventual jump over values */ int p_nil = NO_JUMP; /* position of an eventual PUSHNIL */ int p_1 = NO_JUMP; /* position of an eventual PUSHINT */ if (ISJUMP(previous) || need_value(fs, v->u.l.f, OP_JMPONF) || need_value(fs, v->u.l.t, OP_JMPONT)) { /* expression needs values */ if (ISJUMP(previous)) luaK_concat(fs, &v->u.l.t, fs->pc-1); /* put `previous' in t. list */ else { j = code_label(fs, OP_JMP, NO_JUMP); /* to jump over both pushes */ /* correct stack for compiler and symbolic execution */ luaK_adjuststack(fs, 1); } p_nil = code_label(fs, OP_PUSHNILJMP, 0); p_1 = code_label(fs, OP_PUSHINT, 1); luaK_patchlist(fs, j, luaK_getlabel(fs)); } final = luaK_getlabel(fs); luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_JMPONF, final); luaK_patchlistaux(fs, v->u.l.t, p_1, OP_JMPONT, final); v->u.l.f = v->u.l.t = NO_JUMP; }
static void lastlistfield (FuncState *fs, struct ConsControl *cc) { if (cc->tostore == 0) return; if (cc->v.k == VCALL) { luaK_setcallreturns(fs, &cc->v, LUA_MULTRET); luaK_codeABx(fs, OP_SETLISTO, cc->t->info, cc->na-1); } else { if (cc->v.k != VVOID) luaK_exp2nextreg(fs, &cc->v); luaK_codeABx(fs, OP_SETLIST, cc->t->info, cc->na-1); } fs->freereg = cc->t->info + 1; /* free registers */ }
static void exprstat (LexState *ls) { /* stat -> func | assignment */ FuncState *fs = ls->fs; struct LHS_assign v; primaryexp(ls, &v.v); if (v.v.k == VCALL) { /* stat -> func */ luaK_setcallreturns(fs, &v.v, 0); /* call statement uses no results */ } else { /* stat -> assignment */ v.prev = NULL; assignment(ls, &v, 1); } }
static void funcargs (LexState *ls, expdesc *f) { FuncState *fs = ls->fs; expdesc args; int base, nparams; int line = ls->linenumber; switch (ls->t.token) { case '(': { /* funcargs -> `(' [ explist1 ] `)' */ if (line != ls->lastline) luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); next(ls); if (ls->t.token == ')') /* arg list is empty? */ args.k = VVOID; else { explist1(ls, &args); luaK_setcallreturns(fs, &args, LUA_MULTRET); } check_match(ls, ')', '(', line); break; } case '{': { /* funcargs -> constructor */ constructor(ls, &args); break; } case TK_STRING: { /* funcargs -> STRING */ codestring(ls, &args, ls->t.seminfo.ts); next(ls); /* must use `seminfo' before `next' */ break; } default: { luaX_syntaxerror(ls, "function arguments expected"); return; } } lua_assert(f->k == VNONRELOC); base = f->info; /* base register for call */ if (args.k == VCALL) nparams = LUA_MULTRET; /* open call */ else { if (args.k != VVOID) luaK_exp2nextreg(fs, &args); /* close last argument */ nparams = fs->freereg - (base+1); } init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); luaK_fixline(fs, line); fs->freereg = base+1; /* call remove function and arguments and leaves (unless changed) one result */ }
static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { FuncState *fs = ls->fs; int extra = nvars - nexps; if (e->k == VCALL) { extra++; /* includes call itself */ if (extra <= 0) extra = 0; else luaK_reserveregs(fs, extra-1); luaK_setcallreturns(fs, e, extra); /* call provides the difference */ } else { if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ if (extra > 0) { int reg = fs->freereg; luaK_reserveregs(fs, extra); luaK_nil(fs, reg, extra); } } }
static void discharge1 (FuncState *fs, expdesc *var) { discharge(fs, var); /* if it has jumps then it is already discharged */ if (var->u.l.t == NO_JUMP && var->u.l.f == NO_JUMP) luaK_setcallreturns(fs, 1); /* call must return 1 value */ }