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 @@
11 /*
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 $
33 ** Lua Parser
44 ** See Copyright Notice in lua.h
55 */
@@ -54,24 +54,6 @@ static void chunk (LexState *ls); · 48 unmodified lines
5454 static void expr (LexState *ls, expdesc *v);
5555
5656
57−
58−static 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−
69−static 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−
7557 static void anchor_token (LexState *ls) {
7658 if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
7759 TString *ts = ls->t.seminfo.ts;
@@ -97,7 +79,7 @@ static void errorlimit (FuncState *fs, int limit, const char *what) { · 19 unmodified lines
9779
9880 static int testnext (LexState *ls, int c) {
9981 if (ls->t.token == c) {
100− next(ls);
82+ luaX_next(ls);
10183 return 1;
10284 }
10385 else return 0;
@@ -111,7 +93,7 @@ static void check (LexState *ls, int c) { · 7 unmodified lines
11193
11294 static void checknext (LexState *ls, int c) {
11395 check(ls, c);
114− next(ls);
96+ luaX_next(ls);
11597 }
11698
11799
@@ -136,7 +118,7 @@ static TString *str_checkname (LexState *ls) { · 18 unmodified lines
136118 TString *ts;
137119 check(ls, TK_NAME);
138120 ts = ls->t.seminfo.ts;
139− next(ls);
121+ luaX_next(ls);
140122 return ts;
141123 }
142124
@@ -184,7 +166,7 @@ static void new_localvar (LexState *ls, TString *name, int n) { · 41 unmodified lines
184166
185167 static void adjustlocalvars (LexState *ls, int nvars) {
186168 FuncState *fs = ls->fs;
187− fs->nactvar = cast(lu_byte, fs->nactvar + nvars);
169+ fs->nactvar = cast_byte(fs->nactvar + nvars);
188170 for (; nvars; nvars--) {
189171 getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc;
190172 }
@@ -216,8 +198,8 @@ static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { · 25 unmodified lines
216198 f->upvalues[f->nups] = name;
217199 luaC_objbarrier(fs->L, f, name);
218200 lua_assert(v->k == VLOCAL || v->k == VUPVAL);
219− fs->upvalues[f->nups].k = cast(lu_byte, v->k);
220− fs->upvalues[f->nups].info = cast(lu_byte, v->u.s.info);
201+ fs->upvalues[f->nups].k = cast_byte(v->k);
202+ fs->upvalues[f->nups].info = cast_byte(v->u.s.info);
221203 return f->nups++;
222204 }
223205
@@ -404,7 +386,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { · 180 unmodified lines
404386 luaX_setinput(L, &lexstate, z, luaS_new(L, name));
405387 open_func(&lexstate, &funcstate);
406388 funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */
407− next(&lexstate); /* read first token */
389+ luaX_next(&lexstate); /* read first token */
408390 chunk(&lexstate);
409391 check(&lexstate, TK_EOS);
410392 close_func(&lexstate);
@@ -426,7 +408,7 @@ static void field (LexState *ls, expdesc *v) { · 15 unmodified lines
426408 FuncState *fs = ls->fs;
427409 expdesc key;
428410 luaK_exp2anyreg(fs, v);
429− next(ls); /* skip the dot or colon */
411+ luaX_next(ls); /* skip the dot or colon */
430412 checkname(ls, &key);
431413 luaK_indexed(fs, v, &key);
432414 }
@@ -434,7 +416,7 @@ static void field (LexState *ls, expdesc *v) { · 1 unmodified line
434416
435417 static void yindex (LexState *ls, expdesc *v) {
436418 /* index -> '[' expr ']' */
437− next(ls); /* skip the '[' */
419+ luaX_next(ls); /* skip the '[' */
438420 expr(ls, v);
439421 luaK_exp2val(ls->fs, v);
440422 checknext(ls, ']');
@@ -530,7 +512,7 @@ static void constructor (LexState *ls, expdesc *t) { · 89 unmodified lines
530512 closelistfield(fs, &cc);
531513 switch(ls->t.token) {
532514 case TK_NAME: { /* may be listfields or recfields */
533− lookahead(ls);
515+ luaX_lookahead(ls);
534516 if (ls->lookahead.token != '=') /* expression? */
535517 listfield(ls, &cc);
536518 else
@@ -571,7 +553,7 @@ static void parlist (LexState *ls) { · 34 unmodified lines
571553 break;
572554 }
573555 case TK_DOTS: { /* param -> `...' */
574− next(ls);
556+ luaX_next(ls);
575557 #if defined(LUA_COMPAT_VARARG)
576558 /* use `arg' as default name */
577559 new_localvarliteral(ls, "arg", nparams++);
@@ -585,7 +567,7 @@ static void parlist (LexState *ls) { · 7 unmodified lines
585567 } while (!f->is_vararg && testnext(ls, ','));
586568 }
587569 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));
589571 luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */
590572 }
591573
@@ -632,7 +614,7 @@ static void funcargs (LexState *ls, expdesc *f) { · 40 unmodified lines
632614 case '(': { /* funcargs -> `(' [ explist1 ] `)' */
633615 if (line != ls->lastline)
634616 luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
635− next(ls);
617+ luaX_next(ls);
636618 if (ls->t.token == ')') /* arg list is empty? */
637619 args.k = VVOID;
638620 else {
@@ -648,7 +630,7 @@ static void funcargs (LexState *ls, expdesc *f) { · 9 unmodified lines
648630 }
649631 case TK_STRING: { /* funcargs -> STRING */
650632 codestring(ls, &args, ls->t.seminfo.ts);
651− next(ls); /* must use `seminfo' before `next' */
633+ luaX_next(ls); /* must use `seminfo' before `next' */
652634 break;
653635 }
654636 default: {
@@ -686,7 +668,7 @@ static void prefixexp (LexState *ls, expdesc *v) { · 31 unmodified lines
686668 switch (ls->t.token) {
687669 case '(': {
688670 int line = ls->linenumber;
689− next(ls);
671+ luaX_next(ls);
690672 expr(ls, v);
691673 check_match(ls, ')', '(', line);
692674 luaK_dischargevars(ls->fs, v);
@@ -724,7 +706,7 @@ static void primaryexp (LexState *ls, expdesc *v) { · 31 unmodified lines
724706 }
725707 case ':': { /* `:' NAME funcargs */
726708 expdesc key;
727− next(ls);
709+ luaX_next(ls);
728710 checkname(ls, &key);
729711 luaK_self(fs, v, &key);
730712 funcargs(ls, v);
@@ -779,7 +761,7 @@ static void simpleexp (LexState *ls, expdesc *v) { · 48 unmodified lines
779761 return;
780762 }
781763 case TK_FUNCTION: {
782− next(ls);
764+ luaX_next(ls);
783765 body(ls, v, 0, ls->linenumber);
784766 return;
785767 }
@@ -788,7 +770,7 @@ static void simpleexp (LexState *ls, expdesc *v) { · 2 unmodified lines
788770 return;
789771 }
790772 }
791− next(ls);
773+ luaX_next(ls);
792774 }
793775
794776
@@ -848,7 +830,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { · 53 unmodified lines
848830 enterlevel(ls);
849831 uop = getunopr(ls->t.token);
850832 if (uop != OPR_NOUNOPR) {
851− next(ls);
833+ luaX_next(ls);
852834 subexpr(ls, v, UNARY_PRIORITY);
853835 luaK_prefix(ls->fs, uop, v);
854836 }
@@ -858,7 +840,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { · 3 unmodified lines
858840 while (op != OPR_NOBINOPR && priority[op].left > limit) {
859841 expdesc v2;
860842 BinOpr nextop;
861− next(ls);
843+ luaX_next(ls);
862844 luaK_infix(ls->fs, op, v);
863845 /* read sub-expression with higher priority */
864846 nextop = subexpr(ls, &v2, priority[op].right);
@@ -1009,7 +991,7 @@ static void whilestat (LexState *ls, int line) { · 144 unmodified lines
1009991 int whileinit;
1010992 int condexit;
1011993 BlockCnt bl;
1012− next(ls); /* skip WHILE */
994+ luaX_next(ls); /* skip WHILE */
1013995 whileinit = luaK_getlabel(fs);
1014996 condexit = cond(ls);
1015997 enterblock(fs, &bl, 1);
@@ -1030,7 +1012,7 @@ static void repeatstat (LexState *ls, int line) { · 14 unmodified lines
10301012 BlockCnt bl1, bl2;
10311013 enterblock(fs, &bl1, 1); /* loop block */
10321014 enterblock(fs, &bl2, 0); /* scope block */
1033− next(ls); /* skip REPEAT */
1015+ luaX_next(ls); /* skip REPEAT */
10341016 chunk(ls);
10351017 check_match(ls, TK_UNTIL, TK_REPEAT, line);
10361018 condexit = cond(ls); /* read condition (inside scope block) */
@@ -1130,7 +1112,7 @@ static void forstat (LexState *ls, int line) { · 93 unmodified lines
11301112 TString *varname;
11311113 BlockCnt bl;
11321114 enterblock(fs, &bl, 1); /* scope for loop and control variables */
1133− next(ls); /* skip `for' */
1115+ luaX_next(ls); /* skip `for' */
11341116 varname = str_checkname(ls); /* first variable name */
11351117 switch (ls->t.token) {
11361118 case '=': fornum(ls, varname, line); break;
@@ -1145,7 +1127,7 @@ static void forstat (LexState *ls, int line) { · 8 unmodified lines
11451127 static int test_then_block (LexState *ls) {
11461128 /* test_then_block -> [IF | ELSEIF] cond THEN block */
11471129 int condexit;
1148− next(ls); /* skip IF or ELSEIF */
1130+ luaX_next(ls); /* skip IF or ELSEIF */
11491131 condexit = cond(ls);
11501132 checknext(ls, TK_THEN);
11511133 block(ls); /* `then' part */
@@ -1167,7 +1149,7 @@ static void ifstat (LexState *ls, int line) { · 15 unmodified lines
11671149 if (ls->t.token == TK_ELSE) {
11681150 luaK_concat(fs, &escapelist, luaK_jump(fs));
11691151 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) */
11711153 block(ls); /* `else' part */
11721154 }
11731155 else
@@ -1228,7 +1210,7 @@ static void funcstat (LexState *ls, int line) { · 54 unmodified lines
12281210 /* funcstat -> FUNCTION funcname body */
12291211 int needself;
12301212 expdesc v, b;
1231− next(ls); /* skip FUNCTION */
1213+ luaX_next(ls); /* skip FUNCTION */
12321214 needself = funcname(ls, &v);
12331215 body(ls, &b, needself, line);
12341216 luaK_storevar(ls->fs, &v, &b);
@@ -1255,7 +1237,7 @@ static void retstat (LexState *ls) { · 20 unmodified lines
12551237 FuncState *fs = ls->fs;
12561238 expdesc e;
12571239 int first, nret; /* registers with returned values */
1258− next(ls); /* skip RETURN */
1240+ luaX_next(ls); /* skip RETURN */
12591241 if (block_follow(ls->t.token) || ls->t.token == ';')
12601242 first = nret = 0; /* return no values */
12611243 else {
@@ -1295,7 +1277,7 @@ static int statement (LexState *ls) { · 33 unmodified lines
12951277 return 0;
12961278 }
12971279 case TK_DO: { /* stat -> DO block END */
1298− next(ls); /* skip DO */
1280+ luaX_next(ls); /* skip DO */
12991281 block(ls);
13001282 check_match(ls, TK_END, TK_DO, line);
13011283 return 0;
@@ -1313,7 +1295,7 @@ static int statement (LexState *ls) { · 11 unmodified lines
13131295 return 0;
13141296 }
13151297 case TK_LOCAL: { /* stat -> localstat */
1316− next(ls); /* skip LOCAL */
1298+ luaX_next(ls); /* skip LOCAL */
13171299 if (testnext(ls, TK_FUNCTION)) /* local function? */
13181300 localfunc(ls);
13191301 else
@@ -1325,7 +1307,7 @@ static int statement (LexState *ls) { · 5 unmodified lines
13251307 return 1; /* must be last statement */
13261308 }
13271309 case TK_BREAK: { /* stat -> breakstat */
1328− next(ls); /* skip BREAK */
1310+ luaX_next(ls); /* skip BREAK */
13291311 breakstat(ls);
13301312 return 1; /* must be last statement */
13311313 }