diff options
Diffstat (limited to 'lparser.c')
-rw-r--r-- | lparser.c | 80 |
1 files changed, 52 insertions, 28 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.c,v 2.67 2009/09/28 16:32:50 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 2.68 2009/09/30 15:38:37 roberto Exp roberto $ |
3 | ** Lua Parser | 3 | ** Lua Parser |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -27,9 +27,14 @@ | |||
27 | 27 | ||
28 | 28 | ||
29 | 29 | ||
30 | /* maximum number of local variables per function (must be smaller | ||
31 | than 250, due to the bytecode format) */ | ||
32 | #define MAXVARS 200 | ||
33 | |||
34 | |||
30 | #define hasmultret(k) ((k) == VCALL || (k) == VVARARG) | 35 | #define hasmultret(k) ((k) == VCALL || (k) == VVARARG) |
31 | 36 | ||
32 | #define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i].idx]) | 37 | |
33 | 38 | ||
34 | #define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m) | 39 | #define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m) |
35 | 40 | ||
@@ -157,15 +162,26 @@ static int registerlocalvar (LexState *ls, TString *varname) { | |||
157 | } | 162 | } |
158 | 163 | ||
159 | 164 | ||
160 | #define new_localvarliteral(ls,v,n) \ | 165 | #define new_localvarliteral(ls,v) \ |
161 | new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n) | 166 | new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1)) |
162 | 167 | ||
163 | 168 | ||
164 | static void new_localvar (LexState *ls, TString *name, int n) { | 169 | static void new_localvar (LexState *ls, TString *name) { |
165 | FuncState *fs = ls->fs; | 170 | FuncState *fs = ls->fs; |
171 | Varlist *vl = ls->varl; | ||
166 | int reg = registerlocalvar(ls, name); | 172 | int reg = registerlocalvar(ls, name); |
167 | luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables"); | 173 | luaY_checklimit(fs, vl->nactvar + 1 - fs->firstlocal, |
168 | fs->actvar[fs->nactvar+n].idx = cast(unsigned short, reg); | 174 | MAXVARS, "local variables"); |
175 | luaM_growvector(ls->L, vl->actvar, vl->nactvar + 1, | ||
176 | vl->actvarsize, vardesc, MAX_INT, "local variables"); | ||
177 | vl->actvar[vl->nactvar++].idx = cast(unsigned short, reg); | ||
178 | } | ||
179 | |||
180 | |||
181 | static LocVar *getlocvar (FuncState *fs, int i) { | ||
182 | int idx = fs->ls->varl->actvar[fs->firstlocal + i].idx; | ||
183 | lua_assert(idx < fs->nlocvars); | ||
184 | return &fs->f->locvars[idx]; | ||
169 | } | 185 | } |
170 | 186 | ||
171 | 187 | ||
@@ -173,14 +189,15 @@ static void adjustlocalvars (LexState *ls, int nvars) { | |||
173 | FuncState *fs = ls->fs; | 189 | FuncState *fs = ls->fs; |
174 | fs->nactvar = cast_byte(fs->nactvar + nvars); | 190 | fs->nactvar = cast_byte(fs->nactvar + nvars); |
175 | for (; nvars; nvars--) { | 191 | for (; nvars; nvars--) { |
176 | getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc; | 192 | getlocvar(fs, fs->nactvar - nvars)->startpc = fs->pc; |
177 | } | 193 | } |
178 | } | 194 | } |
179 | 195 | ||
180 | 196 | ||
181 | static void removevars (FuncState *fs, int tolevel) { | 197 | static void removevars (FuncState *fs, int tolevel) { |
198 | fs->ls->varl->nactvar -= (fs->nactvar - tolevel); | ||
182 | while (fs->nactvar > tolevel) | 199 | while (fs->nactvar > tolevel) |
183 | getlocvar(fs, --fs->nactvar).endpc = fs->pc; | 200 | getlocvar(fs, --fs->nactvar)->endpc = fs->pc; |
184 | } | 201 | } |
185 | 202 | ||
186 | 203 | ||
@@ -212,7 +229,7 @@ static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { | |||
212 | static int searchvar (FuncState *fs, TString *n) { | 229 | static int searchvar (FuncState *fs, TString *n) { |
213 | int i; | 230 | int i; |
214 | for (i=fs->nactvar-1; i >= 0; i--) { | 231 | for (i=fs->nactvar-1; i >= 0; i--) { |
215 | if (n == getlocvar(fs, i).varname) | 232 | if (n == getlocvar(fs, i)->varname) |
216 | return i; | 233 | return i; |
217 | } | 234 | } |
218 | return -1; /* not found */ | 235 | return -1; /* not found */ |
@@ -350,6 +367,7 @@ static void open_func (LexState *ls, FuncState *fs) { | |||
350 | fs->nups = 0; | 367 | fs->nups = 0; |
351 | fs->nlocvars = 0; | 368 | fs->nlocvars = 0; |
352 | fs->nactvar = 0; | 369 | fs->nactvar = 0; |
370 | fs->firstlocal = ls->varl->nactvar; | ||
353 | fs->envreg = NO_REG; | 371 | fs->envreg = NO_REG; |
354 | fs->bl = NULL; | 372 | fs->bl = NULL; |
355 | fs->h = luaH_new(L); | 373 | fs->h = luaH_new(L); |
@@ -392,13 +410,15 @@ static void close_func (LexState *ls) { | |||
392 | } | 410 | } |
393 | 411 | ||
394 | 412 | ||
395 | Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { | 413 | Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Varlist *varl, |
414 | const char *name) { | ||
396 | struct LexState lexstate; | 415 | struct LexState lexstate; |
397 | struct FuncState funcstate; | 416 | struct FuncState funcstate; |
398 | TString *tname = luaS_new(L, name); | 417 | TString *tname = luaS_new(L, name); |
399 | setsvalue2s(L, L->top, tname); /* protect name */ | 418 | setsvalue2s(L, L->top, tname); /* protect name */ |
400 | incr_top(L); | 419 | incr_top(L); |
401 | lexstate.buff = buff; | 420 | lexstate.buff = buff; |
421 | lexstate.varl = varl; | ||
402 | luaX_setinput(L, &lexstate, z, tname); | 422 | luaX_setinput(L, &lexstate, z, tname); |
403 | open_func(&lexstate, &funcstate); | 423 | open_func(&lexstate, &funcstate); |
404 | funcstate.f->is_vararg = 1; /* main func. is always vararg */ | 424 | funcstate.f->is_vararg = 1; /* main func. is always vararg */ |
@@ -565,7 +585,8 @@ static void parlist (LexState *ls) { | |||
565 | do { | 585 | do { |
566 | switch (ls->t.token) { | 586 | switch (ls->t.token) { |
567 | case TK_NAME: { /* param -> NAME */ | 587 | case TK_NAME: { /* param -> NAME */ |
568 | new_localvar(ls, str_checkname(ls), nparams++); | 588 | new_localvar(ls, str_checkname(ls)); |
589 | nparams++; | ||
569 | break; | 590 | break; |
570 | } | 591 | } |
571 | case TK_DOTS: { /* param -> `...' */ | 592 | case TK_DOTS: { /* param -> `...' */ |
@@ -590,7 +611,7 @@ static void body (LexState *ls, expdesc *e, int needself, int line) { | |||
590 | new_fs.f->linedefined = line; | 611 | new_fs.f->linedefined = line; |
591 | checknext(ls, '('); | 612 | checknext(ls, '('); |
592 | if (needself) { | 613 | if (needself) { |
593 | new_localvarliteral(ls, "self", 0); | 614 | new_localvarliteral(ls, "self"); |
594 | adjustlocalvars(ls, 1); | 615 | adjustlocalvars(ls, 1); |
595 | } | 616 | } |
596 | parlist(ls); | 617 | parlist(ls); |
@@ -1083,10 +1104,10 @@ static void fornum (LexState *ls, TString *varname, int line) { | |||
1083 | /* fornum -> NAME = exp1,exp1[,exp1] forbody */ | 1104 | /* fornum -> NAME = exp1,exp1[,exp1] forbody */ |
1084 | FuncState *fs = ls->fs; | 1105 | FuncState *fs = ls->fs; |
1085 | int base = fs->freereg; | 1106 | int base = fs->freereg; |
1086 | new_localvarliteral(ls, "(for index)", 0); | 1107 | new_localvarliteral(ls, "(for index)"); |
1087 | new_localvarliteral(ls, "(for limit)", 1); | 1108 | new_localvarliteral(ls, "(for limit)"); |
1088 | new_localvarliteral(ls, "(for step)", 2); | 1109 | new_localvarliteral(ls, "(for step)"); |
1089 | new_localvar(ls, varname, 3); | 1110 | new_localvar(ls, varname); |
1090 | checknext(ls, '='); | 1111 | checknext(ls, '='); |
1091 | exp1(ls); /* initial value */ | 1112 | exp1(ls); /* initial value */ |
1092 | checknext(ls, ','); | 1113 | checknext(ls, ','); |
@@ -1105,17 +1126,19 @@ static void forlist (LexState *ls, TString *indexname) { | |||
1105 | /* forlist -> NAME {,NAME} IN explist1 forbody */ | 1126 | /* forlist -> NAME {,NAME} IN explist1 forbody */ |
1106 | FuncState *fs = ls->fs; | 1127 | FuncState *fs = ls->fs; |
1107 | expdesc e; | 1128 | expdesc e; |
1108 | int nvars = 0; | 1129 | int nvars = 4; /* gen, state, control, plus at least one declared var */ |
1109 | int line; | 1130 | int line; |
1110 | int base = fs->freereg; | 1131 | int base = fs->freereg; |
1111 | /* create control variables */ | 1132 | /* create control variables */ |
1112 | new_localvarliteral(ls, "(for generator)", nvars++); | 1133 | new_localvarliteral(ls, "(for generator)"); |
1113 | new_localvarliteral(ls, "(for state)", nvars++); | 1134 | new_localvarliteral(ls, "(for state)"); |
1114 | new_localvarliteral(ls, "(for control)", nvars++); | 1135 | new_localvarliteral(ls, "(for control)"); |
1115 | /* create declared variables */ | 1136 | /* create declared variables */ |
1116 | new_localvar(ls, indexname, nvars++); | 1137 | new_localvar(ls, indexname); |
1117 | while (testnext(ls, ',')) | 1138 | while (testnext(ls, ',')) { |
1118 | new_localvar(ls, str_checkname(ls), nvars++); | 1139 | new_localvar(ls, str_checkname(ls)); |
1140 | nvars++; | ||
1141 | } | ||
1119 | checknext(ls, TK_IN); | 1142 | checknext(ls, TK_IN); |
1120 | line = ls->linenumber; | 1143 | line = ls->linenumber; |
1121 | adjust_assign(ls, 3, explist1(ls, &e), &e); | 1144 | adjust_assign(ls, 3, explist1(ls, &e), &e); |
@@ -1180,14 +1203,14 @@ static void ifstat (LexState *ls, int line) { | |||
1180 | static void localfunc (LexState *ls) { | 1203 | static void localfunc (LexState *ls) { |
1181 | expdesc v, b; | 1204 | expdesc v, b; |
1182 | FuncState *fs = ls->fs; | 1205 | FuncState *fs = ls->fs; |
1183 | new_localvar(ls, str_checkname(ls), 0); | 1206 | new_localvar(ls, str_checkname(ls)); |
1184 | init_exp(&v, VLOCAL, fs->freereg); | 1207 | init_exp(&v, VLOCAL, fs->freereg); |
1185 | luaK_reserveregs(fs, 1); | 1208 | luaK_reserveregs(fs, 1); |
1186 | adjustlocalvars(ls, 1); | 1209 | adjustlocalvars(ls, 1); |
1187 | body(ls, &b, 0, ls->linenumber); | 1210 | body(ls, &b, 0, ls->linenumber); |
1188 | luaK_storevar(fs, &v, &b); | 1211 | luaK_storevar(fs, &v, &b); |
1189 | /* debug information will only see the variable after this point! */ | 1212 | /* debug information will only see the variable after this point! */ |
1190 | getlocvar(fs, fs->nactvar - 1).startpc = fs->pc; | 1213 | getlocvar(fs, fs->nactvar - 1)->startpc = fs->pc; |
1191 | } | 1214 | } |
1192 | 1215 | ||
1193 | 1216 | ||
@@ -1197,7 +1220,8 @@ static void localstat (LexState *ls) { | |||
1197 | int nexps; | 1220 | int nexps; |
1198 | expdesc e; | 1221 | expdesc e; |
1199 | do { | 1222 | do { |
1200 | new_localvar(ls, str_checkname(ls), nvars++); | 1223 | new_localvar(ls, str_checkname(ls)); |
1224 | nvars++; | ||
1201 | } while (testnext(ls, ',')); | 1225 | } while (testnext(ls, ',')); |
1202 | if (testnext(ls, '=')) | 1226 | if (testnext(ls, '=')) |
1203 | nexps = explist1(ls, &e); | 1227 | nexps = explist1(ls, &e); |
@@ -1243,7 +1267,7 @@ static void instat (LexState *ls, int line) { | |||
1243 | BlockCnt bl; | 1267 | BlockCnt bl; |
1244 | luaX_next(ls); /* skip IN */ | 1268 | luaX_next(ls); /* skip IN */ |
1245 | enterblock(fs, &bl, 0); /* scope for environment variable */ | 1269 | enterblock(fs, &bl, 0); /* scope for environment variable */ |
1246 | new_localvarliteral(ls, "(environment)", 0); | 1270 | new_localvarliteral(ls, "(environment)"); |
1247 | fs->envreg = exp1(ls); /* new environment */ | 1271 | fs->envreg = exp1(ls); /* new environment */ |
1248 | adjustlocalvars(ls, 1); | 1272 | adjustlocalvars(ls, 1); |
1249 | checknext(ls, TK_DO); | 1273 | checknext(ls, TK_DO); |