aboutsummaryrefslogtreecommitdiff
path: root/lparser.c
diff options
context:
space:
mode:
Diffstat (limited to 'lparser.c')
-rw-r--r--lparser.c154
1 files changed, 84 insertions, 70 deletions
diff --git a/lparser.c b/lparser.c
index c875f784..2e9912a3 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 1.67 2000/03/09 13:57:37 roberto Exp roberto $ 2** $Id: lparser.c,v 1.68 2000/03/10 18:37:44 roberto Exp roberto $
3** LL(1) Parser and code generator for Lua 3** LL(1) Parser and code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -115,9 +115,10 @@ static void checklimit (LexState *ls, int val, int limit, const char *msg) {
115 115
116 116
117static void check_debugline (LexState *ls) { 117static void check_debugline (LexState *ls) {
118 if (ls->L->debug && ls->linenumber != ls->fs->lastsetline) { 118 FuncState *fs = ls->fs;
119 luaK_U(ls, OP_SETLINE, ls->linenumber, 0); 119 if (ls->L->debug && ls->linenumber != fs->lastsetline) {
120 ls->fs->lastsetline = ls->linenumber; 120 luaK_U(fs, OP_SETLINE, ls->linenumber, 0);
121 fs->lastsetline = ls->linenumber;
121 } 122 }
122} 123}
123 124
@@ -130,11 +131,11 @@ static void check_match (LexState *ls, int what, int who, int where) {
130} 131}
131 132
132 133
133static int string_constant (LexState *ls, FuncState *fs, TString *s) { 134static int string_constant (FuncState *fs, TString *s) {
134 Proto *f = fs->f; 135 Proto *f = fs->f;
135 int c = s->constindex; 136 int c = s->constindex;
136 if (c >= f->nkstr || f->kstr[c] != s) { 137 if (c >= f->nkstr || f->kstr[c] != s) {
137 luaM_growvector(ls->L, f->kstr, f->nkstr, 1, TString *, 138 luaM_growvector(fs->L, f->kstr, f->nkstr, 1, TString *,
138 constantEM, MAXARG_U); 139 constantEM, MAXARG_U);
139 c = f->nkstr++; 140 c = f->nkstr++;
140 f->kstr[c] = s; 141 f->kstr[c] = s;
@@ -145,7 +146,7 @@ static int string_constant (LexState *ls, FuncState *fs, TString *s) {
145 146
146 147
147static void code_string (LexState *ls, TString *s) { 148static void code_string (LexState *ls, TString *s) {
148 luaK_kstr(ls, string_constant(ls, ls->fs, s)); 149 luaK_kstr(ls, string_constant(ls->fs, s));
149} 150}
150 151
151 152
@@ -153,7 +154,7 @@ static int checkname (LexState *ls) {
153 int sc; 154 int sc;
154 if (ls->token != TK_NAME) 155 if (ls->token != TK_NAME)
155 luaK_error(ls, "<name> expected"); 156 luaK_error(ls, "<name> expected");
156 sc = string_constant(ls, ls->fs, ls->seminfo.ts); 157 sc = string_constant(ls->fs, ls->seminfo.ts);
157 next(ls); 158 next(ls);
158 return sc; 159 return sc;
159} 160}
@@ -226,7 +227,7 @@ static void singlevar (LexState *ls, TString *n, expdesc *var, int prev) {
226 if (aux_localname(level, n) >= 0) 227 if (aux_localname(level, n) >= 0)
227 luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str); 228 luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str);
228 var->k = VGLOBAL; 229 var->k = VGLOBAL;
229 var->u.index = string_constant(ls, fs, n); 230 var->u.index = string_constant(fs, n);
230 } 231 }
231} 232}
232 233
@@ -249,29 +250,31 @@ static int indexupvalue (LexState *ls, TString *n) {
249 250
250 251
251static void pushupvalue (LexState *ls, TString *n) { 252static void pushupvalue (LexState *ls, TString *n) {
252 if (ls->fs->prev == NULL) 253 FuncState *fs = ls->fs;
254 if (fs->prev == NULL)
253 luaX_syntaxerror(ls, "cannot access upvalue in main", n->str); 255 luaX_syntaxerror(ls, "cannot access upvalue in main", n->str);
254 if (aux_localname(ls->fs, n) >= 0) 256 if (aux_localname(ls->fs, n) >= 0)
255 luaX_syntaxerror(ls, "cannot access an upvalue in current scope", n->str); 257 luaX_syntaxerror(ls, "cannot access an upvalue in current scope", n->str);
256 luaK_U(ls, OP_PUSHUPVALUE, indexupvalue(ls, n), 1); 258 luaK_U(fs, OP_PUSHUPVALUE, indexupvalue(ls, n), 1);
257} 259}
258 260
259 261
260static void adjust_mult_assign (LexState *ls, int nvars, int nexps) { 262static void adjust_mult_assign (LexState *ls, int nvars, int nexps) {
263 FuncState *fs = ls->fs;
261 int diff = nexps - nvars; 264 int diff = nexps - nvars;
262 if (nexps == 0 || !luaK_lastisopen(ls)) { /* list is empty or closed */ 265 if (nexps == 0 || !luaK_lastisopen(fs)) { /* list is empty or closed */
263 /* push or pop eventual difference between list lengths */ 266 /* push or pop eventual difference between list lengths */
264 luaK_adjuststack(ls, diff); 267 luaK_adjuststack(fs, diff);
265 } 268 }
266 else { /* list ends in a function call; must correct it */ 269 else { /* list ends in a function call; must correct it */
267 diff--; /* do not count function call itself */ 270 diff--; /* do not count function call itself */
268 if (diff <= 0) { /* more variables than values? */ 271 if (diff <= 0) { /* more variables than values? */
269 /* function call must provide extra values */ 272 /* function call must provide extra values */
270 luaK_setcallreturns(ls, -diff); 273 luaK_setcallreturns(fs, -diff);
271 } 274 }
272 else { /* more values than variables */ 275 else { /* more values than variables */
273 luaK_setcallreturns(ls, 0); /* call should provide no value */ 276 luaK_setcallreturns(fs, 0); /* call should provide no value */
274 luaK_adjuststack(ls, diff); /* pop eventual extra values */ 277 luaK_adjuststack(fs, diff); /* pop eventual extra values */
275 } 278 }
276 } 279 }
277} 280}
@@ -285,9 +288,9 @@ static void code_args (LexState *ls, int nparams, int dots) {
285 fs->f->numparams = nparams; 288 fs->f->numparams = nparams;
286 fs->f->is_vararg = dots; 289 fs->f->is_vararg = dots;
287 if (!dots) 290 if (!dots)
288 luaK_deltastack(ls, nparams); 291 luaK_deltastack(fs, nparams);
289 else { 292 else {
290 luaK_deltastack(ls, nparams+1); 293 luaK_deltastack(fs, nparams+1);
291 add_localvar(ls, luaS_newfixed(ls->L, "arg")); 294 add_localvar(ls, luaS_newfixed(ls->L, "arg"));
292 } 295 }
293} 296}
@@ -298,7 +301,7 @@ static int getvarname (LexState *ls, expdesc *var) {
298 case VGLOBAL: 301 case VGLOBAL:
299 return var->u.index; 302 return var->u.index;
300 case VLOCAL: 303 case VLOCAL:
301 return string_constant(ls, ls->fs, ls->fs->localvar[var->u.index]); 304 return string_constant(ls->fs, ls->fs->localvar[var->u.index]);
302 break; 305 break;
303 default: 306 default:
304 error_unexpected(ls); /* there is no `var name' */ 307 error_unexpected(ls); /* there is no `var name' */
@@ -308,15 +311,16 @@ static int getvarname (LexState *ls, expdesc *var) {
308 311
309 312
310static void func_onstack (LexState *ls, FuncState *func) { 313static void func_onstack (LexState *ls, FuncState *func) {
311 Proto *f = ls->fs->f; 314 FuncState *fs = ls->fs;
315 Proto *f = fs->f;
312 int i; 316 int i;
313 for (i=0; i<func->nupvalues; i++) 317 for (i=0; i<func->nupvalues; i++)
314 luaK_tostack(ls, &func->upvalues[i], 1); 318 luaK_tostack(ls, &func->upvalues[i], 1);
315 luaM_growvector(ls->L, f->kproto, f->nkproto, 1, Proto *, 319 luaM_growvector(ls->L, f->kproto, f->nkproto, 1, Proto *,
316 constantEM, MAXARG_A); 320 constantEM, MAXARG_A);
317 f->kproto[f->nkproto++] = func->f; 321 f->kproto[f->nkproto++] = func->f;
318 luaK_deltastack(ls, 1); /* CLOSURE puts one extra element before popping */ 322 luaK_deltastack(fs, 1); /* CLOSURE puts one extra element before popping */
319 luaK_AB(ls, OP_CLOSURE, f->nkproto-1, func->nupvalues, -func->nupvalues); 323 luaK_AB(fs, OP_CLOSURE, f->nkproto-1, func->nupvalues, -func->nupvalues);
320} 324}
321 325
322 326
@@ -324,6 +328,8 @@ static void init_state (LexState *ls, FuncState *fs, TString *source) {
324 lua_State *L = ls->L; 328 lua_State *L = ls->L;
325 Proto *f = luaF_newproto(ls->L); 329 Proto *f = luaF_newproto(ls->L);
326 fs->prev = ls->fs; /* linked list of funcstates */ 330 fs->prev = ls->fs; /* linked list of funcstates */
331 fs->ls = ls;
332 fs->L = ls->L;
327 ls->fs = fs; 333 ls->fs = fs;
328 fs->stacksize = 0; 334 fs->stacksize = 0;
329 fs->nlocalvar = 0; 335 fs->nlocalvar = 0;
@@ -346,19 +352,20 @@ static void init_state (LexState *ls, FuncState *fs, TString *source) {
346 352
347 353
348static void close_func (LexState *ls) { 354static void close_func (LexState *ls) {
355 lua_State *L = ls->L;
349 FuncState *fs = ls->fs; 356 FuncState *fs = ls->fs;
350 Proto *f = fs->f; 357 Proto *f = fs->f;
351 luaK_0(ls, OP_END, 0); 358 luaK_0(fs, OP_END, 0);
352 luaM_reallocvector(ls->L, f->code, fs->pc, Instruction); 359 luaM_reallocvector(L, f->code, fs->pc, Instruction);
353 luaM_reallocvector(ls->L, f->kstr, f->nkstr, TString *); 360 luaM_reallocvector(L, f->kstr, f->nkstr, TString *);
354 luaM_reallocvector(ls->L, f->knum, f->nknum, Number); 361 luaM_reallocvector(L, f->knum, f->nknum, Number);
355 luaM_reallocvector(ls->L, f->kproto, f->nkproto, Proto *); 362 luaM_reallocvector(L, f->kproto, f->nkproto, Proto *);
356 if (fs->nvars != -1) { /* debug information? */ 363 if (fs->nvars != -1) { /* debug information? */
357 luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */ 364 luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */
358 luaM_reallocvector(ls->L, f->locvars, fs->nvars, LocVar); 365 luaM_reallocvector(L, f->locvars, fs->nvars, LocVar);
359 } 366 }
360 ls->fs = fs->prev; 367 ls->fs = fs->prev;
361 ls->L->top--; /* pop function */ 368 L->top--; /* pop function */
362} 369}
363 370
364 371
@@ -423,7 +430,7 @@ static void funcargs (LexState *ls, int slf) {
423 check_match(ls, ')', '(', line); 430 check_match(ls, ')', '(', line);
424#ifdef LUA_COMPAT_ARGRET 431#ifdef LUA_COMPAT_ARGRET
425 if (nargs > 0) /* arg list is not empty? */ 432 if (nargs > 0) /* arg list is not empty? */
426 luaK_setcallreturns(ls, 1); /* last call returns only 1 value */ 433 luaK_setcallreturns(fs, 1); /* last call returns only 1 value */
427#endif 434#endif
428 break; 435 break;
429 } 436 }
@@ -442,7 +449,7 @@ static void funcargs (LexState *ls, int slf) {
442 break; 449 break;
443 } 450 }
444 fs->stacksize = slevel; /* call will remove function and arguments */ 451 fs->stacksize = slevel; /* call will remove function and arguments */
445 luaK_AB(ls, OP_CALL, slevel, MULT_RET, 0); 452 luaK_AB(fs, OP_CALL, slevel, MULT_RET, 0);
446} 453}
447 454
448 455
@@ -469,10 +476,10 @@ static void var_or_func_tail (LexState *ls, expdesc *v) {
469 next(ls); 476 next(ls);
470 name = checkname(ls); 477 name = checkname(ls);
471 luaK_tostack(ls, v, 1); /* `v' must be on stack */ 478 luaK_tostack(ls, v, 1); /* `v' must be on stack */
472 luaK_U(ls, OP_PUSHSELF, name, 1); 479 luaK_U(ls->fs, OP_PUSHSELF, name, 1);
473 funcargs(ls, 1); 480 funcargs(ls, 1);
474 v->k = VEXP; 481 v->k = VEXP;
475 v->u.l.t = v->u.l.f = 0; 482 v->u.l.t = v->u.l.f = NO_JUMP;
476 break; 483 break;
477 } 484 }
478 485
@@ -480,7 +487,7 @@ static void var_or_func_tail (LexState *ls, expdesc *v) {
480 luaK_tostack(ls, v, 1); /* `v' must be on stack */ 487 luaK_tostack(ls, v, 1); /* `v' must be on stack */
481 funcargs(ls, 0); 488 funcargs(ls, 0);
482 v->k = VEXP; 489 v->k = VEXP;
483 v->u.l.t = v->u.l.f = 0; 490 v->u.l.t = v->u.l.f = NO_JUMP;
484 break; 491 break;
485 492
486 default: return; /* should be follow... */ 493 default: return; /* should be follow... */
@@ -494,7 +501,7 @@ static void var_or_func (LexState *ls, expdesc *v) {
494 if (optional(ls, '%')) { /* upvalue? */ 501 if (optional(ls, '%')) { /* upvalue? */
495 pushupvalue(ls, str_checkname(ls)); 502 pushupvalue(ls, str_checkname(ls));
496 v->k = VEXP; 503 v->k = VEXP;
497 v->u.l.t = v->u.l.f = 0; 504 v->u.l.t = v->u.l.f = NO_JUMP;
498 } 505 }
499 else /* variable name */ 506 else /* variable name */
500 singlevar(ls, str_checkname(ls), v, 0); 507 singlevar(ls, str_checkname(ls), v, 0);
@@ -532,6 +539,7 @@ static void recfield (LexState *ls) {
532 539
533static int recfields (LexState *ls) { 540static int recfields (LexState *ls) {
534 /* recfields -> { ',' recfield } [','] */ 541 /* recfields -> { ',' recfield } [','] */
542 FuncState *fs = ls->fs;
535 int n = 1; /* one has been read before */ 543 int n = 1; /* one has been read before */
536 int mod_n = 1; /* mod_n == n%RFIELDS_PER_FLUSH */ 544 int mod_n = 1; /* mod_n == n%RFIELDS_PER_FLUSH */
537 while (ls->token == ',') { 545 while (ls->token == ',') {
@@ -541,18 +549,19 @@ static int recfields (LexState *ls) {
541 recfield(ls); 549 recfield(ls);
542 n++; 550 n++;
543 if (++mod_n == RFIELDS_PER_FLUSH) { 551 if (++mod_n == RFIELDS_PER_FLUSH) {
544 luaK_U(ls, OP_SETMAP, RFIELDS_PER_FLUSH-1, -2*RFIELDS_PER_FLUSH); 552 luaK_U(fs, OP_SETMAP, RFIELDS_PER_FLUSH-1, -2*RFIELDS_PER_FLUSH);
545 mod_n = 0; 553 mod_n = 0;
546 } 554 }
547 } 555 }
548 if (mod_n) 556 if (mod_n)
549 luaK_U(ls, OP_SETMAP, mod_n-1, -2*mod_n); 557 luaK_U(fs, OP_SETMAP, mod_n-1, -2*mod_n);
550 return n; 558 return n;
551} 559}
552 560
553 561
554static int listfields (LexState *ls) { 562static int listfields (LexState *ls) {
555 /* listfields -> { ',' exp1 } [','] */ 563 /* listfields -> { ',' exp1 } [','] */
564 FuncState *fs = ls->fs;
556 int n = 1; /* one has been read before */ 565 int n = 1; /* one has been read before */
557 int mod_n = 1; /* mod_n == n%LFIELDS_PER_FLUSH */ 566 int mod_n = 1; /* mod_n == n%LFIELDS_PER_FLUSH */
558 while (ls->token == ',') { 567 while (ls->token == ',') {
@@ -564,13 +573,13 @@ static int listfields (LexState *ls) {
564 checklimit(ls, n, MAXARG_A*LFIELDS_PER_FLUSH, 573 checklimit(ls, n, MAXARG_A*LFIELDS_PER_FLUSH,
565 "items in a list initializer"); 574 "items in a list initializer");
566 if (++mod_n == LFIELDS_PER_FLUSH) { 575 if (++mod_n == LFIELDS_PER_FLUSH) {
567 luaK_AB(ls, OP_SETLIST, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH-1, 576 luaK_AB(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH-1,
568 -LFIELDS_PER_FLUSH); 577 -LFIELDS_PER_FLUSH);
569 mod_n = 0; 578 mod_n = 0;
570 } 579 }
571 } 580 }
572 if (mod_n > 0) 581 if (mod_n > 0)
573 luaK_AB(ls, OP_SETLIST, n/LFIELDS_PER_FLUSH, mod_n-1, -mod_n); 582 luaK_AB(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH, mod_n-1, -mod_n);
574 return n; 583 return n;
575} 584}
576 585
@@ -618,8 +627,9 @@ static void constructor_part (LexState *ls, constdesc *cd) {
618 627
619static void constructor (LexState *ls) { 628static void constructor (LexState *ls) {
620 /* constructor -> '{' constructor_part [';' constructor_part] '}' */ 629 /* constructor -> '{' constructor_part [';' constructor_part] '}' */
630 FuncState *fs = ls->fs;
621 int line = ls->linenumber; 631 int line = ls->linenumber;
622 int pc = luaK_U(ls, OP_CREATETABLE, 0, 1); 632 int pc = luaK_U(fs, OP_CREATETABLE, 0, 1);
623 int nelems; 633 int nelems;
624 constdesc cd; 634 constdesc cd;
625 check(ls, '{'); 635 check(ls, '{');
@@ -635,7 +645,7 @@ static void constructor (LexState *ls) {
635 } 645 }
636 check_match(ls, '}', '{', line); 646 check_match(ls, '}', '{', line);
637 /* set initial table size */ 647 /* set initial table size */
638 SETARG_U(ls->fs->f->code[pc], nelems); 648 SETARG_U(fs->f->code[pc], nelems);
639} 649}
640 650
641/* }====================================================================== */ 651/* }====================================================================== */
@@ -651,12 +661,13 @@ static void constructor (LexState *ls) {
651 661
652 662
653static void simpleexp (LexState *ls, expdesc *v) { 663static void simpleexp (LexState *ls, expdesc *v) {
664 FuncState *fs = ls->fs;
654 check_debugline(ls); 665 check_debugline(ls);
655 switch (ls->token) { 666 switch (ls->token) {
656 case TK_NUMBER: { /* simpleexp -> NUMBER */ 667 case TK_NUMBER: { /* simpleexp -> NUMBER */
657 Number r = ls->seminfo.r; 668 Number r = ls->seminfo.r;
658 next(ls); 669 next(ls);
659 luaK_number(ls, r); 670 luaK_number(fs, r);
660 break; 671 break;
661 } 672 }
662 673
@@ -666,7 +677,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
666 break; 677 break;
667 678
668 case TK_NIL: /* simpleexp -> NIL */ 679 case TK_NIL: /* simpleexp -> NIL */
669 luaK_adjuststack(ls, -1); 680 luaK_adjuststack(fs, -1);
670 next(ls); 681 next(ls);
671 break; 682 break;
672 683
@@ -694,7 +705,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
694 return; 705 return;
695 } 706 }
696 v->k = VEXP; 707 v->k = VEXP;
697 v->u.l.t = v->u.l.f = 0; 708 v->u.l.t = v->u.l.f = NO_JUMP;
698} 709}
699 710
700 711
@@ -776,7 +787,7 @@ static void block (LexState *ls) {
776 FuncState *fs = ls->fs; 787 FuncState *fs = ls->fs;
777 int nlocalvar = fs->nlocalvar; 788 int nlocalvar = fs->nlocalvar;
778 chunk(ls); 789 chunk(ls);
779 luaK_adjuststack(ls, fs->nlocalvar - nlocalvar); /* remove local variables */ 790 luaK_adjuststack(fs, fs->nlocalvar - nlocalvar); /* remove local variables */
780 for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--) 791 for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--)
781 luaI_unregisterlocalvar(ls, fs->lastsetline); 792 luaI_unregisterlocalvar(ls, fs->lastsetline);
782} 793}
@@ -806,7 +817,7 @@ static int assignment (LexState *ls, expdesc *v, int nvars) {
806 luaK_storevar(ls, v); 817 luaK_storevar(ls, v);
807 } 818 }
808 else { /* indexed var with values in between*/ 819 else { /* indexed var with values in between*/
809 luaK_U(ls, OP_SETTABLE, left+(nvars-1), -1); 820 luaK_U(ls->fs, OP_SETTABLE, left+(nvars-1), -1);
810 left += 2; /* table&index are not popped, because they aren't on top */ 821 left += 2; /* table&index are not popped, because they aren't on top */
811 } 822 }
812 return left; 823 return left;
@@ -822,7 +833,7 @@ static void whilestat (LexState *ls, int line) {
822 /* whilestat -> WHILE exp1 DO block END */ 833 /* whilestat -> WHILE exp1 DO block END */
823 Instruction buffer[MAX_WHILE_EXP]; 834 Instruction buffer[MAX_WHILE_EXP];
824 FuncState *fs = ls->fs; 835 FuncState *fs = ls->fs;
825 int while_init = luaK_getlabel(ls); 836 int while_init = luaK_getlabel(fs);
826 int loopentry; /* point to jump to repeat the loop */ 837 int loopentry; /* point to jump to repeat the loop */
827 int cond_init; /* init of condition, after the move */ 838 int cond_init; /* init of condition, after the move */
828 int cond_size; 839 int cond_size;
@@ -830,7 +841,7 @@ static void whilestat (LexState *ls, int line) {
830 int i; 841 int i;
831 next(ls); /* skip WHILE */ 842 next(ls); /* skip WHILE */
832 expr(ls, &v); /* read condition */ 843 expr(ls, &v); /* read condition */
833 luaK_goiffalse(ls, &v, 0); 844 luaK_goiffalse(fs, &v, 0);
834 cond_size = fs->pc - while_init; 845 cond_size = fs->pc - while_init;
835 /* save condition (to move it to after body) */ 846 /* save condition (to move it to after body) */
836 if (cond_size > MAX_WHILE_EXP) 847 if (cond_size > MAX_WHILE_EXP)
@@ -838,31 +849,32 @@ static void whilestat (LexState *ls, int line) {
838 for (i=0; i<cond_size; i++) buffer[i] = fs->f->code[while_init+i]; 849 for (i=0; i<cond_size; i++) buffer[i] = fs->f->code[while_init+i];
839 /* go back to state prior condition */ 850 /* go back to state prior condition */
840 fs->pc = while_init; 851 fs->pc = while_init;
841 luaK_S(ls, OP_JMP, 0, 0); /* initial jump to condition */ 852 luaK_S(fs, OP_JMP, 0, 0); /* initial jump to condition */
842 check(ls, TK_DO); 853 check(ls, TK_DO);
843 loopentry = luaK_getlabel(ls); 854 loopentry = luaK_getlabel(fs);
844 block(ls); 855 block(ls);
845 check_match(ls, TK_END, TK_WHILE, line); 856 check_match(ls, TK_END, TK_WHILE, line);
846 cond_init = luaK_getlabel(ls); 857 cond_init = luaK_getlabel(fs);
847 luaK_fixjump(ls, while_init, cond_init); 858 luaK_fixjump(fs, while_init, cond_init);
848 /* correct `v' and copy condition to new position */ 859 /* correct `v' and copy condition to new position */
849 if (v.u.l.t != 0) v.u.l.t += cond_init-while_init; 860 if (v.u.l.t != NO_JUMP) v.u.l.t += cond_init-while_init;
850 for (i=0; i<cond_size; i++) luaK_primitivecode(ls, buffer[i]); 861 for (i=0; i<cond_size; i++) luaK_primitivecode(fs, buffer[i]);
851 luaK_patchlist(ls, v.u.l.t, loopentry); 862 luaK_patchlist(fs, v.u.l.t, loopentry);
852 luaK_getlabel(ls); /* mark possible jump to this point */ 863 luaK_getlabel(fs); /* mark possible jump to this point */
853} 864}
854 865
855 866
856static void repeatstat (LexState *ls, int line) { 867static void repeatstat (LexState *ls, int line) {
857 /* repeatstat -> REPEAT block UNTIL exp1 */ 868 /* repeatstat -> REPEAT block UNTIL exp1 */
858 int repeat_init = luaK_getlabel(ls); 869 FuncState *fs = ls->fs;
870 int repeat_init = luaK_getlabel(fs);
859 expdesc v; 871 expdesc v;
860 next(ls); 872 next(ls);
861 block(ls); 873 block(ls);
862 check_match(ls, TK_UNTIL, TK_REPEAT, line); 874 check_match(ls, TK_UNTIL, TK_REPEAT, line);
863 expr(ls, &v); 875 expr(ls, &v);
864 luaK_goiftrue(ls, &v, 0); 876 luaK_goiftrue(fs, &v, 0);
865 luaK_patchlist(ls, v.u.l.f, repeat_init); 877 luaK_patchlist(fs, v.u.l.f, repeat_init);
866} 878}
867 879
868 880
@@ -935,17 +947,18 @@ static int funcstat (LexState *ls, int line) {
935 947
936static void namestat (LexState *ls) { 948static void namestat (LexState *ls) {
937 /* stat -> func | ['%'] NAME assignment */ 949 /* stat -> func | ['%'] NAME assignment */
950 FuncState *fs = ls->fs;
938 expdesc v; 951 expdesc v;
939 check_debugline(ls); 952 check_debugline(ls);
940 var_or_func(ls, &v); 953 var_or_func(ls, &v);
941 if (v.k == VEXP) { /* stat -> func */ 954 if (v.k == VEXP) { /* stat -> func */
942 if (!luaK_lastisopen(ls)) /* is just an upvalue? */ 955 if (!luaK_lastisopen(fs)) /* is just an upvalue? */
943 luaK_error(ls, "syntax error"); 956 luaK_error(ls, "syntax error");
944 luaK_setcallreturns(ls, 0); /* call statement uses no results */ 957 luaK_setcallreturns(fs, 0); /* call statement uses no results */
945 } 958 }
946 else { /* stat -> ['%'] NAME assignment */ 959 else { /* stat -> ['%'] NAME assignment */
947 int left = assignment(ls, &v, 1); 960 int left = assignment(ls, &v, 1);
948 luaK_adjuststack(ls, left); /* remove eventual garbage left on stack */ 961 luaK_adjuststack(fs, left); /* remove eventual garbage left on stack */
949 } 962 }
950} 963}
951 964
@@ -957,11 +970,11 @@ static void ifpart (LexState *ls, int line) {
957 int elseinit; 970 int elseinit;
958 next(ls); /* skip IF or ELSEIF */ 971 next(ls); /* skip IF or ELSEIF */
959 expr(ls, &v); /* cond */ 972 expr(ls, &v); /* cond */
960 luaK_goiftrue(ls, &v, 0); 973 luaK_goiftrue(fs, &v, 0);
961 check(ls, TK_THEN); 974 check(ls, TK_THEN);
962 block(ls); /* `then' part */ 975 block(ls); /* `then' part */
963 luaK_S(ls, OP_JMP, 0, 0); /* 2nd jump: over `else' part */ 976 luaK_S(fs, OP_JMP, 0, 0); /* 2nd jump: over `else' part */
964 elseinit = luaK_getlabel(ls); /* address of 2nd jump == elseinit-1 */ 977 elseinit = luaK_getlabel(fs); /* address of 2nd jump == elseinit-1 */
965 if (ls->token == TK_ELSEIF) 978 if (ls->token == TK_ELSEIF)
966 ifpart(ls, line); 979 ifpart(ls, line);
967 else { 980 else {
@@ -970,13 +983,13 @@ static void ifpart (LexState *ls, int line) {
970 check_match(ls, TK_END, TK_IF, line); 983 check_match(ls, TK_END, TK_IF, line);
971 } 984 }
972 if (fs->pc > elseinit) { /* is there an `else' part? */ 985 if (fs->pc > elseinit) { /* is there an `else' part? */
973 luaK_fixjump(ls, elseinit-1, luaK_getlabel(ls)); /* fix 2nd jump */ 986 luaK_fixjump(fs, elseinit-1, luaK_getlabel(fs)); /* fix 2nd jump */
974 } 987 }
975 else { /* no else part */ 988 else { /* no else part */
976 fs->pc--; /* remove 2nd jump */ 989 fs->pc--; /* remove 2nd jump */
977 elseinit = luaK_getlabel(ls); /* `elseinit' points to end */ 990 elseinit = luaK_getlabel(fs); /* `elseinit' points to end */
978 } 991 }
979 luaK_patchlist(ls, v.u.l.f, elseinit); /* fix 1st jump to `else' part */ 992 luaK_patchlist(fs, v.u.l.f, elseinit); /* fix 1st jump to `else' part */
980} 993}
981 994
982 995
@@ -1080,12 +1093,13 @@ static void body (LexState *ls, int needself, int line) {
1080static void ret (LexState *ls) { 1093static void ret (LexState *ls) {
1081 /* ret -> [RETURN explist sc] */ 1094 /* ret -> [RETURN explist sc] */
1082 if (ls->token == TK_RETURN) { 1095 if (ls->token == TK_RETURN) {
1096 FuncState *fs = ls->fs;
1083 int nexps; /* number of expressions returned */ 1097 int nexps; /* number of expressions returned */
1084 check_debugline(ls); 1098 check_debugline(ls);
1085 next(ls); 1099 next(ls);
1086 nexps = explist(ls); 1100 nexps = explist(ls);
1087 luaK_retcode(ls, ls->fs->nlocalvar, nexps); 1101 luaK_retcode(fs, ls->fs->nlocalvar, nexps);
1088 ls->fs->stacksize = ls->fs->nlocalvar; /* removes all temp values */ 1102 fs->stacksize = fs->nlocalvar; /* removes all temp values */
1089 optional(ls, ';'); 1103 optional(ls, ';');
1090 } 1104 }
1091} 1105}