annotate lib/lua/lua-5.2.2/lparser.c @ 2553:48708da03b7f

Party Creation UI logic separated from MainMenu
author a.parshin
date Wed, 13 May 2015 02:20:05 +0200
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: lparser.c,v 2.130 2013/02/06 13:37:39 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Lua Parser
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #define lparser_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 #include "lcode.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "ldebug.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "ldo.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include "lfunc.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #include "llex.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #include "lmem.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 #include "lobject.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 #include "lopcodes.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 #include "lparser.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 #include "lstate.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25 #include "lstring.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 #include "ltable.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30 /* maximum number of local variables per function (must be smaller
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 than 250, due to the bytecode format) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 #define MAXVARS 200
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35 #define hasmultret(k) ((k) == VCALL || (k) == VVARARG)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40 ** nodes for block list (list of active blocks)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 typedef struct BlockCnt {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 struct BlockCnt *previous; /* chain */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44 short firstlabel; /* index of first label in this block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 short firstgoto; /* index of first pending goto in this block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 lu_byte nactvar; /* # active locals outside the block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 lu_byte upval; /* true if some variable in the block is an upvalue */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 lu_byte isloop; /* true if `block' is a loop */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 } BlockCnt;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 ** prototypes for recursive non-terminal functions
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 static void statement (LexState *ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 static void expr (LexState *ls, expdesc *v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60 static void anchor_token (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 /* last token from outer function must be EOS */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 lua_assert(ls->fs != NULL || ls->t.token == TK_EOS);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 TString *ts = ls->t.seminfo.ts;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65 luaX_newstring(ls, getstr(ts), ts->tsv.len);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 /* semantic error */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 static l_noret semerror (LexState *ls, const char *msg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72 ls->t.token = 0; /* remove 'near to' from final message */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 luaX_syntaxerror(ls, msg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77 static l_noret error_expected (LexState *ls, int token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 luaX_syntaxerror(ls,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79 luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 static l_noret errorlimit (FuncState *fs, int limit, const char *what) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 lua_State *L = fs->ls->L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 const char *msg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 int line = fs->f->linedefined;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 const char *where = (line == 0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 ? "main function"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 : luaO_pushfstring(L, "function at line %d", line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90 msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 what, limit, where);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 luaX_syntaxerror(fs->ls, msg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 static void checklimit (FuncState *fs, int v, int l, const char *what) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 if (v > l) errorlimit(fs, l, what);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 static int testnext (LexState *ls, int c) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102 if (ls->t.token == c) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103 luaX_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 else return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 static void check (LexState *ls, int c) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 if (ls->t.token != c)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112 error_expected(ls, c);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 static void checknext (LexState *ls, int c) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117 check(ls, c);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 luaX_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 static void check_match (LexState *ls, int what, int who, int where) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 if (!testnext(ls, what)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 if (where == ls->linenumber)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 error_expected(ls, what);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 "%s expected (to close %s at line %d)",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 luaX_token2str(ls, what), luaX_token2str(ls, who), where));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 static TString *str_checkname (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 TString *ts;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 check(ls, TK_NAME);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 ts = ls->t.seminfo.ts;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143 luaX_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 return ts;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148 static void init_exp (expdesc *e, expkind k, int i) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 e->f = e->t = NO_JUMP;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 e->k = k;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151 e->u.info = i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155 static void codestring (LexState *ls, expdesc *e, TString *s) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156 init_exp(e, VK, luaK_stringK(ls->fs, s));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160 static void checkname (LexState *ls, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161 codestring(ls, e, str_checkname(ls));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 static int registerlocalvar (LexState *ls, TString *varname) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 Proto *f = fs->f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 int oldsize = f->sizelocvars;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169 luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170 LocVar, SHRT_MAX, "local variables");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171 while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 f->locvars[fs->nlocvars].varname = varname;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173 luaC_objbarrier(ls->L, f, varname);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174 return fs->nlocvars++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178 static void new_localvar (LexState *ls, TString *name) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 Dyndata *dyd = ls->dyd;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181 int reg = registerlocalvar(ls, name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182 checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183 MAXVARS, "local variables");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185 dyd->actvar.size, Vardesc, MAX_INT, "local variables");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190 static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191 new_localvar(ls, luaX_newstring(ls, name, sz));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194 #define new_localvarliteral(ls,v) \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195 new_localvarliteral_(ls, "" v, (sizeof(v)/sizeof(char))-1)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198 static LocVar *getlocvar (FuncState *fs, int i) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199 int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200 lua_assert(idx < fs->nlocvars);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 return &fs->f->locvars[idx];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205 static void adjustlocalvars (LexState *ls, int nvars) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207 fs->nactvar = cast_byte(fs->nactvar + nvars);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208 for (; nvars; nvars--) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 getlocvar(fs, fs->nactvar - nvars)->startpc = fs->pc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214 static void removevars (FuncState *fs, int tolevel) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215 fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216 while (fs->nactvar > tolevel)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 getlocvar(fs, --fs->nactvar)->endpc = fs->pc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 static int searchupvalue (FuncState *fs, TString *name) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223 Upvaldesc *up = fs->f->upvalues;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224 for (i = 0; i < fs->nups; i++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225 if (luaS_eqstr(up[i].name, name)) return i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227 return -1; /* not found */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231 static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232 Proto *f = fs->f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 int oldsize = f->sizeupvalues;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234 checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235 luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236 Upvaldesc, MAXUPVAL, "upvalues");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237 while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238 f->upvalues[fs->nups].instack = (v->k == VLOCAL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239 f->upvalues[fs->nups].idx = cast_byte(v->u.info);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240 f->upvalues[fs->nups].name = name;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241 luaC_objbarrier(fs->ls->L, f, name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242 return fs->nups++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246 static int searchvar (FuncState *fs, TString *n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248 for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249 if (luaS_eqstr(n, getlocvar(fs, i)->varname))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250 return i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 return -1; /* not found */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 Mark block where variable at given level was defined
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 (to emit close instructions later).
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
259 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
260 static void markupval (FuncState *fs, int level) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
261 BlockCnt *bl = fs->bl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
262 while (bl->nactvar > level) bl = bl->previous;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
263 bl->upval = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
264 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
265
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
266
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
267 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
268 Find variable with given name 'n'. If it is an upvalue, add this
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
269 upvalue into all intermediate functions.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
270 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
271 static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
272 if (fs == NULL) /* no more levels? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
273 return VVOID; /* default is global */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
274 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
275 int v = searchvar(fs, n); /* look up locals at current level */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
276 if (v >= 0) { /* found? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
277 init_exp(var, VLOCAL, v); /* variable is local */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
278 if (!base)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
279 markupval(fs, v); /* local will be used as an upval */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
280 return VLOCAL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
281 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
282 else { /* not found as local at current level; try upvalues */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
283 int idx = searchupvalue(fs, n); /* try existing upvalues */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
284 if (idx < 0) { /* not found? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
285 if (singlevaraux(fs->prev, n, var, 0) == VVOID) /* try upper levels */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
286 return VVOID; /* not found; is a global */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
287 /* else was LOCAL or UPVAL */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
288 idx = newupvalue(fs, n, var); /* will be a new upvalue */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
289 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
290 init_exp(var, VUPVAL, idx);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
291 return VUPVAL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
292 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
293 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
294 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
295
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
296
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
297 static void singlevar (LexState *ls, expdesc *var) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
298 TString *varname = str_checkname(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
299 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
300 if (singlevaraux(fs, varname, var, 1) == VVOID) { /* global name? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
301 expdesc key;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
302 singlevaraux(fs, ls->envn, var, 1); /* get environment variable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
303 lua_assert(var->k == VLOCAL || var->k == VUPVAL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
304 codestring(ls, &key, varname); /* key is variable name */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
305 luaK_indexed(fs, var, &key); /* env[varname] */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
306 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
307 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
308
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
309
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
310 static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
311 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
312 int extra = nvars - nexps;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
313 if (hasmultret(e->k)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
314 extra++; /* includes call itself */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
315 if (extra < 0) extra = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
316 luaK_setreturns(fs, e, extra); /* last exp. provides the difference */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
317 if (extra > 1) luaK_reserveregs(fs, extra-1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
318 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
319 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
320 if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
321 if (extra > 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
322 int reg = fs->freereg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
323 luaK_reserveregs(fs, extra);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
324 luaK_nil(fs, reg, extra);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
325 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
326 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
327 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
328
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
329
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
330 static void enterlevel (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
331 lua_State *L = ls->L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
332 ++L->nCcalls;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
333 checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "C levels");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
334 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
335
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
336
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
337 #define leavelevel(ls) ((ls)->L->nCcalls--)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
338
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
339
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
340 static void closegoto (LexState *ls, int g, Labeldesc *label) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
341 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
342 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
343 Labellist *gl = &ls->dyd->gt;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
344 Labeldesc *gt = &gl->arr[g];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
345 lua_assert(luaS_eqstr(gt->name, label->name));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
346 if (gt->nactvar < label->nactvar) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
347 TString *vname = getlocvar(fs, gt->nactvar)->varname;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
348 const char *msg = luaO_pushfstring(ls->L,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
349 "<goto %s> at line %d jumps into the scope of local " LUA_QS,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
350 getstr(gt->name), gt->line, getstr(vname));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
351 semerror(ls, msg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
352 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
353 luaK_patchlist(fs, gt->pc, label->pc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
354 /* remove goto from pending list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
355 for (i = g; i < gl->n - 1; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
356 gl->arr[i] = gl->arr[i + 1];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
357 gl->n--;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
358 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
359
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
360
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
361 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
362 ** try to close a goto with existing labels; this solves backward jumps
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
363 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
364 static int findlabel (LexState *ls, int g) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
365 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
366 BlockCnt *bl = ls->fs->bl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
367 Dyndata *dyd = ls->dyd;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
368 Labeldesc *gt = &dyd->gt.arr[g];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
369 /* check labels in current block for a match */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
370 for (i = bl->firstlabel; i < dyd->label.n; i++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
371 Labeldesc *lb = &dyd->label.arr[i];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
372 if (luaS_eqstr(lb->name, gt->name)) { /* correct label? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
373 if (gt->nactvar > lb->nactvar &&
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
374 (bl->upval || dyd->label.n > bl->firstlabel))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
375 luaK_patchclose(ls->fs, gt->pc, lb->nactvar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
376 closegoto(ls, g, lb); /* close it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
377 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
378 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
379 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
380 return 0; /* label not found; cannot close goto */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
381 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
382
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
383
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
384 static int newlabelentry (LexState *ls, Labellist *l, TString *name,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
385 int line, int pc) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
386 int n = l->n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
387 luaM_growvector(ls->L, l->arr, n, l->size,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
388 Labeldesc, SHRT_MAX, "labels/gotos");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
389 l->arr[n].name = name;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
390 l->arr[n].line = line;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
391 l->arr[n].nactvar = ls->fs->nactvar;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
392 l->arr[n].pc = pc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
393 l->n++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
394 return n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
395 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
396
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
397
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
398 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
399 ** check whether new label 'lb' matches any pending gotos in current
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
400 ** block; solves forward jumps
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
401 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
402 static void findgotos (LexState *ls, Labeldesc *lb) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
403 Labellist *gl = &ls->dyd->gt;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
404 int i = ls->fs->bl->firstgoto;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
405 while (i < gl->n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
406 if (luaS_eqstr(gl->arr[i].name, lb->name))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
407 closegoto(ls, i, lb);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
408 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
409 i++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
410 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
411 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
412
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
413
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
414 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
415 ** "export" pending gotos to outer level, to check them against
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
416 ** outer labels; if the block being exited has upvalues, and
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
417 ** the goto exits the scope of any variable (which can be the
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
418 ** upvalue), close those variables being exited.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
419 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
420 static void movegotosout (FuncState *fs, BlockCnt *bl) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
421 int i = bl->firstgoto;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
422 Labellist *gl = &fs->ls->dyd->gt;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
423 /* correct pending gotos to current block and try to close it
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
424 with visible labels */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
425 while (i < gl->n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
426 Labeldesc *gt = &gl->arr[i];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
427 if (gt->nactvar > bl->nactvar) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
428 if (bl->upval)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
429 luaK_patchclose(fs, gt->pc, bl->nactvar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
430 gt->nactvar = bl->nactvar;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
431 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
432 if (!findlabel(fs->ls, i))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
433 i++; /* move to next one */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
434 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
435 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
436
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
437
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
438 static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
439 bl->isloop = isloop;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
440 bl->nactvar = fs->nactvar;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
441 bl->firstlabel = fs->ls->dyd->label.n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
442 bl->firstgoto = fs->ls->dyd->gt.n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
443 bl->upval = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
444 bl->previous = fs->bl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
445 fs->bl = bl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
446 lua_assert(fs->freereg == fs->nactvar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
447 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
448
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
449
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
450 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
451 ** create a label named "break" to resolve break statements
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
452 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
453 static void breaklabel (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
454 TString *n = luaS_new(ls->L, "break");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
455 int l = newlabelentry(ls, &ls->dyd->label, n, 0, ls->fs->pc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
456 findgotos(ls, &ls->dyd->label.arr[l]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
457 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
458
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
459 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
460 ** generates an error for an undefined 'goto'; choose appropriate
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
461 ** message when label name is a reserved word (which can only be 'break')
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
462 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
463 static l_noret undefgoto (LexState *ls, Labeldesc *gt) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
464 const char *msg = isreserved(gt->name)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
465 ? "<%s> at line %d not inside a loop"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
466 : "no visible label " LUA_QS " for <goto> at line %d";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
467 msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
468 semerror(ls, msg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
469 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
470
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
471
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
472 static void leaveblock (FuncState *fs) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
473 BlockCnt *bl = fs->bl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
474 LexState *ls = fs->ls;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
475 if (bl->previous && bl->upval) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
476 /* create a 'jump to here' to close upvalues */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
477 int j = luaK_jump(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
478 luaK_patchclose(fs, j, bl->nactvar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
479 luaK_patchtohere(fs, j);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
480 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
481 if (bl->isloop)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
482 breaklabel(ls); /* close pending breaks */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
483 fs->bl = bl->previous;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
484 removevars(fs, bl->nactvar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
485 lua_assert(bl->nactvar == fs->nactvar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
486 fs->freereg = fs->nactvar; /* free registers */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
487 ls->dyd->label.n = bl->firstlabel; /* remove local labels */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
488 if (bl->previous) /* inner block? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
489 movegotosout(fs, bl); /* update pending gotos to outer block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
490 else if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
491 undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
492 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
493
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
494
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
495 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
496 ** adds a new prototype into list of prototypes
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
497 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
498 static Proto *addprototype (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
499 Proto *clp;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
500 lua_State *L = ls->L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
501 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
502 Proto *f = fs->f; /* prototype of current function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
503 if (fs->np >= f->sizep) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
504 int oldsize = f->sizep;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
505 luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
506 while (oldsize < f->sizep) f->p[oldsize++] = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
507 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
508 f->p[fs->np++] = clp = luaF_newproto(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
509 luaC_objbarrier(L, f, clp);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
510 return clp;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
511 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
512
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
513
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
514 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
515 ** codes instruction to create new closure in parent function.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
516 ** The OP_CLOSURE instruction must use the last available register,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
517 ** so that, if it invokes the GC, the GC knows which registers
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
518 ** are in use at that time.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
519 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
520 static void codeclosure (LexState *ls, expdesc *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
521 FuncState *fs = ls->fs->prev;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
522 init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
523 luaK_exp2nextreg(fs, v); /* fix it at the last register */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
524 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
525
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
526
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
527 static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
528 lua_State *L = ls->L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
529 Proto *f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
530 fs->prev = ls->fs; /* linked list of funcstates */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
531 fs->ls = ls;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
532 ls->fs = fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
533 fs->pc = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
534 fs->lasttarget = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
535 fs->jpc = NO_JUMP;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
536 fs->freereg = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
537 fs->nk = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
538 fs->np = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
539 fs->nups = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
540 fs->nlocvars = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
541 fs->nactvar = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
542 fs->firstlocal = ls->dyd->actvar.n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
543 fs->bl = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
544 f = fs->f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
545 f->source = ls->source;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
546 f->maxstacksize = 2; /* registers 0/1 are always valid */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
547 fs->h = luaH_new(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
548 /* anchor table of constants (to avoid being collected) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
549 sethvalue2s(L, L->top, fs->h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
550 incr_top(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
551 enterblock(fs, bl, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
552 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
553
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
554
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
555 static void close_func (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
556 lua_State *L = ls->L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
557 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
558 Proto *f = fs->f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
559 luaK_ret(fs, 0, 0); /* final return */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
560 leaveblock(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
561 luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
562 f->sizecode = fs->pc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
563 luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
564 f->sizelineinfo = fs->pc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
565 luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
566 f->sizek = fs->nk;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
567 luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
568 f->sizep = fs->np;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
569 luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
570 f->sizelocvars = fs->nlocvars;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
571 luaM_reallocvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
572 f->sizeupvalues = fs->nups;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
573 lua_assert(fs->bl == NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
574 ls->fs = fs->prev;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
575 /* last token read was anchored in defunct function; must re-anchor it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
576 anchor_token(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
577 L->top--; /* pop table of constants */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
578 luaC_checkGC(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
579 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
580
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
581
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
582
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
583 /*============================================================*/
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
584 /* GRAMMAR RULES */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
585 /*============================================================*/
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
586
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
587
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
588 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
589 ** check whether current token is in the follow set of a block.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
590 ** 'until' closes syntactical blocks, but do not close scope,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
591 ** so it handled in separate.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
592 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
593 static int block_follow (LexState *ls, int withuntil) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
594 switch (ls->t.token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
595 case TK_ELSE: case TK_ELSEIF:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
596 case TK_END: case TK_EOS:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
597 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
598 case TK_UNTIL: return withuntil;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
599 default: return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
600 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
601 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
602
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
603
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
604 static void statlist (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
605 /* statlist -> { stat [`;'] } */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
606 while (!block_follow(ls, 1)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
607 if (ls->t.token == TK_RETURN) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
608 statement(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
609 return; /* 'return' must be last statement */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
610 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
611 statement(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
612 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
613 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
614
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
615
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
616 static void fieldsel (LexState *ls, expdesc *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
617 /* fieldsel -> ['.' | ':'] NAME */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
618 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
619 expdesc key;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
620 luaK_exp2anyregup(fs, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
621 luaX_next(ls); /* skip the dot or colon */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
622 checkname(ls, &key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
623 luaK_indexed(fs, v, &key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
624 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
625
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
626
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
627 static void yindex (LexState *ls, expdesc *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
628 /* index -> '[' expr ']' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
629 luaX_next(ls); /* skip the '[' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
630 expr(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
631 luaK_exp2val(ls->fs, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
632 checknext(ls, ']');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
633 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
634
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
635
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
636 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
637 ** {======================================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
638 ** Rules for Constructors
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
639 ** =======================================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
640 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
641
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
642
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
643 struct ConsControl {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
644 expdesc v; /* last list item read */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
645 expdesc *t; /* table descriptor */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
646 int nh; /* total number of `record' elements */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
647 int na; /* total number of array elements */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
648 int tostore; /* number of array elements pending to be stored */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
649 };
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
650
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
651
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
652 static void recfield (LexState *ls, struct ConsControl *cc) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
653 /* recfield -> (NAME | `['exp1`]') = exp1 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
654 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
655 int reg = ls->fs->freereg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
656 expdesc key, val;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
657 int rkkey;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
658 if (ls->t.token == TK_NAME) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
659 checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
660 checkname(ls, &key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
661 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
662 else /* ls->t.token == '[' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
663 yindex(ls, &key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
664 cc->nh++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
665 checknext(ls, '=');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
666 rkkey = luaK_exp2RK(fs, &key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
667 expr(ls, &val);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
668 luaK_codeABC(fs, OP_SETTABLE, cc->t->u.info, rkkey, luaK_exp2RK(fs, &val));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
669 fs->freereg = reg; /* free registers */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
670 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
671
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
672
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
673 static void closelistfield (FuncState *fs, struct ConsControl *cc) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
674 if (cc->v.k == VVOID) return; /* there is no list item */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
675 luaK_exp2nextreg(fs, &cc->v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
676 cc->v.k = VVOID;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
677 if (cc->tostore == LFIELDS_PER_FLUSH) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
678 luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
679 cc->tostore = 0; /* no more items pending */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
680 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
681 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
682
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
683
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
684 static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
685 if (cc->tostore == 0) return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
686 if (hasmultret(cc->v.k)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
687 luaK_setmultret(fs, &cc->v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
688 luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
689 cc->na--; /* do not count last expression (unknown number of elements) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
690 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
691 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
692 if (cc->v.k != VVOID)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
693 luaK_exp2nextreg(fs, &cc->v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
694 luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
695 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
696 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
697
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
698
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
699 static void listfield (LexState *ls, struct ConsControl *cc) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
700 /* listfield -> exp */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
701 expr(ls, &cc->v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
702 checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
703 cc->na++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
704 cc->tostore++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
705 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
706
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
707
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
708 static void field (LexState *ls, struct ConsControl *cc) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
709 /* field -> listfield | recfield */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
710 switch(ls->t.token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
711 case TK_NAME: { /* may be 'listfield' or 'recfield' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
712 if (luaX_lookahead(ls) != '=') /* expression? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
713 listfield(ls, cc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
714 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
715 recfield(ls, cc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
716 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
717 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
718 case '[': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
719 recfield(ls, cc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
720 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
721 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
722 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
723 listfield(ls, cc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
724 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
725 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
726 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
727 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
728
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
729
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
730 static void constructor (LexState *ls, expdesc *t) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
731 /* constructor -> '{' [ field { sep field } [sep] ] '}'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
732 sep -> ',' | ';' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
733 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
734 int line = ls->linenumber;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
735 int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
736 struct ConsControl cc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
737 cc.na = cc.nh = cc.tostore = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
738 cc.t = t;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
739 init_exp(t, VRELOCABLE, pc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
740 init_exp(&cc.v, VVOID, 0); /* no value (yet) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
741 luaK_exp2nextreg(ls->fs, t); /* fix it at stack top */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
742 checknext(ls, '{');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
743 do {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
744 lua_assert(cc.v.k == VVOID || cc.tostore > 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
745 if (ls->t.token == '}') break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
746 closelistfield(fs, &cc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
747 field(ls, &cc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
748 } while (testnext(ls, ',') || testnext(ls, ';'));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
749 check_match(ls, '}', '{', line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
750 lastlistfield(fs, &cc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
751 SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
752 SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
753 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
754
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
755 /* }====================================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
756
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
757
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
758
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
759 static void parlist (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
760 /* parlist -> [ param { `,' param } ] */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
761 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
762 Proto *f = fs->f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
763 int nparams = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
764 f->is_vararg = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
765 if (ls->t.token != ')') { /* is `parlist' not empty? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
766 do {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
767 switch (ls->t.token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
768 case TK_NAME: { /* param -> NAME */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
769 new_localvar(ls, str_checkname(ls));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
770 nparams++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
771 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
772 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
773 case TK_DOTS: { /* param -> `...' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
774 luaX_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
775 f->is_vararg = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
776 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
777 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
778 default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
779 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
780 } while (!f->is_vararg && testnext(ls, ','));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
781 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
782 adjustlocalvars(ls, nparams);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
783 f->numparams = cast_byte(fs->nactvar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
784 luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
785 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
786
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
787
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
788 static void body (LexState *ls, expdesc *e, int ismethod, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
789 /* body -> `(' parlist `)' block END */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
790 FuncState new_fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
791 BlockCnt bl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
792 new_fs.f = addprototype(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
793 new_fs.f->linedefined = line;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
794 open_func(ls, &new_fs, &bl);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
795 checknext(ls, '(');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
796 if (ismethod) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
797 new_localvarliteral(ls, "self"); /* create 'self' parameter */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
798 adjustlocalvars(ls, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
799 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
800 parlist(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
801 checknext(ls, ')');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
802 statlist(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
803 new_fs.f->lastlinedefined = ls->linenumber;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
804 check_match(ls, TK_END, TK_FUNCTION, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
805 codeclosure(ls, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
806 close_func(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
807 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
808
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
809
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
810 static int explist (LexState *ls, expdesc *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
811 /* explist -> expr { `,' expr } */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
812 int n = 1; /* at least one expression */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
813 expr(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
814 while (testnext(ls, ',')) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
815 luaK_exp2nextreg(ls->fs, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
816 expr(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
817 n++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
818 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
819 return n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
820 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
821
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
822
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
823 static void funcargs (LexState *ls, expdesc *f, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
824 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
825 expdesc args;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
826 int base, nparams;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
827 switch (ls->t.token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
828 case '(': { /* funcargs -> `(' [ explist ] `)' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
829 luaX_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
830 if (ls->t.token == ')') /* arg list is empty? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
831 args.k = VVOID;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
832 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
833 explist(ls, &args);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
834 luaK_setmultret(fs, &args);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
835 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
836 check_match(ls, ')', '(', line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
837 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
838 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
839 case '{': { /* funcargs -> constructor */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
840 constructor(ls, &args);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
841 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
842 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
843 case TK_STRING: { /* funcargs -> STRING */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
844 codestring(ls, &args, ls->t.seminfo.ts);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
845 luaX_next(ls); /* must use `seminfo' before `next' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
846 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
847 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
848 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
849 luaX_syntaxerror(ls, "function arguments expected");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
850 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
851 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
852 lua_assert(f->k == VNONRELOC);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
853 base = f->u.info; /* base register for call */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
854 if (hasmultret(args.k))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
855 nparams = LUA_MULTRET; /* open call */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
856 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
857 if (args.k != VVOID)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
858 luaK_exp2nextreg(fs, &args); /* close last argument */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
859 nparams = fs->freereg - (base+1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
860 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
861 init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
862 luaK_fixline(fs, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
863 fs->freereg = base+1; /* call remove function and arguments and leaves
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
864 (unless changed) one result */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
865 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
867
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
868
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
869
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
870 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
871 ** {======================================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
872 ** Expression parsing
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
873 ** =======================================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
874 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
875
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
876
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
877 static void primaryexp (LexState *ls, expdesc *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
878 /* primaryexp -> NAME | '(' expr ')' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
879 switch (ls->t.token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
880 case '(': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
881 int line = ls->linenumber;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
882 luaX_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
883 expr(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
884 check_match(ls, ')', '(', line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
885 luaK_dischargevars(ls->fs, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
886 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
887 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
888 case TK_NAME: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
889 singlevar(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
890 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
891 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
892 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
893 luaX_syntaxerror(ls, "unexpected symbol");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
894 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
895 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
896 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
897
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
898
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
899 static void suffixedexp (LexState *ls, expdesc *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
900 /* suffixedexp ->
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
901 primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
902 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
903 int line = ls->linenumber;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
904 primaryexp(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
905 for (;;) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
906 switch (ls->t.token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
907 case '.': { /* fieldsel */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
908 fieldsel(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
909 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
910 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
911 case '[': { /* `[' exp1 `]' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
912 expdesc key;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
913 luaK_exp2anyregup(fs, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
914 yindex(ls, &key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
915 luaK_indexed(fs, v, &key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
916 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
917 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
918 case ':': { /* `:' NAME funcargs */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
919 expdesc key;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
920 luaX_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
921 checkname(ls, &key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
922 luaK_self(fs, v, &key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
923 funcargs(ls, v, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
924 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
925 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
926 case '(': case TK_STRING: case '{': { /* funcargs */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
927 luaK_exp2nextreg(fs, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
928 funcargs(ls, v, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
929 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
930 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
931 default: return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
932 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
933 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
934 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
935
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
936
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
937 static void simpleexp (LexState *ls, expdesc *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
938 /* simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... |
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
939 constructor | FUNCTION body | suffixedexp */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
940 switch (ls->t.token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
941 case TK_NUMBER: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
942 init_exp(v, VKNUM, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
943 v->u.nval = ls->t.seminfo.r;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
944 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
945 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
946 case TK_STRING: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
947 codestring(ls, v, ls->t.seminfo.ts);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
948 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
949 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
950 case TK_NIL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
951 init_exp(v, VNIL, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
952 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
953 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
954 case TK_TRUE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
955 init_exp(v, VTRUE, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
956 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
957 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
958 case TK_FALSE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
959 init_exp(v, VFALSE, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
960 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
961 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
962 case TK_DOTS: { /* vararg */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
963 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
964 check_condition(ls, fs->f->is_vararg,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
965 "cannot use " LUA_QL("...") " outside a vararg function");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
966 init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
967 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
968 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
969 case '{': { /* constructor */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
970 constructor(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
971 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
972 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
973 case TK_FUNCTION: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
974 luaX_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
975 body(ls, v, 0, ls->linenumber);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
976 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
977 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
978 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
979 suffixedexp(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
980 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
981 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
982 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
983 luaX_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
984 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
985
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
986
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
987 static UnOpr getunopr (int op) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
988 switch (op) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
989 case TK_NOT: return OPR_NOT;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
990 case '-': return OPR_MINUS;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
991 case '#': return OPR_LEN;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
992 default: return OPR_NOUNOPR;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
993 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
994 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
995
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
996
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
997 static BinOpr getbinopr (int op) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
998 switch (op) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
999 case '+': return OPR_ADD;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1000 case '-': return OPR_SUB;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1001 case '*': return OPR_MUL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1002 case '/': return OPR_DIV;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1003 case '%': return OPR_MOD;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1004 case '^': return OPR_POW;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1005 case TK_CONCAT: return OPR_CONCAT;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1006 case TK_NE: return OPR_NE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1007 case TK_EQ: return OPR_EQ;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1008 case '<': return OPR_LT;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1009 case TK_LE: return OPR_LE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1010 case '>': return OPR_GT;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1011 case TK_GE: return OPR_GE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1012 case TK_AND: return OPR_AND;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1013 case TK_OR: return OPR_OR;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1014 default: return OPR_NOBINOPR;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1015 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1016 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1017
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1018
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1019 static const struct {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1020 lu_byte left; /* left priority for each binary operator */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1021 lu_byte right; /* right priority */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1022 } priority[] = { /* ORDER OPR */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1023 {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `*' `/' `%' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1024 {10, 9}, {5, 4}, /* ^, .. (right associative) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1025 {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1026 {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1027 {2, 2}, {1, 1} /* and, or */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1028 };
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1029
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1030 #define UNARY_PRIORITY 8 /* priority for unary operators */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1031
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1032
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1033 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1034 ** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1035 ** where `binop' is any binary operator with a priority higher than `limit'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1036 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1037 static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1038 BinOpr op;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1039 UnOpr uop;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1040 enterlevel(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1041 uop = getunopr(ls->t.token);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1042 if (uop != OPR_NOUNOPR) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1043 int line = ls->linenumber;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1044 luaX_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1045 subexpr(ls, v, UNARY_PRIORITY);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1046 luaK_prefix(ls->fs, uop, v, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1047 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1048 else simpleexp(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1049 /* expand while operators have priorities higher than `limit' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1050 op = getbinopr(ls->t.token);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1051 while (op != OPR_NOBINOPR && priority[op].left > limit) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1052 expdesc v2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1053 BinOpr nextop;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1054 int line = ls->linenumber;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1055 luaX_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1056 luaK_infix(ls->fs, op, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1057 /* read sub-expression with higher priority */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1058 nextop = subexpr(ls, &v2, priority[op].right);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1059 luaK_posfix(ls->fs, op, v, &v2, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1060 op = nextop;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1061 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1062 leavelevel(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1063 return op; /* return first untreated operator */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1064 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1065
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1066
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1067 static void expr (LexState *ls, expdesc *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1068 subexpr(ls, v, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1069 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1070
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1071 /* }==================================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1072
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1073
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1074
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1075 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1076 ** {======================================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1077 ** Rules for Statements
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1078 ** =======================================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1079 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1080
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1081
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1082 static void block (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1083 /* block -> statlist */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1084 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1085 BlockCnt bl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1086 enterblock(fs, &bl, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1087 statlist(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1088 leaveblock(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1089 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1090
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1091
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1092 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1093 ** structure to chain all variables in the left-hand side of an
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1094 ** assignment
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1095 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1096 struct LHS_assign {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1097 struct LHS_assign *prev;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1098 expdesc v; /* variable (global, local, upvalue, or indexed) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1099 };
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1100
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1101
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1102 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1103 ** check whether, in an assignment to an upvalue/local variable, the
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1104 ** upvalue/local variable is begin used in a previous assignment to a
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1105 ** table. If so, save original upvalue/local value in a safe place and
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1106 ** use this safe copy in the previous assignment.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1107 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1108 static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1109 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1110 int extra = fs->freereg; /* eventual position to save local variable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1111 int conflict = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1112 for (; lh; lh = lh->prev) { /* check all previous assignments */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1113 if (lh->v.k == VINDEXED) { /* assigning to a table? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1114 /* table is the upvalue/local being assigned now? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1115 if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1116 conflict = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1117 lh->v.u.ind.vt = VLOCAL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1118 lh->v.u.ind.t = extra; /* previous assignment will use safe copy */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1119 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1120 /* index is the local being assigned? (index cannot be upvalue) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1121 if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1122 conflict = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1123 lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1124 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1125 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1126 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1127 if (conflict) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1128 /* copy upvalue/local value to a temporary (in position 'extra') */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1129 OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1130 luaK_codeABC(fs, op, extra, v->u.info, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1131 luaK_reserveregs(fs, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1132 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1133 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1134
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1135
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1136 static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1137 expdesc e;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1138 check_condition(ls, vkisvar(lh->v.k), "syntax error");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1139 if (testnext(ls, ',')) { /* assignment -> ',' suffixedexp assignment */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1140 struct LHS_assign nv;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1141 nv.prev = lh;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1142 suffixedexp(ls, &nv.v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1143 if (nv.v.k != VINDEXED)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1144 check_conflict(ls, lh, &nv.v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1145 checklimit(ls->fs, nvars + ls->L->nCcalls, LUAI_MAXCCALLS,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1146 "C levels");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1147 assignment(ls, &nv, nvars+1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1148 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1149 else { /* assignment -> `=' explist */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1150 int nexps;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1151 checknext(ls, '=');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1152 nexps = explist(ls, &e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1153 if (nexps != nvars) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1154 adjust_assign(ls, nvars, nexps, &e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1155 if (nexps > nvars)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1156 ls->fs->freereg -= nexps - nvars; /* remove extra values */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1157 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1158 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1159 luaK_setoneret(ls->fs, &e); /* close last expression */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1160 luaK_storevar(ls->fs, &lh->v, &e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1161 return; /* avoid default */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1162 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1163 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1164 init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1165 luaK_storevar(ls->fs, &lh->v, &e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1166 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1167
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1168
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1169 static int cond (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1170 /* cond -> exp */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1171 expdesc v;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1172 expr(ls, &v); /* read condition */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1173 if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1174 luaK_goiftrue(ls->fs, &v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1175 return v.f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1176 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1177
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1178
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1179 static void gotostat (LexState *ls, int pc) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1180 int line = ls->linenumber;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1181 TString *label;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1182 int g;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1183 if (testnext(ls, TK_GOTO))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1184 label = str_checkname(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1185 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1186 luaX_next(ls); /* skip break */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1187 label = luaS_new(ls->L, "break");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1188 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1189 g = newlabelentry(ls, &ls->dyd->gt, label, line, pc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1190 findlabel(ls, g); /* close it if label already defined */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1191 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1192
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1193
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1194 /* check for repeated labels on the same block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1195 static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1196 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1197 for (i = fs->bl->firstlabel; i < ll->n; i++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1198 if (luaS_eqstr(label, ll->arr[i].name)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1199 const char *msg = luaO_pushfstring(fs->ls->L,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1200 "label " LUA_QS " already defined on line %d",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1201 getstr(label), ll->arr[i].line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1202 semerror(fs->ls, msg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1203 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1204 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1205 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1206
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1207
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1208 /* skip no-op statements */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1209 static void skipnoopstat (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1210 while (ls->t.token == ';' || ls->t.token == TK_DBCOLON)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1211 statement(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1212 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1213
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1214
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1215 static void labelstat (LexState *ls, TString *label, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1216 /* label -> '::' NAME '::' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1217 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1218 Labellist *ll = &ls->dyd->label;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1219 int l; /* index of new label being created */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1220 checkrepeated(fs, ll, label); /* check for repeated labels */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1221 checknext(ls, TK_DBCOLON); /* skip double colon */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1222 /* create new entry for this label */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1223 l = newlabelentry(ls, ll, label, line, fs->pc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1224 skipnoopstat(ls); /* skip other no-op statements */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1225 if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1226 /* assume that locals are already out of scope */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1227 ll->arr[l].nactvar = fs->bl->nactvar;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1228 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1229 findgotos(ls, &ll->arr[l]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1230 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1231
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1232
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1233 static void whilestat (LexState *ls, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1234 /* whilestat -> WHILE cond DO block END */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1235 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1236 int whileinit;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1237 int condexit;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1238 BlockCnt bl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1239 luaX_next(ls); /* skip WHILE */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1240 whileinit = luaK_getlabel(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1241 condexit = cond(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1242 enterblock(fs, &bl, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1243 checknext(ls, TK_DO);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1244 block(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1245 luaK_jumpto(fs, whileinit);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1246 check_match(ls, TK_END, TK_WHILE, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1247 leaveblock(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1248 luaK_patchtohere(fs, condexit); /* false conditions finish the loop */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1249 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1250
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1251
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1252 static void repeatstat (LexState *ls, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1253 /* repeatstat -> REPEAT block UNTIL cond */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1254 int condexit;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1255 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1256 int repeat_init = luaK_getlabel(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1257 BlockCnt bl1, bl2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1258 enterblock(fs, &bl1, 1); /* loop block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1259 enterblock(fs, &bl2, 0); /* scope block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1260 luaX_next(ls); /* skip REPEAT */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1261 statlist(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1262 check_match(ls, TK_UNTIL, TK_REPEAT, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1263 condexit = cond(ls); /* read condition (inside scope block) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1264 if (bl2.upval) /* upvalues? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1265 luaK_patchclose(fs, condexit, bl2.nactvar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1266 leaveblock(fs); /* finish scope */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1267 luaK_patchlist(fs, condexit, repeat_init); /* close the loop */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1268 leaveblock(fs); /* finish loop */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1269 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1270
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1271
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1272 static int exp1 (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1273 expdesc e;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1274 int reg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1275 expr(ls, &e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1276 luaK_exp2nextreg(ls->fs, &e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1277 lua_assert(e.k == VNONRELOC);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1278 reg = e.u.info;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1279 return reg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1280 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1281
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1282
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1283 static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1284 /* forbody -> DO block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1285 BlockCnt bl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1286 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1287 int prep, endfor;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1288 adjustlocalvars(ls, 3); /* control variables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1289 checknext(ls, TK_DO);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1290 prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1291 enterblock(fs, &bl, 0); /* scope for declared variables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1292 adjustlocalvars(ls, nvars);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1293 luaK_reserveregs(fs, nvars);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1294 block(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1295 leaveblock(fs); /* end of scope for declared variables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1296 luaK_patchtohere(fs, prep);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1297 if (isnum) /* numeric for? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1298 endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1299 else { /* generic for */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1300 luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1301 luaK_fixline(fs, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1302 endfor = luaK_codeAsBx(fs, OP_TFORLOOP, base + 2, NO_JUMP);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1303 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1304 luaK_patchlist(fs, endfor, prep + 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1305 luaK_fixline(fs, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1306 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1307
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1308
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1309 static void fornum (LexState *ls, TString *varname, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1310 /* fornum -> NAME = exp1,exp1[,exp1] forbody */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1311 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1312 int base = fs->freereg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1313 new_localvarliteral(ls, "(for index)");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1314 new_localvarliteral(ls, "(for limit)");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1315 new_localvarliteral(ls, "(for step)");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1316 new_localvar(ls, varname);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1317 checknext(ls, '=');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1318 exp1(ls); /* initial value */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1319 checknext(ls, ',');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1320 exp1(ls); /* limit */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1321 if (testnext(ls, ','))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1322 exp1(ls); /* optional step */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1323 else { /* default step = 1 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1324 luaK_codek(fs, fs->freereg, luaK_numberK(fs, 1));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1325 luaK_reserveregs(fs, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1326 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1327 forbody(ls, base, line, 1, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1328 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1329
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1330
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1331 static void forlist (LexState *ls, TString *indexname) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1332 /* forlist -> NAME {,NAME} IN explist forbody */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1333 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1334 expdesc e;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1335 int nvars = 4; /* gen, state, control, plus at least one declared var */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1336 int line;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1337 int base = fs->freereg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1338 /* create control variables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1339 new_localvarliteral(ls, "(for generator)");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1340 new_localvarliteral(ls, "(for state)");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1341 new_localvarliteral(ls, "(for control)");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1342 /* create declared variables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1343 new_localvar(ls, indexname);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1344 while (testnext(ls, ',')) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1345 new_localvar(ls, str_checkname(ls));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1346 nvars++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1347 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1348 checknext(ls, TK_IN);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1349 line = ls->linenumber;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1350 adjust_assign(ls, 3, explist(ls, &e), &e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1351 luaK_checkstack(fs, 3); /* extra space to call generator */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1352 forbody(ls, base, line, nvars - 3, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1353 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1354
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1355
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1356 static void forstat (LexState *ls, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1357 /* forstat -> FOR (fornum | forlist) END */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1358 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1359 TString *varname;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1360 BlockCnt bl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1361 enterblock(fs, &bl, 1); /* scope for loop and control variables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1362 luaX_next(ls); /* skip `for' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1363 varname = str_checkname(ls); /* first variable name */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1364 switch (ls->t.token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1365 case '=': fornum(ls, varname, line); break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1366 case ',': case TK_IN: forlist(ls, varname); break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1367 default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1368 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1369 check_match(ls, TK_END, TK_FOR, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1370 leaveblock(fs); /* loop scope (`break' jumps to this point) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1371 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1372
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1373
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1374 static void test_then_block (LexState *ls, int *escapelist) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1375 /* test_then_block -> [IF | ELSEIF] cond THEN block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1376 BlockCnt bl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1377 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1378 expdesc v;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1379 int jf; /* instruction to skip 'then' code (if condition is false) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1380 luaX_next(ls); /* skip IF or ELSEIF */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1381 expr(ls, &v); /* read condition */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1382 checknext(ls, TK_THEN);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1383 if (ls->t.token == TK_GOTO || ls->t.token == TK_BREAK) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1384 luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1385 enterblock(fs, &bl, 0); /* must enter block before 'goto' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1386 gotostat(ls, v.t); /* handle goto/break */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1387 skipnoopstat(ls); /* skip other no-op statements */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1388 if (block_follow(ls, 0)) { /* 'goto' is the entire block? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1389 leaveblock(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1390 return; /* and that is it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1391 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1392 else /* must skip over 'then' part if condition is false */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1393 jf = luaK_jump(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1394 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1395 else { /* regular case (not goto/break) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1396 luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1397 enterblock(fs, &bl, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1398 jf = v.f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1399 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1400 statlist(ls); /* `then' part */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1401 leaveblock(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1402 if (ls->t.token == TK_ELSE ||
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1403 ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1404 luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1405 luaK_patchtohere(fs, jf);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1406 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1407
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1408
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1409 static void ifstat (LexState *ls, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1410 /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1411 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1412 int escapelist = NO_JUMP; /* exit list for finished parts */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1413 test_then_block(ls, &escapelist); /* IF cond THEN block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1414 while (ls->t.token == TK_ELSEIF)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1415 test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1416 if (testnext(ls, TK_ELSE))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1417 block(ls); /* `else' part */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1418 check_match(ls, TK_END, TK_IF, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1419 luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1420 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1421
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1422
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1423 static void localfunc (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1424 expdesc b;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1425 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1426 new_localvar(ls, str_checkname(ls)); /* new local variable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1427 adjustlocalvars(ls, 1); /* enter its scope */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1428 body(ls, &b, 0, ls->linenumber); /* function created in next register */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1429 /* debug information will only see the variable after this point! */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1430 getlocvar(fs, b.u.info)->startpc = fs->pc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1431 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1432
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1433
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1434 static void localstat (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1435 /* stat -> LOCAL NAME {`,' NAME} [`=' explist] */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1436 int nvars = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1437 int nexps;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1438 expdesc e;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1439 do {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1440 new_localvar(ls, str_checkname(ls));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1441 nvars++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1442 } while (testnext(ls, ','));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1443 if (testnext(ls, '='))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1444 nexps = explist(ls, &e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1445 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1446 e.k = VVOID;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1447 nexps = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1448 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1449 adjust_assign(ls, nvars, nexps, &e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1450 adjustlocalvars(ls, nvars);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1451 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1452
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1453
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1454 static int funcname (LexState *ls, expdesc *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1455 /* funcname -> NAME {fieldsel} [`:' NAME] */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1456 int ismethod = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1457 singlevar(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1458 while (ls->t.token == '.')
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1459 fieldsel(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1460 if (ls->t.token == ':') {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1461 ismethod = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1462 fieldsel(ls, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1463 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1464 return ismethod;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1465 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1466
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1467
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1468 static void funcstat (LexState *ls, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1469 /* funcstat -> FUNCTION funcname body */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1470 int ismethod;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1471 expdesc v, b;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1472 luaX_next(ls); /* skip FUNCTION */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1473 ismethod = funcname(ls, &v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1474 body(ls, &b, ismethod, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1475 luaK_storevar(ls->fs, &v, &b);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1476 luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1477 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1478
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1479
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1480 static void exprstat (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1481 /* stat -> func | assignment */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1482 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1483 struct LHS_assign v;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1484 suffixedexp(ls, &v.v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1485 if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1486 v.prev = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1487 assignment(ls, &v, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1488 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1489 else { /* stat -> func */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1490 check_condition(ls, v.v.k == VCALL, "syntax error");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1491 SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1492 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1493 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1494
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1495
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1496 static void retstat (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1497 /* stat -> RETURN [explist] [';'] */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1498 FuncState *fs = ls->fs;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1499 expdesc e;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1500 int first, nret; /* registers with returned values */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1501 if (block_follow(ls, 1) || ls->t.token == ';')
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1502 first = nret = 0; /* return no values */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1503 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1504 nret = explist(ls, &e); /* optional return values */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1505 if (hasmultret(e.k)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1506 luaK_setmultret(fs, &e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1507 if (e.k == VCALL && nret == 1) { /* tail call? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1508 SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1509 lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1510 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1511 first = fs->nactvar;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1512 nret = LUA_MULTRET; /* return all values */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1513 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1514 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1515 if (nret == 1) /* only one single value? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1516 first = luaK_exp2anyreg(fs, &e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1517 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1518 luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1519 first = fs->nactvar; /* return all `active' values */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1520 lua_assert(nret == fs->freereg - first);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1521 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1522 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1523 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1524 luaK_ret(fs, first, nret);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1525 testnext(ls, ';'); /* skip optional semicolon */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1526 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1527
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1528
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1529 static void statement (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1530 int line = ls->linenumber; /* may be needed for error messages */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1531 enterlevel(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1532 switch (ls->t.token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1533 case ';': { /* stat -> ';' (empty statement) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1534 luaX_next(ls); /* skip ';' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1535 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1536 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1537 case TK_IF: { /* stat -> ifstat */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1538 ifstat(ls, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1539 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1540 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1541 case TK_WHILE: { /* stat -> whilestat */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1542 whilestat(ls, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1543 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1544 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1545 case TK_DO: { /* stat -> DO block END */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1546 luaX_next(ls); /* skip DO */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1547 block(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1548 check_match(ls, TK_END, TK_DO, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1549 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1550 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1551 case TK_FOR: { /* stat -> forstat */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1552 forstat(ls, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1553 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1554 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1555 case TK_REPEAT: { /* stat -> repeatstat */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1556 repeatstat(ls, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1557 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1558 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1559 case TK_FUNCTION: { /* stat -> funcstat */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1560 funcstat(ls, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1561 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1562 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1563 case TK_LOCAL: { /* stat -> localstat */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1564 luaX_next(ls); /* skip LOCAL */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1565 if (testnext(ls, TK_FUNCTION)) /* local function? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1566 localfunc(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1567 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1568 localstat(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1569 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1570 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1571 case TK_DBCOLON: { /* stat -> label */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1572 luaX_next(ls); /* skip double colon */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1573 labelstat(ls, str_checkname(ls), line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1574 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1575 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1576 case TK_RETURN: { /* stat -> retstat */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1577 luaX_next(ls); /* skip RETURN */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1578 retstat(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1579 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1580 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1581 case TK_BREAK: /* stat -> breakstat */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1582 case TK_GOTO: { /* stat -> 'goto' NAME */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1583 gotostat(ls, luaK_jump(ls->fs));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1584 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1585 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1586 default: { /* stat -> func | assignment */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1587 exprstat(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1588 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1589 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1590 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1591 lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1592 ls->fs->freereg >= ls->fs->nactvar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1593 ls->fs->freereg = ls->fs->nactvar; /* free registers */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1594 leavelevel(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1595 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1596
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1597 /* }====================================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1598
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1599
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1600 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1601 ** compiles the main function, which is a regular vararg function with an
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1602 ** upvalue named LUA_ENV
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1603 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1604 static void mainfunc (LexState *ls, FuncState *fs) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1605 BlockCnt bl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1606 expdesc v;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1607 open_func(ls, fs, &bl);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1608 fs->f->is_vararg = 1; /* main function is always vararg */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1609 init_exp(&v, VLOCAL, 0); /* create and... */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1610 newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1611 luaX_next(ls); /* read first token */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1612 statlist(ls); /* parse main body */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1613 check(ls, TK_EOS);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1614 close_func(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1615 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1616
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1617
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1618 Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1619 Dyndata *dyd, const char *name, int firstchar) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1620 LexState lexstate;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1621 FuncState funcstate;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1622 Closure *cl = luaF_newLclosure(L, 1); /* create main closure */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1623 /* anchor closure (to avoid being collected) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1624 setclLvalue(L, L->top, cl);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1625 incr_top(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1626 funcstate.f = cl->l.p = luaF_newproto(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1627 funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1628 lexstate.buff = buff;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1629 lexstate.dyd = dyd;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1630 dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1631 luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1632 mainfunc(&lexstate, &funcstate);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1633 lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1634 /* all scopes should be correctly finished */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1635 lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1636 return cl; /* it's on the stack too */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1637 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1638