History

Lua 5.1-rc1

Lua Team committed 20 years ago

Commit 53d3c321 parent 27f2ec746 files · +1822 −1531Browse files

Showing the changes to src/lparser.c (1 of 46 files). Show all

Unified · Split

src/lparser.c+31−49
@@ -1,5 +1,5 @@
1/*1/*
2** $Id: lparser.c,v 2.38 2005/10/24 17:38:47 roberto Exp $2** $Id: lparser.c,v 2.40 2005/12/22 16:19:56 roberto Exp $
3** Lua Parser3** Lua Parser
4** See Copyright Notice in lua.h4** See Copyright Notice in lua.h
5*/5*/
@@ -54,24 +54,6 @@ static void chunk (LexState *ls); · 48 unmodified lines
54static void expr (LexState *ls, expdesc *v);54static void expr (LexState *ls, expdesc *v);
55 55
56 56
57
58static void next (LexState *ls) {
59 ls->lastline = ls->linenumber;
60 if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
61 ls->t = ls->lookahead; /* use this one */
62 ls->lookahead.token = TK_EOS; /* and discharge it */
63 }
64 else
65 ls->t.token = luaX_lex(ls, &ls->t.seminfo); /* read next token */
66}
67
68
69static void lookahead (LexState *ls) {
70 lua_assert(ls->lookahead.token == TK_EOS);
71 ls->lookahead.token = luaX_lex(ls, &ls->lookahead.seminfo);
72}
73
74
75static void anchor_token (LexState *ls) {57static void anchor_token (LexState *ls) {
76 if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {58 if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
77 TString *ts = ls->t.seminfo.ts;59 TString *ts = ls->t.seminfo.ts;
@@ -97,7 +79,7 @@ static void errorlimit (FuncState *fs, int limit, const char *what) { · 19 unmodified lines
97 79
98static int testnext (LexState *ls, int c) {80static int testnext (LexState *ls, int c) {
99 if (ls->t.token == c) {81 if (ls->t.token == c) {
100 next(ls);82 luaX_next(ls);
101 return 1;83 return 1;
102 }84 }
103 else return 0;85 else return 0;
@@ -111,7 +93,7 @@ static void check (LexState *ls, int c) { · 7 unmodified lines
111 93
112static void checknext (LexState *ls, int c) {94static void checknext (LexState *ls, int c) {
113 check(ls, c);95 check(ls, c);
114 next(ls);96 luaX_next(ls);
115}97}
116 98
117 99
@@ -136,7 +118,7 @@ static TString *str_checkname (LexState *ls) { · 18 unmodified lines
136 TString *ts;118 TString *ts;
137 check(ls, TK_NAME);119 check(ls, TK_NAME);
138 ts = ls->t.seminfo.ts;120 ts = ls->t.seminfo.ts;
139 next(ls);121 luaX_next(ls);
140 return ts;122 return ts;
141}123}
142 124
@@ -184,7 +166,7 @@ static void new_localvar (LexState *ls, TString *name, int n) { · 41 unmodified lines
184 166
185static void adjustlocalvars (LexState *ls, int nvars) {167static void adjustlocalvars (LexState *ls, int nvars) {
186 FuncState *fs = ls->fs;168 FuncState *fs = ls->fs;
187 fs->nactvar = cast(lu_byte, fs->nactvar + nvars);169 fs->nactvar = cast_byte(fs->nactvar + nvars);
188 for (; nvars; nvars--) {170 for (; nvars; nvars--) {
189 getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc;171 getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc;
190 }172 }
@@ -216,8 +198,8 @@ static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { · 25 unmodified lines
216 f->upvalues[f->nups] = name;198 f->upvalues[f->nups] = name;
217 luaC_objbarrier(fs->L, f, name);199 luaC_objbarrier(fs->L, f, name);
218 lua_assert(v->k == VLOCAL || v->k == VUPVAL);200 lua_assert(v->k == VLOCAL || v->k == VUPVAL);
219 fs->upvalues[f->nups].k = cast(lu_byte, v->k);201 fs->upvalues[f->nups].k = cast_byte(v->k);
220 fs->upvalues[f->nups].info = cast(lu_byte, v->u.s.info);202 fs->upvalues[f->nups].info = cast_byte(v->u.s.info);
221 return f->nups++;203 return f->nups++;
222}204}
223 205
@@ -404,7 +386,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { · 180 unmodified lines
404 luaX_setinput(L, &lexstate, z, luaS_new(L, name));386 luaX_setinput(L, &lexstate, z, luaS_new(L, name));
405 open_func(&lexstate, &funcstate);387 open_func(&lexstate, &funcstate);
406 funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */388 funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */
407 next(&lexstate); /* read first token */389 luaX_next(&lexstate); /* read first token */
408 chunk(&lexstate);390 chunk(&lexstate);
409 check(&lexstate, TK_EOS);391 check(&lexstate, TK_EOS);
410 close_func(&lexstate);392 close_func(&lexstate);
@@ -426,7 +408,7 @@ static void field (LexState *ls, expdesc *v) { · 15 unmodified lines
426 FuncState *fs = ls->fs;408 FuncState *fs = ls->fs;
427 expdesc key;409 expdesc key;
428 luaK_exp2anyreg(fs, v);410 luaK_exp2anyreg(fs, v);
429 next(ls); /* skip the dot or colon */411 luaX_next(ls); /* skip the dot or colon */
430 checkname(ls, &key);412 checkname(ls, &key);
431 luaK_indexed(fs, v, &key);413 luaK_indexed(fs, v, &key);
432}414}
@@ -434,7 +416,7 @@ static void field (LexState *ls, expdesc *v) { · 1 unmodified line
434 416
435static void yindex (LexState *ls, expdesc *v) {417static void yindex (LexState *ls, expdesc *v) {
436 /* index -> '[' expr ']' */418 /* index -> '[' expr ']' */
437 next(ls); /* skip the '[' */419 luaX_next(ls); /* skip the '[' */
438 expr(ls, v);420 expr(ls, v);
439 luaK_exp2val(ls->fs, v);421 luaK_exp2val(ls->fs, v);
440 checknext(ls, ']');422 checknext(ls, ']');
@@ -530,7 +512,7 @@ static void constructor (LexState *ls, expdesc *t) { · 89 unmodified lines
530 closelistfield(fs, &cc);512 closelistfield(fs, &cc);
531 switch(ls->t.token) {513 switch(ls->t.token) {
532 case TK_NAME: { /* may be listfields or recfields */514 case TK_NAME: { /* may be listfields or recfields */
533 lookahead(ls);515 luaX_lookahead(ls);
534 if (ls->lookahead.token != '=') /* expression? */516 if (ls->lookahead.token != '=') /* expression? */
535 listfield(ls, &cc);517 listfield(ls, &cc);
536 else518 else
@@ -571,7 +553,7 @@ static void parlist (LexState *ls) { · 34 unmodified lines
571 break;553 break;
572 }554 }
573 case TK_DOTS: { /* param -> `...' */555 case TK_DOTS: { /* param -> `...' */
574 next(ls);556 luaX_next(ls);
575#if defined(LUA_COMPAT_VARARG)557#if defined(LUA_COMPAT_VARARG)
576 /* use `arg' as default name */558 /* use `arg' as default name */
577 new_localvarliteral(ls, "arg", nparams++);559 new_localvarliteral(ls, "arg", nparams++);
@@ -585,7 +567,7 @@ static void parlist (LexState *ls) { · 7 unmodified lines
585 } while (!f->is_vararg && testnext(ls, ','));567 } while (!f->is_vararg && testnext(ls, ','));
586 }568 }
587 adjustlocalvars(ls, nparams);569 adjustlocalvars(ls, nparams);
588 f->numparams = fs->nactvar - (f->is_vararg & VARARG_HASARG);570 f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG));
589 luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */571 luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */
590}572}
591 573
@@ -632,7 +614,7 @@ static void funcargs (LexState *ls, expdesc *f) { · 40 unmodified lines
632 case '(': { /* funcargs -> `(' [ explist1 ] `)' */614 case '(': { /* funcargs -> `(' [ explist1 ] `)' */
633 if (line != ls->lastline)615 if (line != ls->lastline)
634 luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");616 luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
635 next(ls);617 luaX_next(ls);
636 if (ls->t.token == ')') /* arg list is empty? */618 if (ls->t.token == ')') /* arg list is empty? */
637 args.k = VVOID;619 args.k = VVOID;
638 else {620 else {
@@ -648,7 +630,7 @@ static void funcargs (LexState *ls, expdesc *f) { · 9 unmodified lines
648 }630 }
649 case TK_STRING: { /* funcargs -> STRING */631 case TK_STRING: { /* funcargs -> STRING */
650 codestring(ls, &args, ls->t.seminfo.ts);632 codestring(ls, &args, ls->t.seminfo.ts);
651 next(ls); /* must use `seminfo' before `next' */633 luaX_next(ls); /* must use `seminfo' before `next' */
652 break;634 break;
653 }635 }
654 default: {636 default: {
@@ -686,7 +668,7 @@ static void prefixexp (LexState *ls, expdesc *v) { · 31 unmodified lines
686 switch (ls->t.token) {668 switch (ls->t.token) {
687 case '(': {669 case '(': {
688 int line = ls->linenumber;670 int line = ls->linenumber;
689 next(ls);671 luaX_next(ls);
690 expr(ls, v);672 expr(ls, v);
691 check_match(ls, ')', '(', line);673 check_match(ls, ')', '(', line);
692 luaK_dischargevars(ls->fs, v);674 luaK_dischargevars(ls->fs, v);
@@ -724,7 +706,7 @@ static void primaryexp (LexState *ls, expdesc *v) { · 31 unmodified lines
724 }706 }
725 case ':': { /* `:' NAME funcargs */707 case ':': { /* `:' NAME funcargs */
726 expdesc key;708 expdesc key;
727 next(ls);709 luaX_next(ls);
728 checkname(ls, &key);710 checkname(ls, &key);
729 luaK_self(fs, v, &key);711 luaK_self(fs, v, &key);
730 funcargs(ls, v);712 funcargs(ls, v);
@@ -779,7 +761,7 @@ static void simpleexp (LexState *ls, expdesc *v) { · 48 unmodified lines
779 return;761 return;
780 }762 }
781 case TK_FUNCTION: {763 case TK_FUNCTION: {
782 next(ls);764 luaX_next(ls);
783 body(ls, v, 0, ls->linenumber);765 body(ls, v, 0, ls->linenumber);
784 return;766 return;
785 }767 }
@@ -788,7 +770,7 @@ static void simpleexp (LexState *ls, expdesc *v) { · 2 unmodified lines
788 return;770 return;
789 }771 }
790 }772 }
791 next(ls);773 luaX_next(ls);
792}774}
793 775
794 776
@@ -848,7 +830,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { · 53 unmodified lines
848 enterlevel(ls);830 enterlevel(ls);
849 uop = getunopr(ls->t.token);831 uop = getunopr(ls->t.token);
850 if (uop != OPR_NOUNOPR) {832 if (uop != OPR_NOUNOPR) {
851 next(ls);833 luaX_next(ls);
852 subexpr(ls, v, UNARY_PRIORITY);834 subexpr(ls, v, UNARY_PRIORITY);
853 luaK_prefix(ls->fs, uop, v);835 luaK_prefix(ls->fs, uop, v);
854 }836 }
@@ -858,7 +840,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { · 3 unmodified lines
858 while (op != OPR_NOBINOPR && priority[op].left > limit) {840 while (op != OPR_NOBINOPR && priority[op].left > limit) {
859 expdesc v2;841 expdesc v2;
860 BinOpr nextop;842 BinOpr nextop;
861 next(ls);843 luaX_next(ls);
862 luaK_infix(ls->fs, op, v);844 luaK_infix(ls->fs, op, v);
863 /* read sub-expression with higher priority */845 /* read sub-expression with higher priority */
864 nextop = subexpr(ls, &v2, priority[op].right);846 nextop = subexpr(ls, &v2, priority[op].right);
@@ -1009,7 +991,7 @@ static void whilestat (LexState *ls, int line) { · 144 unmodified lines
1009 int whileinit;991 int whileinit;
1010 int condexit;992 int condexit;
1011 BlockCnt bl;993 BlockCnt bl;
1012 next(ls); /* skip WHILE */994 luaX_next(ls); /* skip WHILE */
1013 whileinit = luaK_getlabel(fs);995 whileinit = luaK_getlabel(fs);
1014 condexit = cond(ls);996 condexit = cond(ls);
1015 enterblock(fs, &bl, 1);997 enterblock(fs, &bl, 1);
@@ -1030,7 +1012,7 @@ static void repeatstat (LexState *ls, int line) { · 14 unmodified lines
1030 BlockCnt bl1, bl2;1012 BlockCnt bl1, bl2;
1031 enterblock(fs, &bl1, 1); /* loop block */1013 enterblock(fs, &bl1, 1); /* loop block */
1032 enterblock(fs, &bl2, 0); /* scope block */1014 enterblock(fs, &bl2, 0); /* scope block */
1033 next(ls); /* skip REPEAT */1015 luaX_next(ls); /* skip REPEAT */
1034 chunk(ls);1016 chunk(ls);
1035 check_match(ls, TK_UNTIL, TK_REPEAT, line);1017 check_match(ls, TK_UNTIL, TK_REPEAT, line);
1036 condexit = cond(ls); /* read condition (inside scope block) */1018 condexit = cond(ls); /* read condition (inside scope block) */
@@ -1130,7 +1112,7 @@ static void forstat (LexState *ls, int line) { · 93 unmodified lines
1130 TString *varname;1112 TString *varname;
1131 BlockCnt bl;1113 BlockCnt bl;
1132 enterblock(fs, &bl, 1); /* scope for loop and control variables */1114 enterblock(fs, &bl, 1); /* scope for loop and control variables */
1133 next(ls); /* skip `for' */1115 luaX_next(ls); /* skip `for' */
1134 varname = str_checkname(ls); /* first variable name */1116 varname = str_checkname(ls); /* first variable name */
1135 switch (ls->t.token) {1117 switch (ls->t.token) {
1136 case '=': fornum(ls, varname, line); break;1118 case '=': fornum(ls, varname, line); break;
@@ -1145,7 +1127,7 @@ static void forstat (LexState *ls, int line) { · 8 unmodified lines
1145static int test_then_block (LexState *ls) {1127static int test_then_block (LexState *ls) {
1146 /* test_then_block -> [IF | ELSEIF] cond THEN block */1128 /* test_then_block -> [IF | ELSEIF] cond THEN block */
1147 int condexit;1129 int condexit;
1148 next(ls); /* skip IF or ELSEIF */1130 luaX_next(ls); /* skip IF or ELSEIF */
1149 condexit = cond(ls);1131 condexit = cond(ls);
1150 checknext(ls, TK_THEN);1132 checknext(ls, TK_THEN);
1151 block(ls); /* `then' part */1133 block(ls); /* `then' part */
@@ -1167,7 +1149,7 @@ static void ifstat (LexState *ls, int line) { · 15 unmodified lines
1167 if (ls->t.token == TK_ELSE) {1149 if (ls->t.token == TK_ELSE) {
1168 luaK_concat(fs, &escapelist, luaK_jump(fs));1150 luaK_concat(fs, &escapelist, luaK_jump(fs));
1169 luaK_patchtohere(fs, flist);1151 luaK_patchtohere(fs, flist);
1170 next(ls); /* skip ELSE (after patch, for correct line info) */1152 luaX_next(ls); /* skip ELSE (after patch, for correct line info) */
1171 block(ls); /* `else' part */1153 block(ls); /* `else' part */
1172 }1154 }
1173 else1155 else
@@ -1228,7 +1210,7 @@ static void funcstat (LexState *ls, int line) { · 54 unmodified lines
1228 /* funcstat -> FUNCTION funcname body */1210 /* funcstat -> FUNCTION funcname body */
1229 int needself;1211 int needself;
1230 expdesc v, b;1212 expdesc v, b;
1231 next(ls); /* skip FUNCTION */1213 luaX_next(ls); /* skip FUNCTION */
1232 needself = funcname(ls, &v);1214 needself = funcname(ls, &v);
1233 body(ls, &b, needself, line);1215 body(ls, &b, needself, line);
1234 luaK_storevar(ls->fs, &v, &b);1216 luaK_storevar(ls->fs, &v, &b);
@@ -1255,7 +1237,7 @@ static void retstat (LexState *ls) { · 20 unmodified lines
1255 FuncState *fs = ls->fs;1237 FuncState *fs = ls->fs;
1256 expdesc e;1238 expdesc e;
1257 int first, nret; /* registers with returned values */1239 int first, nret; /* registers with returned values */
1258 next(ls); /* skip RETURN */1240 luaX_next(ls); /* skip RETURN */
1259 if (block_follow(ls->t.token) || ls->t.token == ';')1241 if (block_follow(ls->t.token) || ls->t.token == ';')
1260 first = nret = 0; /* return no values */1242 first = nret = 0; /* return no values */
1261 else {1243 else {
@@ -1295,7 +1277,7 @@ static int statement (LexState *ls) { · 33 unmodified lines
1295 return 0;1277 return 0;
1296 }1278 }
1297 case TK_DO: { /* stat -> DO block END */1279 case TK_DO: { /* stat -> DO block END */
1298 next(ls); /* skip DO */1280 luaX_next(ls); /* skip DO */
1299 block(ls);1281 block(ls);
1300 check_match(ls, TK_END, TK_DO, line);1282 check_match(ls, TK_END, TK_DO, line);
1301 return 0;1283 return 0;
@@ -1313,7 +1295,7 @@ static int statement (LexState *ls) { · 11 unmodified lines
1313 return 0;1295 return 0;
1314 }1296 }
1315 case TK_LOCAL: { /* stat -> localstat */1297 case TK_LOCAL: { /* stat -> localstat */
1316 next(ls); /* skip LOCAL */1298 luaX_next(ls); /* skip LOCAL */
1317 if (testnext(ls, TK_FUNCTION)) /* local function? */1299 if (testnext(ls, TK_FUNCTION)) /* local function? */
1318 localfunc(ls);1300 localfunc(ls);
1319 else1301 else
@@ -1325,7 +1307,7 @@ static int statement (LexState *ls) { · 5 unmodified lines
1325 return 1; /* must be last statement */1307 return 1; /* must be last statement */
1326 }1308 }
1327 case TK_BREAK: { /* stat -> breakstat */1309 case TK_BREAK: { /* stat -> breakstat */
1328 next(ls); /* skip BREAK */1310 luaX_next(ls); /* skip BREAK */
1329 breakstat(ls);1311 breakstat(ls);
1330 return 1; /* must be last statement */1312 return 1; /* must be last statement */
1331 }1313 }