aboutsummaryrefslogtreecommitdiff
path: root/lparser.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-11-22 11:12:07 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-11-22 11:12:07 -0200
commit29ede6aa13144ff7b69c57a87be1ee93f57ae896 (patch)
treeadcfb5dcff7db55481cd675349e23dec0e63c939 /lparser.c
parent951897c09319ae5474a4b86bb7d615136577caa0 (diff)
downloadlua-29ede6aa13144ff7b69c57a87be1ee93f57ae896.tar.gz
lua-29ede6aa13144ff7b69c57a87be1ee93f57ae896.tar.bz2
lua-29ede6aa13144ff7b69c57a87be1ee93f57ae896.zip
first implementation of multiple states (reentrant code).
Diffstat (limited to 'lparser.c')
-rw-r--r--lparser.c117
1 files changed, 61 insertions, 56 deletions
diff --git a/lparser.c b/lparser.c
index 740ab03a..c96ea0d0 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 1.41 1999/09/20 14:15:18 roberto Exp roberto $ 2** $Id: lparser.c,v 1.42 1999/11/04 17:23:12 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*/
@@ -147,14 +147,15 @@ static void checklimit (LexState *ls, int val, int limit, const char *msg) {
147} 147}
148 148
149 149
150static void check_pc (FuncState *fs, int n) { 150static void check_pc (LexState *ls, int n) {
151 luaM_growvector(fs->f->code, fs->pc, n, Byte, codeEM, MAX_INT); 151 luaM_growvector(ls->L, ls->fs->f->code, ls->fs->pc, n,
152 Byte, codeEM, MAX_INT);
152} 153}
153 154
154 155
155static void code_byte (FuncState *fs, Byte c) { 156static void code_byte (LexState *ls, Byte c) {
156 check_pc(fs, 1); 157 check_pc(ls, 1);
157 fs->f->code[fs->pc++] = c; 158 ls->fs->f->code[ls->fs->pc++] = c;
158} 159}
159 160
160 161
@@ -204,7 +205,7 @@ static int fix_opcode (LexState *ls, int pc, OpCode op, int arg) {
204 if (tomove > 0) { /* need to open space? */ 205 if (tomove > 0) { /* need to open space? */
205 FuncState *fs = ls->fs; 206 FuncState *fs = ls->fs;
206 TProtoFunc *f = fs->f; 207 TProtoFunc *f = fs->f;
207 check_pc(fs, tomove); 208 check_pc(ls, tomove);
208 luaO_memup(f->code+pc+tomove, f->code+pc, fs->pc-pc); 209 luaO_memup(f->code+pc+tomove, f->code+pc, fs->pc-pc);
209 fs->pc += tomove; 210 fs->pc += tomove;
210 } 211 }
@@ -215,7 +216,7 @@ static int fix_opcode (LexState *ls, int pc, OpCode op, int arg) {
215 216
216static void code_oparg (LexState *ls, OpCode op, int arg, int delta) { 217static void code_oparg (LexState *ls, OpCode op, int arg, int delta) {
217 int size = codesize(arg); 218 int size = codesize(arg);
218 check_pc(ls->fs, size); 219 check_pc(ls, size);
219 code_oparg_at(ls, ls->fs->pc, op, arg, delta); 220 code_oparg_at(ls, ls->fs->pc, op, arg, delta);
220 ls->fs->pc += size; 221 ls->fs->pc += size;
221} 222}
@@ -223,7 +224,7 @@ static void code_oparg (LexState *ls, OpCode op, int arg, int delta) {
223 224
224static void code_opcode (LexState *ls, OpCode op, int delta) { 225static void code_opcode (LexState *ls, OpCode op, int delta) {
225 deltastack(ls, delta); 226 deltastack(ls, delta);
226 code_byte(ls->fs, (Byte)op); 227 code_byte(ls, (Byte)op);
227} 228}
228 229
229 230
@@ -234,24 +235,24 @@ static void code_constant (LexState *ls, int c) {
234 235
235static void assertglobal (LexState *ls, int index) { 236static void assertglobal (LexState *ls, int index) {
236 TObject *o = &ls->fs->f->consts[index]; 237 TObject *o = &ls->fs->f->consts[index];
237 LUA_ASSERT(ttype(o) == LUA_T_STRING, "global name is not a string"); 238 LUA_ASSERT(ls->L, ttype(o) == LUA_T_STRING, "global name is not a string");
238 luaS_assertglobal(tsvalue(o)); 239 luaS_assertglobal(ls->L, tsvalue(o));
239} 240}
240 241
241 242
242static int next_constant (FuncState *fs) { 243static int next_constant (LexState *ls, TProtoFunc *f) {
243 TProtoFunc *f = fs->f; 244 luaM_growvector(ls->L, f->consts, f->nconsts, 1,
244 luaM_growvector(f->consts, f->nconsts, 1, TObject, constantEM, MAX_ARG); 245 TObject, constantEM, MAX_ARG);
245 return f->nconsts++; 246 return f->nconsts++;
246} 247}
247 248
248 249
249static int string_constant (FuncState *fs, TaggedString *s) { 250static int string_constant (LexState *ls, FuncState *fs, TaggedString *s) {
250 TProtoFunc *f = fs->f; 251 TProtoFunc *f = fs->f;
251 int c = s->constindex; 252 int c = s->constindex;
252 if (!(c < f->nconsts && 253 if (!(c < f->nconsts &&
253 ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) { 254 ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
254 c = next_constant(fs); 255 c = next_constant(ls, f);
255 ttype(&f->consts[c]) = LUA_T_STRING; 256 ttype(&f->consts[c]) = LUA_T_STRING;
256 tsvalue(&f->consts[c]) = s; 257 tsvalue(&f->consts[c]) = s;
257 s->constindex = c; /* hint for next time */ 258 s->constindex = c; /* hint for next time */
@@ -261,23 +262,24 @@ static int string_constant (FuncState *fs, TaggedString *s) {
261 262
262 263
263static void code_string (LexState *ls, TaggedString *s) { 264static void code_string (LexState *ls, TaggedString *s) {
264 code_constant(ls, string_constant(ls->fs, s)); 265 code_constant(ls, string_constant(ls, ls->fs, s));
265} 266}
266 267
267 268
268#define LIM 20 269#define LIM 20
269static int real_constant (FuncState *fs, real r) { 270static int real_constant (LexState *ls, real r) {
270 /* check whether 'r' has appeared within the last LIM entries */ 271 /* check whether 'r' has appeared within the last LIM entries */
271 TObject *cnt = fs->f->consts; 272 TProtoFunc *f = ls->fs->f;
272 int c = fs->f->nconsts; 273 TObject *cnt = f->consts;
274 int c = f->nconsts;
273 int lim = c < LIM ? 0 : c-LIM; 275 int lim = c < LIM ? 0 : c-LIM;
274 while (--c >= lim) { 276 while (--c >= lim) {
275 if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r) 277 if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
276 return c; 278 return c;
277 } 279 }
278 /* not found; create a new entry */ 280 /* not found; create a new entry */
279 c = next_constant(fs); 281 c = next_constant(ls, f);
280 cnt = fs->f->consts; /* 'next_constant' may have reallocated this vector */ 282 cnt = f->consts; /* 'next_constant' may reallocate this vector */
281 ttype(&cnt[c]) = LUA_T_NUMBER; 283 ttype(&cnt[c]) = LUA_T_NUMBER;
282 nvalue(&cnt[c]) = r; 284 nvalue(&cnt[c]) = r;
283 return c; 285 return c;
@@ -291,7 +293,7 @@ static void code_number (LexState *ls, real f) {
291 code_oparg(ls, (f<0) ? PUSHNUMBERNEG : PUSHNUMBER, (int)af, 1); 293 code_oparg(ls, (f<0) ? PUSHNUMBERNEG : PUSHNUMBER, (int)af, 1);
292 } 294 }
293 else 295 else
294 code_constant(ls, real_constant(ls->fs, f)); 296 code_constant(ls, real_constant(ls, f));
295} 297}
296 298
297 299
@@ -304,16 +306,17 @@ static void flush_record (LexState *ls, int n) {
304static void flush_list (LexState *ls, int m, int n) { 306static void flush_list (LexState *ls, int m, int n) {
305 if (n > 0) { 307 if (n > 0) {
306 code_oparg(ls, SETLIST, m, -n); 308 code_oparg(ls, SETLIST, m, -n);
307 code_byte(ls->fs, (Byte)n); 309 code_byte(ls, (Byte)n);
308 } 310 }
309} 311}
310 312
311 313
312static void luaI_registerlocalvar (FuncState *fs, TaggedString *varname, 314static void luaI_registerlocalvar (LexState *ls, TaggedString *varname,
313 int line) { 315 int line) {
316 FuncState *fs = ls->fs;
314 if (fs->nvars != -1) { /* debug information? */ 317 if (fs->nvars != -1) { /* debug information? */
315 TProtoFunc *f = fs->f; 318 TProtoFunc *f = fs->f;
316 luaM_growvector(f->locvars, fs->nvars, 1, LocVar, "", MAX_INT); 319 luaM_growvector(ls->L, f->locvars, fs->nvars, 1, LocVar, "", MAX_INT);
317 f->locvars[fs->nvars].varname = varname; 320 f->locvars[fs->nvars].varname = varname;
318 f->locvars[fs->nvars].line = line; 321 f->locvars[fs->nvars].line = line;
319 fs->nvars++; 322 fs->nvars++;
@@ -321,8 +324,8 @@ static void luaI_registerlocalvar (FuncState *fs, TaggedString *varname,
321} 324}
322 325
323 326
324static void luaI_unregisterlocalvar (FuncState *fs, int line) { 327static void luaI_unregisterlocalvar (LexState *ls, int line) {
325 luaI_registerlocalvar(fs, NULL, line); 328 luaI_registerlocalvar(ls, NULL, line);
326} 329}
327 330
328 331
@@ -330,7 +333,7 @@ static void store_localvar (LexState *ls, TaggedString *name, int n) {
330 FuncState *fs = ls->fs; 333 FuncState *fs = ls->fs;
331 checklimit(ls, fs->nlocalvar+n+1, MAXLOCALS, "local variables"); 334 checklimit(ls, fs->nlocalvar+n+1, MAXLOCALS, "local variables");
332 fs->localvar[fs->nlocalvar+n] = name; 335 fs->localvar[fs->nlocalvar+n] = name;
333 luaI_registerlocalvar(fs, name, ls->linenumber); 336 luaI_registerlocalvar(ls, name, ls->linenumber);
334} 337}
335 338
336 339
@@ -371,7 +374,7 @@ static void singlevar (LexState *ls, TaggedString *n, vardesc *var, int prev) {
371 if (aux_localname(level, n) >= 0) 374 if (aux_localname(level, n) >= 0)
372 luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str); 375 luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str);
373 var->k = VGLOBAL; 376 var->k = VGLOBAL;
374 var->info = string_constant(fs, n); 377 var->info = string_constant(ls, fs, n);
375 } 378 }
376} 379}
377 380
@@ -404,7 +407,7 @@ static void pushupvalue (LexState *ls, TaggedString *n) {
404 407
405 408
406static void check_debugline (LexState *ls) { 409static void check_debugline (LexState *ls) {
407 if (L->debug && ls->linenumber != ls->fs->lastsetline) { 410 if (ls->L->debug && ls->linenumber != ls->fs->lastsetline) {
408 code_oparg(ls, SETLINE, ls->linenumber, 0); 411 code_oparg(ls, SETLINE, ls->linenumber, 0);
409 ls->fs->lastsetline = ls->linenumber; 412 ls->fs->lastsetline = ls->linenumber;
410 } 413 }
@@ -464,7 +467,7 @@ static void code_args (LexState *ls, int nparams, int dots) {
464 else { 467 else {
465 fs->f->code[1] = (Byte)(nparams+ZEROVARARG); 468 fs->f->code[1] = (Byte)(nparams+ZEROVARARG);
466 deltastack(ls, nparams+1); 469 deltastack(ls, nparams+1);
467 add_localvar(ls, luaS_new("arg")); 470 add_localvar(ls, luaS_new(ls->L, "arg"));
468 } 471 }
469} 472}
470 473
@@ -515,7 +518,7 @@ static void storevar (LexState *ls, const vardesc *var) {
515 code_opcode(ls, SETTABLEPOP, -3); 518 code_opcode(ls, SETTABLEPOP, -3);
516 break; 519 break;
517 default: 520 default:
518 LUA_INTERNALERROR("invalid var kind to store"); 521 LUA_INTERNALERROR(ls->L, "invalid var kind to store");
519 } 522 }
520} 523}
521 524
@@ -548,7 +551,7 @@ static void codeIf (LexState *ls, int thenAdd, int elseAdd) {
548static void func_onstack (LexState *ls, FuncState *func) { 551static void func_onstack (LexState *ls, FuncState *func) {
549 FuncState *fs = ls->fs; 552 FuncState *fs = ls->fs;
550 int i; 553 int i;
551 int c = next_constant(fs); 554 int c = next_constant(ls, fs->f);
552 ttype(&fs->f->consts[c]) = LUA_T_PROTO; 555 ttype(&fs->f->consts[c]) = LUA_T_PROTO;
553 fs->f->consts[c].value.tf = func->f; 556 fs->f->consts[c].value.tf = func->f;
554 if (func->nupvalues == 0) 557 if (func->nupvalues == 0)
@@ -558,13 +561,14 @@ static void func_onstack (LexState *ls, FuncState *func) {
558 lua_pushvar(ls, &func->upvalues[i]); 561 lua_pushvar(ls, &func->upvalues[i]);
559 deltastack(ls, 1); /* CLOSURE puts one extra element (before poping) */ 562 deltastack(ls, 1); /* CLOSURE puts one extra element (before poping) */
560 code_oparg(ls, CLOSURE, c, -func->nupvalues); 563 code_oparg(ls, CLOSURE, c, -func->nupvalues);
561 code_byte(fs, (Byte)func->nupvalues); 564 code_byte(ls, (Byte)func->nupvalues);
562 } 565 }
563} 566}
564 567
565 568
566static void init_state (LexState *ls, FuncState *fs, TaggedString *source) { 569static void init_state (LexState *ls, FuncState *fs, TaggedString *source) {
567 TProtoFunc *f = luaF_newproto(); 570 lua_State *L = ls->L;
571 TProtoFunc *f = luaF_newproto(ls->L);
568 fs->prev = ls->fs; /* linked list of funcstates */ 572 fs->prev = ls->fs; /* linked list of funcstates */
569 ls->fs = fs; 573 ls->fs = fs;
570 fs->stacksize = 0; 574 fs->stacksize = 0;
@@ -577,10 +581,11 @@ static void init_state (LexState *ls, FuncState *fs, TaggedString *source) {
577 fs->pc = 0; 581 fs->pc = 0;
578 f->code = NULL; 582 f->code = NULL;
579 fs->nvars = (L->debug) ? 0 : -1; /* flag no debug information? */ 583 fs->nvars = (L->debug) ? 0 : -1; /* flag no debug information? */
580 code_byte(fs, 0); /* to be filled with maxstacksize */ 584 code_byte(ls, 0); /* to be filled with maxstacksize */
581 code_byte(fs, 0); /* to be filled with arg information */ 585 code_byte(ls, 0); /* to be filled with arg information */
582 /* push function (to avoid GC) */ 586 /* push function (to avoid GC) */
583 tfvalue(L->stack.top) = f; ttype(L->stack.top) = LUA_T_PROTO; 587 tfvalue(L->stack.top) = f;
588 ttype(L->stack.top) = LUA_T_PROTO;
584 incr_top; 589 incr_top;
585} 590}
586 591
@@ -590,14 +595,14 @@ static void close_func (LexState *ls) {
590 TProtoFunc *f = fs->f; 595 TProtoFunc *f = fs->f;
591 code_opcode(ls, ENDCODE, 0); 596 code_opcode(ls, ENDCODE, 0);
592 f->code[0] = (Byte)fs->maxstacksize; 597 f->code[0] = (Byte)fs->maxstacksize;
593 luaM_reallocvector(f->code, fs->pc, Byte); 598 luaM_reallocvector(ls->L, f->code, fs->pc, Byte);
594 luaM_reallocvector(f->consts, f->nconsts, TObject); 599 luaM_reallocvector(ls->L, f->consts, f->nconsts, TObject);
595 if (fs->nvars != -1) { /* debug information? */ 600 if (fs->nvars != -1) { /* debug information? */
596 luaI_registerlocalvar(fs, NULL, -1); /* flag end of vector */ 601 luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */
597 luaM_reallocvector(f->locvars, fs->nvars, LocVar); 602 luaM_reallocvector(ls->L, f->locvars, fs->nvars, LocVar);
598 } 603 }
599 ls->fs = fs->prev; 604 ls->fs = fs->prev;
600 L->stack.top--; /* pop function */ 605 ls->L->stack.top--; /* pop function */
601} 606}
602 607
603 608
@@ -664,7 +669,7 @@ static int checkname (LexState *ls) {
664 int sc; 669 int sc;
665 if (ls->token != NAME) 670 if (ls->token != NAME)
666 luaX_error(ls, "`NAME' expected"); 671 luaX_error(ls, "`NAME' expected");
667 sc = string_constant(ls->fs, ls->seminfo.ts); 672 sc = string_constant(ls, ls->fs, ls->seminfo.ts);
668 next(ls); 673 next(ls);
669 return sc; 674 return sc;
670} 675}
@@ -685,11 +690,11 @@ static int optional (LexState *ls, int c) {
685} 690}
686 691
687 692
688TProtoFunc *luaY_parser (ZIO *z) { 693TProtoFunc *luaY_parser (lua_State *L, ZIO *z) {
689 struct LexState lexstate; 694 struct LexState lexstate;
690 struct FuncState funcstate; 695 struct FuncState funcstate;
691 luaX_setinput(&lexstate, z); 696 luaX_setinput(L, &lexstate, z);
692 init_state(&lexstate, &funcstate, luaS_new(zname(z))); 697 init_state(&lexstate, &funcstate, luaS_new(L, zname(z)));
693 next(&lexstate); /* read first token */ 698 next(&lexstate); /* read first token */
694 chunk(&lexstate); 699 chunk(&lexstate);
695 if (lexstate.token != EOS) 700 if (lexstate.token != EOS)
@@ -708,7 +713,7 @@ TProtoFunc *luaY_parser (ZIO *z) {
708static void chunk (LexState *ls) { 713static void chunk (LexState *ls) {
709 /* chunk -> { stat [;] } ret */ 714 /* chunk -> { stat [;] } ret */
710 while (stat(ls)) { 715 while (stat(ls)) {
711 LUA_ASSERT(ls->fs->stacksize == ls->fs->nlocalvar, 716 LUA_ASSERT(ls->L, ls->fs->stacksize == ls->fs->nlocalvar,
712 "stack size != # local vars"); 717 "stack size != # local vars");
713 optional(ls, ';'); 718 optional(ls, ';');
714 } 719 }
@@ -728,7 +733,7 @@ static void whilestat (LexState *ls, int line) {
728 block(ls); 733 block(ls);
729 check_match(ls, END, WHILE, line); 734 check_match(ls, END, WHILE, line);
730 cond_size = cond_end-while_init; 735 cond_size = cond_end-while_init;
731 check_pc(fs, cond_size); 736 check_pc(ls, cond_size);
732 memcpy(f->code+fs->pc, f->code+while_init, cond_size); 737 memcpy(f->code+fs->pc, f->code+while_init, cond_size);
733 luaO_memdown(f->code+while_init, f->code+cond_end, fs->pc-while_init); 738 luaO_memdown(f->code+while_init, f->code+cond_end, fs->pc-while_init);
734 while_init += JMPSIZE + fix_jump(ls, while_init, JMP, fs->pc-cond_size); 739 while_init += JMPSIZE + fix_jump(ls, while_init, JMP, fs->pc-cond_size);
@@ -841,7 +846,7 @@ static int stat (LexState *ls) {
841 846
842static int SaveWord (LexState *ls) { 847static int SaveWord (LexState *ls) {
843 int res = ls->fs->pc; 848 int res = ls->fs->pc;
844 check_pc(ls->fs, JMPSIZE); 849 check_pc(ls, JMPSIZE);
845 ls->fs->pc += JMPSIZE; /* open space */ 850 ls->fs->pc += JMPSIZE; /* open space */
846 return res; 851 return res;
847} 852}
@@ -864,7 +869,7 @@ static void block (LexState *ls) {
864 chunk(ls); 869 chunk(ls);
865 adjuststack(ls, fs->nlocalvar - nlocalvar); 870 adjuststack(ls, fs->nlocalvar - nlocalvar);
866 for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--) 871 for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--)
867 luaI_unregisterlocalvar(fs, fs->lastsetline); 872 luaI_unregisterlocalvar(ls, fs->lastsetline);
868} 873}
869 874
870static int funcname (LexState *ls, vardesc *v) { 875static int funcname (LexState *ls, vardesc *v) {
@@ -888,7 +893,7 @@ static void body (LexState *ls, int needself, int line) {
888 newfs.f->lineDefined = line; 893 newfs.f->lineDefined = line;
889 check(ls, '('); 894 check(ls, '(');
890 if (needself) 895 if (needself)
891 add_localvar(ls, luaS_new("self")); 896 add_localvar(ls, luaS_new(ls->L, "self"));
892 parlist(ls); 897 parlist(ls);
893 check(ls, ')'); 898 check(ls, ')');
894 chunk(ls); 899 chunk(ls);
@@ -1173,9 +1178,9 @@ static int funcparams (LexState *ls, int slf) {
1173 luaX_error(ls, "function arguments expected"); 1178 luaX_error(ls, "function arguments expected");
1174 break; 1179 break;
1175 } 1180 }
1176 code_byte(fs, CALL); 1181 code_byte(ls, CALL);
1177 code_byte(fs, 0); /* save space for nresult */ 1182 code_byte(ls, 0); /* save space for nresult */
1178 code_byte(fs, (Byte)(nparams+slf)); 1183 code_byte(ls, (Byte)(nparams+slf));
1179 return fs->pc-1; 1184 return fs->pc-1;
1180} 1185}
1181 1186