diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-12-07 16:31:16 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-12-07 16:31:16 -0200 |
| commit | 70751dd27c4225955fe018d6829ea45e3e66a51c (patch) | |
| tree | f190cfa201bcb793247463e948e8ed0de8b9994b | |
| parent | 39a8082f50c7321d75425f08a551a1d331dcea2d (diff) | |
| download | lua-70751dd27c4225955fe018d6829ea45e3e66a51c.tar.gz lua-70751dd27c4225955fe018d6829ea45e3e66a51c.tar.bz2 lua-70751dd27c4225955fe018d6829ea45e3e66a51c.zip | |
details
| -rw-r--r-- | lparser.c | 37 | ||||
| -rw-r--r-- | ltablib.c | 30 |
2 files changed, 36 insertions, 31 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lparser.c,v 2.9 2004/12/03 20:44:19 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 2.10 2004/12/03 20:50:25 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 | */ |
| @@ -109,10 +109,15 @@ static int testnext (LexState *ls, int c) { | |||
| 109 | 109 | ||
| 110 | 110 | ||
| 111 | static void check (LexState *ls, int c) { | 111 | static void check (LexState *ls, int c) { |
| 112 | if (!testnext(ls, c)) | 112 | if (ls->t.token != c) |
| 113 | error_expected(ls, c); | 113 | error_expected(ls, c); |
| 114 | } | 114 | } |
| 115 | 115 | ||
| 116 | static void checknext (LexState *ls, int c) { | ||
| 117 | check(ls, c); | ||
| 118 | next(ls); | ||
| 119 | } | ||
| 120 | |||
| 116 | 121 | ||
| 117 | #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } | 122 | #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } |
| 118 | 123 | ||
| @@ -133,7 +138,7 @@ static void check_match (LexState *ls, int what, int who, int where) { | |||
| 133 | 138 | ||
| 134 | static TString *str_checkname (LexState *ls) { | 139 | static TString *str_checkname (LexState *ls) { |
| 135 | TString *ts; | 140 | TString *ts; |
| 136 | if (ls->t.token != TK_NAME) error_expected(ls, TK_NAME); | 141 | check(ls, TK_NAME); |
| 137 | ts = ls->t.seminfo.ts; | 142 | ts = ls->t.seminfo.ts; |
| 138 | next(ls); | 143 | next(ls); |
| 139 | return ts; | 144 | return ts; |
| @@ -396,7 +401,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { | |||
| 396 | funcstate.f->is_vararg = NEWSTYLEVARARG; | 401 | funcstate.f->is_vararg = NEWSTYLEVARARG; |
| 397 | next(&lexstate); /* read first token */ | 402 | next(&lexstate); /* read first token */ |
| 398 | chunk(&lexstate); | 403 | chunk(&lexstate); |
| 399 | check_condition(&lexstate, (lexstate.t.token == TK_EOS), "<eof> expected"); | 404 | check(&lexstate, TK_EOS); |
| 400 | close_func(&lexstate); | 405 | close_func(&lexstate); |
| 401 | lua_assert(funcstate.prev == NULL); | 406 | lua_assert(funcstate.prev == NULL); |
| 402 | lua_assert(funcstate.f->nups == 0); | 407 | lua_assert(funcstate.f->nups == 0); |
| @@ -428,7 +433,7 @@ static void yindex (LexState *ls, expdesc *v) { | |||
| 428 | next(ls); /* skip the '[' */ | 433 | next(ls); /* skip the '[' */ |
| 429 | expr(ls, v); | 434 | expr(ls, v); |
| 430 | luaK_exp2val(ls->fs, v); | 435 | luaK_exp2val(ls->fs, v); |
| 431 | check(ls, ']'); | 436 | checknext(ls, ']'); |
| 432 | } | 437 | } |
| 433 | 438 | ||
| 434 | 439 | ||
| @@ -460,7 +465,7 @@ static void recfield (LexState *ls, struct ConsControl *cc) { | |||
| 460 | } | 465 | } |
| 461 | else /* ls->t.token == '[' */ | 466 | else /* ls->t.token == '[' */ |
| 462 | yindex(ls, &key); | 467 | yindex(ls, &key); |
| 463 | check(ls, '='); | 468 | checknext(ls, '='); |
| 464 | luaK_exp2RK(fs, &key); | 469 | luaK_exp2RK(fs, &key); |
| 465 | expr(ls, &val); | 470 | expr(ls, &val); |
| 466 | luaK_codeABC(fs, OP_SETTABLE, cc->t->info, luaK_exp2RK(fs, &key), | 471 | luaK_codeABC(fs, OP_SETTABLE, cc->t->info, luaK_exp2RK(fs, &key), |
| @@ -514,7 +519,7 @@ static void constructor (LexState *ls, expdesc *t) { | |||
| 514 | init_exp(t, VRELOCABLE, pc); | 519 | init_exp(t, VRELOCABLE, pc); |
| 515 | init_exp(&cc.v, VVOID, 0); /* no value (yet) */ | 520 | init_exp(&cc.v, VVOID, 0); /* no value (yet) */ |
| 516 | luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ | 521 | luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ |
| 517 | check(ls, '{'); | 522 | checknext(ls, '{'); |
| 518 | do { | 523 | do { |
| 519 | lua_assert(cc.v.k == VVOID || cc.tostore > 0); | 524 | lua_assert(cc.v.k == VVOID || cc.tostore > 0); |
| 520 | testnext(ls, ';'); /* compatibility only */ | 525 | testnext(ls, ';'); /* compatibility only */ |
| @@ -584,13 +589,13 @@ static void body (LexState *ls, expdesc *e, int needself, int line) { | |||
| 584 | FuncState new_fs; | 589 | FuncState new_fs; |
| 585 | open_func(ls, &new_fs); | 590 | open_func(ls, &new_fs); |
| 586 | new_fs.f->lineDefined = line; | 591 | new_fs.f->lineDefined = line; |
| 587 | check(ls, '('); | 592 | checknext(ls, '('); |
| 588 | if (needself) { | 593 | if (needself) { |
| 589 | new_localvarliteral(ls, "self", 0); | 594 | new_localvarliteral(ls, "self", 0); |
| 590 | adjustlocalvars(ls, 1); | 595 | adjustlocalvars(ls, 1); |
| 591 | } | 596 | } |
| 592 | parlist(ls); | 597 | parlist(ls); |
| 593 | check(ls, ')'); | 598 | checknext(ls, ')'); |
| 594 | chunk(ls); | 599 | chunk(ls); |
| 595 | check_match(ls, TK_END, TK_FUNCTION, line); | 600 | check_match(ls, TK_END, TK_FUNCTION, line); |
| 596 | close_func(ls); | 601 | close_func(ls); |
| @@ -944,7 +949,7 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { | |||
| 944 | } | 949 | } |
| 945 | else { /* assignment -> `=' explist1 */ | 950 | else { /* assignment -> `=' explist1 */ |
| 946 | int nexps; | 951 | int nexps; |
| 947 | check(ls, '='); | 952 | checknext(ls, '='); |
| 948 | nexps = explist1(ls, &e); | 953 | nexps = explist1(ls, &e); |
| 949 | if (nexps != nvars) { | 954 | if (nexps != nvars) { |
| 950 | adjust_assign(ls, nvars, nexps, &e); | 955 | adjust_assign(ls, nvars, nexps, &e); |
| @@ -1011,7 +1016,7 @@ static void whilestat (LexState *ls, int line) { | |||
| 1011 | codeexp[i] = fs->f->code[expinit + i]; | 1016 | codeexp[i] = fs->f->code[expinit + i]; |
| 1012 | fs->pc = expinit; /* remove `exp' code */ | 1017 | fs->pc = expinit; /* remove `exp' code */ |
| 1013 | enterblock(fs, &bl, 1); | 1018 | enterblock(fs, &bl, 1); |
| 1014 | check(ls, TK_DO); | 1019 | checknext(ls, TK_DO); |
| 1015 | blockinit = luaK_getlabel(fs); | 1020 | blockinit = luaK_getlabel(fs); |
| 1016 | block(ls); | 1021 | block(ls); |
| 1017 | luaK_patchtohere(fs, whileinit); /* initial jump jumps to here */ | 1022 | luaK_patchtohere(fs, whileinit); /* initial jump jumps to here */ |
| @@ -1059,7 +1064,7 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { | |||
| 1059 | FuncState *fs = ls->fs; | 1064 | FuncState *fs = ls->fs; |
| 1060 | int prep, endfor; | 1065 | int prep, endfor; |
| 1061 | adjustlocalvars(ls, 3); /* control variables */ | 1066 | adjustlocalvars(ls, 3); /* control variables */ |
| 1062 | check(ls, TK_DO); | 1067 | checknext(ls, TK_DO); |
| 1063 | prep = luaK_codeAsBx(fs, (isnum ? OP_FORPREP : OP_TFORPREP), base, NO_JUMP); | 1068 | prep = luaK_codeAsBx(fs, (isnum ? OP_FORPREP : OP_TFORPREP), base, NO_JUMP); |
| 1064 | enterblock(fs, &bl, 0); /* scope for declared variables */ | 1069 | enterblock(fs, &bl, 0); /* scope for declared variables */ |
| 1065 | adjustlocalvars(ls, nvars); | 1070 | adjustlocalvars(ls, nvars); |
| @@ -1082,9 +1087,9 @@ static void fornum (LexState *ls, TString *varname, int line) { | |||
| 1082 | new_localvarliteral(ls, "(for limit)", 1); | 1087 | new_localvarliteral(ls, "(for limit)", 1); |
| 1083 | new_localvarliteral(ls, "(for step)", 2); | 1088 | new_localvarliteral(ls, "(for step)", 2); |
| 1084 | new_localvar(ls, varname, 3); | 1089 | new_localvar(ls, varname, 3); |
| 1085 | check(ls, '='); | 1090 | checknext(ls, '='); |
| 1086 | exp1(ls); /* initial value */ | 1091 | exp1(ls); /* initial value */ |
| 1087 | check(ls, ','); | 1092 | checknext(ls, ','); |
| 1088 | exp1(ls); /* limit */ | 1093 | exp1(ls); /* limit */ |
| 1089 | if (testnext(ls, ',')) | 1094 | if (testnext(ls, ',')) |
| 1090 | exp1(ls); /* optional step */ | 1095 | exp1(ls); /* optional step */ |
| @@ -1111,7 +1116,7 @@ static void forlist (LexState *ls, TString *indexname) { | |||
| 1111 | new_localvar(ls, indexname, nvars++); | 1116 | new_localvar(ls, indexname, nvars++); |
| 1112 | while (testnext(ls, ',')) | 1117 | while (testnext(ls, ',')) |
| 1113 | new_localvar(ls, str_checkname(ls), nvars++); | 1118 | new_localvar(ls, str_checkname(ls), nvars++); |
| 1114 | check(ls, TK_IN); | 1119 | checknext(ls, TK_IN); |
| 1115 | line = ls->linenumber; | 1120 | line = ls->linenumber; |
| 1116 | adjust_assign(ls, 3, explist1(ls, &e), &e); | 1121 | adjust_assign(ls, 3, explist1(ls, &e), &e); |
| 1117 | luaK_checkstack(fs, 3); /* extra space to call generator */ | 1122 | luaK_checkstack(fs, 3); /* extra space to call generator */ |
| @@ -1142,7 +1147,7 @@ static int test_then_block (LexState *ls) { | |||
| 1142 | int flist; | 1147 | int flist; |
| 1143 | next(ls); /* skip IF or ELSEIF */ | 1148 | next(ls); /* skip IF or ELSEIF */ |
| 1144 | flist = cond(ls); | 1149 | flist = cond(ls); |
| 1145 | check(ls, TK_THEN); | 1150 | checknext(ls, TK_THEN); |
| 1146 | block(ls); /* `then' part */ | 1151 | block(ls); /* `then' part */ |
| 1147 | return flist; | 1152 | return flist; |
| 1148 | } | 1153 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltablib.c,v 1.25 2004/05/10 18:06:14 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.26 2004/06/15 13:37:21 roberto Exp roberto $ |
| 3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -19,7 +19,7 @@ | |||
| 19 | #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n)) | 19 | #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n)) |
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | static int luaB_foreachi (lua_State *L) { | 22 | static int foreachi (lua_State *L) { |
| 23 | int i; | 23 | int i; |
| 24 | int n = aux_getn(L, 1); | 24 | int n = aux_getn(L, 1); |
| 25 | luaL_checktype(L, 2, LUA_TFUNCTION); | 25 | luaL_checktype(L, 2, LUA_TFUNCTION); |
| @@ -36,7 +36,7 @@ static int luaB_foreachi (lua_State *L) { | |||
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | 38 | ||
| 39 | static int luaB_foreach (lua_State *L) { | 39 | static int foreach (lua_State *L) { |
| 40 | luaL_checktype(L, 1, LUA_TTABLE); | 40 | luaL_checktype(L, 1, LUA_TTABLE); |
| 41 | luaL_checktype(L, 2, LUA_TFUNCTION); | 41 | luaL_checktype(L, 2, LUA_TFUNCTION); |
| 42 | lua_pushnil(L); /* first key */ | 42 | lua_pushnil(L); /* first key */ |
| @@ -54,13 +54,13 @@ static int luaB_foreach (lua_State *L) { | |||
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | 56 | ||
| 57 | static int luaB_getn (lua_State *L) { | 57 | static int getn (lua_State *L) { |
| 58 | lua_pushinteger(L, aux_getn(L, 1)); | 58 | lua_pushinteger(L, aux_getn(L, 1)); |
| 59 | return 1; | 59 | return 1; |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | 62 | ||
| 63 | static int luaB_setn (lua_State *L) { | 63 | static int setn (lua_State *L) { |
| 64 | luaL_checktype(L, 1, LUA_TTABLE); | 64 | luaL_checktype(L, 1, LUA_TTABLE); |
| 65 | luaL_setn(L, 1, luaL_checkint(L, 2)); | 65 | luaL_setn(L, 1, luaL_checkint(L, 2)); |
| 66 | lua_pushvalue(L, 1); | 66 | lua_pushvalue(L, 1); |
| @@ -68,7 +68,7 @@ static int luaB_setn (lua_State *L) { | |||
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | 70 | ||
| 71 | static int luaB_tinsert (lua_State *L) { | 71 | static int tinsert (lua_State *L) { |
| 72 | int v = lua_gettop(L); /* number of arguments */ | 72 | int v = lua_gettop(L); /* number of arguments */ |
| 73 | int e = aux_getn(L, 1) + LUA_FIRSTINDEX; /* first empty element */ | 73 | int e = aux_getn(L, 1) + LUA_FIRSTINDEX; /* first empty element */ |
| 74 | int pos; /* where to insert new element */ | 74 | int pos; /* where to insert new element */ |
| @@ -90,7 +90,7 @@ static int luaB_tinsert (lua_State *L) { | |||
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | 92 | ||
| 93 | static int luaB_tremove (lua_State *L) { | 93 | static int tremove (lua_State *L) { |
| 94 | int e = aux_getn(L, 1) + LUA_FIRSTINDEX - 1; | 94 | int e = aux_getn(L, 1) + LUA_FIRSTINDEX - 1; |
| 95 | int pos = luaL_optint(L, 2, e); | 95 | int pos = luaL_optint(L, 2, e); |
| 96 | if (e < LUA_FIRSTINDEX) return 0; /* table is `empty' */ | 96 | if (e < LUA_FIRSTINDEX) return 0; /* table is `empty' */ |
| @@ -220,7 +220,7 @@ static void auxsort (lua_State *L, int l, int u) { | |||
| 220 | } /* repeat the routine for the larger one */ | 220 | } /* repeat the routine for the larger one */ |
| 221 | } | 221 | } |
| 222 | 222 | ||
| 223 | static int luaB_sort (lua_State *L) { | 223 | static int sort (lua_State *L) { |
| 224 | int n = aux_getn(L, 1); | 224 | int n = aux_getn(L, 1); |
| 225 | luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ | 225 | luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ |
| 226 | if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ | 226 | if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ |
| @@ -235,13 +235,13 @@ static int luaB_sort (lua_State *L) { | |||
| 235 | 235 | ||
| 236 | static const luaL_reg tab_funcs[] = { | 236 | static const luaL_reg tab_funcs[] = { |
| 237 | {"concat", str_concat}, | 237 | {"concat", str_concat}, |
| 238 | {"foreach", luaB_foreach}, | 238 | {"foreach", foreach}, |
| 239 | {"foreachi", luaB_foreachi}, | 239 | {"foreachi", foreachi}, |
| 240 | {"getn", luaB_getn}, | 240 | {"getn", getn}, |
| 241 | {"setn", luaB_setn}, | 241 | {"setn", setn}, |
| 242 | {"sort", luaB_sort}, | 242 | {"sort", sort}, |
| 243 | {"insert", luaB_tinsert}, | 243 | {"insert", tinsert}, |
| 244 | {"remove", luaB_tremove}, | 244 | {"remove", tremove}, |
| 245 | {NULL, NULL} | 245 | {NULL, NULL} |
| 246 | }; | 246 | }; |
| 247 | 247 | ||
