diff options
-rw-r--r-- | lparser.c | 49 | ||||
-rw-r--r-- | lparser.h | 12 |
2 files changed, 36 insertions, 25 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.c,v 1.210 2003/05/14 12:32:46 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 1.211 2003/05/14 21:02:39 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 | */ |
@@ -39,9 +39,9 @@ | |||
39 | typedef struct BlockCnt { | 39 | typedef struct BlockCnt { |
40 | struct BlockCnt *previous; /* chain */ | 40 | struct BlockCnt *previous; /* chain */ |
41 | int breaklist; /* list of jumps out of this loop */ | 41 | int breaklist; /* list of jumps out of this loop */ |
42 | int nactvar; /* # active local variables outside the breakable structure */ | 42 | lu_byte nactvar; /* # active locals outside the breakable structure */ |
43 | int upval; /* true if some variable in the block is an upvalue */ | 43 | lu_byte upval; /* true if some variable in the block is an upvalue */ |
44 | int isbreakable; /* true if `block' is a loop */ | 44 | lu_byte isbreakable; /* true if `block' is a loop */ |
45 | } BlockCnt; | 45 | } BlockCnt; |
46 | 46 | ||
47 | 47 | ||
@@ -187,7 +187,8 @@ static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { | |||
187 | luaM_growvector(fs->L, fs->f->upvalues, f->nups, fs->f->sizeupvalues, | 187 | luaM_growvector(fs->L, fs->f->upvalues, f->nups, fs->f->sizeupvalues, |
188 | TString *, MAX_INT, ""); | 188 | TString *, MAX_INT, ""); |
189 | fs->f->upvalues[f->nups] = name; | 189 | fs->f->upvalues[f->nups] = name; |
190 | fs->upvalues[f->nups] = *v; | 190 | fs->upvalues[f->nups].k = v->k; |
191 | fs->upvalues[f->nups].info = v->info; | ||
191 | return f->nups++; | 192 | return f->nups++; |
192 | } | 193 | } |
193 | 194 | ||
@@ -266,7 +267,7 @@ static void code_params (LexState *ls, int nparams, TString *dots) { | |||
266 | Proto *f = fs->f; | 267 | Proto *f = fs->f; |
267 | adjustlocalvars(ls, nparams); | 268 | adjustlocalvars(ls, nparams); |
268 | luaX_checklimit(ls, fs->nactvar, MAXPARAMS, "parameters"); | 269 | luaX_checklimit(ls, fs->nactvar, MAXPARAMS, "parameters"); |
269 | f->numparams = cast(lu_byte, fs->nactvar); | 270 | f->numparams = fs->nactvar; |
270 | if (!dots) | 271 | if (!dots) |
271 | f->is_vararg = 0; | 272 | f->is_vararg = 0; |
272 | else { | 273 | else { |
@@ -925,12 +926,14 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { | |||
925 | } | 926 | } |
926 | 927 | ||
927 | 928 | ||
928 | static void cond (LexState *ls, expdesc *v) { | 929 | static int cond (LexState *ls) { |
929 | /* cond -> exp */ | 930 | /* cond -> exp */ |
930 | expr(ls, v); /* read condition */ | 931 | expdesc v; |
931 | if (v->k == VNIL) v->k = VFALSE; /* `falses' are all equal here */ | 932 | expr(ls, &v); /* read condition */ |
932 | luaK_goiftrue(ls->fs, v); | 933 | if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */ |
933 | luaK_patchtohere(ls->fs, v->t); | 934 | luaK_goiftrue(ls->fs, &v); |
935 | luaK_patchtohere(ls->fs, v.t); | ||
936 | return v.f; | ||
934 | } | 937 | } |
935 | 938 | ||
936 | 939 | ||
@@ -998,14 +1001,14 @@ static void repeatstat (LexState *ls, int line) { | |||
998 | /* repeatstat -> REPEAT block UNTIL cond */ | 1001 | /* repeatstat -> REPEAT block UNTIL cond */ |
999 | FuncState *fs = ls->fs; | 1002 | FuncState *fs = ls->fs; |
1000 | int repeat_init = luaK_getlabel(fs); | 1003 | int repeat_init = luaK_getlabel(fs); |
1001 | expdesc v; | 1004 | int flist; |
1002 | BlockCnt bl; | 1005 | BlockCnt bl; |
1003 | enterblock(fs, &bl, 1); | 1006 | enterblock(fs, &bl, 1); |
1004 | next(ls); | 1007 | next(ls); |
1005 | block(ls); | 1008 | block(ls); |
1006 | check_match(ls, TK_UNTIL, TK_REPEAT, line); | 1009 | check_match(ls, TK_UNTIL, TK_REPEAT, line); |
1007 | cond(ls, &v); | 1010 | flist = cond(ls); |
1008 | luaK_patchlist(fs, v.f, repeat_init); | 1011 | luaK_patchlist(fs, flist, repeat_init); |
1009 | leaveblock(fs); | 1012 | leaveblock(fs); |
1010 | } | 1013 | } |
1011 | 1014 | ||
@@ -1104,34 +1107,36 @@ static void forstat (LexState *ls, int line) { | |||
1104 | } | 1107 | } |
1105 | 1108 | ||
1106 | 1109 | ||
1107 | static void test_then_block (LexState *ls, expdesc *v) { | 1110 | static int test_then_block (LexState *ls) { |
1108 | /* test_then_block -> [IF | ELSEIF] cond THEN block */ | 1111 | /* test_then_block -> [IF | ELSEIF] cond THEN block */ |
1112 | int flist; | ||
1109 | next(ls); /* skip IF or ELSEIF */ | 1113 | next(ls); /* skip IF or ELSEIF */ |
1110 | cond(ls, v); | 1114 | flist = cond(ls); |
1111 | check(ls, TK_THEN); | 1115 | check(ls, TK_THEN); |
1112 | block(ls); /* `then' part */ | 1116 | block(ls); /* `then' part */ |
1117 | return flist; | ||
1113 | } | 1118 | } |
1114 | 1119 | ||
1115 | 1120 | ||
1116 | static void ifstat (LexState *ls, int line) { | 1121 | static void ifstat (LexState *ls, int line) { |
1117 | /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ | 1122 | /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ |
1118 | FuncState *fs = ls->fs; | 1123 | FuncState *fs = ls->fs; |
1119 | expdesc v; | 1124 | int flist; |
1120 | int escapelist = NO_JUMP; | 1125 | int escapelist = NO_JUMP; |
1121 | test_then_block(ls, &v); /* IF cond THEN block */ | 1126 | flist = test_then_block(ls); /* IF cond THEN block */ |
1122 | while (ls->t.token == TK_ELSEIF) { | 1127 | while (ls->t.token == TK_ELSEIF) { |
1123 | luaK_concat(fs, &escapelist, luaK_jump(fs)); | 1128 | luaK_concat(fs, &escapelist, luaK_jump(fs)); |
1124 | luaK_patchtohere(fs, v.f); | 1129 | luaK_patchtohere(fs, flist); |
1125 | test_then_block(ls, &v); /* ELSEIF cond THEN block */ | 1130 | flist = test_then_block(ls); /* ELSEIF cond THEN block */ |
1126 | } | 1131 | } |
1127 | if (ls->t.token == TK_ELSE) { | 1132 | if (ls->t.token == TK_ELSE) { |
1128 | luaK_concat(fs, &escapelist, luaK_jump(fs)); | 1133 | luaK_concat(fs, &escapelist, luaK_jump(fs)); |
1129 | luaK_patchtohere(fs, v.f); | 1134 | luaK_patchtohere(fs, flist); |
1130 | next(ls); /* skip ELSE (after patch, for correct line info) */ | 1135 | next(ls); /* skip ELSE (after patch, for correct line info) */ |
1131 | block(ls); /* `else' part */ | 1136 | block(ls); /* `else' part */ |
1132 | } | 1137 | } |
1133 | else | 1138 | else |
1134 | luaK_concat(fs, &escapelist, v.f); | 1139 | luaK_concat(fs, &escapelist, flist); |
1135 | luaK_patchtohere(fs, escapelist); | 1140 | luaK_patchtohere(fs, escapelist); |
1136 | check_match(ls, TK_END, TK_IF, line); | 1141 | check_match(ls, TK_END, TK_IF, line); |
1137 | } | 1142 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.h,v 1.46 2002/12/19 11:11:55 roberto Exp roberto $ | 2 | ** $Id: lparser.h,v 1.47 2003/02/11 10:46:24 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 | */ |
@@ -41,6 +41,12 @@ typedef struct expdesc { | |||
41 | } expdesc; | 41 | } expdesc; |
42 | 42 | ||
43 | 43 | ||
44 | typedef struct upvaldesc { | ||
45 | expkind k; | ||
46 | int info; | ||
47 | } upvaldesc; | ||
48 | |||
49 | |||
44 | struct BlockCnt; /* defined in lparser.c */ | 50 | struct BlockCnt; /* defined in lparser.c */ |
45 | 51 | ||
46 | 52 | ||
@@ -59,8 +65,8 @@ typedef struct FuncState { | |||
59 | int nk; /* number of elements in `k' */ | 65 | int nk; /* number of elements in `k' */ |
60 | int np; /* number of elements in `p' */ | 66 | int np; /* number of elements in `p' */ |
61 | int nlocvars; /* number of elements in `locvars' */ | 67 | int nlocvars; /* number of elements in `locvars' */ |
62 | int nactvar; /* number of active local variables */ | 68 | lu_byte nactvar; /* number of active local variables */ |
63 | expdesc upvalues[MAXUPVALUES]; /* upvalues */ | 69 | upvaldesc upvalues[MAXUPVALUES]; /* upvalues */ |
64 | int actvar[MAXVARS]; /* declared-variable stack */ | 70 | int actvar[MAXVARS]; /* declared-variable stack */ |
65 | } FuncState; | 71 | } FuncState; |
66 | 72 | ||