diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-06 10:52:37 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-06 10:52:37 -0300 |
| commit | cff22f57ddb6ff65edc7d8bac323f8c7af10c668 (patch) | |
| tree | c18718889622c1a45af8b3f67a899ba30b545925 | |
| parent | 168ea16acb62231d56147b38edeb8c2aca4ed2f8 (diff) | |
| download | lua-cff22f57ddb6ff65edc7d8bac323f8c7af10c668.tar.gz lua-cff22f57ddb6ff65edc7d8bac323f8c7af10c668.tar.bz2 lua-cff22f57ddb6ff65edc7d8bac323f8c7af10c668.zip | |
some refactoring
| -rw-r--r-- | lparser.c | 38 |
1 files changed, 18 insertions, 20 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lparser.c,v 1.185 2002/06/03 14:09:57 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 1.186 2002/06/06 13:16:02 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 | */ |
| @@ -107,8 +107,11 @@ static void check_match (LexState *ls, int what, int who, int where) { | |||
| 107 | 107 | ||
| 108 | 108 | ||
| 109 | static TString *str_checkname (LexState *ls) { | 109 | static TString *str_checkname (LexState *ls) { |
| 110 | TString *ts; | ||
| 110 | check_condition(ls, (ls->t.token == TK_NAME), "<name> expected"); | 111 | check_condition(ls, (ls->t.token == TK_NAME), "<name> expected"); |
| 111 | return ls->t.seminfo.ts; | 112 | ts = ls->t.seminfo.ts; |
| 113 | next(ls); | ||
| 114 | return ts; | ||
| 112 | } | 115 | } |
| 113 | 116 | ||
| 114 | 117 | ||
| @@ -126,7 +129,6 @@ static void codestring (LexState *ls, expdesc *e, TString *s) { | |||
| 126 | 129 | ||
| 127 | static void checkname(LexState *ls, expdesc *e) { | 130 | static void checkname(LexState *ls, expdesc *e) { |
| 128 | codestring(ls, e, str_checkname(ls)); | 131 | codestring(ls, e, str_checkname(ls)); |
| 129 | next(ls); | ||
| 130 | } | 132 | } |
| 131 | 133 | ||
| 132 | 134 | ||
| @@ -204,7 +206,7 @@ static void markupval (FuncState *fs, int level) { | |||
| 204 | } | 206 | } |
| 205 | 207 | ||
| 206 | 208 | ||
| 207 | static void singlevar (FuncState *fs, TString *n, expdesc *var, int base) { | 209 | static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { |
| 208 | if (fs == NULL) /* no more levels? */ | 210 | if (fs == NULL) /* no more levels? */ |
| 209 | init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ | 211 | init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ |
| 210 | else { | 212 | else { |
| @@ -215,7 +217,7 @@ static void singlevar (FuncState *fs, TString *n, expdesc *var, int base) { | |||
| 215 | markupval(fs, v); /* local will be used as an upval */ | 217 | markupval(fs, v); /* local will be used as an upval */ |
| 216 | } | 218 | } |
| 217 | else { /* not found at current level; try upper one */ | 219 | else { /* not found at current level; try upper one */ |
| 218 | singlevar(fs->prev, n, var, 0); | 220 | singlevaraux(fs->prev, n, var, 0); |
| 219 | if (var->k == VGLOBAL) { | 221 | if (var->k == VGLOBAL) { |
| 220 | if (base) | 222 | if (base) |
| 221 | var->info = luaK_stringK(fs, n); /* info points to global name */ | 223 | var->info = luaK_stringK(fs, n); /* info points to global name */ |
| @@ -229,6 +231,11 @@ static void singlevar (FuncState *fs, TString *n, expdesc *var, int base) { | |||
| 229 | } | 231 | } |
| 230 | 232 | ||
| 231 | 233 | ||
| 234 | static void singlevar (LexState *ls, expdesc *var, int base) { | ||
| 235 | singlevaraux(ls->fs, str_checkname(ls), var, base); | ||
| 236 | } | ||
| 237 | |||
| 238 | |||
| 232 | static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { | 239 | static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { |
| 233 | FuncState *fs = ls->fs; | 240 | FuncState *fs = ls->fs; |
| 234 | int extra = nvars - nexps; | 241 | int extra = nvars - nexps; |
| @@ -597,15 +604,13 @@ static void prefixexp (LexState *ls, expdesc *v) { | |||
| 597 | return; | 604 | return; |
| 598 | } | 605 | } |
| 599 | case TK_NAME: { | 606 | case TK_NAME: { |
| 600 | singlevar(ls->fs, str_checkname(ls), v, 1); | 607 | singlevar(ls, v, 1); |
| 601 | next(ls); | ||
| 602 | return; | 608 | return; |
| 603 | } | 609 | } |
| 604 | case '%': { /* for compatibility only */ | 610 | case '%': { /* for compatibility only */ |
| 605 | next(ls); /* skip `%' */ | 611 | next(ls); /* skip `%' */ |
| 606 | singlevar(ls->fs, str_checkname(ls), v, 1); | 612 | singlevar(ls, v, 1); |
| 607 | check_condition(ls, v->k == VUPVAL, "global upvalues are obsolete"); | 613 | check_condition(ls, v->k == VUPVAL, "global upvalues are obsolete"); |
| 608 | next(ls); | ||
| 609 | return; | 614 | return; |
| 610 | } | 615 | } |
| 611 | default: { | 616 | default: { |
| @@ -1018,10 +1023,8 @@ static void forlist (LexState *ls, TString *indexname) { | |||
| 1018 | new_localvarstr(ls, "(for generator)", nvars++); | 1023 | new_localvarstr(ls, "(for generator)", nvars++); |
| 1019 | new_localvarstr(ls, "(for state)", nvars++); | 1024 | new_localvarstr(ls, "(for state)", nvars++); |
| 1020 | new_localvar(ls, indexname, nvars++); | 1025 | new_localvar(ls, indexname, nvars++); |
| 1021 | while (testnext(ls, ',')) { | 1026 | while (testnext(ls, ',')) |
| 1022 | new_localvar(ls, str_checkname(ls), nvars++); | 1027 | new_localvar(ls, str_checkname(ls), nvars++); |
| 1023 | next(ls); | ||
| 1024 | } | ||
| 1025 | check(ls, TK_IN); | 1028 | check(ls, TK_IN); |
| 1026 | line = ls->linenumber; | 1029 | line = ls->linenumber; |
| 1027 | adjust_assign(ls, 3, explist1(ls, &e), &e); | 1030 | adjust_assign(ls, 3, explist1(ls, &e), &e); |
| @@ -1047,7 +1050,6 @@ static void forstat (LexState *ls, int line) { | |||
| 1047 | enterblock(fs, &bl, 1); | 1050 | enterblock(fs, &bl, 1); |
| 1048 | next(ls); /* skip `for' */ | 1051 | next(ls); /* skip `for' */ |
| 1049 | varname = str_checkname(ls); /* first variable name */ | 1052 | varname = str_checkname(ls); /* first variable name */ |
| 1050 | next(ls); /* skip var name */ | ||
| 1051 | switch (ls->t.token) { | 1053 | switch (ls->t.token) { |
| 1052 | case '=': fornum(ls, varname, line); break; | 1054 | case '=': fornum(ls, varname, line); break; |
| 1053 | case ',': case TK_IN: forlist(ls, varname); break; | 1055 | case ',': case TK_IN: forlist(ls, varname); break; |
| @@ -1099,7 +1101,6 @@ static void localstat (LexState *ls) { | |||
| 1099 | next(ls); /* skip LOCAL */ | 1101 | next(ls); /* skip LOCAL */ |
| 1100 | do { | 1102 | do { |
| 1101 | new_localvar(ls, str_checkname(ls), nvars++); | 1103 | new_localvar(ls, str_checkname(ls), nvars++); |
| 1102 | next(ls); /* skip var name */ | ||
| 1103 | } while (testnext(ls, ',')); | 1104 | } while (testnext(ls, ',')); |
| 1104 | if (testnext(ls, '=')) | 1105 | if (testnext(ls, '=')) |
| 1105 | nexps = explist1(ls, &e); | 1106 | nexps = explist1(ls, &e); |
| @@ -1115,11 +1116,9 @@ static void localstat (LexState *ls) { | |||
| 1115 | static int funcname (LexState *ls, expdesc *v) { | 1116 | static int funcname (LexState *ls, expdesc *v) { |
| 1116 | /* funcname -> NAME {field} [`:' NAME] */ | 1117 | /* funcname -> NAME {field} [`:' NAME] */ |
| 1117 | int needself = 0; | 1118 | int needself = 0; |
| 1118 | singlevar(ls->fs, str_checkname(ls), v, 1); | 1119 | singlevar(ls, v, 1); |
| 1119 | next(ls); /* skip var name */ | 1120 | while (ls->t.token == '.') |
| 1120 | while (ls->t.token == '.') { | ||
| 1121 | luaY_field(ls, v); | 1121 | luaY_field(ls, v); |
| 1122 | } | ||
| 1123 | if (ls->t.token == ':') { | 1122 | if (ls->t.token == ':') { |
| 1124 | needself = 1; | 1123 | needself = 1; |
| 1125 | luaY_field(ls, v); | 1124 | luaY_field(ls, v); |
| @@ -1261,11 +1260,10 @@ static void parlist (LexState *ls) { | |||
| 1261 | if (ls->t.token != ')') { /* is `parlist' not empty? */ | 1260 | if (ls->t.token != ')') { /* is `parlist' not empty? */ |
| 1262 | do { | 1261 | do { |
| 1263 | switch (ls->t.token) { | 1262 | switch (ls->t.token) { |
| 1264 | case TK_DOTS: dots = 1; break; | 1263 | case TK_DOTS: dots = 1; next(ls); break; |
| 1265 | case TK_NAME: new_localvar(ls, str_checkname(ls), nparams++); break; | 1264 | case TK_NAME: new_localvar(ls, str_checkname(ls), nparams++); break; |
| 1266 | default: luaX_syntaxerror(ls, "<name> or `...' expected"); | 1265 | default: luaX_syntaxerror(ls, "<name> or `...' expected"); |
| 1267 | } | 1266 | } |
| 1268 | next(ls); | ||
| 1269 | } while (!dots && testnext(ls, ',')); | 1267 | } while (!dots && testnext(ls, ',')); |
| 1270 | } | 1268 | } |
| 1271 | code_params(ls, nparams, dots); | 1269 | code_params(ls, nparams, dots); |
