History

Lua 3.0

Lua Team committed 29 years ago

Commit 9bc113c1 parent 100a7f178 files · +6569 −11586Browse files

Showing the changes to src (33 of 78 files). Show all

Unified · Split

33 files
  1. src/Makefile +12 −6
  2. src/auxlib.c +81 −0
  3. src/auxlib.h +30 −0
  4. src/fallback.c +286 −109
  5. src/fallback.h +50 −22
  6. src/func.c +26 −16
  7. src/func.h +3 −2
  8. src/hash.c +147 −134
  9. src/hash.h +17 −16
  10. src/inout.c +278 −170
  11. src/inout.h +8 −17
  12. src/lex.c +174 −45
  13. src/lex.h +3 −4
  14. src/lua.stx +29 −34
  15. src/luac/dump.c +11 −19
  16. src/luac/luac.c +112 −27
  17. src/luac/luac.h +6 −5
  18. src/luac/print.c +213 −20
  19. src/luac/print.h +6 −3
  20. src/luamem.c +159 −0
  21. src/luamem.h +3 −3
  22. src/mem.c +0 −58
  23. src/opcode.c +517 −369
  24. src/opcode.h +46 −25
  25. src/parser.c +344 −316
  26. src/table.c +84 −96
  27. src/table.h +6 −5
  28. src/tree.c +92 −26
  29. src/tree.h +14 −4
  30. src/undump.c +71 −74
  31. src/undump.h +11 −4
  32. src/zio.c +79 −0
  33. src/zio.h +48 −0
src/Makefile+12−6
@@ -4,19 +4,23 @@ LUA= .. · 3 unmodified lines
4 4
5include $(LUA)/config5include $(LUA)/config
6 6
7OBJS= fallback.o\7OBJS= auxlib.o\
8 fallback.o\
8 func.o\9 func.o\
9 hash.o\10 hash.o\
10 inout.o\11 inout.o\
11 lex.o\12 lex.o\
12 mem.o\13 luamem.o\
13 opcode.o\14 opcode.o\
14 parser.o\15 parser.o\
15 table.o\16 table.o\
16 tree.o\17 tree.o\
17 undump.o18 undump.o\
19 zio.o
18 20
19SRCS= fallback.c\21SRCS= auxlib.c\
22 auxlib.h\
23 fallback.c\
20 fallback.h\24 fallback.h\
21 func.c\25 func.c\
22 func.h\26 func.h\
@@ -26,8 +30,8 @@ SRCS= fallback.c\ · 3 unmodified lines
26 inout.h\30 inout.h\
27 lex.c\31 lex.c\
28 lex.h\32 lex.h\
29 mem.c\33 luamem.c\
30 mem.h\34 luamem.h\
31 opcode.c\35 opcode.c\
32 opcode.h\36 opcode.h\
33 parser.c\37 parser.c\
@@ -39,6 +43,8 @@ SRCS= fallback.c\ · 5 unmodified lines
39 types.h\43 types.h\
40 undump.c\44 undump.c\
41 undump.h\45 undump.h\
46 zio.c\
47 zio.h\
42 lua.stx48 lua.stx
43 49
44SLIB= $(LIB)/liblua.a50SLIB= $(LIB)/liblua.a
src/auxlib.c+81−0
@@ -0,0 +1,81 @@
1char *rcs_auxlib="$Id: auxlib.c,v 1.5 1997/04/14 15:30:03 roberto Exp $";
2
3#include <stdio.h>
4#include <stdarg.h>
5#include <string.h>
6
7#include "lua.h"
8#include "auxlib.h"
9#include "luadebug.h"
10
11
12
13int luaI_findstring (char *name, char *list[])
14{
15 int i;
16 for (i=0; list[i]; i++)
17 if (strcmp(list[i], name) == 0)
18 return i;
19 return -1; /* name not found */
20}
21
22
23void luaL_arg_check(int cond, int numarg, char *extramsg)
24{
25 if (!cond) {
26 char *funcname;
27 lua_getobjname(lua_stackedfunction(0), &funcname);
28 if (funcname == NULL)
29 funcname = "???";
30 if (extramsg == NULL)
31 luaL_verror("bad argument #%d to function `%s'", numarg, funcname);
32 else
33 luaL_verror("bad argument #%d to function `%s' (%s)",
34 numarg, funcname, extramsg);
35 }
36}
37
38char *luaL_check_string (int numArg)
39{
40 lua_Object o = lua_getparam(numArg);
41 luaL_arg_check(lua_isstring(o), numArg, "string expected");
42 return lua_getstring(o);
43}
44
45char *luaL_opt_string (int numArg, char *def)
46{
47 return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
48 luaL_check_string(numArg);
49}
50
51double luaL_check_number (int numArg)
52{
53 lua_Object o = lua_getparam(numArg);
54 luaL_arg_check(lua_isnumber(o), numArg, "number expected");
55 return lua_getnumber(o);
56}
57
58
59double luaL_opt_number (int numArg, double def)
60{
61 return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
62 luaL_check_number(numArg);
63}
64
65void luaL_openlib (struct luaL_reg *l, int n)
66{
67 int i;
68 for (i=0; i<n; i++)
69 lua_register(l[i].name, l[i].func);
70}
71
72
73void luaL_verror (char *fmt, ...)
74{
75 char buff[1000];
76 va_list argp;
77 va_start(argp, fmt);
78 vsprintf(buff, fmt, argp);
79 va_end(argp);
80 lua_error(buff);
81}
src/auxlib.h+30−0
@@ -0,0 +1,30 @@
1/*
2** $Id: auxlib.h,v 1.3 1997/04/07 14:48:53 roberto Exp $
3*/
4
5#ifndef auxlib_h
6#define auxlib_h
7
8#include "lua.h"
9
10struct luaL_reg {
11 char *name;
12 lua_CFunction func;
13};
14
15void luaL_openlib (struct luaL_reg *l, int n);
16void luaL_arg_check(int cond, int numarg, char *extramsg);
17char *luaL_check_string (int numArg);
18char *luaL_opt_string (int numArg, char *def);
19double luaL_check_number (int numArg);
20double luaL_opt_number (int numArg, double def);
21void luaL_verror (char *fmt, ...);
22
23
24
25/* -- private part (only for Lua modules */
26
27int luaI_findstring (char *name, char *list[]);
28
29
30#endif
src/fallback.c+286−109
@@ -3,130 +3,37 @@ · 2 unmodified lines
3** TecCGraf - PUC-Rio3** TecCGraf - PUC-Rio
4*/4*/
5 5
6char *rcs_fallback="$Id: fallback.c,v 1.25 1996/04/25 14:10:00 roberto Exp $";6char *rcs_fallback="$Id: fallback.c,v 2.9 1997/06/23 18:27:53 roberto Exp $";
7 7
8#include <stdio.h>8#include <stdio.h>
9#include <string.h>9#include <string.h>
10 10
11#include "mem.h"11#include "auxlib.h"
12#include "luamem.h"
12#include "fallback.h"13#include "fallback.h"
13#include "opcode.h"14#include "opcode.h"
14#include "lua.h"15#include "lua.h"
15#include "table.h"16#include "table.h"
17#include "tree.h"
18#include "hash.h"
16 19
17 20
18static void errorFB (void);
19static void indexFB (void);
20static void gettableFB (void);
21static void arithFB (void);
22static void concatFB (void);
23static void orderFB (void);
24static void GDFB (void);
25static void funcFB (void);
26 21
27 22/* -------------------------------------------
28/*
29** Warning: This list must be in the same order as the #define's
30*/
31struct FB luaI_fallBacks[] = {
32{"error", {LUA_T_CFUNCTION, {errorFB}}, 1, 0},
33{"index", {LUA_T_CFUNCTION, {indexFB}}, 2, 1},
34{"gettable", {LUA_T_CFUNCTION, {gettableFB}}, 2, 1},
35{"arith", {LUA_T_CFUNCTION, {arithFB}}, 3, 1},
36{"order", {LUA_T_CFUNCTION, {orderFB}}, 3, 1},
37{"concat", {LUA_T_CFUNCTION, {concatFB}}, 2, 1},
38{"settable", {LUA_T_CFUNCTION, {gettableFB}}, 3, 0},
39{"gc", {LUA_T_CFUNCTION, {GDFB}}, 1, 0},
40{"function", {LUA_T_CFUNCTION, {funcFB}}, -1, -1},
41 /* no fixed number of params or results */
42{"getglobal", {LUA_T_CFUNCTION, {indexFB}}, 1, 1}
43 /* same default behavior of index FB */
44};
45
46#define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
47
48void luaI_setfallback (void)
49{
50 int i;
51 char *name = lua_getstring(lua_getparam(1));
52 lua_Object func = lua_getparam(2);
53 if (name == NULL || !lua_isfunction(func))
54 lua_error("incorrect argument to function `setfallback'");
55 for (i=0; i<N_FB; i++)
56 {
57 if (strcmp(luaI_fallBacks[i].kind, name) == 0)
58 {
59 luaI_pushobject(&luaI_fallBacks[i].function);
60 luaI_fallBacks[i].function = *luaI_Address(func);
61 return;
62 }
63 }
64 /* name not found */
65 lua_error("incorrect argument to function `setfallback'");
66}
67
68
69static void errorFB (void)
70{
71 lua_Object o = lua_getparam(1);
72 if (lua_isstring(o))
73 fprintf (stderr, "lua: %s\n", lua_getstring(o));
74 else
75 fprintf(stderr, "lua: unknown error\n");
76}
77
78
79static void indexFB (void)
80{
81 lua_pushnil();
82}
83
84
85static void gettableFB (void)
86{
87 lua_error("indexed expression not a table");
88}
89
90
91static void arithFB (void)
92{
93 lua_error("unexpected type at conversion to number");
94}
95
96static void concatFB (void)
97{
98 lua_error("unexpected type at conversion to string");
99}
100
101
102static void orderFB (void)
103{
104 lua_error("unexpected type at comparison");
105}
106
107static void GDFB (void) { }
108
109static void funcFB (void)
110{
111 lua_error("call expression not a function");
112}
113
114
115/*
116** Reference routines23** Reference routines
117*/24*/
118 25
119static struct ref {26static struct ref {
120 Object o;27 TObject o;
121 enum {LOCK, HOLD, FREE, COLLECTED} status;28 enum {LOCK, HOLD, FREE, COLLECTED} status;
122} *refArray = NULL;29} *refArray = NULL;
123static int refSize = 0;30static int refSize = 0;
124 31
125int luaI_ref (Object *object, int lock)32int luaI_ref (TObject *object, int lock)
126{33{
127 int i;34 int i;
128 int oldSize;35 int oldSize;
129 if (tag(object) == LUA_T_NIL)36 if (ttype(object) == LUA_T_NIL)
130 return -1; /* special ref for nil */37 return -1; /* special ref for nil */
131 for (i=0; i<refSize; i++)38 for (i=0; i<refSize; i++)
132 if (refArray[i].status == FREE)39 if (refArray[i].status == FREE)
@@ -151,9 +58,9 @@ void lua_unref (int ref) · 18 unmodified lines
151}58}
152 59
153 60
154Object *luaI_getref (int ref)61TObject *luaI_getref (int ref)
155{62{
156 static Object nul = {LUA_T_NIL, {0}};63 static TObject nul = {LUA_T_NIL, {0}};
157 if (ref == -1)64 if (ref == -1)
158 return &nul;65 return &nul;
159 if (ref >= 0 && ref < refSize &&66 if (ref >= 0 && ref < refSize &&
@@ -164,7 +71,7 @@ Object *luaI_getref (int ref) · 4 unmodified lines
164}71}
165 72
166 73
167void luaI_travlock (int (*fn)(Object *))74void luaI_travlock (int (*fn)(TObject *))
168{75{
169 int i;76 int i;
170 for (i=0; i<refSize; i++)77 for (i=0; i<refSize; i++)
@@ -181,11 +88,281 @@ void luaI_invalidaterefs (void) · 10 unmodified lines
181 refArray[i].status = COLLECTED;88 refArray[i].status = COLLECTED;
182}89}
183 90
184char *luaI_travfallbacks (int (*fn)(Object *))91
92/* -------------------------------------------
93* Internal Methods
94*/
95
96char *luaI_eventname[] = { /* ORDER IM */
97 "gettable", "settable", "index", "getglobal", "setglobal", "add",
98 "sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge",
99 "concat", "gc", "function",
100 NULL
101};
102
103
104
105static int luaI_checkevent (char *name, char *list[])
106{
107 int e = luaI_findstring(name, list);
108 if (e < 0)
109 luaL_verror("`%s' is not a valid event name", name);
110 return e;
111}
112
113
114struct IM *luaI_IMtable = NULL;
115
116static int IMtable_size = 0;
117static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
118
119
120/* events in LUA_T_LINE are all allowed, since this is used as a
121* 'placeholder' for "default" fallbacks
122*/
123static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */
124{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
125{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_LINE */
126{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */
127{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */
128{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
129{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
130{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
131{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
132{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */
133{1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */
134};
135
136static int validevent (lua_Type t, int e)
137{ /* ORDER LUA_T */
138 return (t < LUA_T_NIL) ? 1 : validevents[-t][e];
139}
140
141
142static void init_entry (int tag)
185{143{
186 int i;144 int i;
187 for (i=0; i<N_FB; i++)145 for (i=0; i<IM_N; i++)
188 if (fn(&luaI_fallBacks[i].function))146 ttype(luaI_getim(tag, i)) = LUA_T_NIL;
189 return luaI_fallBacks[i].kind;147}
148
149void luaI_initfallbacks (void)
150{
151 if (luaI_IMtable == NULL) {
152 int i;
153 IMtable_size = NUM_TYPES+10;
154 luaI_IMtable = newvector(IMtable_size, struct IM);
155 for (i=LUA_T_NIL; i<=LUA_T_USERDATA; i++)
156 init_entry(i);
157 }
158}
159
160int lua_newtag (void)
161{
162 --last_tag;
163 if ((-last_tag) >= IMtable_size) {
164 luaI_initfallbacks();
165 IMtable_size = growvector(&luaI_IMtable, IMtable_size,
166 struct IM, memEM, MAX_INT);
167 }
168 init_entry(last_tag);
169 return last_tag;
170}
171
172
173static void checktag (int tag)
174{
175 if (!(last_tag <= tag && tag <= 0))
176 luaL_verror("%d is not a valid tag", tag);
177}
178
179void luaI_realtag (int tag)
180{
181 if (!(last_tag <= tag && tag < LUA_T_NIL))
182 luaL_verror("tag %d is not result of `newtag'", tag);
183}
184
185
186void luaI_settag (int tag, TObject *o)
187{
188 luaI_realtag(tag);
189 switch (ttype(o)) {
190 case LUA_T_ARRAY:
191 o->value.a->htag = tag;
192 break;
193 case LUA_T_USERDATA:
194 o->value.ts->tag = tag;
195 break;
196 default:
197 luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]);
198 }
199}
200
201
202int luaI_efectivetag (TObject *o)
203{
204 lua_Type t = ttype(o);
205 if (t == LUA_T_USERDATA) {
206 int tag = o->value.ts->tag;
207 return (tag >= 0) ? LUA_T_USERDATA : tag;
208 }
209 else if (t == LUA_T_ARRAY)
210 return o->value.a->htag;
211 else return t;
212}
213
214
215void luaI_gettagmethod (void)
216{
217 int t = (int)luaL_check_number(1);
218 int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
219 checktag(t);
220 if (validevent(t, e))
221 luaI_pushobject(luaI_getim(t,e));
222}
223
224
225void luaI_settagmethod (void)
226{
227 int t = (int)luaL_check_number(1);
228 int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
229 lua_Object func = lua_getparam(3);
230 checktag(t);
231 if (!validevent(t, e))
232 luaL_verror("cannot change internal method `%s' for tag %d",
233 luaI_eventname[e], t);
234 luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
235 3, "function expected");
236 luaI_pushobject(luaI_getim(t,e));
237 *luaI_getim(t, e) = *luaI_Address(func);
238}
239
240
241static void stderrorim (void)
242{
243 lua_Object s = lua_getparam(1);
244 if (lua_isstring(s))
245 fprintf(stderr, "lua: %s\n", lua_getstring(s));
246}
247
248static TObject errorim = {LUA_T_CFUNCTION, {stderrorim}};
249
250
251TObject *luaI_geterrorim (void)
252{
253 return &errorim;
254}
255
256void luaI_seterrormethod (void)
257{
258 lua_Object func = lua_getparam(1);
259 luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
260 1, "function expected");
261 luaI_pushobject(&errorim);
262 errorim = *luaI_Address(func);
263}
264
265char *luaI_travfallbacks (int (*fn)(TObject *))
266{
267 int e;
268 if (fn(&errorim))
269 return "error";
270 for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
271 int t;
272 for (t=0; t>=last_tag; t--)
273 if (fn(luaI_getim(t,e)))
274 return luaI_eventname[e];
275 }
190 return NULL;276 return NULL;
191}277}
278
279
280/*
281* ===================================================================
282* compatibility with old fallback system
283*/
284#if LUA_COMPAT2_5
285
286static void errorFB (void)
287{
288 lua_Object o = lua_getparam(1);
289 if (lua_isstring(o))
290 fprintf (stderr, "lua: %s\n", lua_getstring(o));
291 else
292 fprintf(stderr, "lua: unknown error\n");
293}
294
295
296static void nilFB (void) { }
297
298
299static void typeFB (void)
300{
301 lua_error("unexpected type");
302}
303
304
305static void fillvalids (IMS e, TObject *func)
306{
307 int t;
308 for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
309 if (validevent(t, e))
310 *luaI_getim(t, e) = *func;
311}
312
313
314void luaI_setfallback (void)
315{
316 static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
317 TObject oldfunc;
318 lua_CFunction replace;
319 char *name = luaL_check_string(1);
320 lua_Object func = lua_getparam(2);
321 luaI_initfallbacks();
322 luaL_arg_check(lua_isfunction(func), 2, "function expected");
323 switch (luaI_findstring(name, oldnames)) {
324 case 0: /* old error fallback */
325 oldfunc = errorim;
326 errorim = *luaI_Address(func);
327 replace = errorFB;
328 break;
329 case 1: /* old getglobal fallback */
330 oldfunc = *luaI_getim(LUA_T_NIL, IM_GETGLOBAL);
331 *luaI_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaI_Address(func);
332 replace = nilFB;
333 break;
334 case 2: { /* old arith fallback */
335 int i;
336 oldfunc = *luaI_getim(LUA_T_NUMBER, IM_POW);
337 for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
338 fillvalids(i, luaI_Address(func));
339 replace = typeFB;
340 break;
341 }
342 case 3: { /* old order fallback */
343 int i;
344 oldfunc = *luaI_getim(LUA_T_LINE, IM_LT);
345 for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
346 fillvalids(i, luaI_Address(func));
347 replace = typeFB;
348 break;
349 }
350 default: {
351 int e;
352 if ((e = luaI_findstring(name, luaI_eventname)) >= 0) {
353 oldfunc = *luaI_getim(LUA_T_LINE, e);
354 fillvalids(e, luaI_Address(func));
355 replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
356 }
357 else {
358 luaL_verror("`%s' is not a valid fallback name", name);
359 replace = NULL; /* to avoid warnings */
360 }
361 }
362 }
363 if (oldfunc.ttype != LUA_T_NIL)
364 luaI_pushobject(&oldfunc);
365 else
366 lua_pushcfunction(replace);
367}
368#endif
src/fallback.h+50−22
@@ -1,5 +1,5 @@
1/*1/*
2** $Id: fallback.h,v 1.13 1996/04/25 14:10:00 roberto Exp $2** $Id: fallback.h,v 1.23 1997/04/24 22:59:57 roberto Exp $
3*/3*/
4 4
5#ifndef fallback_h5#ifndef fallback_h
@@ -8,30 +8,58 @@ · 2 unmodified lines
8#include "lua.h"8#include "lua.h"
9#include "opcode.h"9#include "opcode.h"
10 10
11extern struct FB {11/*
12 char *kind;12* WARNING: if you change the order of this enumeration,
13 Object function;13* grep "ORDER IM"
14 int nParams;14*/
15 int nResults;15typedef enum {
16} luaI_fallBacks[];16 IM_GETTABLE = 0,
17 17 IM_SETTABLE,
18#define FB_ERROR 018 IM_INDEX,
19#define FB_INDEX 119 IM_GETGLOBAL,
20#define FB_GETTABLE 220 IM_SETGLOBAL,
21#define FB_ARITH 321 IM_ADD,
22#define FB_ORDER 422 IM_SUB,
23#define FB_CONCAT 523 IM_MUL,
24#define FB_SETTABLE 624 IM_DIV,
25#define FB_GC 725 IM_POW,
26#define FB_FUNCTION 826 IM_UNM,
27#define FB_GETGLOBAL 927 IM_LT,
28 IM_LE,
29 IM_GT,
30 IM_GE,
31 IM_CONCAT,
32 IM_GC,
33 IM_FUNCTION
34} IMS;
35
36#define IM_N 18
37
38
39extern struct IM {
40 TObject int_method[IM_N];
41} *luaI_IMtable;
42
43extern char *luaI_eventname[];
44
45#define luaI_getim(tag,event) (&luaI_IMtable[-(tag)].int_method[event])
46#define luaI_getimbyObj(o,e) (luaI_getim(luaI_efectivetag(o),(e)))
28 47
29void luaI_setfallback (void);48void luaI_setfallback (void);
30int luaI_ref (Object *object, int lock);49int luaI_ref (TObject *object, int lock);
31Object *luaI_getref (int ref);50TObject *luaI_getref (int ref);
32void luaI_travlock (int (*fn)(Object *));51void luaI_travlock (int (*fn)(TObject *));
33void luaI_invalidaterefs (void);52void luaI_invalidaterefs (void);
34char *luaI_travfallbacks (int (*fn)(Object *));53char *luaI_travfallbacks (int (*fn)(TObject *));
54
55void luaI_settag (int tag, TObject *o);
56void luaI_realtag (int tag);
57TObject *luaI_geterrorim (void);
58int luaI_efectivetag (TObject *o);
59void luaI_settagmethod (void);
60void luaI_gettagmethod (void);
61void luaI_seterrormethod (void);
62void luaI_initfallbacks (void);
35 63
36#endif64#endif
37 65
src/func.c+26−16
@@ -2,9 +2,10 @@ · 1 unmodified line
2 2
3#include "luadebug.h"3#include "luadebug.h"
4#include "table.h"4#include "table.h"
5#include "mem.h"5#include "luamem.h"
6#include "func.h"6#include "func.h"
7#include "opcode.h"7#include "opcode.h"
8#include "inout.h"
8 9
9 10
10static TFunc *function_root = NULL;11static TFunc *function_root = NULL;
@@ -23,7 +24,7 @@ void luaI_initTFunc (TFunc *f) · 12 unmodified lines
23 f->size = 0;24 f->size = 0;
24 f->code = NULL;25 f->code = NULL;
25 f->lineDefined = 0;26 f->lineDefined = 0;
26 f->fileName = NULL;27 f->fileName = lua_parsedfile;
27 f->locvars = NULL;28 f->locvars = NULL;
28}29}
29 30
@@ -49,47 +50,56 @@ void luaI_freefunc (TFunc *f) · 19 unmodified lines
49 luaI_free (f);50 luaI_free (f);
50}51}
51 52
53
54void luaI_funcfree (TFunc *l)
55{
56 while (l) {
57 TFunc *next = l->next;
58 luaI_freefunc(l);
59 l = next;
60 }
61}
62
52/*63/*
53** Garbage collection function.64** Garbage collection function.
54** This function traverse the function list freeing unindexed functions
55*/65*/
56Long luaI_funccollector (void)66TFunc *luaI_funccollector (long *acum)
57{67{
58 TFunc *curr = function_root;68 TFunc *curr = function_root;
59 TFunc *prev = NULL;69 TFunc *prev = NULL;
60 Long counter = 0;70 TFunc *frees = NULL;
61 while (curr)71 long counter = 0;
62 {72 while (curr) {
63 TFunc *next = curr->next;73 TFunc *next = curr->next;
64 if (!curr->marked)74 if (!curr->marked) {
65 {
66 if (prev == NULL)75 if (prev == NULL)
67 function_root = next;76 function_root = next;
68 else77 else
69 prev->next = next;78 prev->next = next;
70 luaI_freefunc (curr);79 curr->next = frees;
80 frees = curr;
71 ++counter;81 ++counter;
72 }82 }
73 else83 else {
74 {
75 curr->marked = 0;84 curr->marked = 0;
76 prev = curr;85 prev = curr;
77 } 86 }
78 curr = next;87 curr = next;
79 }88 }
80 return counter;89 *acum += counter;
90 return frees;
81}91}
82 92
83 93
84void lua_funcinfo (lua_Object func, char **filename, int *linedefined)94void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
85{95{
86 Object *f = luaI_Address(func);96 TObject *f = luaI_Address(func);
87 if (f->tag == LUA_T_MARK || f->tag == LUA_T_FUNCTION)97 if (f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION)
88 {98 {
89 *filename = f->value.tf->fileName;99 *filename = f->value.tf->fileName;
90 *linedefined = f->value.tf->lineDefined;100 *linedefined = f->value.tf->lineDefined;
91 }101 }
92 else if (f->tag == LUA_T_CMARK || f->tag == LUA_T_CFUNCTION)102 else if (f->ttype == LUA_T_CMARK || f->ttype == LUA_T_CFUNCTION)
93 {103 {
94 *filename = "(C)";104 *filename = "(C)";
95 *linedefined = -1;105 *linedefined = -1;
src/func.h+3−2
@@ -1,5 +1,5 @@
1/*1/*
2** $Id: func.h,v 1.8 1996/03/14 15:54:20 roberto Exp $2** $Id: func.h,v 1.9 1997/05/14 18:38:29 roberto Exp $
3*/3*/
4 4
5#ifndef func_h5#ifndef func_h
@@ -30,7 +30,8 @@ typedef struct TFunc · 24 unmodified lines
30 LocVar *locvars;30 LocVar *locvars;
31} TFunc;31} TFunc;
32 32
33Long luaI_funccollector (void);33TFunc *luaI_funccollector (long *cont);
34void luaI_funcfree (TFunc *l);
34void luaI_insertfunction (TFunc *f);35void luaI_insertfunction (TFunc *f);
35 36
36void luaI_initTFunc (TFunc *f);37void luaI_initTFunc (TFunc *f);
src/hash.c+147−134
@@ -3,14 +3,15 @@ · 2 unmodified lines
3** hash manager for lua3** hash manager for lua
4*/4*/
5 5
6char *rcs_hash="$Id: hash.c,v 2.32 1996/11/18 13:48:44 roberto Exp $";6char *rcs_hash="$Id: hash.c,v 2.43 1997/05/14 18:38:29 roberto Exp $";
7 7
8 8
9#include "mem.h"9#include "luamem.h"
10#include "opcode.h"10#include "opcode.h"
11#include "hash.h"11#include "hash.h"
12#include "table.h"12#include "table.h"
13#include "lua.h"13#include "lua.h"
14#include "auxlib.h"
14 15
15 16
16#define nhash(t) ((t)->nhash)17#define nhash(t) ((t)->nhash)
@@ -24,13 +25,15 @@ char *rcs_hash="$Id: hash.c,v 2.32 1996/11/18 13:48:44 roberto Exp $"; · 7 unmodified lines
24 25
25#define REHASH_LIMIT 0.70 /* avoid more than this % full */26#define REHASH_LIMIT 0.70 /* avoid more than this % full */
26 27
28#define TagDefault LUA_T_ARRAY;
29
27 30
28static Hash *listhead = NULL;31static Hash *listhead = NULL;
29 32
30 33
31/* hash dimensions values */34/* hash dimensions values */
32static Long dimensions[] = 35static Long dimensions[] =
33 {3L, 5L, 7L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,36 {5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
34 12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,37 12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
35 1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};38 1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
36 39
@@ -46,16 +49,32 @@ int luaI_redimension (int nhash) · 9 unmodified lines
46 return 0; /* to avoid warnings */49 return 0; /* to avoid warnings */
47}50}
48 51
49static int hashindex (Hash *t, Object *ref) /* hash function */52
53int lua_equalObj (TObject *t1, TObject *t2)
54{
55 if (ttype(t1) != ttype(t2)) return 0;
56 switch (ttype(t1))
57 {
58 case LUA_T_NIL: return 1;
59 case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
60 case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2);
61 case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
62 case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf;
63 case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
64 default:
65 lua_error("internal error in `lua_equalObj'");
66 return 0; /* UNREACHEABLE */
67 }
68}
69
70
71static long int hashindex (TObject *ref)
50{72{
51 long int h;73 long int h;
52 switch (tag(ref)) {74 switch (ttype(ref)) {
53 case LUA_T_NIL:
54 lua_error ("unexpected type to index table");
55 h = 0; /* UNREACHEABLE */
56 case LUA_T_NUMBER:75 case LUA_T_NUMBER:
57 h = (long int)nvalue(ref); break;76 h = (long int)nvalue(ref); break;
58 case LUA_T_STRING:77 case LUA_T_STRING: case LUA_T_USERDATA:
59 h = tsvalue(ref)->hash; break;78 h = tsvalue(ref)->hash; break;
60 case LUA_T_FUNCTION:79 case LUA_T_FUNCTION:
61 h = (IntPoint)ref->value.tf; break;80 h = (IntPoint)ref->value.tf; break;
@@ -63,38 +82,29 @@ static int hashindex (Hash *t, Object *ref) /* hash function */ · 1 unmodified line
63 h = (IntPoint)fvalue(ref); break;82 h = (IntPoint)fvalue(ref); break;
64 case LUA_T_ARRAY:83 case LUA_T_ARRAY:
65 h = (IntPoint)avalue(ref); break;84 h = (IntPoint)avalue(ref); break;
66 default: /* user data */85 default:
67 h = (IntPoint)uvalue(ref); break;86 lua_error ("unexpected type to index table");
87 h = 0; /* UNREACHEABLE */
68 }88 }
69 if (h < 0) h = -h;89 if (h < 0) h = -h;
70 return h%nhash(t); /* make it a valid index */90 return h;
71}91}
72 92
73int lua_equalObj (Object *t1, Object *t2)93
94static int present (Hash *t, TObject *key)
74{95{
75 if (tag(t1) != tag(t2)) return 0;96 long int h = hashindex(key);
76 switch (tag(t1))97 int tsize = nhash(t);
77 {98 int h1 = h%tsize;
78 case LUA_T_NIL: return 1;99 TObject *rf = ref(node(t, h1));
79 case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);100 if (ttype(rf) != LUA_T_NIL && !lua_equalObj(key, rf)) {
80 case LUA_T_STRING: return svalue(t1) == svalue(t2);101 int h2 = h%(tsize-2) + 1;
81 case LUA_T_ARRAY: return avalue(t1) == avalue(t2);102 do {
82 case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf;103 h1 = (h1+h2)%tsize;
83 case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);104 rf = ref(node(t, h1));
84 default: return uvalue(t1) == uvalue(t2);105 } while (ttype(rf) != LUA_T_NIL && !lua_equalObj(key, rf));
85 }106 }
86}107 return h1;
87
88static int present (Hash *t, Object *ref)
89{
90 int h = hashindex(t, ref);
91 while (tag(ref(node(t, h))) != LUA_T_NIL)
92 {
93 if (lua_equalObj(ref, ref(node(t, h))))
94 return h;
95 h = (h+1) % nhash(t);
96 }
97 return h;
98}108}
99 109
100 110
@@ -106,7 +116,7 @@ static Node *hashnodecreate (int nhash) · 5 unmodified lines
106 int i;116 int i;
107 Node *v = newvector (nhash, Node);117 Node *v = newvector (nhash, Node);
108 for (i=0; i<nhash; i++)118 for (i=0; i<nhash; i++)
109 tag(ref(&v[i])) = LUA_T_NIL;119 ttype(ref(&v[i])) = LUA_T_NIL;
110 return v;120 return v;
111}121}
112 122
@@ -121,6 +131,7 @@ static Hash *hashcreate (int nhash) · 8 unmodified lines
121 nhash(t) = nhash;131 nhash(t) = nhash;
122 nuse(t) = 0;132 nuse(t) = 0;
123 markarray(t) = 0;133 markarray(t) = 0;
134 t->htag = TagDefault;
124 return t;135 return t;
125}136}
126 137
@@ -146,7 +157,7 @@ void lua_hashmark (Hash *h) · 19 unmodified lines
146 for (i=0; i<nhash(h); i++)157 for (i=0; i<nhash(h); i++)
147 {158 {
148 Node *n = node(h,i);159 Node *n = node(h,i);
149 if (tag(ref(n)) != LUA_T_NIL)160 if (ttype(ref(n)) != LUA_T_NIL)
150 {161 {
151 lua_markobject(&n->ref);162 lua_markobject(&n->ref);
152 lua_markobject(&n->val);163 lua_markobject(&n->val);
@@ -156,49 +167,50 @@ void lua_hashmark (Hash *h) · 3 unmodified lines
156}167}
157 168
158 169
159static void call_fallbacks (void)170void luaI_hashcallIM (Hash *l)
160{171{
161 Hash *curr_array;172 TObject t;
162 Object t;173 ttype(&t) = LUA_T_ARRAY;
163 tag(&t) = LUA_T_ARRAY;174 for (; l; l=l->next) {
164 for (curr_array = listhead; curr_array; curr_array = curr_array->next)175 avalue(&t) = l;
165 if (markarray(curr_array) != 1)176 luaI_gcIM(&t);
166 {177 }
167 avalue(&t) = curr_array;
168 luaI_gcFB(&t);
169 }
170 tag(&t) = LUA_T_NIL;
171 luaI_gcFB(&t); /* end of list */
172}178}
173 179
174 180
175/*181void luaI_hashfree (Hash *frees)
176** Garbage collection to arrays
177** Delete all unmarked arrays.
178*/
179Long lua_hashcollector (void)
180{182{
181 Hash *curr_array = listhead, *prev = NULL;183 while (frees) {
182 Long counter = 0;184 Hash *next = frees->next;
183 call_fallbacks();185 hashdelete(frees);
184 while (curr_array != NULL)186 frees = next;
185 {
186 Hash *next = curr_array->next;
187 if (markarray(curr_array) != 1)
188 {
189 if (prev == NULL) listhead = next;
190 else prev->next = next;
191 hashdelete(curr_array);
192 ++counter;
193 }187 }
194 else188}
195 {189
196 markarray(curr_array) = 0;190
197 prev = curr_array;191Hash *luaI_hashcollector (long *acum)
192{
193 Hash *curr_array = listhead, *prev = NULL, *frees = NULL;
194 long counter = 0;
195 while (curr_array != NULL) {
196 Hash *next = curr_array->next;
197 if (markarray(curr_array) != 1) {
198 if (prev == NULL)
199 listhead = next;
200 else
201 prev->next = next;
202 curr_array->next = frees;
203 frees = curr_array;
204 ++counter;
205 }
206 else {
207 markarray(curr_array) = 0;
208 prev = curr_array;
209 }
210 curr_array = next;
198 }211 }
199 curr_array = next;212 *acum += counter;
200 }213 return frees;
201 return counter;
202}214}
203 215
204 216
@@ -220,32 +232,45 @@ Hash *lua_createarray (int nhash) · 15 unmodified lines
220 232
221 233
222/*234/*
223** Re-hash235** Rehash:
236** Check if table has deleted slots. It it has, it does not need to
237** grow, since rehash will reuse them.
224*/238*/
239static int emptyslots (Hash *t)
240{
241 int i;
242 for (i=nhash(t)-1; i>=0; i--) {
243 Node *n = node(t, i);
244 if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) == LUA_T_NIL)
245 return 1;
246 }
247 return 0;
248}
249
225static void rehash (Hash *t)250static void rehash (Hash *t)
226{251{
227 int i;252 int nold = nhash(t);
228 int nold = nhash(t);253 Node *vold = nodevector(t);
229 Node *vold = nodevector(t);254 int i;
230 nhash(t) = luaI_redimension(nhash(t));255 if (!emptyslots(t))
231 nodevector(t) = hashnodecreate(nhash(t));256 nhash(t) = luaI_redimension(nhash(t));
232 for (i=0; i<nold; i++)257 nodevector(t) = hashnodecreate(nhash(t));
233 {258 for (i=0; i<nold; i++) {
234 Node *n = vold+i;259 Node *n = vold+i;
235 if (tag(ref(n)) != LUA_T_NIL && tag(val(n)) != LUA_T_NIL)260 if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
236 *node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */261 *node(t, present(t, ref(n))) = *n; /* copy old node to new hash */
237 }262 }
238 luaI_free(vold);263 luaI_free(vold);
239}264}
240 265
241/*266/*
242** If the hash node is present, return its pointer, otherwise return267** If the hash node is present, return its pointer, otherwise return
243** null.268** null.
244*/269*/
245Object *lua_hashget (Hash *t, Object *ref)270TObject *lua_hashget (Hash *t, TObject *ref)
246{271{
247 int h = present(t, ref);272 int h = present(t, ref);
248 if (tag(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));273 if (ttype(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
249 else return NULL;274 else return NULL;
250}275}
251 276
@@ -254,25 +279,19 @@ Object *lua_hashget (Hash *t, Object *ref) · 2 unmodified lines
254** If the hash node is present, return its pointer, otherwise create a new279** If the hash node is present, return its pointer, otherwise create a new
255** node for the given reference and also return its pointer.280** node for the given reference and also return its pointer.
256*/281*/
257Object *lua_hashdefine (Hash *t, Object *ref)282TObject *lua_hashdefine (Hash *t, TObject *ref)
258{283{
259 int h;284 Node *n = node(t, present(t, ref));
260 Node *n;285 if (ttype(ref(n)) == LUA_T_NIL) {
261 h = present(t, ref);286 nuse(t)++;
262 n = node(t, h);287 if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) {
263 if (tag(ref(n)) == LUA_T_NIL)288 rehash(t);
264 {289 n = node(t, present(t, ref));
265 nuse(t)++;290 }
266 if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT)291 *ref(n) = *ref;
267 {292 ttype(val(n)) = LUA_T_NIL;
268 rehash(t);
269 h = present(t, ref);
270 n = node(t, h);
271 }293 }
272 *ref(n) = *ref;294 return (val(n));
273 tag(val(n)) = LUA_T_NIL;
274 }
275 return (val(n));
276}295}
277 296
278 297
@@ -284,36 +303,30 @@ Object *lua_hashdefine (Hash *t, Object *ref) · 5 unmodified lines
284*/303*/
285static void hashnext (Hash *t, int i)304static void hashnext (Hash *t, int i)
286{305{
287 if (i >= nhash(t))306 Node *n;
288 return;307 int tsize = nhash(t);
289 while (tag(ref(node(t,i))) == LUA_T_NIL || tag(val(node(t,i))) == LUA_T_NIL)308 if (i >= tsize)
290 {309 return;
291 if (++i >= nhash(t))310 n = node(t, i);
292 return;311 while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) {
293 }312 if (++i >= tsize)
294 luaI_pushobject(ref(node(t,i)));313 return;
295 luaI_pushobject(val(node(t,i)));314 n = node(t, i);
315 }
316 luaI_pushobject(ref(node(t,i)));
317 luaI_pushobject(val(node(t,i)));
296}318}
297 319
298void lua_next (void)320void lua_next (void)
299{321{
300 Hash *t;322 Hash *t;
301 lua_Object o = lua_getparam(1);323 lua_Object o = lua_getparam(1);
302 lua_Object r = lua_getparam(2);324 lua_Object r = lua_getparam(2);
303 if (o == LUA_NOOBJECT || r == LUA_NOOBJECT)325 luaL_arg_check(lua_istable(o), 1, "table expected");
304 lua_error ("too few arguments to function `next'");326 luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected");
305 if (lua_getparam(3) != LUA_NOOBJECT)327 t = avalue(luaI_Address(o));
306 lua_error ("too many arguments to function `next'");328 if (lua_isnil(r))
307 if (!lua_istable(o))329 hashnext(t, 0);
308 lua_error ("first argument of function `next' is not a table");330 else
309 t = avalue(luaI_Address(o));331 hashnext(t, present(t, luaI_Address(r))+1);
310 if (lua_isnil(r))
311 {
312 hashnext(t, 0);
313 }
314 else
315 {
316 int h = present (t, luaI_Address(r));
317 hashnext(t, h+1);
318 }
319}332}
src/hash.h+17−16
@@ -1,7 +1,7 @@
1/*1/*
2** hash.h2** hash.h
3** hash manager for lua3** hash manager for lua
4** $Id: hash.h,v 2.12 1996/05/06 14:30:27 roberto Exp $4** $Id: hash.h,v 2.16 1997/05/14 18:38:29 roberto Exp $
5*/5*/
6 6
7#ifndef hash_h7#ifndef hash_h
@@ -10,29 +10,30 @@ · 2 unmodified lines
10#include "types.h"10#include "types.h"
11#include "opcode.h"11#include "opcode.h"
12 12
13typedef struct node13typedef struct node {
14{14 TObject ref;
15 Object ref;15 TObject val;
16 Object val;
17} Node;16} Node;
18 17
19typedef struct Hash18typedef struct Hash {
20{19 struct Hash *next;
21 struct Hash *next;20 Node *node;
22 Node *node;21 int nhash;
23 int nhash;22 int nuse;
24 int nuse;23 int htag;
25 char mark;24 char mark;
26} Hash;25} Hash;
27 26
28 27
29int lua_equalObj (Object *t1, Object *t2);28int lua_equalObj (TObject *t1, TObject *t2);
30int luaI_redimension (int nhash);29int luaI_redimension (int nhash);
31Hash *lua_createarray (int nhash);30Hash *lua_createarray (int nhash);
32void lua_hashmark (Hash *h);31void lua_hashmark (Hash *h);
33Long lua_hashcollector (void);32Hash *luaI_hashcollector (long *count);
34Object *lua_hashget (Hash *t, Object *ref);33void luaI_hashcallIM (Hash *l);
35Object *lua_hashdefine (Hash *t, Object *ref);34void luaI_hashfree (Hash *frees);
35TObject *lua_hashget (Hash *t, TObject *ref);
36TObject *lua_hashdefine (Hash *t, TObject *ref);
36void lua_next (void);37void lua_next (void);
37 38
38#endif39#endif
src/inout.c+278−170
@@ -5,18 +5,24 @@ · 4 unmodified lines
5** Also provides some predefined lua functions.5** Also provides some predefined lua functions.
6*/6*/
7 7
8char *rcs_inout="$Id: inout.c,v 2.43 1996/09/25 12:57:22 roberto Exp $";8char *rcs_inout="$Id: inout.c,v 2.69 1997/06/27 22:38:49 roberto Exp $";
9 9
10#include <stdio.h>10#include <stdio.h>
11#include <string.h>11#include <string.h>
12 12
13#include "auxlib.h"
14#include "fallback.h"
15#include "hash.h"
16#include "inout.h"
13#include "lex.h"17#include "lex.h"
18#include "lua.h"
19#include "luamem.h"
20#include "luamem.h"
14#include "opcode.h"21#include "opcode.h"
15#include "inout.h"
16#include "table.h"22#include "table.h"
17#include "tree.h"23#include "tree.h"
18#include "lua.h"24#include "undump.h"
19#include "mem.h"25#include "zio.h"
20 26
21 27
22/* Exported variables */28/* Exported variables */
@@ -24,92 +30,93 @@ Word lua_linenumber; · 1 unmodified line
24char *lua_parsedfile;30char *lua_parsedfile;
25 31
26 32
27static FILE *fp;33char *luaI_typenames[] = { /* ORDER LUA_T */
28static char *st;34 "userdata", "line", "cmark", "mark", "function",
35 "function", "table", "string", "number", "nil",
36 NULL
37};
29 38
30/*
31** Function to get the next character from the input file
32*/
33static int fileinput (void)
34{
35 int c = fgetc(fp);
36 return (c == EOF) ? 0 : c;
37}
38 39
39/*40
40** Function to get the next character from the input string41void luaI_setparsedfile (char *name)
41*/
42static int stringinput (void)
43{42{
44 return *st++;43 lua_parsedfile = luaI_createfixedstring(name)->str;
45}44}
46 45
47/*46
48** Function to open a file to be input unit. 47int lua_doFILE (FILE *f, int bin)
49** Return the file.
50*/
51FILE *lua_openfile (char *fn)
52{48{
53 lua_setinput (fileinput);49 ZIO z;
54 if (fn == NULL)50 luaZ_Fopen(&z, f);
55 {51 if (bin)
56 fp = stdin;52 return luaI_undump(&z);
57 fn = "(stdin)";53 else {
58 }54 lua_setinput(&z);
59 else55 return lua_domain();
60 fp = fopen (fn, "r");56 }
61 if (fp == NULL)57}
62 return NULL;
63 lua_parsedfile = luaI_createfixedstring(fn)->str;
64 return fp;
65}
66 58
67/*59
68** Function to close an opened file60int lua_dofile (char *filename)
69*/
70void lua_closefile (void)
71{61{
72 if (fp != NULL && fp != stdin)62 int status;
73 {63 int c;
74 fclose (fp);64 FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
75 fp = NULL;65 if (f == NULL)
76 }66 return 2;
77}67 luaI_setparsedfile(filename?filename:"(stdin)");
68 c = fgetc(f);
69 ungetc(c, f);
70 if (c == ID_CHUNK) {
71 f = freopen(filename, "rb", f); /* set binary mode */
72 status = lua_doFILE(f, 1);
73 }
74 else {
75 if (c == '#')
76 while ((c=fgetc(f)) != '\n') /* skip first line */;
77 status = lua_doFILE(f, 0);
78 }
79 if (f != stdin)
80 fclose(f);
81 return status;
82}
83
84
78 85
79/*
80** Function to open a string to be input unit
81*/
82#define SIZE_PREF 20 /* size of string prefix to appear in error messages */86#define SIZE_PREF 20 /* size of string prefix to appear in error messages */
83void lua_openstring (char *s)
84{
85 char buff[SIZE_PREF+25];
86 lua_setinput(stringinput);
87 st = s;
88 strcpy(buff, "(dostring) >> ");
89 strncat(buff, s, SIZE_PREF);
90 if (strlen(s) > SIZE_PREF) strcat(buff, "...");
91 lua_parsedfile = luaI_createfixedstring(buff)->str;
92}
93 87
94/*88
95** Function to close an opened string89int lua_dobuffer (char *buff, int size)
96*/
97void lua_closestring (void)
98{90{
91 int status;
92 ZIO z;
93 luaI_setparsedfile("(buffer)");
94 luaZ_mopen(&z, buff, size);
95 status = luaI_undump(&z);
96 return status;
99}97}
100 98
101 99
102static void check_arg (int cond, char *func)100int lua_dostring (char *str)
103{101{
104 if (!cond)102 int status;
105 {103 char buff[SIZE_PREF+25];
106 char buff[100];104 char *temp;
107 sprintf(buff, "incorrect argument to function `%s'", func);105 ZIO z;
108 lua_error(buff);106 if (str == NULL) return 1;
109 }107 sprintf(buff, "(dostring) >> %.20s", str);
108 temp = strchr(buff, '\n');
109 if (temp) *temp = 0; /* end string after first line */
110 luaI_setparsedfile(buff);
111 luaZ_sopen(&z, str);
112 lua_setinput(&z);
113 status = lua_domain();
114 return status;
110}115}
111 116
112 117
118
119
113static int passresults (void)120static int passresults (void)
114{121{
115 int arg = 0;122 int arg = 0;
@@ -118,30 +125,53 @@ static int passresults (void) · 2 unmodified lines
118 lua_pushobject(obj);125 lua_pushobject(obj);
119 return arg-1;126 return arg-1;
120}127}
128
129
130static void packresults (void)
131{
132 int arg = 0;
133 lua_Object obj;
134 lua_Object table = lua_createtable();
135 while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT) {
136 lua_pushobject(table);
137 lua_pushnumber(arg);
138 lua_pushobject(obj);
139 lua_rawsettable();
140 }
141 lua_pushobject(table);
142 lua_pushstring("n");
143 lua_pushnumber(arg-1);
144 lua_rawsettable();
145 lua_pushobject(table); /* final result */
146}
121 147
122/*148/*
123** Internal function: do a string149** Internal function: do a string
124*/150*/
125void lua_internaldostring (void)151static void lua_internaldostring (void)
126{152{
127 lua_Object obj = lua_getparam (1);153 lua_Object err = lua_getparam(2);
128 if (lua_isstring(obj) && lua_dostring(lua_getstring(obj)) == 0)154 if (err != LUA_NOOBJECT) { /* set new error method */
155 luaL_arg_check(lua_isnil(err) || lua_isfunction(err), 2,
156 "must be a valid error handler");
157 lua_pushobject(err);
158 err = lua_seterrormethod();
159 }
160 if (lua_dostring(luaL_check_string(1)) == 0)
129 if (passresults() == 0)161 if (passresults() == 0)
130 lua_pushuserdata(NULL); /* at least one result to signal no errors */162 lua_pushuserdata(NULL); /* at least one result to signal no errors */
163 if (err != LUA_NOOBJECT) { /* restore old error method */
164 lua_pushobject(err);
165 lua_seterrormethod();
166 }
131}167}
132 168
133/*169/*
134** Internal function: do a file170** Internal function: do a file
135*/171*/
136void lua_internaldofile (void)172static void lua_internaldofile (void)
137{173{
138 lua_Object obj = lua_getparam (1);174 char *fname = luaL_opt_string(1, NULL);
139 char *fname = NULL;
140 if (lua_isstring(obj))
141 fname = lua_getstring(obj);
142 else if (obj != LUA_NOOBJECT)
143 lua_error("invalid argument to function `dofile'");
144 /* else fname = NULL */
145 if (lua_dofile(fname) == 0)175 if (lua_dofile(fname) == 0)
146 if (passresults() == 0)176 if (passresults() == 0)
147 lua_pushuserdata(NULL); /* at least one result to signal no errors */177 lua_pushuserdata(NULL); /* at least one result to signal no errors */
@@ -150,36 +180,28 @@ void lua_internaldofile (void) · 2 unmodified lines
150 180
151static char *tostring (lua_Object obj)181static char *tostring (lua_Object obj)
152{182{
153 char *buff = luaI_buffer(20);183 TObject *o = luaI_Address(obj);
154 if (lua_isstring(obj)) /* get strings and numbers */184 switch (ttype(o)) {
155 return lua_getstring(obj);185 case LUA_T_NUMBER: case LUA_T_STRING:
156 else switch(lua_type(obj))186 return lua_getstring(obj);
157 {187 case LUA_T_ARRAY: case LUA_T_FUNCTION:
158 case LUA_T_FUNCTION:188 case LUA_T_CFUNCTION: case LUA_T_NIL:
159 sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);189 return luaI_typenames[-ttype(o)];
160 break;190 case LUA_T_USERDATA: {
161 case LUA_T_CFUNCTION:191 char *buff = luaI_buffer(30);
162 sprintf(buff, "cfunction: %p", lua_getcfunction(obj));192 sprintf(buff, "userdata: %p", o->value.ts->u.v);
163 break;193 return buff;
164 case LUA_T_ARRAY:194 }
165 sprintf(buff, "table: %p", avalue(luaI_Address(obj)));195 default: return "<unknown object>";
166 break;
167 case LUA_T_NIL:
168 sprintf(buff, "nil");
169 break;
170 default:
171 sprintf(buff, "userdata: %p", lua_getuserdata(obj));
172 break;
173 }196 }
174 return buff;
175}197}
176 198
177void luaI_tostring (void)199static void luaI_tostring (void)
178{200{
179 lua_pushstring(tostring(lua_getparam(1)));201 lua_pushstring(tostring(lua_getparam(1)));
180}202}
181 203
182void luaI_print (void)204static void luaI_print (void)
183{205{
184 int i = 1;206 int i = 1;
185 lua_Object obj;207 lua_Object obj;
@@ -187,45 +209,18 @@ void luaI_print (void) · 1 unmodified line
187 printf("%s\n", tostring(obj));209 printf("%s\n", tostring(obj));
188}210}
189 211
190/*212static void luaI_type (void)
191** Internal function: return an object type.
192*/
193void luaI_type (void)
194{213{
195 lua_Object o = lua_getparam(1);214 lua_Object o = lua_getparam(1);
196 int t;215 luaL_arg_check(o != LUA_NOOBJECT, 1, "no argument");
197 if (o == LUA_NOOBJECT)216 lua_pushstring(luaI_typenames[-ttype(luaI_Address(o))]);
198 lua_error("no parameter to function 'type'");217 lua_pushnumber(lua_tag(o));
199 t = lua_type(o);
200 switch (t)
201 {
202 case LUA_T_NIL :
203 lua_pushliteral("nil");
204 break;
205 case LUA_T_NUMBER :
206 lua_pushliteral("number");
207 break;
208 case LUA_T_STRING :
209 lua_pushliteral("string");
210 break;
211 case LUA_T_ARRAY :
212 lua_pushliteral("table");
213 break;
214 case LUA_T_FUNCTION :
215 case LUA_T_CFUNCTION :
216 lua_pushliteral("function");
217 break;
218 default :
219 lua_pushliteral("userdata");
220 break;
221 }
222 lua_pushnumber(t);
223}218}
224 219
225/*220/*
226** Internal function: convert an object to a number221** Internal function: convert an object to a number
227*/222*/
228void lua_obj2number (void)223static void lua_obj2number (void)
229{224{
230 lua_Object o = lua_getparam(1);225 lua_Object o = lua_getparam(1);
231 if (lua_isnumber(o))226 if (lua_isnumber(o))
@@ -233,68 +228,181 @@ void lua_obj2number (void) · 1 unmodified line
233}228}
234 229
235 230
236void luaI_error (void)231static void luaI_error (void)
237{232{
238 char *s = lua_getstring(lua_getparam(1));233 char *s = lua_getstring(lua_getparam(1));
239 if (s == NULL) s = "(no message)";234 if (s == NULL) s = "(no message)";
240 lua_error(s);235 lua_error(s);
241}236}
242 237
243void luaI_assert (void)238static void luaI_assert (void)
244{239{
245 lua_Object p = lua_getparam(1);240 lua_Object p = lua_getparam(1);
246 if (p == LUA_NOOBJECT || lua_isnil(p))241 if (p == LUA_NOOBJECT || lua_isnil(p))
247 lua_error("assertion failed!");242 lua_error("assertion failed!");
248}243}
249 244
250void luaI_setglobal (void)245static void luaI_setglobal (void)
246{
247 lua_Object value = lua_getparam(2);
248 luaL_arg_check(value != LUA_NOOBJECT, 2, NULL);
249 lua_pushobject(value);
250 lua_setglobal(luaL_check_string(1));
251 lua_pushobject(value); /* return given value */
252}
253
254static void luaI_rawsetglobal (void)
251{255{
252 lua_Object name = lua_getparam(1);
253 lua_Object value = lua_getparam(2);256 lua_Object value = lua_getparam(2);
254 check_arg(lua_isstring(name), "setglobal");257 luaL_arg_check(value != LUA_NOOBJECT, 2, NULL);
255 lua_pushobject(value);258 lua_pushobject(value);
256 lua_storeglobal(lua_getstring(name));259 lua_rawsetglobal(luaL_check_string(1));
257 lua_pushobject(value); /* return given value */260 lua_pushobject(value); /* return given value */
258}261}
259 262
260void luaI_getglobal (void)263static void luaI_getglobal (void)
261{264{
262 lua_Object name = lua_getparam(1);265 lua_pushobject(lua_getglobal(luaL_check_string(1)));
263 check_arg(lua_isstring(name), "getglobal");
264 lua_pushobject(lua_getglobal(lua_getstring(name)));
265}266}
266 267
267#define MAXPARAMS 256268static void luaI_rawgetglobal (void)
268void luaI_call (void)269{
270 lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
271}
272
273static void luatag (void)
274{
275 lua_pushnumber(lua_tag(lua_getparam(1)));
276}
277
278
279static int getnarg (lua_Object table)
280{
281 lua_Object temp;
282 /* temp = table.n */
283 lua_pushobject(table); lua_pushstring("n"); temp = lua_gettable();
284 return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
285}
286
287static void luaI_call (void)
269{288{
270 lua_Object f = lua_getparam(1);289 lua_Object f = lua_getparam(1);
271 lua_Object arg = lua_getparam(2);290 lua_Object arg = lua_getparam(2);
272 lua_Object temp, params[MAXPARAMS];291 int withtable = (strcmp(luaL_opt_string(3, ""), "pack") == 0);
273 int narg, i;292 int narg, i;
274 check_arg(lua_istable(arg), "call");293 luaL_arg_check(lua_isfunction(f), 1, "function expected");
275 check_arg(lua_isfunction(f), "call");294 luaL_arg_check(lua_istable(arg), 2, "table expected");
276 /* narg = arg.n */295 narg = getnarg(arg);
277 lua_pushobject(arg);296 /* push arg[1...n] */
278 lua_pushstring("n");
279 temp = lua_getsubscript();
280 narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
281 /* read arg[1...n] */
282 for (i=0; i<narg; i++) {297 for (i=0; i<narg; i++) {
283 if (i>=MAXPARAMS)298 lua_Object temp;
284 lua_error("argument list too long in function `call'");299 /* temp = arg[i+1] */
285 lua_pushobject(arg);300 lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable();
286 lua_pushnumber(i+1);301 if (narg == MAX_WORD && lua_isnil(temp))
287 params[i] = lua_getsubscript();
288 if (narg == MAXPARAMS+1 && lua_isnil(params[i])) {
289 narg = i;
290 break;302 break;
291 }303 lua_pushobject(temp);
292 }304 }
293 /* push parameters and do the call */
294 for (i=0; i<narg; i++)
295 lua_pushobject(params[i]);
296 if (lua_callfunction(f))305 if (lua_callfunction(f))
297 lua_error(NULL);306 lua_error(NULL);
307 else if (withtable)
308 packresults();
298 else309 else
299 passresults();310 passresults();
300}311}
312
313static void luaIl_settag (void)
314{
315 lua_Object o = lua_getparam(1);
316 luaL_arg_check(lua_istable(o), 1, "table expected");
317 lua_pushobject(o);
318 lua_settag(luaL_check_number(2));
319}
320
321static void luaIl_newtag (void)
322{
323 lua_pushnumber(lua_newtag());
324}
325
326static void rawgettable (void)
327{
328 lua_Object t = lua_getparam(1);
329 lua_Object i = lua_getparam(2);
330 luaL_arg_check(t != LUA_NOOBJECT, 1, NULL);
331 luaL_arg_check(i != LUA_NOOBJECT, 2, NULL);
332 lua_pushobject(t);
333 lua_pushobject(i);
334 lua_pushobject(lua_rawgettable());
335}
336
337static void rawsettable (void)
338{
339 lua_Object t = lua_getparam(1);
340 lua_Object i = lua_getparam(2);
341 lua_Object v = lua_getparam(3);
342 luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT,
343 0, NULL);
344 lua_pushobject(t);
345 lua_pushobject(i);
346 lua_pushobject(v);
347 lua_rawsettable();
348}
349
350
351static void luaI_collectgarbage (void)
352{
353 lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
354}
355
356
357/*
358** Internal functions
359*/
360static struct {
361 char *name;
362 lua_CFunction func;
363} int_funcs[] = {
364 {"assert", luaI_assert},
365 {"call", luaI_call},
366 {"collectgarbage", luaI_collectgarbage},
367 {"dofile", lua_internaldofile},
368 {"dostring", lua_internaldostring},
369 {"error", luaI_error},
370 {"getglobal", luaI_getglobal},
371 {"newtag", luaIl_newtag},
372 {"next", lua_next},
373 {"nextvar", luaI_nextvar},
374 {"print", luaI_print},
375 {"rawgetglobal", luaI_rawgetglobal},
376 {"rawgettable", rawgettable},
377 {"rawsetglobal", luaI_rawsetglobal},
378 {"rawsettable", rawsettable},
379 {"seterrormethod", luaI_seterrormethod},
380#if LUA_COMPAT2_5
381 {"setfallback", luaI_setfallback},
382#endif
383 {"setglobal", luaI_setglobal},
384 {"settagmethod", luaI_settagmethod},
385 {"gettagmethod", luaI_gettagmethod},
386 {"settag", luaIl_settag},
387 {"tonumber", lua_obj2number},
388 {"tostring", luaI_tostring},
389 {"tag", luatag},
390 {"type", luaI_type}
391};
392
393#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
394
395
396void luaI_predefine (void)
397{
398 int i;
399 Word n;
400 for (i=0; i<INTFUNCSIZE; i++) {
401 n = luaI_findsymbolbyname(int_funcs[i].name);
402 s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
403 }
404 n = luaI_findsymbolbyname("_VERSION");
405 s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION);
406}
407
408
src/inout.h+8−17
@@ -1,5 +1,5 @@
1/*1/*
2** $Id: inout.h,v 1.16 1996/05/28 21:07:32 roberto Exp $2** $Id: inout.h,v 1.20 1997/06/19 18:04:34 roberto Exp $
3*/3*/
4 4
5 5
@@ -14,21 +14,12 @@ extern Word lua_linenumber; · 8 unmodified lines
14extern Word lua_debugline;14extern Word lua_debugline;
15extern char *lua_parsedfile;15extern char *lua_parsedfile;
16 16
17FILE *lua_openfile (char *fn);17void luaI_setparsedfile (char *name);
18void lua_closefile (void);18
19void lua_openstring (char *s);19void luaI_predefine (void);
20void lua_closestring (void);20
21 21int lua_dobuffer (char *buff, int size);
22void lua_internaldofile (void);22int lua_doFILE (FILE *f, int bin);
23void lua_internaldostring (void);23
24void luaI_tostring (void);
25void luaI_print (void);
26void luaI_type (void);
27void lua_obj2number (void);
28void luaI_error (void);
29void luaI_assert (void);
30void luaI_setglobal (void);
31void luaI_getglobal (void);
32void luaI_call (void);
33 24
34#endif25#endif
src/lex.c+174−45
@@ -1,10 +1,11 @@
1char *rcs_lex = "$Id: lex.c,v 2.39 1996/11/08 19:08:30 roberto Exp $";1char *rcs_lex = "$Id: lex.c,v 3.5 1997/06/16 16:50:22 roberto Exp $";
2 2
3 3
4#include <ctype.h>4#include <ctype.h>
5#include <string.h>5#include <string.h>
6 6
7#include "mem.h"7#include "auxlib.h"
8#include "luamem.h"
8#include "tree.h"9#include "tree.h"
9#include "table.h"10#include "table.h"
10#include "lex.h"11#include "lex.h"
@@ -12,33 +13,58 @@ char *rcs_lex = "$Id: lex.c,v 2.39 1996/11/08 19:08:30 roberto Exp $"; · 1 unmodified line
12#include "luadebug.h"13#include "luadebug.h"
13#include "parser.h"14#include "parser.h"
14 15
15#define MINBUFF 26016#define MINBUFF 250
16 17
17#define next() (current = input())18static int current; /* look ahead character */
19static ZIO *lex_z;
20
21
22#define next() (current = zgetc(lex_z))
18#define save(x) (yytext[tokensize++] = (x))23#define save(x) (yytext[tokensize++] = (x))
19#define save_and_next() (save(current), next())24#define save_and_next() (save(current), next())
20 25
21 26
22static int current; /* look ahead character */27#define MAX_IFS 5
23static Input input; /* input function */28
29/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
30
31static struct {
32 int elsepart; /* true if its in the $else part */
33 int condition; /* true if $if condition is true */
34 int skip; /* true if part must be skiped */
35} ifstate[MAX_IFS];
36
37static int iflevel; /* level of nested $if's */
24 38
25 39
26void lua_setinput (Input fn)40void lua_setinput (ZIO *z)
27{41{
28 current = '\n';42 current = '\n';
29 lua_linenumber = 0;43 lua_linenumber = 0;
30 input = fn;44 iflevel = 0;
45 ifstate[0].skip = 0;
46 ifstate[0].elsepart = 1; /* to avoid a free $else */
47 lex_z = z;
31}48}
32 49
33void luaI_syntaxerror (char *s)50
51static void luaI_auxsyntaxerror (char *s)
52{
53 luaL_verror("%s;\n> at line %d in file %s",
54 s, lua_linenumber, lua_parsedfile);
55}
56
57static void luaI_auxsynterrbf (char *s, char *token)
34{58{
35 char msg[256];
36 char *token = luaI_buffer(1);
37 if (token[0] == 0)59 if (token[0] == 0)
38 token = "<eof>";60 token = "<eof>";
39 sprintf (msg,"%s;\n> last token read: \"%s\" at line %d in file %s",61 luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s",
40 s, token, lua_linenumber, lua_parsedfile);62 s, token, lua_linenumber, lua_parsedfile);
41 lua_error (msg);63}
64
65void luaI_syntaxerror (char *s)
66{
67 luaI_auxsynterrbf(s, luaI_buffer(1));
42}68}
43 69
44 70
@@ -78,26 +104,122 @@ void luaI_addReserved (void) · 33 unmodified lines
78 }104 }
79}105}
80 106
81static int inclinenumber (int pragma_allowed)107
108/*
109** Pragma handling
110*/
111
112#define PRAGMASIZE 20
113
114static void skipspace (void)
115{
116 while (current == ' ' || current == '\t') next();
117}
118
119
120static int checkcond (char *buff)
121{
122 static char *opts[] = {"nil", "1"};
123 int i = luaI_findstring(buff, opts);
124 if (i >= 0) return i;
125 else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
126 return luaI_globaldefined(buff);
127 else {
128 luaI_auxsynterrbf("invalid $if condition", buff);
129 return 0; /* to avoid warnings */
130 }
131}
132
133
134static void readname (char *buff)
135{
136 int i = 0;
137 skipspace();
138 while (isalnum((unsigned char)current) || current == '_') {
139 if (i >= PRAGMASIZE) {
140 buff[PRAGMASIZE] = 0;
141 luaI_auxsynterrbf("pragma too long", buff);
142 }
143 buff[i++] = current;
144 next();
145 }
146 buff[i] = 0;
147}
148
149
150static void inclinenumber (void);
151
152
153static void ifskip (void)
154{
155 while (ifstate[iflevel].skip) {
156 if (current == '\n')
157 inclinenumber();
158 else if (current == EOZ)
159 luaI_auxsyntaxerror("input ends inside a $if");
160 else next();
161 }
162}
163
164
165static void inclinenumber (void)
82{166{
167 static char *pragmas [] =
168 {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
169 next(); /* skip '\n' */
83 ++lua_linenumber;170 ++lua_linenumber;
84 if (pragma_allowed && current == '$') { /* is a pragma? */171 if (current == '$') { /* is a pragma? */
85 char *buff = luaI_buffer(MINBUFF+1);172 char buff[PRAGMASIZE+1];
86 int i = 0;173 int ifnot = 0;
174 int skip = ifstate[iflevel].skip;
87 next(); /* skip $ */175 next(); /* skip $ */
88 while (isalnum(current)) {176 readname(buff);
89 if (i >= MINBUFF) luaI_syntaxerror("pragma too long");177 switch (luaI_findstring(buff, pragmas)) {
90 buff[i++] = current;178 case 0: /* debug */
91 next();179 if (!skip) lua_debug = 1;
180 break;
181 case 1: /* nodebug */
182 if (!skip) lua_debug = 0;
183 break;
184 case 2: /* endinput */
185 if (!skip) {
186 current = EOZ;
187 iflevel = 0; /* to allow $endinput inside a $if */
188 }
189 break;
190 case 3: /* end */
191 if (iflevel-- == 0)
192 luaI_auxsyntaxerror("unmatched $endif");
193 break;
194 case 4: /* ifnot */
195 ifnot = 1;
196 /* go through */
197 case 5: /* if */
198 if (iflevel == MAX_IFS-1)
199 luaI_auxsyntaxerror("too many nested `$ifs'");
200 readname(buff);
201 iflevel++;
202 ifstate[iflevel].elsepart = 0;
203 ifstate[iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
204 ifstate[iflevel].skip = skip || !ifstate[iflevel].condition;
205 break;
206 case 6: /* else */
207 if (ifstate[iflevel].elsepart)
208 luaI_auxsyntaxerror("unmatched $else");
209 ifstate[iflevel].elsepart = 1;
210 ifstate[iflevel].skip =
211 ifstate[iflevel-1].skip || ifstate[iflevel].condition;
212 break;
213 default:
214 luaI_auxsynterrbf("invalid pragma", buff);
92 }215 }
93 buff[i] = 0;216 skipspace();
94 if (strcmp(buff, "debug") == 0)217 if (current == '\n') /* pragma must end with a '\n' ... */
95 lua_debug = 1;218 inclinenumber();
96 else if (strcmp(buff, "nodebug") == 0)219 else if (current != EOZ) /* or eof */
97 lua_debug = 0;220 luaI_auxsyntaxerror("invalid pragma format");
98 else luaI_syntaxerror("invalid pragma");221 ifskip();
99 }222 }
100 return lua_linenumber;
101}223}
102 224
103static int read_long_string (char *yytext, int buffsize)225static int read_long_string (char *yytext, int buffsize)
@@ -110,7 +232,7 @@ static int read_long_string (char *yytext, int buffsize) · 6 unmodified lines
110 yytext = luaI_buffer(buffsize *= 2);232 yytext = luaI_buffer(buffsize *= 2);
111 switch (current)233 switch (current)
112 {234 {
113 case 0:235 case EOZ:
114 save(0);236 save(0);
115 return WRONGTOKEN;237 return WRONGTOKEN;
116 case '[':238 case '[':
@@ -131,8 +253,8 @@ static int read_long_string (char *yytext, int buffsize) · 14 unmodified lines
131 }253 }
132 continue;254 continue;
133 case '\n':255 case '\n':
134 save_and_next();256 save('\n');
135 inclinenumber(0);257 inclinenumber();
136 continue;258 continue;
137 default:259 default:
138 save_and_next();260 save_and_next();
@@ -162,8 +284,8 @@ int luaY_lex (void) · 23 unmodified lines
162 switch (current)284 switch (current)
163 {285 {
164 case '\n':286 case '\n':
165 next();287 inclinenumber();
166 linelasttoken = inclinenumber(1);288 linelasttoken = lua_linenumber;
167 continue;289 continue;
168 290
169 case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */291 case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
@@ -173,7 +295,7 @@ int luaY_lex (void) · 3 unmodified lines
173 case '-':295 case '-':
174 save_and_next();296 save_and_next();
175 if (current != '-') return '-';297 if (current != '-') return '-';
176 do { next(); } while (current != '\n' && current != 0);298 do { next(); } while (current != '\n' && current != EOZ);
177 continue;299 continue;
178 300
179 case '[':301 case '[':
@@ -216,7 +338,7 @@ int luaY_lex (void) · 36 unmodified lines
216 yytext = luaI_buffer(buffsize *= 2);338 yytext = luaI_buffer(buffsize *= 2);
217 switch (current)339 switch (current)
218 {340 {
219 case 0:341 case EOZ:
220 case '\n':342 case '\n':
221 save(0);343 save(0);
222 return WRONGTOKEN;344 return WRONGTOKEN;
@@ -227,7 +349,7 @@ int luaY_lex (void) · 4 unmodified lines
227 case 'n': save('\n'); next(); break;349 case 'n': save('\n'); next(); break;
228 case 't': save('\t'); next(); break;350 case 't': save('\t'); next(); break;
229 case 'r': save('\r'); next(); break;351 case 'r': save('\r'); next(); break;
230 case '\n': save_and_next(); inclinenumber(0); break;352 case '\n': save('\n'); inclinenumber(); break;
231 default : save_and_next(); break;353 default : save_and_next(); break;
232 }354 }
233 break;355 break;
@@ -258,7 +380,9 @@ int luaY_lex (void) · 24 unmodified lines
258 case '_':380 case '_':
259 {381 {
260 TaggedString *ts;382 TaggedString *ts;
261 do { save_and_next(); } while (isalnum(current) || current == '_');383 do {
384 save_and_next();
385 } while (isalnum((unsigned char)current) || current == '_');
262 save(0);386 save(0);
263 ts = lua_createstring(yytext);387 ts = lua_createstring(yytext);
264 if (ts->marked > 2)388 if (ts->marked > 2)
@@ -280,7 +404,7 @@ int luaY_lex (void) · 15 unmodified lines
280 }404 }
281 else return CONC; /* .. */405 else return CONC; /* .. */
282 }406 }
283 else if (!isdigit(current)) return '.';407 else if (!isdigit((unsigned char)current)) return '.';
284 /* current is a digit: goes through to number */408 /* current is a digit: goes through to number */
285 a=0.0;409 a=0.0;
286 goto fraction;410 goto fraction;
@@ -291,7 +415,7 @@ int luaY_lex (void) · 4 unmodified lines
291 do {415 do {
292 a=10.0*a+(current-'0');416 a=10.0*a+(current-'0');
293 save_and_next();417 save_and_next();
294 } while (isdigit(current));418 } while (isdigit((unsigned char)current));
295 if (current == '.') {419 if (current == '.') {
296 save_and_next();420 save_and_next();
297 if (current == '.')421 if (current == '.')
@@ -300,7 +424,7 @@ int luaY_lex (void) · 2 unmodified lines
300 }424 }
301 fraction:425 fraction:
302 { double da=0.1;426 { double da=0.1;
303 while (isdigit(current))427 while (isdigit((unsigned char)current))
304 {428 {
305 a+=(current-'0')*da;429 a+=(current-'0')*da;
306 da/=10.0;430 da/=10.0;
@@ -314,11 +438,12 @@ int luaY_lex (void) · 7 unmodified lines
314 save_and_next();438 save_and_next();
315 neg=(current=='-');439 neg=(current=='-');
316 if (current == '+' || current == '-') save_and_next();440 if (current == '+' || current == '-') save_and_next();
317 if (!isdigit(current)) { save(0); return WRONGTOKEN; }441 if (!isdigit((unsigned char)current)) {
442 save(0); return WRONGTOKEN; }
318 do {443 do {
319 e=10.0*e+(current-'0');444 e=10.0*e+(current-'0');
320 save_and_next();445 save_and_next();
321 } while (isdigit(current));446 } while (isdigit((unsigned char)current));
322 for (ea=neg?0.1:10.0; e>0; e>>=1)447 for (ea=neg?0.1:10.0; e>0; e>>=1)
323 {448 {
324 if (e & 1) a*=ea;449 if (e & 1) a*=ea;
@@ -330,11 +455,15 @@ int luaY_lex (void) · 5 unmodified lines
330 return NUMBER;455 return NUMBER;
331 }456 }
332 457
333 default: /* also end of program (0) */458 case EOZ:
334 {459 save(0);
460 if (iflevel > 0)
461 luaI_syntaxerror("missing $endif");
462 return 0;
463
464 default:
335 save_and_next();465 save_and_next();
336 return yytext[0];466 return yytext[0];
337 }
338 }467 }
339 }468 }
340}469}
src/lex.h+3−4
@@ -1,16 +1,15 @@
1/*1/*
2** lex.h2** lex.h
3** TecCGraf - PUC-Rio3** TecCGraf - PUC-Rio
4** $Id: lex.h,v 1.3 1996/11/08 12:49:35 roberto Exp $4** $Id: lex.h,v 1.4 1997/06/16 16:50:22 roberto Exp $
5*/5*/
6 6
7#ifndef lex_h7#ifndef lex_h
8#define lex_h8#define lex_h
9 9
10#include "zio.h"
10 11
11typedef int (*Input) (void);12void lua_setinput (ZIO *z);
12
13void lua_setinput (Input fn);
14void luaI_syntaxerror (char *s);13void luaI_syntaxerror (char *s);
15int luaY_lex (void);14int luaY_lex (void);
16void luaI_addReserved (void);15void luaI_addReserved (void);
src/lua.stx+29−34
@@ -1,13 +1,13 @@
1%{1%{
2 2
3char *rcs_luastx = "$Id: lua.stx,v 3.41 1996/11/08 12:49:35 roberto Exp $";3char *rcs_luastx = "$Id: lua.stx,v 3.47 1997/06/19 17:46:12 roberto Exp $";
4 4
5#include <stdio.h>5#include <stdio.h>
6#include <stdlib.h>6#include <stdlib.h>
7#include <string.h>7#include <string.h>
8 8
9#include "luadebug.h"9#include "luadebug.h"
10#include "mem.h"10#include "luamem.h"
11#include "lex.h"11#include "lex.h"
12#include "opcode.h"12#include "opcode.h"
13#include "hash.h"13#include "hash.h"
@@ -50,8 +50,6 @@ static TaggedString *localvar[MAXLOCALS]; /* store local variable names */ · 36 unmodified lines
50static int nlocalvar=0; /* number of local variables */50static int nlocalvar=0; /* number of local variables */
51 51
52#define MAXFIELDS FIELDS_PER_FLUSH*252#define MAXFIELDS FIELDS_PER_FLUSH*2
53static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
54static int nfields=0;
55 53
56int lua_debug = 0;54int lua_debug = 0;
57 55
@@ -103,22 +101,11 @@ static void code_word_at (Byte *p, int n) · 45 unmodified lines
103 memcpy(p, &w, sizeof(Word));101 memcpy(p, &w, sizeof(Word));
104}102}
105 103
106static void push_field (Word name)
107{
108 if (nfields < MAXFIELDS)
109 fields[nfields++] = name;
110 else
111 yyerror ("too many fields in nested constructors");
112}
113
114static void flush_record (int n)104static void flush_record (int n)
115{105{
116 int i;
117 if (n == 0) return;106 if (n == 0) return;
118 code_byte(STORERECORD);107 code_byte(STOREMAP);
119 code_byte(n);108 code_byte(n);
120 for (i=0; i<n; i++)
121 code_word(fields[--nfields]);
122}109}
123 110
124static void flush_list (int m, int n)111static void flush_list (int m, int n)
@@ -161,11 +148,22 @@ static void add_varbuffer (Long var) · 36 unmodified lines
161 yyerror ("variable buffer overflow");148 yyerror ("variable buffer overflow");
162}149}
163 150
151static void code_string (Word w)
152{
153 code_byte(PUSHSTRING);
154 code_word(w);
155}
156
157static void code_constant (TaggedString *s)
158{
159 code_string(luaI_findconstant(s));
160}
161
164static void code_number (float f)162static void code_number (float f)
165{ 163{
166 Word i = (Word)f;164 Word i;
167 if (f == (float)i) /* f has an (short) integer value */165 if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) {
168 {166 /* f has an (short) integer value */
169 if (i <= 2) code_byte(PUSH0 + i);167 if (i <= 2) code_byte(PUSH0 + i);
170 else if (i <= 255)168 else if (i <= 255)
171 {169 {
@@ -459,7 +457,7 @@ void lua_parse (TFunc *tf) · 287 unmodified lines
459 457
460%% /* beginning of rules section */458%% /* beginning of rules section */
461 459
462chunk : chunklist ret460chunk : chunklist ret ;
463 461
464chunklist : /* empty */462chunklist : /* empty */
465 | chunklist stat sc463 | chunklist stat sc
@@ -477,8 +475,7 @@ function : FUNCTION funcname body · 11 unmodified lines
477funcname : var { $$ =$1; init_func(); }475funcname : var { $$ =$1; init_func(); }
478 | varexp ':' NAME476 | varexp ':' NAME
479 {477 {
480 code_byte(PUSHSTRING);478 code_constant($3);
481 code_word(luaI_findconstant($3));
482 $$ = 0; /* indexed variable */479 $$ = 0; /* indexed variable */
483 init_func();480 init_func();
484 add_localvar(luaI_createfixedstring("self"));481 add_localvar(luaI_createfixedstring("self"));
@@ -492,7 +489,6 @@ body : '(' parlist ')' block END · 7 unmodified lines
492 luaI_initTFunc($$);489 luaI_initTFunc($$);
493 $$->size = pc;490 $$->size = pc;
494 $$->code = newvector(pc, Byte);491 $$->code = newvector(pc, Byte);
495 $$->fileName = lua_parsedfile;
496 $$->lineDefined = $2;492 $$->lineDefined = $2;
497 memcpy($$->code, basepc, pc*sizeof(Byte));493 memcpy($$->code, basepc, pc*sizeof(Byte));
498 if (lua_debug)494 if (lua_debug)
@@ -540,7 +536,7 @@ stat : IF expr1 THEN PrepJump block PrepJump elsepart END · 41 unmodified lines
540 lua_codeadjust (0);536 lua_codeadjust (0);
541 }537 }
542 } 538 }
543 | functioncall539 | functioncall {;}
544 | LOCAL localdeclist decinit540 | LOCAL localdeclist decinit
545 { nlocalvar += $2;541 { nlocalvar += $2;
546 adjust_mult_assign($2, $3, 0);542 adjust_mult_assign($2, $3, 0);
@@ -605,9 +601,8 @@ expr : '(' expr ')' { $$ = $2; } · 58 unmodified lines
605 | NUMBER { code_number($1); $$ = 0; }601 | NUMBER { code_number($1); $$ = 0; }
606 | STRING602 | STRING
607 {603 {
608 code_byte(PUSHSTRING);604 code_string($1);
609 code_word($1);605 $$ = 0;
610 $$ = 0;
611 }606 }
612 | NIL {code_byte(PUSHNIL); $$ = 0; }607 | NIL {code_byte(PUSHNIL); $$ = 0; }
613 | functioncall { $$ = $1; }608 | functioncall { $$ = $1; }
@@ -723,12 +718,13 @@ ffieldlist1 : ffield {$$=1;} · 109 unmodified lines
723 }718 }
724 ; 719 ;
725 720
726ffield : NAME '=' expr1 721ffield : ffieldkey '=' expr1
727 {
728 push_field(luaI_findconstant($1));
729 }
730 ;722 ;
731 723
724ffieldkey : '[' expr1 ']'
725 | NAME { code_constant($1); }
726 ;
727
732lfieldlist : /* empty */ { $$ = 0; }728lfieldlist : /* empty */ { $$ = 0; }
733 | lfieldlist1 lastcomma { $$ = $1; }729 | lfieldlist1 lastcomma { $$ = $1; }
734 ;730 ;
@@ -762,9 +758,8 @@ var : singlevar { $$ = $1; } · 27 unmodified lines
762 }758 }
763 | varexp '.' NAME759 | varexp '.' NAME
764 {760 {
765 code_byte(PUSHSTRING);761 code_constant($3);
766 code_word(luaI_findconstant($3));762 $$ = 0; /* indexed variable */
767 $$ = 0; /* indexed variable */
768 }763 }
769 ;764 ;
770 765
src/luac/dump.c+11−19
@@ -3,15 +3,13 @@ · 2 unmodified lines
3** thread and save bytecodes to file3** thread and save bytecodes to file
4*/4*/
5 5
6char* rcs_dump="$Id: dump.c,v 1.17 1996/11/18 11:18:29 lhf Exp $";6char* rcs_dump="$Id: dump.c,v 1.20 1997/06/19 14:56:04 lhf Exp $";
7 7
8#include <stdio.h>8#include <stdio.h>
9#include <stdlib.h>9#include <stdlib.h>
10#include <string.h>10#include <string.h>
11#include "luac.h"11#include "luac.h"
12 12
13static TFunc* lastF=NULL; /* list of functions seen in code */
14
15static int SawVar(int i, int at)13static int SawVar(int i, int at)
16{14{
17 int old=VarLoc(i);15 int old=VarLoc(i);
@@ -34,7 +32,7 @@ static void ThreadCode(Byte* code, Byte* end) · 16 unmodified lines
34 for (i=0; i<lua_nconstant; i++) StrLoc(i)=0;32 for (i=0; i<lua_nconstant; i++) StrLoc(i)=0;
35 for (p=code; p!=end;)33 for (p=code; p!=end;)
36 {34 {
37 OpCode op=(OpCode)*p;35 int op=*p;
38 int at=p-code+1;36 int at=p-code+1;
39 switch (op)37 switch (op)
40 {38 {
@@ -89,6 +87,8 @@ static void ThreadCode(Byte* code, Byte* end) · 48 unmodified lines
89 case STORELIST0:87 case STORELIST0:
90 case ADJUST:88 case ADJUST:
91 case RETCODE:89 case RETCODE:
90 case VARARGS:
91 case STOREMAP:
92 p+=2;92 p+=2;
93 break;93 break;
94 case PUSHWORD:94 case PUSHWORD:
@@ -107,8 +107,11 @@ static void ThreadCode(Byte* code, Byte* end) · 12 unmodified lines
107 case PUSHFLOAT:107 case PUSHFLOAT:
108 p+=5; /* assumes sizeof(float)==4 */108 p+=5; /* assumes sizeof(float)==4 */
109 break;109 break;
110 case PUSHSELF:110 case PUSHFUNCTION:
111 p+=sizeof(TFunc*)+1;
112 break;
111 case PUSHSTRING:113 case PUSHSTRING:
114 case PUSHSELF:
112 {115 {
113 Word w;116 Word w;
114 p++;117 p++;
@@ -117,16 +120,6 @@ static void ThreadCode(Byte* code, Byte* end) · 2 unmodified lines
117 memcpy(p-2,&w,sizeof(w));120 memcpy(p-2,&w,sizeof(w));
118 break;121 break;
119 }122 }
120 case PUSHFUNCTION:
121 {
122 TFunc* tf;
123 p++;
124 get_code(tf,p);
125 tf->marked=at;
126 tf->next=NULL; /* TODO: remove? */
127 lastF=lastF->next=tf;
128 break;
129 }
130 case PUSHGLOBAL:123 case PUSHGLOBAL:
131 case STOREGLOBAL:124 case STOREGLOBAL:
132 {125 {
@@ -151,8 +144,8 @@ static void ThreadCode(Byte* code, Byte* end) · 18 unmodified lines
151 }144 }
152 break;145 break;
153 }146 }
154 default:147 default: /* cannot happen */
155 fprintf(stderr,"luac: cannot happen: opcode=%d",*p);148 fprintf(stderr,"luac: bad opcode %d at %d\n",*p,(int)(p-code));
156 exit(1);149 exit(1);
157 break;150 break;
158 }151 }
@@ -186,7 +179,7 @@ static void DumpString(char* s, FILE* D) · 27 unmodified lines
186 int n=strlen(s)+1;179 int n=strlen(s)+1;
187 if ((Word)n != n)180 if ((Word)n != n)
188 {181 {
189 fprintf(stderr,"luac: string too long: \"%.32s...\"\n",s);182 fprintf(stderr,"luac: string too long (%d bytes): \"%.32s...\"\n",n,s);
190 exit(1);183 exit(1);
191 }184 }
192 DumpWord(n,D);185 DumpWord(n,D);
@@ -220,7 +213,6 @@ static void DumpStrings(FILE* D) · 27 unmodified lines
220 213
221void DumpFunction(TFunc* tf, FILE* D)214void DumpFunction(TFunc* tf, FILE* D)
222{215{
223 lastF=tf;
224 ThreadCode(tf->code,tf->code+tf->size);216 ThreadCode(tf->code,tf->code+tf->size);
225 fputc(ID_FUN,D);217 fputc(ID_FUN,D);
226 DumpSize(tf->size,D);218 DumpSize(tf->size,D);
src/luac/luac.c+112−27
@@ -1,25 +1,39 @@
1/*1/*
2** luac.c2** luac.c
3** lua compiler (saves bytecodes to files)3** lua compiler (saves bytecodes to files; also list binary files)
4*/4*/
5 5
6char* rcs_luac="$Id: luac.c,v 1.18 1996/11/16 20:14:23 lhf Exp $";6char* rcs_luac="$Id: luac.c,v 1.23 1997/06/20 20:34:04 lhf Exp $";
7 7
8#include <stdio.h>8#include <stdio.h>
9#include <stdlib.h>9#include <stdlib.h>
10#include <string.h>10#include <string.h>
11#include "luac.h"11#include "luac.h"
12#include "lex.h"
13#include "zio.h"
12 14
13static void compile(char* filename);15static void compile(char* filename);
16static void undump(char* filename);
14 17
15static int listing=0; /* list bytecodes? */18static int listing=0; /* list bytecodes? */
16static int dumping=1; /* dump bytecodes? */19static int dumping=1; /* dump bytecodes? */
20static int undumping=0; /* undump bytecodes? */
17static FILE* D; /* output file */21static FILE* D; /* output file */
18 22
19static void usage(void)23static void usage(void)
20{24{
21 fprintf(stderr,"usage: luac [-dlpv] [-o output] file ...\n");25 fprintf(stderr,
22 exit(0);26 "usage: luac [-c | -u] [-d] [-l] [-p] [-q] [-v] [-o output] file ...\n"
27 " -c\tcompile (default)\n"
28 " -u\tundump\n"
29 " -d\tgenerate debugging information\n"
30 " -l\tlist (default for -u)\n"
31 " -o\toutput file for -c (default \"luac.out\")\n"
32 " -p\tparse only\n"
33 " -q\tquiet (default for -c)\n"
34 " -v\tshow version information\n"
35 );
36 exit(1);
23}37}
24 38
25#define IS(s) (strcmp(argv[i],s)==0)39#define IS(s) (strcmp(argv[i],s)==0)
@@ -34,6 +48,11 @@ int main(int argc, char* argv[]) · 8 unmodified lines
34 break;48 break;
35 else if (IS("-")) /* use stdin */49 else if (IS("-")) /* use stdin */
36 break;50 break;
51 else if (IS("-c")) /* compile (and dump) */
52 {
53 dumping=1;
54 undumping=0;
55 }
37 else if (IS("-d")) /* debug */56 else if (IS("-d")) /* debug */
38 lua_debug=1;57 lua_debug=1;
39 else if (IS("-l")) /* list */58 else if (IS("-l")) /* list */
@@ -42,6 +61,14 @@ int main(int argc, char* argv[]) · 2 unmodified lines
42 d=argv[++i];61 d=argv[++i];
43 else if (IS("-p")) /* parse only (for timing purposes) */62 else if (IS("-p")) /* parse only (for timing purposes) */
44 dumping=0;63 dumping=0;
64 else if (IS("-q")) /* quiet */
65 listing=0;
66 else if (IS("-u")) /* undump */
67 {
68 dumping=0;
69 undumping=1;
70 listing=1;
71 }
45 else if (IS("-v")) /* show version */72 else if (IS("-v")) /* show version */
46 printf("%s %s\n(written by %s)\n\n",LUA_VERSION,LUA_COPYRIGHT,LUA_AUTHORS);73 printf("%s %s\n(written by %s)\n\n",LUA_VERSION,LUA_COPYRIGHT,LUA_AUTHORS);
47 else /* unknown option */74 else /* unknown option */
@@ -50,56 +77,114 @@ int main(int argc, char* argv[]) · 2 unmodified lines
50 --i; /* fake new argv[0] */77 --i; /* fake new argv[0] */
51 argc-=i;78 argc-=i;
52 argv+=i;79 argv+=i;
53 if (argc<2) usage();80 if (dumping)
54 for (i=1; i<argc; i++)81 {
55 if (IS(d))82 if (argc<2) usage();
83 for (i=1; i<argc; i++) /* play safe with output file */
84 if (IS(d))
85 {
86 fprintf(stderr,"luac: will not overwrite input file \"%s\"\n",d);
87 exit(1);
88 }
89 D=fopen(d,"wb"); /* must open in binary mode */
90 if (D==NULL)
56 {91 {
57 fprintf(stderr,"luac: will not overwrite input file \"%s\"\n",d);92 fprintf(stderr,"luac: cannot open ");
93 perror(d);
58 exit(1);94 exit(1);
59 }95 }
60 D=(dumping) ? fopen(d,"wb") : stdout; /* must open in binary mode */96 for (i=1; i<argc; i++) compile(IS("-")? NULL : argv[i]);
61 if (D==NULL)97 fclose(D);
98 }
99 if (undumping)
62 {100 {
63 fprintf(stderr,"luac: cannot open ");101 if (argc<2)
64 perror(d);102 undump("luac.out");
65 exit(1);103 else
104 for (i=1; i<argc; i++) undump(IS("-")? NULL : argv[i]);
66 }105 }
67 for (i=1; i<argc; i++) compile(IS("-")? NULL : argv[i]);
68 fclose(D);
69 return 0;106 return 0;
70}107}
71 108
72static void do_dump(TFunc* tf) /* only for tf==main */109static void do_dump(TFunc* Main)
73{110{
74 if (dumping) DumpHeader(D);111 TFunc* tf;
75 while (tf!=NULL)112 LinkFunctions(Main);
113 if (listing)
114 {
115 for (tf=Main; tf!=NULL; tf=tf->next) PrintFunction(tf,Main);
116 }
117 if (dumping)
76 {118 {
77 TFunc* nf;119 DumpHeader(D);
78 if (listing) PrintFunction(tf);120 for (tf=Main; tf!=NULL; tf=tf->next) DumpFunction(tf,D);
79 if (dumping) DumpFunction(tf,D);121 }
80 nf=tf->next; /* list only built after first main */122 for (tf=Main; tf!=NULL; )
123 {
124 TFunc* nf=tf->next;
81 luaI_freefunc(tf);125 luaI_freefunc(tf);
82 tf=nf;126 tf=nf;
83 }127 }
84}128}
85 129
86static void do_compile(void)130static void do_compile(ZIO* z)
87{131{
88 TFunc* tf=new(TFunc);132 TFunc* tf=new(TFunc);
133 lua_setinput(z);
89 luaI_initTFunc(tf);134 luaI_initTFunc(tf);
90 tf->fileName = lua_parsedfile;135 tf->fileName=lua_parsedfile;
91 lua_parse(tf);136 lua_parse(tf);
92 do_dump(tf);137 do_dump(tf);
93}138}
94 139
95static void compile(char* filename)140static void compile(char* filename)
96{141{
97 if (lua_openfile(filename)==NULL)142 FILE* f= (filename==NULL) ? stdin : fopen(filename, "r");
143 if (f==NULL)
144 {
145 fprintf(stderr,"luac: cannot open ");
146 perror(filename);
147 exit(1);
148 }
149 else
150 {
151 ZIO z;
152 zFopen(&z,f);
153 luaI_setparsedfile(filename?filename:"(stdin)");
154 do_compile(&z);
155 fclose(f);
156 }
157}
158
159static void do_undump(ZIO* z)
160{
161 TFunc* Main;
162 while ((Main=luaI_undump1(z)))
163 {
164 if (listing)
165 {
166 TFunc* tf;
167 for (tf=Main; tf!=NULL; tf=tf->next)
168 PrintFunction(tf,Main);
169 }
170 luaI_freefunc(Main); /* TODO: free others */
171 }
172}
173
174static void undump(char* filename)
175{
176 FILE* f= (filename==NULL) ? stdin : fopen(filename, "rb");
177 if (f==NULL)
98 {178 {
99 fprintf(stderr,"luac: cannot open ");179 fprintf(stderr,"luac: cannot open ");
100 perror(filename);180 perror(filename);
101 exit(1);181 exit(1);
102 }182 }
103 do_compile();183 else
104 lua_closefile();184 {
185 ZIO z;
186 zFopen(&z,f);
187 do_undump(&z);
188 fclose(f);
189 }
105}190}
src/luac/luac.h+6−5
@@ -1,24 +1,25 @@
1/*1/*
2** luac.h2** luac.h
3** definitions for luac compiler3** definitions for luac compiler
4** $Id: luac.h,v 1.5 1996/03/08 21:41:47 lhf Exp $4** $Id: luac.h,v 1.8 1997/06/19 17:32:08 lhf Exp $
5*/5*/
6 6
7#include "inout.h"7#include "inout.h"
8#include "mem.h"8#include "luamem.h"
9#include "opcode.h"9#include "opcode.h"
10#include "table.h"10#include "table.h"
11#include "undump.h"11#include "undump.h"
12 12
13#define VarStr(i) (lua_table[i].varname->str)13#define VarStr(i) (lua_table[i].varname->str)
14#define VarLoc(i) (lua_table[i].varname->varindex)14#define VarLoc(i) (lua_table[i].varname->u.s.varindex)
15#define StrStr(i) (lua_constant[i]->str)15#define StrStr(i) (lua_constant[i]->str)
16#define StrLoc(i) (lua_constant[i]->constindex)16#define StrLoc(i) (lua_constant[i]->u.s.constindex)
17 17
18extern Word lua_ntable;18extern Word lua_ntable;
19extern Word lua_nconstant;19extern Word lua_nconstant;
20extern int lua_debug;20extern int lua_debug;
21 21
22void LinkFunctions(TFunc* tf);
23void PrintFunction(TFunc* tf, TFunc* Main);
22void DumpHeader(FILE* D);24void DumpHeader(FILE* D);
23void DumpFunction(TFunc* tf, FILE* D);25void DumpFunction(TFunc* tf, FILE* D);
24void PrintFunction(TFunc* tf);
src/luac/print.c+213−20
@@ -3,7 +3,7 @@ · 2 unmodified lines
3** print bytecodes3** print bytecodes
4*/4*/
5 5
6char* rcs_print="$Id: print.c,v 1.11 1996/11/18 11:24:16 lhf Exp $";6char* rcs_print="$Id: print.c,v 1.17 1997/06/25 17:07:28 lhf Exp $";
7 7
8#include <stdio.h>8#include <stdio.h>
9#include <stdlib.h>9#include <stdlib.h>
@@ -11,20 +11,134 @@ char* rcs_print="$Id: print.c,v 1.11 1996/11/18 11:24:16 lhf Exp $"; · 1 unmodified line
11#include "luac.h"11#include "luac.h"
12#include "print.h"12#include "print.h"
13 13
14static LocVar* V=NULL;14void LinkFunctions(TFunc* m)
15
16static char* LocStr(int i)
17{15{
18 if (V==NULL) return ""; else return V[i].varname->str;16 static TFunc* lastF; /* list of functions seen in code */
17 Byte* code=m->code;
18 Byte* end=code+m->size;
19 Byte* p;
20 if (IsMain(m)) lastF=m;
21 for (p=code; p!=end;)
22 {
23 int op=*p;
24 int at=p-code+1;
25 switch (op)
26 {
27 case PUSHNIL:
28 case PUSH0:
29 case PUSH1:
30 case PUSH2:
31 case PUSHLOCAL0:
32 case PUSHLOCAL1:
33 case PUSHLOCAL2:
34 case PUSHLOCAL3:
35 case PUSHLOCAL4:
36 case PUSHLOCAL5:
37 case PUSHLOCAL6:
38 case PUSHLOCAL7:
39 case PUSHLOCAL8:
40 case PUSHLOCAL9:
41 case PUSHINDEXED:
42 case STORELOCAL0:
43 case STORELOCAL1:
44 case STORELOCAL2:
45 case STORELOCAL3:
46 case STORELOCAL4:
47 case STORELOCAL5:
48 case STORELOCAL6:
49 case STORELOCAL7:
50 case STORELOCAL8:
51 case STORELOCAL9:
52 case STOREINDEXED0:
53 case ADJUST0:
54 case EQOP:
55 case LTOP:
56 case LEOP:
57 case GTOP:
58 case GEOP:
59 case ADDOP:
60 case SUBOP:
61 case MULTOP:
62 case DIVOP:
63 case POWOP:
64 case CONCOP:
65 case MINUSOP:
66 case NOTOP:
67 case POP:
68 case RETCODE0:
69 p++;
70 break;
71 case PUSHBYTE:
72 case PUSHLOCAL:
73 case STORELOCAL:
74 case STOREINDEXED:
75 case STORELIST0:
76 case ADJUST:
77 case RETCODE:
78 case VARARGS:
79 case STOREMAP:
80 p+=2;
81 break;
82 case PUSHWORD:
83 case PUSHSTRING:
84 case PUSHGLOBAL:
85 case PUSHSELF:
86 case STOREGLOBAL:
87 case CREATEARRAY:
88 case ONTJMP:
89 case ONFJMP:
90 case JMP:
91 case UPJMP:
92 case IFFJMP:
93 case IFFUPJMP:
94 case CALLFUNC:
95 case SETLINE:
96 case STORELIST:
97 p+=3;
98 break;
99 case PUSHFLOAT:
100 p+=5; /* assumes sizeof(float)==4 */
101 break;
102 case PUSHFUNCTION:
103 {
104 TFunc* tf;
105 p++;
106 get_code(tf,p);
107 tf->marked=at;
108 tf->next=NULL; /* TODO: remove? */
109 lastF=lastF->next=tf;
110 break;
111 }
112 case STORERECORD:
113 {
114 int n=*++p;
115 p+=2*n+1;
116 break;
117 }
118 default: /* cannot happen */
119 fprintf(stderr,"luac: bad opcode %d at %d\n",*p,(int)(p-code));
120 exit(1);
121 break;
122 }
123 }
19}124}
20 125
21static void PrintCode(Byte* code, Byte* end)126#define LocStr(i) luaI_getlocalname(tf,i+1,line)
127
128static void PrintCode(TFunc* tf)
22{129{
130 Byte* code=tf->code;
131 Byte* end=code+tf->size;
23 Byte* p;132 Byte* p;
133 int line=0;
24 for (p=code; p!=end;)134 for (p=code; p!=end;)
25 {135 {
26 OpCode op=(OpCode)*p;136 int op=*p;
27 if (op>SETLINE) op=SETLINE+1;137 if (op>=NOPCODES)
138 {
139 fprintf(stderr,"luac: bad opcode %d at %d\n",op,(int)(p-code));
140 exit(1);
141 }
28 printf("%6d\t%s",(int)(p-code),OpCodeName[op]);142 printf("%6d\t%s",(int)(p-code),OpCodeName[op]);
29 switch (op)143 switch (op)
30 {144 {
@@ -64,7 +178,7 @@ static void PrintCode(Byte* code, Byte* end) · 33 unmodified lines
64 case PUSHLOCAL9:178 case PUSHLOCAL9:
65 {179 {
66 int i=op-PUSHLOCAL0;180 int i=op-PUSHLOCAL0;
67 printf("\t%d\t; %s",i,LocStr(i));181 if (tf->locvars) printf("\t\t; %s",LocStr(i));
68 p++;182 p++;
69 break;183 break;
70 }184 }
@@ -80,7 +194,7 @@ static void PrintCode(Byte* code, Byte* end) · 9 unmodified lines
80 case STORELOCAL9:194 case STORELOCAL9:
81 {195 {
82 int i=op-STORELOCAL0;196 int i=op-STORELOCAL0;
83 printf("\t%d\t; %s",i,LocStr(i));197 if (tf->locvars) printf("\t\t; %s",LocStr(i));
84 p++;198 p++;
85 break;199 break;
86 }200 }
@@ -88,7 +202,7 @@ static void PrintCode(Byte* code, Byte* end) · 1 unmodified line
88 case STORELOCAL:202 case STORELOCAL:
89 {203 {
90 int i=*(p+1);204 int i=*(p+1);
91 printf("\t%d\t; %s",i,LocStr(i));205 if (tf->locvars) printf("\t%d\t; %s",i,LocStr(i));
92 p+=2;206 p+=2;
93 break;207 break;
94 }208 }
@@ -97,23 +211,40 @@ static void PrintCode(Byte* code, Byte* end) · 2 unmodified lines
97 case STORELIST0:211 case STORELIST0:
98 case ADJUST:212 case ADJUST:
99 case RETCODE:213 case RETCODE:
214 case VARARGS:
215 case STOREMAP:
100 printf("\t%d",*(p+1));216 printf("\t%d",*(p+1));
101 p+=2;217 p+=2;
102 break;218 break;
103 case PUSHWORD:219 case PUSHWORD:
104 case CREATEARRAY:220 case CREATEARRAY:
221 case SETLINE:
222 {
223 Word w;
224 p++;
225 get_word(w,p);
226 printf("\t%d",w);
227 if (op==SETLINE) line=w;
228 break;
229 }
105 case ONTJMP:230 case ONTJMP:
106 case ONFJMP:231 case ONFJMP:
107 case JMP:232 case JMP:
108 case UPJMP:
109 case IFFJMP:233 case IFFJMP:
234 { /* suggested by Norman Ramsey <nr@cs.virginia.edu> */
235 Word w;
236 p++;
237 get_word(w,p);
238 printf("\t%d\t\t; to %d",w,(int)(p-code)+w);
239 break;
240 }
241 case UPJMP:
110 case IFFUPJMP:242 case IFFUPJMP:
111 case SETLINE:243 { /* suggested by Norman Ramsey <nr@cs.virginia.edu> */
112 {
113 Word w;244 Word w;
114 p++;245 p++;
115 get_word(w,p);246 get_word(w,p);
116 printf("\t%d",w);247 printf("\t%d\t\t; to %d",w,(int)(p-code)-w);
117 break;248 break;
118 }249 }
119 case PUSHFLOAT:250 case PUSHFLOAT:
@@ -170,7 +301,8 @@ static void PrintCode(Byte* code, Byte* end) · 50 unmodified lines
170 break;301 break;
171 }302 }
172 default:303 default:
173 printf("\tcannot happen: opcode=%d",*p);304 printf("\tcannot happen: opcode=%d\n",*p);
305 fprintf(stderr,"luac: bad opcode %d at %d\n",op,(int)(p-code));
174 exit(1);306 exit(1);
175 break;307 break;
176 }308 }
@@ -178,13 +310,74 @@ static void PrintCode(Byte* code, Byte* end) · 1 unmodified line
178 }310 }
179}311}
180 312
181void PrintFunction(TFunc* tf)313#undef LocStr
314
315static void PrintLocals(LocVar* v, int n)
182{316{
317 int i=0;
318 if (v==NULL || v->varname==NULL) return;
319 if (n>0)
320 {
321 printf("parameters:");
322 for (i=0; i<n; v++,i++) printf(" %s[%d@%d]",v->varname->str,i,v->line);
323 printf("\n");
324 }
325 if (v->varname!=NULL)
326 {
327 printf("locals:");
328 for (; v->line>=0; v++)
329 {
330 if (v->varname==NULL)
331#if 0
332 printf(" %s[%d@%d]","*",--i,v->line);
333#else
334 --i;
335#endif
336 else
337 printf(" %s[%d@%d]",v->varname->str,i++,v->line);
338 }
339 printf("\n");
340 }
341}
342
343void PrintFunction(TFunc* tf, TFunc* Main)
344{
345 int n=0;
183 if (IsMain(tf))346 if (IsMain(tf))
184 printf("\nmain of \"%s\" (%d bytes at %p)\n",tf->fileName,tf->size,tf);347 printf("\nmain of \"%s\" (%d bytes at %p)\n",tf->fileName,tf->size,tf);
185 else348 else
186 printf("\nfunction \"%s\":%d (%d bytes at %p); used at main+%d\n",349 {
350 Byte* p;
351 p=tf->code; /* get number of parameters */
352 while (*p==SETLINE) p+=3;
353 if (*p==ADJUST) n=p[1];
354 p=Main->code+tf->marked+sizeof(TFunc*);
355 printf("\nfunction ");
356 switch (*p) /* try to get name */
357 {
358 case STOREGLOBAL:
359 {
360 Word w;
361 p++; get_word(w,p); printf("%s defined at ",VarStr(w));
362 break;
363 }
364 case STOREINDEXED0: /* try method definition */
365 {
366 if (p[-11]==PUSHGLOBAL && p[-8]==PUSHSTRING)
367 {
368 Word w;
369 Byte* op=p;
370 int c=(tf->locvars && n>0 && strcmp(tf->locvars->varname->str,"self")==0)
371 ? ':' : '.';
372 p=op-11; p++; get_word(w,p); printf("%s%c",VarStr(w),c);
373 p=op-8; p++; get_word(w,p); printf("%s defined at ",StrStr(w));
374 }
375 break;
376 }
377 }
378 printf("\"%s\":%d (%d bytes at %p); used at main+%d\n",
187 tf->fileName,tf->lineDefined,tf->size,tf,tf->marked);379 tf->fileName,tf->lineDefined,tf->size,tf,tf->marked);
188 V=tf->locvars;380 }
189 PrintCode(tf->code,tf->code+tf->size);381 PrintLocals(tf->locvars,n);
382 PrintCode(tf);
190}383}
src/luac/print.h+6−3
@@ -1,10 +1,10 @@
1/*1/*
2** print.h2** print.h
3** opcode names3** opcode names
4** $Id: print.h,v 1.1 1996/02/23 19:04:13 lhf Exp $4** $Id: print.h,v 1.3 1997/04/14 14:42:50 lhf Exp $
5*/5*/
6 6
7static char* OpCodeName[]={ /* attention: same order as enum in opcode.h */7static char* OpCodeName[]={ /* ATTENTION: same order as enum in opcode.h */
8 "PUSHNIL",8 "PUSHNIL",
9 "PUSH0",9 "PUSH0",
10 "PUSH1",10 "PUSH1",
@@ -72,5 +72,8 @@ static char* OpCodeName[]={ /* attention: same order as enum in opcode.h */ · 61 unmodified lines
72 "RETCODE0",72 "RETCODE0",
73 "RETCODE",73 "RETCODE",
74 "SETLINE",74 "SETLINE",
75 ""75 "VARARGS",
76 "STOREMAP"
76};77};
78
79#define NOPCODES (sizeof(OpCodeName)/sizeof(OpCodeName[0]))
src/luamem.c+159−0
@@ -0,0 +1,159 @@
1/*
2** mem.c
3** TecCGraf - PUC-Rio
4*/
5
6char *rcs_luamem = "$Id: luamem.c,v 1.16 1997/04/01 21:23:20 roberto Exp $";
7
8#include <stdlib.h>
9
10#include "luamem.h"
11#include "lua.h"
12
13
14#define DEBUG 0
15
16#if !DEBUG
17
18void luaI_free (void *block)
19{
20 if (block)
21 {
22 *((char *)block) = -1; /* to catch errors */
23 free(block);
24 }
25}
26
27
28void *luaI_realloc (void *oldblock, unsigned long size)
29{
30 void *block;
31 size_t s = (size_t)size;
32 if (s != size)
33 lua_error("Allocation Error: Block too big");
34 block = oldblock ? realloc(oldblock, s) : malloc(s);
35 if (block == NULL)
36 lua_error(memEM);
37 return block;
38}
39
40
41int luaI_growvector (void **block, unsigned long nelems, int size,
42 char *errormsg, unsigned long limit)
43{
44 if (nelems >= limit)
45 lua_error(errormsg);
46 nelems = (nelems == 0) ? 20 : nelems*2;
47 if (nelems > limit)
48 nelems = limit;
49 *block = luaI_realloc(*block, nelems*size);
50 return (int)nelems;
51}
52
53
54void* luaI_buffer (unsigned long size)
55{
56 static unsigned long buffsize = 0;
57 static char* buffer = NULL;
58 if (size > buffsize)
59 buffer = luaI_realloc(buffer, buffsize=size);
60 return buffer;
61}
62
63#else
64/* DEBUG */
65
66#include <stdio.h>
67
68# define assert(ex) {if (!(ex)){(void)fprintf(stderr, \
69 "Assertion failed: file \"%s\", line %d\n", __FILE__, __LINE__);exit(1);}}
70
71#define MARK 55
72
73static unsigned long numblocks = 0;
74static unsigned long totalmem = 0;
75
76
77static void message (void)
78{
79#define inrange(x,y) ((x) < (((y)*3)/2) && (x) > (((y)*2)/3))
80 static int count = 0;
81 static unsigned long lastnumblocks = 0;
82 static unsigned long lasttotalmem = 0;
83 if (!inrange(numblocks, lastnumblocks) || !inrange(totalmem, lasttotalmem)
84 || count++ >= 5000)
85 {
86 fprintf(stderr,"blocks = %lu mem = %luK\n", numblocks, totalmem/1000);
87 count = 0;
88 lastnumblocks = numblocks;
89 lasttotalmem = totalmem;
90 }
91}
92
93
94void luaI_free (void *block)
95{
96 if (block)
97 {
98 unsigned long *b = (unsigned long *)block - 1;
99 unsigned long size = *b;
100 assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK);
101 numblocks--;
102 totalmem -= size;
103 free(b);
104 message();
105 }
106}
107
108
109void *luaI_realloc (void *oldblock, unsigned long size)
110{
111 unsigned long *block;
112 unsigned long realsize = sizeof(unsigned long)+size+sizeof(char);
113 if (realsize != (size_t)realsize)
114 lua_error("Allocation Error: Block too big");
115 if (oldblock)
116 {
117 unsigned long *b = (unsigned long *)oldblock - 1;
118 unsigned long oldsize = *b;
119 assert(*(((char *)b)+oldsize+sizeof(unsigned long)) == MARK);
120 totalmem -= oldsize;
121 numblocks--;
122 block = (unsigned long *)realloc(b, realsize);
123 }
124 else
125 block = (unsigned long *)malloc(realsize);
126 if (block == NULL)
127 lua_error("not enough memory");
128 totalmem += size;
129 numblocks++;
130 *block = size;
131 *(((char *)block)+size+sizeof(unsigned long)) = MARK;
132 message();
133 return block+1;
134}
135
136
137int luaI_growvector (void **block, unsigned long nelems, int size,
138 char *errormsg, unsigned long limit)
139{
140 if (nelems >= limit)
141 lua_error(errormsg);
142 nelems = (nelems == 0) ? 20 : nelems*2;
143 if (nelems > limit)
144 nelems = limit;
145 *block = luaI_realloc(*block, nelems*size);
146 return (int)nelems;
147}
148
149
150void* luaI_buffer (unsigned long size)
151{
152 static unsigned long buffsize = 0;
153 static char* buffer = NULL;
154 if (size > buffsize)
155 buffer = luaI_realloc(buffer, buffsize=size);
156 return buffer;
157}
158
159#endif
src/luamem.hrenamed from src/mem.h+3−3
@@ -1,11 +1,11 @@
1/*1/*
2** mem.c2** mem.c
3** memory manager for lua3** memory manager for lua
4** $Id: mem.h,v 1.8 1996/05/24 14:31:10 roberto Exp $4** $Id: luamem.h,v 1.9 1997/03/31 14:10:11 roberto Exp $
5*/5*/
6 6
7#ifndef mem_h7#ifndef luamem_h
8#define mem_h8#define luamem_h
9 9
10#ifndef NULL10#ifndef NULL
11#define NULL 011#define NULL 0
src/mem.c+0−58
@@ -1,58 +0,0 @@
1/*
2** mem.c
3** TecCGraf - PUC-Rio
4*/
5
6char *rcs_mem = "$Id: mem.c,v 1.13 1996/05/24 14:31:10 roberto Exp $";
7
8#include <stdlib.h>
9
10#include "mem.h"
11#include "lua.h"
12
13
14void luaI_free (void *block)
15{
16 if (block)
17 {
18 *((int *)block) = -1; /* to catch errors */
19 free(block);
20 }
21}
22
23
24void *luaI_realloc (void *oldblock, unsigned long size)
25{
26 void *block;
27 size_t s = (size_t)size;
28 if (s != size)
29 lua_error("Allocation Error: Block too big");
30 block = oldblock ? realloc(oldblock, s) : malloc(s);
31 if (block == NULL)
32 lua_error(memEM);
33 return block;
34}
35
36
37int luaI_growvector (void **block, unsigned long nelems, int size,
38 char *errormsg, unsigned long limit)
39{
40 if (nelems >= limit)
41 lua_error(errormsg);
42 nelems = (nelems == 0) ? 20 : nelems*2;
43 if (nelems > limit)
44 nelems = limit;
45 *block = luaI_realloc(*block, nelems*size);
46 return (int)nelems;
47}
48
49
50void* luaI_buffer (unsigned long size)
51{
52 static unsigned long buffsize = 0;
53 static char* buffer = NULL;
54 if (size > buffsize)
55 buffer = luaI_realloc(buffer, buffsize=size);
56 return buffer;
57}
58
src/opcode.c+517−369
@@ -3,7 +3,7 @@ · 2 unmodified lines
3** TecCGraf - PUC-Rio3** TecCGraf - PUC-Rio
4*/4*/
5 5
6char *rcs_opcode="$Id: opcode.c,v 3.77 1996/11/18 13:48:44 roberto Exp $";6char *rcs_opcode="$Id: opcode.c,v 4.15 1997/06/26 21:40:57 roberto Exp $";
7 7
8#include <setjmp.h>8#include <setjmp.h>
9#include <stdio.h>9#include <stdio.h>
@@ -11,17 +11,18 @@ char *rcs_opcode="$Id: opcode.c,v 3.77 1996/11/18 13:48:44 roberto Exp $"; · 1 unmodified line
11#include <stdlib.h>11#include <stdlib.h>
12 12
13#include "luadebug.h"13#include "luadebug.h"
14#include "mem.h"14#include "luamem.h"
15#include "opcode.h"15#include "opcode.h"
16#include "hash.h"16#include "hash.h"
17#include "inout.h"17#include "inout.h"
18#include "table.h"18#include "table.h"
19#include "lua.h"19#include "lua.h"
20#include "fallback.h"20#include "fallback.h"
21#include "undump.h"21#include "auxlib.h"
22#include "lex.h"
22 23
23#define tonumber(o) ((tag(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))24#define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
24#define tostring(o) ((tag(o) != LUA_T_STRING) && (lua_tostring(o) != 0))25#define tostring(o) ((ttype(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
25 26
26 27
27#define STACK_SIZE 12828#define STACK_SIZE 128
@@ -32,14 +33,14 @@ char *rcs_opcode="$Id: opcode.c,v 3.77 1996/11/18 13:48:44 roberto Exp $"; · 4 unmodified lines
32 33
33typedef int StkId; /* index to stack elements */34typedef int StkId; /* index to stack elements */
34 35
35static Object initial_stack;36static TObject initial_stack;
36 37
37static Object *stackLimit = &initial_stack+1;38static TObject *stackLimit = &initial_stack+1;
38static Object *stack = &initial_stack;39static TObject *stack = &initial_stack;
39static Object *top = &initial_stack;40static TObject *top = &initial_stack;
40 41
41 42
42/* macros to convert from lua_Object to (Object *) and back */43/* macros to convert from lua_Object to (TObject *) and back */
43 44
44#define Address(lo) ((lo)+stack-1)45#define Address(lo) ((lo)+stack-1)
45#define Ref(st) ((st)-stack+1)46#define Ref(st) ((st)-stack+1)
@@ -51,13 +52,13 @@ static Object *top = &initial_stack; · 5 unmodified lines
51#define incr_top if (++top >= stackLimit) growstack()52#define incr_top if (++top >= stackLimit) growstack()
52 53
53struct C_Lua_Stack {54struct C_Lua_Stack {
54 StkId base; /* when Lua calls C or C calls Lua, points to */55 StkId base; /* when Lua calls C or C calls Lua, points to */
55 /* the first slot after the last parameter. */56 /* the first slot after the last parameter. */
56 int num; /* when Lua calls C, has the number of parameters; */57 StkId lua2C; /* points to first element of "array" lua2C */
57 /* when C calls Lua, has the number of results. */58 int num; /* size of "array" lua2C */
58};59};
59 60
60static struct C_Lua_Stack CLS_current = {0, 0};61static struct C_Lua_Stack CLS_current = {0, 0, 0};
61 62
62static jmp_buf *errorJmp = NULL; /* current error recover point */63static jmp_buf *errorJmp = NULL; /* current error recover point */
63 64
@@ -72,7 +73,7 @@ static void do_call (StkId base, int nResults); · 8 unmodified lines
72 73
73 74
74 75
75Object *luaI_Address (lua_Object o)76TObject *luaI_Address (lua_Object o)
76{77{
77 return Address(o);78 return Address(o);
78}79}
@@ -84,7 +85,7 @@ Object *luaI_Address (lua_Object o) · 5 unmodified lines
84static void lua_initstack (void)85static void lua_initstack (void)
85{86{
86 Long maxstack = STACK_SIZE;87 Long maxstack = STACK_SIZE;
87 stack = newvector(maxstack, Object);88 stack = newvector(maxstack, TObject);
88 stackLimit = stack+maxstack;89 stackLimit = stack+maxstack;
89 top = stack;90 top = stack;
90 *(top++) = initial_stack;91 *(top++) = initial_stack;
@@ -105,7 +106,7 @@ static void growstack (void) · 14 unmodified lines
105 static int limit = STACK_LIMIT;106 static int limit = STACK_LIMIT;
106 StkId t = top-stack;107 StkId t = top-stack;
107 Long stacksize = stackLimit - stack;108 Long stacksize = stackLimit - stack;
108 stacksize = growvector(&stack, stacksize, Object, stackEM, limit+100);109 stacksize = growvector(&stack, stacksize, TObject, stackEM, limit+100);
109 stackLimit = stack+stacksize;110 stackLimit = stack+stacksize;
110 top = stack + t;111 top = stack + t;
111 if (stacksize >= limit)112 if (stacksize >= limit)
@@ -134,16 +135,16 @@ static char *lua_strconc (char *l, char *r) · 22 unmodified lines
134** Convert, if possible, to a number object.135** Convert, if possible, to a number object.
135** Return 0 if success, not 0 if error.136** Return 0 if success, not 0 if error.
136*/137*/
137static int lua_tonumber (Object *obj)138static int lua_tonumber (TObject *obj)
138{139{
139 float t;140 float t;
140 char c;141 char c;
141 if (tag(obj) != LUA_T_STRING)142 if (ttype(obj) != LUA_T_STRING)
142 return 1;143 return 1;
143 else if (sscanf(svalue(obj), "%f %c",&t, &c) == 1)144 else if (sscanf(svalue(obj), "%f %c",&t, &c) == 1)
144 {145 {
145 nvalue(obj) = t;146 nvalue(obj) = t;
146 tag(obj) = LUA_T_NUMBER;147 ttype(obj) = LUA_T_NUMBER;
147 return 0;148 return 0;
148 }149 }
149 else150 else
@@ -152,39 +153,54 @@ static int lua_tonumber (Object *obj) · 2 unmodified lines
152 153
153 154
154/*155/*
155** Convert, if possible, to a string tag156** Convert, if possible, to a string ttype
156** Return 0 in success or not 0 on error.157** Return 0 in success or not 0 on error.
157*/158*/
158static int lua_tostring (Object *obj)159static int lua_tostring (TObject *obj)
159{160{
160 char s[256];161 if (ttype(obj) != LUA_T_NUMBER)
161 if (tag(obj) != LUA_T_NUMBER)162 return 1;
162 return 1;163 else {
163 if ((int) nvalue(obj) == nvalue(obj))164 char s[60];
164 sprintf (s, "%d", (int) nvalue(obj));165 real f = nvalue(obj);
165 else166 int i;
166 sprintf (s, "%g", nvalue(obj));167 if ((real)(-MAX_INT) <= f && f <= (real)MAX_INT && (real)(i=(int)f) == f)
167 tsvalue(obj) = lua_createstring(s);168 sprintf (s, "%d", i);
168 tag(obj) = LUA_T_STRING;169 else
169 return 0;170 sprintf (s, "%g", nvalue(obj));
171 tsvalue(obj) = lua_createstring(s);
172 ttype(obj) = LUA_T_STRING;
173 return 0;
174 }
170}175}
171 176
172 177
173/*178/*
174** Adjust stack. Set top to the given value, pushing NILs if needed.179** Adjust stack. Set top to the given value, pushing NILs if needed.
175*/180*/
176static void adjust_top (StkId newtop)181static void adjust_top_aux (StkId newtop)
177{182{
178 Object *nt;183 TObject *nt;
179 lua_checkstack(stack+newtop);184 lua_checkstack(stack+newtop);
180 nt = stack+newtop; /* warning: previous call may change stack */185 nt = stack+newtop; /* warning: previous call may change stack */
181 while (top < nt) tag(top++) = LUA_T_NIL;186 while (top < nt) ttype(top++) = LUA_T_NIL;
182 top = nt; /* top could be bigger than newtop */
183}187}
184 188
189
190#define adjust_top(newtop) { if (newtop <= top-stack) \
191 top = stack+newtop; \
192 else adjust_top_aux(newtop); }
193
185#define adjustC(nParams) adjust_top(CLS_current.base+nParams)194#define adjustC(nParams) adjust_top(CLS_current.base+nParams)
186 195
187 196
197static void checkCparams (int nParams)
198{
199 if (top-stack < CLS_current.base+nParams)
200 lua_error("API error - wrong number of arguments in C2lua stack");
201}
202
203
188/*204/*
189** Open a hole below "nelems" from the top.205** Open a hole below "nelems" from the top.
190*/206*/
@@ -197,13 +213,37 @@ static void open_stack (int nelems) · 6 unmodified lines
197}213}
198 214
199 215
216static lua_Object put_luaObject (TObject *o)
217{
218 open_stack((top-stack)-CLS_current.base);
219 stack[CLS_current.base++] = *o;
220 return CLS_current.base; /* this is +1 real position (see Ref) */
221}
222
223
224static lua_Object put_luaObjectonTop (void)
225{
226 open_stack((top-stack)-CLS_current.base);
227 stack[CLS_current.base++] = *(--top);
228 return CLS_current.base; /* this is +1 real position (see Ref) */
229}
230
231
232lua_Object lua_pop (void)
233{
234 checkCparams(1);
235 return put_luaObjectonTop();
236}
237
238
239
200/*240/*
201** call Line hook241** call Line hook
202*/242*/
203static void lineHook (int line)243static void lineHook (int line)
204{244{
205 struct C_Lua_Stack oldCLS = CLS_current;245 struct C_Lua_Stack oldCLS = CLS_current;
206 StkId old_top = CLS_current.base = top-stack;246 StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
207 CLS_current.num = 0;247 CLS_current.num = 0;
208 (*lua_linehook)(line);248 (*lua_linehook)(line);
209 top = stack+old_top;249 top = stack+old_top;
@@ -218,13 +258,13 @@ static void lineHook (int line) · 8 unmodified lines
218static void callHook (StkId base, lua_Type type, int isreturn)258static void callHook (StkId base, lua_Type type, int isreturn)
219{259{
220 struct C_Lua_Stack oldCLS = CLS_current;260 struct C_Lua_Stack oldCLS = CLS_current;
221 StkId old_top = CLS_current.base = top-stack;261 StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
222 CLS_current.num = 0;262 CLS_current.num = 0;
223 if (isreturn)263 if (isreturn)
224 (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);264 (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
225 else265 else
226 {266 {
227 Object *f = stack+base-1;267 TObject *f = stack+base-1;
228 if (type == LUA_T_MARK)268 if (type == LUA_T_MARK)
229 (*lua_callhook)(Ref(f), f->value.tf->fileName, f->value.tf->lineDefined);269 (*lua_callhook)(Ref(f), f->value.tf->fileName, f->value.tf->lineDefined);
230 else270 else
@@ -246,6 +286,7 @@ static StkId callC (lua_CFunction func, StkId base) · 15 unmodified lines
246 StkId firstResult;286 StkId firstResult;
247 CLS_current.num = (top-stack) - base;287 CLS_current.num = (top-stack) - base;
248 /* incorporate parameters on the stack */288 /* incorporate parameters on the stack */
289 CLS_current.lua2C = base;
249 CLS_current.base = base+CLS_current.num; /* == top-stack */290 CLS_current.base = base+CLS_current.num; /* == top-stack */
250 if (lua_callhook)291 if (lua_callhook)
251 callHook(base, LUA_T_CMARK, 0);292 callHook(base, LUA_T_CMARK, 0);
@@ -257,15 +298,11 @@ static StkId callC (lua_CFunction func, StkId base) · 5 unmodified lines
257 return firstResult;298 return firstResult;
258}299}
259 300
260/*301static void callIM (TObject *f, int nParams, int nResults)
261** Call the specified fallback, putting it on the stack below its arguments
262*/
263static void callFB (int fb)
264{302{
265 int nParams = luaI_fallBacks[fb].nParams;
266 open_stack(nParams);303 open_stack(nParams);
267 *(top-nParams-1) = luaI_fallBacks[fb].function;304 *(top-nParams-1) = *f;
268 do_call((top-stack)-nParams, luaI_fallBacks[fb].nResults);305 do_call((top-stack)-nParams, nResults);
269}306}
270 307
271 308
@@ -278,28 +315,28 @@ static void callFB (int fb) · 6 unmodified lines
278static void do_call (StkId base, int nResults)315static void do_call (StkId base, int nResults)
279{316{
280 StkId firstResult;317 StkId firstResult;
281 Object *func = stack+base-1;318 TObject *func = stack+base-1;
282 int i;319 int i;
283 if (tag(func) == LUA_T_CFUNCTION)320 if (ttype(func) == LUA_T_CFUNCTION) {
284 {321 ttype(func) = LUA_T_CMARK;
285 tag(func) = LUA_T_CMARK;
286 firstResult = callC(fvalue(func), base);322 firstResult = callC(fvalue(func), base);
287 }323 }
288 else if (tag(func) == LUA_T_FUNCTION)324 else if (ttype(func) == LUA_T_FUNCTION) {
289 {325 ttype(func) = LUA_T_MARK;
290 tag(func) = LUA_T_MARK;
291 firstResult = lua_execute(func->value.tf->code, base);326 firstResult = lua_execute(func->value.tf->code, base);
292 }327 }
293 else328 else { /* func is not a function */
294 { /* func is not a function */329 /* Check the tag method for invalid functions */
295 /* Call the fallback for invalid functions */330 TObject *im = luaI_getimbyObj(func, IM_FUNCTION);
331 if (ttype(im) == LUA_T_NIL)
332 lua_error("call expression not a function");
296 open_stack((top-stack)-(base-1));333 open_stack((top-stack)-(base-1));
297 stack[base-1] = luaI_fallBacks[FB_FUNCTION].function;334 stack[base-1] = *im;
298 do_call(base, nResults);335 do_call(base, nResults);
299 return;336 return;
300 }337 }
301 /* adjust the number of results */338 /* adjust the number of results */
302 if (nResults != MULT_RET && top - (stack+firstResult) != nResults)339 if (nResults != MULT_RET)
303 adjust_top(firstResult+nResults);340 adjust_top(firstResult+nResults);
304 /* move results to base-1 (to erase parameters and function) */341 /* move results to base-1 (to erase parameters and function) */
305 base--;342 base--;
@@ -316,58 +353,110 @@ static void do_call (StkId base, int nResults) · 10 unmodified lines
316*/353*/
317static void pushsubscript (void)354static void pushsubscript (void)
318{355{
319 if (tag(top-2) != LUA_T_ARRAY)356 TObject *im;
320 callFB(FB_GETTABLE);357 if (ttype(top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */
321 else 358 im = luaI_getimbyObj(top-2, IM_GETTABLE);
322 {359 else { /* object is a table... */
323 Object *h = lua_hashget(avalue(top-2), top-1);360 int tg = (top-2)->value.a->htag;
324 if (h == NULL || tag(h) == LUA_T_NIL)361 im = luaI_getim(tg, IM_GETTABLE);
325 callFB(FB_INDEX);362 if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */
326 else363 TObject *h = lua_hashget(avalue(top-2), top-1);
327 {364 if (h != NULL && ttype(h) != LUA_T_NIL) {
328 --top;365 --top;
329 *(top-1) = *h;366 *(top-1) = *h;
367 }
368 else if (ttype(im=luaI_getim(tg, IM_INDEX)) != LUA_T_NIL)
369 callIM(im, 2, 1);
370 else {
371 --top;
372 ttype(top-1) = LUA_T_NIL;
373 }
374 return;
330 }375 }
376 /* else it has a "gettable" method, go through to next command */
377 }
378 /* object is not a table, or it has a "gettable" method */
379 if (ttype(im) != LUA_T_NIL)
380 callIM(im, 2, 1);
381 else
382 lua_error("indexed expression not a table");
383}
384
385
386lua_Object lua_rawgettable (void)
387{
388 checkCparams(2);
389 if (ttype(top-2) != LUA_T_ARRAY)
390 lua_error("indexed expression not a table in raw gettable");
391 else {
392 TObject *h = lua_hashget(avalue(top-2), top-1);
393 --top;
394 if (h != NULL)
395 *(top-1) = *h;
396 else
397 ttype(top-1) = LUA_T_NIL;
331 }398 }
399 return put_luaObjectonTop();
332}400}
333 401
334 402
335/*403/*
336** Function to store indexed based on values at the top404** Function to store indexed based on values at the top
405** mode = 0: raw store (without internal methods)
406** mode = 1: normal store (with internal methods)
407** mode = 2: "deep stack" store (with internal methods)
337*/408*/
338static void storesubscript (void)409static void storesubscript (TObject *t, int mode)
339{410{
340 if (tag(top-3) != LUA_T_ARRAY)411 TObject *im = (mode == 0) ? NULL : luaI_getimbyObj(t, IM_SETTABLE);
341 callFB(FB_SETTABLE);412 if (ttype(t) == LUA_T_ARRAY && (im == NULL || ttype(im) == LUA_T_NIL)) {
342 else413 TObject *h = lua_hashdefine(avalue(t), t+1);
343 {414 *h = *(top-1);
344 Object *h = lua_hashdefine (avalue(top-3), top-2);415 top -= (mode == 2) ? 1 : 3;
345 *h = *(top-1);416 }
346 top -= 3;417 else { /* object is not a table, and/or has a specific "settable" method */
347 }418 if (im && ttype(im) != LUA_T_NIL) {
419 if (mode == 2) {
420 lua_checkstack(top+2);
421 *(top+1) = *(top-1);
422 *(top) = *(t+1);
423 *(top-1) = *t;
424 top += 2;
425 }
426 callIM(im, 3, 0);
427 }
428 else
429 lua_error("indexed expression not a table");
430 }
348}431}
349 432
350 433
351static void getglobal (Word n)434static void getglobal (Word n)
352{435{
353 *top = lua_table[n].object;436 TObject *value = &lua_table[n].object;
354 incr_top;437 TObject *im = luaI_getimbyObj(value, IM_GETGLOBAL);
355 if (tag(top-1) == LUA_T_NIL)438 if (ttype(im) == LUA_T_NIL) { /* default behavior */
356 { /* must call getglobal fallback */439 *top = *value;
357 tag(top-1) = LUA_T_STRING;440 incr_top;
358 tsvalue(top-1) = lua_table[n].varname;
359 callFB(FB_GETGLOBAL);
360 }441 }
442 else {
443 ttype(top) = LUA_T_STRING;
444 tsvalue(top) = lua_table[n].varname;
445 incr_top;
446 *top = *value;
447 incr_top;
448 callIM(im, 2, 1);
449 }
361}450}
362 451
363/*452/*
364** Traverse all objects on stack453** Traverse all objects on stack
365*/454*/
366void lua_travstack (int (*fn)(Object *))455void lua_travstack (int (*fn)(TObject *))
367{456{
368 Object *o;457 StkId i;
369 for (o = top-1; o >= stack; o--)458 for (i = (top-1)-stack; i>=0; i--)
370 fn (o);459 fn (stack+i);
371}460}
372 461
373 462
@@ -377,8 +466,11 @@ void lua_travstack (int (*fn)(Object *)) · 3 unmodified lines
377 466
378static void lua_message (char *s)467static void lua_message (char *s)
379{468{
380 lua_pushstring(s);469 TObject *im = luaI_geterrorim();
381 callFB(FB_ERROR);470 if (ttype(im) != LUA_T_NIL) {
471 lua_pushstring(s);
472 callIM(im, 1, 0);
473 }
382}474}
383 475
384/*476/*
@@ -399,25 +491,25 @@ void lua_error (char *s) · 14 unmodified lines
399 491
400lua_Function lua_stackedfunction (int level)492lua_Function lua_stackedfunction (int level)
401{493{
402 Object *p = top;494 StkId i;
403 while (--p >= stack)495 for (i = (top-1)-stack; i>=0; i--)
404 if (p->tag == LUA_T_MARK || p->tag == LUA_T_CMARK)496 if (stack[i].ttype == LUA_T_MARK || stack[i].ttype == LUA_T_CMARK)
405 if (level-- == 0)497 if (level-- == 0)
406 return Ref(p);498 return Ref(stack+i);
407 return LUA_NOOBJECT;499 return LUA_NOOBJECT;
408}500}
409 501
410 502
411int lua_currentline (lua_Function func)503int lua_currentline (lua_Function func)
412{504{
413 Object *f = Address(func);505 TObject *f = Address(func);
414 return (f+1 < top && (f+1)->tag == LUA_T_LINE) ? (f+1)->value.i : -1;506 return (f+1 < top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
415}507}
416 508
417 509
418lua_Object lua_getlocal (lua_Function func, int local_number, char **name)510lua_Object lua_getlocal (lua_Function func, int local_number, char **name)
419{511{
420 Object *f = luaI_Address(func);512 TObject *f = luaI_Address(func);
421 *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));513 *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
422 if (*name)514 if (*name)
423 {515 {
@@ -431,9 +523,9 @@ lua_Object lua_getlocal (lua_Function func, int local_number, char **name) · 7 unmodified lines
431 523
432int lua_setlocal (lua_Function func, int local_number)524int lua_setlocal (lua_Function func, int local_number)
433{525{
434 Object *f = Address(func);526 TObject *f = Address(func);
435 char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));527 char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
436 adjustC(1);528 checkCparams(1);
437 --top;529 --top;
438 if (name)530 if (name)
439 {531 {
@@ -446,6 +538,29 @@ int lua_setlocal (lua_Function func, int local_number) · 6 unmodified lines
446 return 0;538 return 0;
447}539}
448 540
541/*
542** Call the function at CLS_current.base, and incorporate results on
543** the Lua2C structure.
544*/
545static void do_callinc (int nResults)
546{
547 StkId base = CLS_current.base;
548 do_call(base+1, nResults);
549 CLS_current.lua2C = base; /* position of the new results */
550 CLS_current.num = (top-stack) - base; /* number of results */
551 CLS_current.base = base + CLS_current.num; /* incorporate results on stack */
552}
553
554
555static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
556{
557 StkId base = (top-stack)-nParams;
558 open_stack(nParams);
559 stack[base].ttype = LUA_T_CFUNCTION;
560 stack[base].value.f = f;
561 do_call(base+1, nResults);
562}
563
449 564
450/*565/*
451** Execute a protected call. Assumes that function is at CLS_current.base and566** Execute a protected call. Assumes that function is at CLS_current.base and
@@ -458,15 +573,11 @@ static int do_protectedrun (int nResults) · 6 unmodified lines
458 struct C_Lua_Stack oldCLS = CLS_current;573 struct C_Lua_Stack oldCLS = CLS_current;
459 jmp_buf *oldErr = errorJmp;574 jmp_buf *oldErr = errorJmp;
460 errorJmp = &myErrorJmp;575 errorJmp = &myErrorJmp;
461 if (setjmp(myErrorJmp) == 0)576 if (setjmp(myErrorJmp) == 0) {
462 {577 do_callinc(nResults);
463 do_call(CLS_current.base+1, nResults);
464 CLS_current.num = (top-stack) - CLS_current.base; /* number of results */
465 CLS_current.base += CLS_current.num; /* incorporate results on the stack */
466 status = 0;578 status = 0;
467 }579 }
468 else580 else { /* an error occurred: restore CLS_current and top */
469 { /* an error occurred: restore CLS_current and top */
470 CLS_current = oldCLS;581 CLS_current = oldCLS;
471 top = stack+CLS_current.base;582 top = stack+CLS_current.base;
472 status = 1;583 status = 1;
@@ -479,13 +590,14 @@ int luaI_dorun (TFunc *tf) · 6 unmodified lines
479{590{
480 int status;591 int status;
481 adjustC(1); /* one slot for the pseudo-function */592 adjustC(1); /* one slot for the pseudo-function */
482 stack[CLS_current.base].tag = LUA_T_FUNCTION;593 stack[CLS_current.base].ttype = LUA_T_FUNCTION;
483 stack[CLS_current.base].value.tf = tf;594 stack[CLS_current.base].value.tf = tf;
484 status = do_protectedrun(MULT_RET);595 status = do_protectedrun(MULT_RET);
485 return status;596 return status;
486}597}
487 598
488static int do_protectedmain (void)599
600int lua_domain (void)
489{601{
490 TFunc tf;602 TFunc tf;
491 int status;603 int status;
@@ -493,23 +605,21 @@ static int do_protectedmain (void) · 1 unmodified line
493 jmp_buf *oldErr = errorJmp;605 jmp_buf *oldErr = errorJmp;
494 errorJmp = &myErrorJmp;606 errorJmp = &myErrorJmp;
495 luaI_initTFunc(&tf);607 luaI_initTFunc(&tf);
496 tf.fileName = lua_parsedfile;608 if (setjmp(myErrorJmp) == 0) {
497 if (setjmp(myErrorJmp) == 0)
498 {
499 lua_parse(&tf);609 lua_parse(&tf);
500 status = luaI_dorun(&tf);610 status = 0;
501 }611 }
502 else612 else {
503 {
504 status = 1;
505 adjustC(0); /* erase extra slot */613 adjustC(0); /* erase extra slot */
614 status = 1;
506 }615 }
616 if (status == 0)
617 status = luaI_dorun(&tf);
507 errorJmp = oldErr;618 errorJmp = oldErr;
508 luaI_free(tf.code);619 luaI_free(tf.code);
509 return status;620 return status;
510}621}
511 622
512
513/*623/*
514** Execute the given lua function. Return 0 on success or 1 on error.624** Execute the given lua function. Return 0 on success or 1 on error.
515*/625*/
@@ -526,71 +636,31 @@ int lua_callfunction (lua_Object function) · 10 unmodified lines
526}636}
527 637
528 638
529int lua_call (char *funcname)639lua_Object lua_gettagmethod (int tag, char *event)
530{640{
531 Word n = luaI_findsymbolbyname(funcname);641 lua_pushnumber(tag);
532 open_stack((top-stack)-CLS_current.base);642 lua_pushstring(event);
533 stack[CLS_current.base] = s_object(n);643 do_unprotectedrun(luaI_gettagmethod, 2, 1);
534 return do_protectedrun(MULT_RET);644 return put_luaObjectonTop();
535}645}
536 646
537 647lua_Object lua_settagmethod (int tag, char *event)
538/*
539** Open file, generate opcode and execute global statement. Return 0 on
540** success or non 0 on error.
541*/
542int lua_dofile (char *filename)
543{648{
544 int status;649 TObject newmethod;
545 int c;650 checkCparams(1);
546 FILE *f = lua_openfile(filename);651 newmethod = *(--top);
547 if (f == NULL)652 lua_pushnumber(tag);
548 return 2;653 lua_pushstring(event);
549 c = fgetc(f);654 *top = newmethod; incr_top;
550 ungetc(c, f);655 do_unprotectedrun(luaI_settagmethod, 3, 1);
551 if (c == ID_CHUNK) {656 return put_luaObjectonTop();
552 f = freopen(filename, "rb", f); /* set binary mode */
553 status = luaI_undump(f);
554 }
555 else {
556 if (c == '#')
557 while ((c=fgetc(f)) != '\n') /* skip first line */;
558 status = do_protectedmain();
559 }
560 lua_closefile();
561 return status;
562}657}
563 658
564/*659lua_Object lua_seterrormethod (void)
565** Generate opcode stored on string and execute global statement. Return 0 on
566** success or non 0 on error.
567*/
568int lua_dostring (char *str)
569{660{
570 int status;661 checkCparams(1);
571 if (str == NULL)662 do_unprotectedrun(luaI_seterrormethod, 1, 1);
572 return 1;663 return put_luaObjectonTop();
573 lua_openstring(str);
574 status = do_protectedmain();
575 lua_closestring();
576 return status;
577}
578
579
580/*
581** API: set a function as a fallback
582*/
583lua_Object lua_setfallback (char *name, lua_CFunction fallback)
584{
585 adjustC(1); /* one slot for the pseudo-function */
586 stack[CLS_current.base].tag = LUA_T_CFUNCTION;
587 stack[CLS_current.base].value.f = luaI_setfallback;
588 lua_pushstring(name);
589 lua_pushcfunction(fallback);
590 if (do_protectedrun(1) == 0)
591 return (Ref(top-1));
592 else
593 return LUA_NOOBJECT;
594}664}
595 665
596 666
@@ -598,12 +668,11 @@ lua_Object lua_setfallback (char *name, lua_CFunction fallback) · 1 unmodified line
598** API: receives on the stack the table and the index.668** API: receives on the stack the table and the index.
599** returns the value.669** returns the value.
600*/670*/
601lua_Object lua_getsubscript (void)671lua_Object lua_gettable (void)
602{672{
603 adjustC(2);673 checkCparams(2);
604 pushsubscript();674 pushsubscript();
605 CLS_current.base++; /* incorporate object in the stack */675 return put_luaObjectonTop();
606 return (Ref(top-1));
607}676}
608 677
609 678
@@ -633,13 +702,25 @@ void lua_endblock (void) · 23 unmodified lines
633 adjustC(0);702 adjustC(0);
634}703}
635 704
705void lua_settag (int tag)
706{
707 checkCparams(1);
708 luaI_settag(tag, --top);
709}
710
636/* 711/*
637** API: receives on the stack the table, the index, and the new value.712** API: receives on the stack the table, the index, and the new value.
638*/713*/
639void lua_storesubscript (void)714void lua_settable (void)
640{715{
641 adjustC(3);716 checkCparams(3);
642 storesubscript();717 storesubscript(top-3, 1);
718}
719
720void lua_rawsettable (void)
721{
722 checkCparams(3);
723 storesubscript(top-3, 0);
643}724}
644 725
645/*726/*
@@ -647,40 +728,59 @@ void lua_storesubscript (void) · 1 unmodified line
647*/728*/
648lua_Object lua_createtable (void)729lua_Object lua_createtable (void)
649{730{
650 adjustC(0);731 TObject o;
651 avalue(top) = lua_createarray(0);732 avalue(&o) = lua_createarray(0);
652 tag(top) = LUA_T_ARRAY;733 ttype(&o) = LUA_T_ARRAY;
653 incr_top;734 return put_luaObject(&o);
654 CLS_current.base++; /* incorporate object in the stack */
655 return Ref(top-1);
656}735}
657 736
658/*737/*
659** Get a parameter, returning the object handle or LUA_NOOBJECT on error.738** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
660** 'number' must be 1 to get the first parameter.739** 'number' must be 1 to get the first parameter.
661*/740*/
662lua_Object lua_getparam (int number)741lua_Object lua_lua2C (int number)
742{
743 if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
744 /* Ref(stack+(CLS_current.lua2C+number-1)) ==
745 stack+(CLS_current.lua2C+number-1)-stack+1 == */
746 return CLS_current.lua2C+number;
747}
748
749int lua_isnil (lua_Object o)
750{
751 return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
752}
753
754int lua_istable (lua_Object o)
755{
756 return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
757}
758
759int lua_isuserdata (lua_Object o)
663{760{
664 if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;761 return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
665 /* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) ==
666 stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */
667 return CLS_current.base-CLS_current.num+number;
668}762}
669 763
670int lua_isnumber (lua_Object object)764int lua_iscfunction (lua_Object o)
671{765{
672 return (object != LUA_NOOBJECT) && (tonumber(Address(object)) == 0);766 int t = lua_tag(o);
767 return (t == LUA_T_CMARK) || (t == LUA_T_CFUNCTION);
673}768}
674 769
675int lua_isstring (lua_Object object)770int lua_isnumber (lua_Object o)
676{771{
677 int t = lua_type(object);772 return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
773}
774
775int lua_isstring (lua_Object o)
776{
777 int t = lua_tag(o);
678 return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);778 return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
679}779}
680 780
681int lua_isfunction (lua_Object object)781int lua_isfunction (lua_Object o)
682{782{
683 int t = lua_type(object);783 int t = lua_tag(o);
684 return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION) ||784 return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION) ||
685 (t == LUA_T_MARK) || (t == LUA_T_CMARK);785 (t == LUA_T_MARK) || (t == LUA_T_CMARK);
686}786}
@@ -700,57 +800,44 @@ real lua_getnumber (lua_Object object) · 13 unmodified lines
700*/800*/
701char *lua_getstring (lua_Object object)801char *lua_getstring (lua_Object object)
702{802{
703 if (object == LUA_NOOBJECT) return NULL;803 if (object == LUA_NOOBJECT || tostring (Address(object)))
704 if (tostring (Address(object))) return NULL;804 return NULL;
705 else return (svalue(Address(object)));805 else return (svalue(Address(object)));
706}806}
707 807
708/*808
709** Given an object handle, return its cfuntion pointer. On error, return NULL.809void *lua_getuserdata (lua_Object object)
710*/
711lua_CFunction lua_getcfunction (lua_Object object)
712{810{
713 if (object == LUA_NOOBJECT || ((tag(Address(object)) != LUA_T_CFUNCTION) &&811 if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
714 (tag(Address(object)) != LUA_T_CMARK)))812 return NULL;
715 return NULL;813 else return tsvalue(Address(object))->u.v;
716 else return (fvalue(Address(object)));
717}814}
718 815
816
719/*817/*
720** Given an object handle, return its user data. On error, return NULL.818** Given an object handle, return its cfuntion pointer. On error, return NULL.
721*/819*/
722void *lua_getuserdata (lua_Object object)820lua_CFunction lua_getcfunction (lua_Object object)
723{821{
724 if (object == LUA_NOOBJECT || tag(Address(object)) < LUA_T_USERDATA)822 if (object == LUA_NOOBJECT || ((ttype(Address(object)) != LUA_T_CFUNCTION) &&
823 (ttype(Address(object)) != LUA_T_CMARK)))
725 return NULL;824 return NULL;
726 else return (uvalue(Address(object)));825 else return (fvalue(Address(object)));
727}826}
728 827
729 828
730lua_Object lua_getref (int ref)829lua_Object lua_getref (int ref)
731{830{
732 Object *o = luaI_getref(ref);831 TObject *o = luaI_getref(ref);
733 if (o == NULL)832 if (o == NULL)
734 return LUA_NOOBJECT;833 return LUA_NOOBJECT;
735 adjustC(0);834 return put_luaObject(o);
736 luaI_pushobject(o);
737 CLS_current.base++; /* incorporate object in the stack */
738 return Ref(top-1);
739}
740
741
742void lua_pushref (int ref)
743{
744 Object *o = luaI_getref(ref);
745 if (o == NULL)
746 lua_error("access to invalid (possibly garbage collected) reference");
747 luaI_pushobject(o);
748}835}
749 836
750 837
751int lua_ref (int lock)838int lua_ref (int lock)
752{839{
753 adjustC(1);840 checkCparams(1);
754 return luaI_ref(--top, lock);841 return luaI_ref(--top, lock);
755}842}
756 843
@@ -761,20 +848,50 @@ int lua_ref (int lock) · 4 unmodified lines
761*/848*/
762lua_Object lua_getglobal (char *name)849lua_Object lua_getglobal (char *name)
763{850{
764 adjustC(0);851 getglobal(luaI_findsymbolbyname(name));
765 getglobal(luaI_findsymbolbyname(name));852 return put_luaObjectonTop();
766 CLS_current.base++; /* incorporate object in the stack */853}
767 return Ref(top-1);854
855
856lua_Object lua_rawgetglobal (char *name)
857{
858 return put_luaObject(&lua_table[luaI_findsymbolbyname(name)].object);
768}859}
769 860
861
770/*862/*
771** Store top of the stack at a global variable array field.863** Store top of the stack at a global variable array field.
772*/864*/
773void lua_storeglobal (char *name)865static void setglobal (Word n)
866{
867 TObject *oldvalue = &lua_table[n].object;
868 TObject *im = luaI_getimbyObj(oldvalue, IM_SETGLOBAL);
869 if (ttype(im) == LUA_T_NIL) /* default behavior */
870 s_object(n) = *(--top);
871 else {
872 TObject newvalue = *(top-1);
873 ttype(top-1) = LUA_T_STRING;
874 tsvalue(top-1) = lua_table[n].varname;
875 *top = *oldvalue;
876 incr_top;
877 *top = newvalue;
878 incr_top;
879 callIM(im, 3, 0);
880 }
881}
882
883
884void lua_setglobal (char *name)
774{885{
775 Word n = luaI_findsymbolbyname(name);886 checkCparams(1);
776 adjustC(1);887 setglobal(luaI_findsymbolbyname(name));
777 s_object(n) = *(--top);888}
889
890void lua_rawsetglobal (char *name)
891{
892 Word n = luaI_findsymbolbyname(name);
893 checkCparams(1);
894 s_object(n) = *(--top);
778}895}
779 896
780/*897/*
@@ -782,60 +899,59 @@ void lua_storeglobal (char *name) · 1 unmodified line
782*/899*/
783void lua_pushnil (void)900void lua_pushnil (void)
784{901{
785 tag(top) = LUA_T_NIL;902 ttype(top) = LUA_T_NIL;
786 incr_top;903 incr_top;
787}904}
788 905
789/*906/*
790** Push an object (tag=number) to stack.907** Push an object (ttype=number) to stack.
791*/908*/
792void lua_pushnumber (real n)909void lua_pushnumber (real n)
793{910{
794 tag(top) = LUA_T_NUMBER; nvalue(top) = n;911 ttype(top) = LUA_T_NUMBER; nvalue(top) = n;
795 incr_top;912 incr_top;
796}913}
797 914
798/*915/*
799** Push an object (tag=string) to stack.916** Push an object (ttype=string) to stack.
800*/917*/
801void lua_pushstring (char *s)918void lua_pushstring (char *s)
802{919{
803 if (s == NULL)920 if (s == NULL)
804 tag(top) = LUA_T_NIL;921 ttype(top) = LUA_T_NIL;
805 else922 else
806 {923 {
807 tsvalue(top) = lua_createstring(s);924 tsvalue(top) = lua_createstring(s);
808 tag(top) = LUA_T_STRING;925 ttype(top) = LUA_T_STRING;
809 }926 }
810 incr_top;927 incr_top;
811}928}
812/*>>>>>>>>>#undef lua_pushliteral929
813void lua_pushliteral(char *s) { lua_pushstring(s); }*/
814 930
815/*931/*
816** Push an object (tag=cfunction) to stack.932** Push an object (ttype=cfunction) to stack.
817*/933*/
818void lua_pushcfunction (lua_CFunction fn)934void lua_pushcfunction (lua_CFunction fn)
819{935{
820 tag(top) = LUA_T_CFUNCTION; fvalue(top) = fn;936 ttype(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
821 incr_top;937 incr_top;
822}938}
823 939
824/*940
825** Push an object (tag=userdata) to stack.941
826*/
827void lua_pushusertag (void *u, int tag)942void lua_pushusertag (void *u, int tag)
828{943{
829 if (tag < LUA_T_USERDATA) 944 if (tag < 0 && tag != LUA_ANYTAG)
830 lua_error("invalid tag in `lua_pushusertag'");945 luaI_realtag(tag); /* error if tag is not valid */
831 tag(top) = tag; uvalue(top) = u;946 tsvalue(top) = luaI_createudata(u, tag);
832 incr_top;947 ttype(top) = LUA_T_USERDATA;
948 incr_top;
833}949}
834 950
835/*951/*
836** Push an object on the stack.952** Push an object on the stack.
837*/953*/
838void luaI_pushobject (Object *o)954void luaI_pushobject (TObject *o)
839{955{
840 *top = *o;956 *top = *o;
841 incr_top;957 incr_top;
@@ -847,78 +963,102 @@ void luaI_pushobject (Object *o) · 5 unmodified lines
847void lua_pushobject (lua_Object o)963void lua_pushobject (lua_Object o)
848{964{
849 if (o == LUA_NOOBJECT)965 if (o == LUA_NOOBJECT)
850 lua_error("attempt to push a NOOBJECT");966 lua_error("API error - attempt to push a NOOBJECT");
851 *top = *Address(o);967 *top = *Address(o);
852 if (tag(top) == LUA_T_MARK) tag(top) = LUA_T_FUNCTION;968 if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION;
853 else if (tag(top) == LUA_T_CMARK) tag(top) = LUA_T_CFUNCTION;969 else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION;
854 incr_top;970 incr_top;
855}971}
856 972
857int lua_type (lua_Object o)973int lua_tag (lua_Object lo)
858{974{
859 if (o == LUA_NOOBJECT)975 if (lo == LUA_NOOBJECT) return LUA_T_NIL;
860 return LUA_T_NIL;976 else {
861 else977 TObject *o = Address(lo);
862 return tag(Address(o));978 lua_Type t = ttype(o);
979 if (t == LUA_T_USERDATA)
980 return o->value.ts->tag;
981 else if (t == LUA_T_ARRAY)
982 return o->value.a->htag;
983 else return t;
984 }
863}985}
864 986
865 987
866void luaI_gcFB (Object *o)988void luaI_gcIM (TObject *o)
867{989{
868 *top = *o;990 TObject *im = luaI_getimbyObj(o, IM_GC);
869 incr_top;991 if (ttype(im) != LUA_T_NIL) {
870 callFB(FB_GC);992 *top = *o;
993 incr_top;
994 callIM(im, 1, 0);
995 }
996}
997
998
999static void call_binTM (IMS event, char *msg)
1000{
1001 TObject *im = luaI_getimbyObj(top-2, event); /* try first operand */
1002 if (ttype(im) == LUA_T_NIL) {
1003 im = luaI_getimbyObj(top-1, event); /* try second operand */
1004 if (ttype(im) == LUA_T_NIL) {
1005 im = luaI_getim(0, event); /* try a 'global' i.m. */
1006 if (ttype(im) == LUA_T_NIL)
1007 lua_error(msg);
1008 }
1009 }
1010 lua_pushstring(luaI_eventname[event]);
1011 callIM(im, 3, 1);
871}1012}
872 1013
873 1014
874static void call_arith (char *op)1015static void call_arith (IMS event)
875{1016{
876 lua_pushstring(op);1017 call_binTM(event, "unexpected type at arithmetic operation");
877 callFB(FB_ARITH);
878}1018}
879 1019
880static void comparison (lua_Type tag_less, lua_Type tag_equal, 1020
881 lua_Type tag_great, char *op)1021static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
1022 lua_Type ttype_great, IMS op)
882{1023{
883 Object *l = top-2;1024 TObject *l = top-2;
884 Object *r = top-1;1025 TObject *r = top-1;
885 int result;1026 int result;
886 if (tag(l) == LUA_T_NUMBER && tag(r) == LUA_T_NUMBER)1027 if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
887 result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;1028 result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
888 else if (tostring(l) || tostring(r))1029 else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
889 {1030 result = strcmp(svalue(l), svalue(r));
890 lua_pushstring(op);1031 else {
891 callFB(FB_ORDER);1032 call_binTM(op, "unexpected type at comparison");
892 return;1033 return;
893 }1034 }
894 else
895 result = strcmp(svalue(l), svalue(r));
896 top--;1035 top--;
897 nvalue(top-1) = 1;1036 nvalue(top-1) = 1;
898 tag(top-1) = (result < 0) ? tag_less : (result == 0) ? tag_equal : tag_great;1037 ttype(top-1) = (result < 0) ? ttype_less :
1038 (result == 0) ? ttype_equal : ttype_great;
899}1039}
900 1040
901 1041
902static void adjust_varargs (StkId first_extra_arg)1042static void adjust_varargs (StkId first_extra_arg)
903{1043{
904 Object arg;1044 TObject arg;
905 Object *firstelem = stack+first_extra_arg;1045 TObject *firstelem = stack+first_extra_arg;
906 int nvararg = top-firstelem;1046 int nvararg = top-firstelem;
907 int i;1047 int i;
908 if (nvararg < 0) nvararg = 0;1048 if (nvararg < 0) nvararg = 0;
909 avalue(&arg) = lua_createarray(nvararg+1); /* +1 for field 'n' */1049 avalue(&arg) = lua_createarray(nvararg+1); /* +1 for field 'n' */
910 tag(&arg) = LUA_T_ARRAY;1050 ttype(&arg) = LUA_T_ARRAY;
911 for (i=0; i<nvararg; i++) {1051 for (i=0; i<nvararg; i++) {
912 Object index;1052 TObject index;
913 tag(&index) = LUA_T_NUMBER;1053 ttype(&index) = LUA_T_NUMBER;
914 nvalue(&index) = i+1;1054 nvalue(&index) = i+1;
915 *(lua_hashdefine(avalue(&arg), &index)) = *(firstelem+i);1055 *(lua_hashdefine(avalue(&arg), &index)) = *(firstelem+i);
916 }1056 }
917 /* store counter in field "n" */ {1057 /* store counter in field "n" */ {
918 Object index, extra;1058 TObject index, extra;
919 tag(&index) = LUA_T_STRING;1059 ttype(&index) = LUA_T_STRING;
920 tsvalue(&index) = lua_createstring("n");1060 tsvalue(&index) = lua_createstring("n");
921 tag(&extra) = LUA_T_NUMBER;1061 ttype(&extra) = LUA_T_NUMBER;
922 nvalue(&extra) = nvararg;1062 nvalue(&extra) = nvararg;
923 *(lua_hashdefine(avalue(&arg), &index)) = extra;1063 *(lua_hashdefine(avalue(&arg), &index)) = extra;
924 }1064 }
@@ -942,22 +1082,22 @@ static StkId lua_execute (Byte *pc, StkId base) · 17 unmodified lines
942 OpCode opcode;1082 OpCode opcode;
943 switch (opcode = (OpCode)*pc++)1083 switch (opcode = (OpCode)*pc++)
944 {1084 {
945 case PUSHNIL: tag(top) = LUA_T_NIL; incr_top; break;1085 case PUSHNIL: ttype(top) = LUA_T_NIL; incr_top; break;
946 1086
947 case PUSH0: case PUSH1: case PUSH2:1087 case PUSH0: case PUSH1: case PUSH2:
948 tag(top) = LUA_T_NUMBER;1088 ttype(top) = LUA_T_NUMBER;
949 nvalue(top) = opcode-PUSH0;1089 nvalue(top) = opcode-PUSH0;
950 incr_top;1090 incr_top;
951 break;1091 break;
952 1092
953 case PUSHBYTE: 1093 case PUSHBYTE:
954 tag(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;1094 ttype(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
955 1095
956 case PUSHWORD:1096 case PUSHWORD:
957 {1097 {
958 Word w;1098 Word w;
959 get_word(w,pc);1099 get_word(w,pc);
960 tag(top) = LUA_T_NUMBER; nvalue(top) = w;1100 ttype(top) = LUA_T_NUMBER; nvalue(top) = w;
961 incr_top;1101 incr_top;
962 }1102 }
963 break;1103 break;
@@ -966,7 +1106,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 2 unmodified lines
966 {1106 {
967 real num;1107 real num;
968 get_float(num,pc);1108 get_float(num,pc);
969 tag(top) = LUA_T_NUMBER; nvalue(top) = num;1109 ttype(top) = LUA_T_NUMBER; nvalue(top) = num;
970 incr_top;1110 incr_top;
971 }1111 }
972 break;1112 break;
@@ -975,7 +1115,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 2 unmodified lines
975 {1115 {
976 Word w;1116 Word w;
977 get_word(w,pc);1117 get_word(w,pc);
978 tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];1118 ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
979 incr_top;1119 incr_top;
980 }1120 }
981 break;1121 break;
@@ -985,7 +1125,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 3 unmodified lines
985 TFunc *f;1125 TFunc *f;
986 get_code(f,pc);1126 get_code(f,pc);
987 luaI_insertfunction(f); /* may take part in GC */1127 luaI_insertfunction(f); /* may take part in GC */
988 top->tag = LUA_T_FUNCTION;1128 top->ttype = LUA_T_FUNCTION;
989 top->value.tf = f;1129 top->value.tf = f;
990 incr_top;1130 incr_top;
991 }1131 }
@@ -1013,10 +1153,10 @@ static StkId lua_execute (Byte *pc, StkId base) · 21 unmodified lines
1013 1153
1014 case PUSHSELF:1154 case PUSHSELF:
1015 {1155 {
1016 Object receiver = *(top-1);1156 TObject receiver = *(top-1);
1017 Word w;1157 Word w;
1018 get_word(w,pc);1158 get_word(w,pc);
1019 tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];1159 ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
1020 incr_top;1160 incr_top;
1021 pushsubscript();1161 pushsubscript();
1022 *top = receiver;1162 *top = receiver;
@@ -1037,47 +1177,32 @@ static StkId lua_execute (Byte *pc, StkId base) · 14 unmodified lines
1037 {1177 {
1038 Word w;1178 Word w;
1039 get_word(w,pc);1179 get_word(w,pc);
1040 s_object(w) = *(--top);1180 setglobal(w);
1041 }1181 }
1042 break;1182 break;
1043 1183
1044 case STOREINDEXED0:1184 case STOREINDEXED0:
1045 storesubscript();1185 storesubscript(top-3, 1);
1046 break;1186 break;
1047 1187
1048 case STOREINDEXED:1188 case STOREINDEXED: {
1049 {1189 int n = *pc++;
1050 int n = *pc++;1190 storesubscript(top-3-n, 2);
1051 if (tag(top-3-n) != LUA_T_ARRAY)1191 break;
1052 {
1053 lua_checkstack(top+2);
1054 *(top+1) = *(top-1);
1055 *(top) = *(top-2-n);
1056 *(top-1) = *(top-3-n);
1057 top += 2;
1058 callFB(FB_SETTABLE);
1059 }
1060 else
1061 {
1062 Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
1063 *h = *(top-1);
1064 top--;
1065 }
1066 }1192 }
1067 break;
1068 1193
1069 case STORELIST0:1194 case STORELIST0:
1070 case STORELIST:1195 case STORELIST:
1071 {1196 {
1072 int m, n;1197 int m, n;
1073 Object *arr;1198 TObject *arr;
1074 if (opcode == STORELIST0) m = 0;1199 if (opcode == STORELIST0) m = 0;
1075 else m = *(pc++) * FIELDS_PER_FLUSH;1200 else m = *(pc++) * FIELDS_PER_FLUSH;
1076 n = *(pc++);1201 n = *(pc++);
1077 arr = top-n-1;1202 arr = top-n-1;
1078 while (n)1203 while (n)
1079 {1204 {
1080 tag(top) = LUA_T_NUMBER; nvalue(top) = n+m;1205 ttype(top) = LUA_T_NUMBER; nvalue(top) = n+m;
1081 *(lua_hashdefine (avalue(arr), top)) = *(top-1);1206 *(lua_hashdefine (avalue(arr), top)) = *(top-1);
1082 top--;1207 top--;
1083 n--;1208 n--;
@@ -1085,15 +1210,15 @@ static StkId lua_execute (Byte *pc, StkId base) · 1 unmodified line
1085 }1210 }
1086 break;1211 break;
1087 1212
1088 case STORERECORD:1213 case STORERECORD: /* opcode obsolete: supersed by STOREMAP */
1089 {1214 {
1090 int n = *(pc++);1215 int n = *(pc++);
1091 Object *arr = top-n-1;1216 TObject *arr = top-n-1;
1092 while (n)1217 while (n)
1093 {1218 {
1094 Word w;1219 Word w;
1095 get_word(w,pc);1220 get_word(w,pc);
1096 tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];1221 ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
1097 *(lua_hashdefine (avalue(arr), top)) = *(top-1);1222 *(lua_hashdefine (avalue(arr), top)) = *(top-1);
1098 top--;1223 top--;
1099 n--;1224 n--;
@@ -1101,13 +1226,25 @@ static StkId lua_execute (Byte *pc, StkId base) · 1 unmodified line
1101 }1226 }
1102 break;1227 break;
1103 1228
1229 case STOREMAP: {
1230 int n = *(pc++);
1231 TObject *arr = top-(2*n)-1;
1232 while (n--) {
1233 *(lua_hashdefine (avalue(arr), top-2)) = *(top-1);
1234 top-=2;
1235 }
1236 }
1237 break;
1238
1104 case ADJUST0:1239 case ADJUST0:
1105 adjust_top(base);1240 adjust_top(base);
1106 break;1241 break;
1107 1242
1108 case ADJUST:1243 case ADJUST: {
1109 adjust_top(base + *(pc++));1244 StkId newtop = base + *(pc++);
1245 adjust_top(newtop);
1110 break;1246 break;
1247 }
1111 1248
1112 case VARARGS:1249 case VARARGS:
1113 adjust_varargs(base + *(pc++));1250 adjust_varargs(base + *(pc++));
@@ -1118,7 +1255,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 4 unmodified lines
1118 Word size;1255 Word size;
1119 get_word(size,pc);1256 get_word(size,pc);
1120 avalue(top) = lua_createarray(size);1257 avalue(top) = lua_createarray(size);
1121 tag(top) = LUA_T_ARRAY;1258 ttype(top) = LUA_T_ARRAY;
1122 incr_top;1259 incr_top;
1123 }1260 }
1124 break;1261 break;
@@ -1127,33 +1264,33 @@ static StkId lua_execute (Byte *pc, StkId base) · 2 unmodified lines
1127 {1264 {
1128 int res = lua_equalObj(top-2, top-1);1265 int res = lua_equalObj(top-2, top-1);
1129 --top;1266 --top;
1130 tag(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;1267 ttype(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
1131 nvalue(top-1) = 1;1268 nvalue(top-1) = 1;
1132 }1269 }
1133 break;1270 break;
1134 1271
1135 case LTOP:1272 case LTOP:
1136 comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "lt");1273 comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
1137 break;1274 break;
1138 1275
1139 case LEOP:1276 case LEOP:
1140 comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "le");1277 comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
1141 break;1278 break;
1142 1279
1143 case GTOP:1280 case GTOP:
1144 comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, "gt");1281 comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
1145 break;1282 break;
1146 1283
1147 case GEOP:1284 case GEOP:
1148 comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, "ge");1285 comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE);
1149 break;1286 break;
1150 1287
1151 case ADDOP:1288 case ADDOP:
1152 {1289 {
1153 Object *l = top-2;1290 TObject *l = top-2;
1154 Object *r = top-1;1291 TObject *r = top-1;
1155 if (tonumber(r) || tonumber(l))1292 if (tonumber(r) || tonumber(l))
1156 call_arith("add");1293 call_arith(IM_ADD);
1157 else1294 else
1158 {1295 {
1159 nvalue(l) += nvalue(r);1296 nvalue(l) += nvalue(r);
@@ -1164,10 +1301,10 @@ static StkId lua_execute (Byte *pc, StkId base) · 4 unmodified lines
1164 1301
1165 case SUBOP:1302 case SUBOP:
1166 {1303 {
1167 Object *l = top-2;1304 TObject *l = top-2;
1168 Object *r = top-1;1305 TObject *r = top-1;
1169 if (tonumber(r) || tonumber(l))1306 if (tonumber(r) || tonumber(l))
1170 call_arith("sub");1307 call_arith(IM_SUB);
1171 else1308 else
1172 {1309 {
1173 nvalue(l) -= nvalue(r);1310 nvalue(l) -= nvalue(r);
@@ -1178,10 +1315,10 @@ static StkId lua_execute (Byte *pc, StkId base) · 4 unmodified lines
1178 1315
1179 case MULTOP:1316 case MULTOP:
1180 {1317 {
1181 Object *l = top-2;1318 TObject *l = top-2;
1182 Object *r = top-1;1319 TObject *r = top-1;
1183 if (tonumber(r) || tonumber(l))1320 if (tonumber(r) || tonumber(l))
1184 call_arith("mul");1321 call_arith(IM_MUL);
1185 else1322 else
1186 {1323 {
1187 nvalue(l) *= nvalue(r);1324 nvalue(l) *= nvalue(r);
@@ -1192,10 +1329,10 @@ static StkId lua_execute (Byte *pc, StkId base) · 4 unmodified lines
1192 1329
1193 case DIVOP:1330 case DIVOP:
1194 {1331 {
1195 Object *l = top-2;1332 TObject *l = top-2;
1196 Object *r = top-1;1333 TObject *r = top-1;
1197 if (tonumber(r) || tonumber(l))1334 if (tonumber(r) || tonumber(l))
1198 call_arith("div");1335 call_arith(IM_DIV);
1199 else1336 else
1200 {1337 {
1201 nvalue(l) /= nvalue(r);1338 nvalue(l) /= nvalue(r);
@@ -1205,36 +1342,34 @@ static StkId lua_execute (Byte *pc, StkId base) · 3 unmodified lines
1205 break;1342 break;
1206 1343
1207 case POWOP:1344 case POWOP:
1208 call_arith("pow");1345 call_arith(IM_POW);
1209 break;1346 break;
1210 1347
1211 case CONCOP:1348 case CONCOP: {
1212 {1349 TObject *l = top-2;
1213 Object *l = top-2;1350 TObject *r = top-1;
1214 Object *r = top-1;1351 if (tostring(l) || tostring(r))
1215 if (tostring(r) || tostring(l))1352 call_binTM(IM_CONCAT, "unexpected type for concatenation");
1216 callFB(FB_CONCAT);1353 else {
1217 else1354 tsvalue(l) = lua_createstring(lua_strconc(svalue(l),svalue(r)));
1218 {1355 --top;
1219 tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));1356 }
1220 --top;
1221 }
1222 }1357 }
1223 break;1358 break;
1224 1359
1225 case MINUSOP:1360 case MINUSOP:
1226 if (tonumber(top-1))1361 if (tonumber(top-1))
1227 {1362 {
1228 tag(top) = LUA_T_NIL;1363 ttype(top) = LUA_T_NIL;
1229 incr_top;1364 incr_top;
1230 call_arith("unm");1365 call_arith(IM_UNM);
1231 }1366 }
1232 else1367 else
1233 nvalue(top-1) = - nvalue(top-1);1368 nvalue(top-1) = - nvalue(top-1);
1234 break;1369 break;
1235 1370
1236 case NOTOP:1371 case NOTOP:
1237 tag(top-1) = (tag(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;1372 ttype(top-1) = (ttype(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
1238 nvalue(top-1) = 1;1373 nvalue(top-1) = 1;
1239 break;1374 break;
1240 1375
@@ -1242,15 +1377,15 @@ static StkId lua_execute (Byte *pc, StkId base) · 1 unmodified line
1242 {1377 {
1243 Word w;1378 Word w;
1244 get_word(w,pc);1379 get_word(w,pc);
1245 if (tag(top-1) != LUA_T_NIL) pc += w;1380 if (ttype(top-1) != LUA_T_NIL) pc += w;
1246 }1381 }
1247 break;1382 break;
1248 1383
1249 case ONFJMP: 1384 case ONFJMP:
1250 {1385 {
1251 Word w;1386 Word w;
1252 get_word(w,pc);1387 get_word(w,pc);
1253 if (tag(top-1) == LUA_T_NIL) pc += w;1388 if (ttype(top-1) == LUA_T_NIL) pc += w;
1254 }1389 }
1255 break;1390 break;
1256 1391
@@ -1275,7 +1410,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 18 unmodified lines
1275 Word w;1410 Word w;
1276 get_word(w,pc);1411 get_word(w,pc);
1277 top--;1412 top--;
1278 if (tag(top) == LUA_T_NIL) pc += w;1413 if (ttype(top) == LUA_T_NIL) pc += w;
1279 }1414 }
1280 break;1415 break;
1281 1416
@@ -1284,7 +1419,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 2 unmodified lines
1284 Word w;1419 Word w;
1285 get_word(w,pc);1420 get_word(w,pc);
1286 top--;1421 top--;
1287 if (tag(top) == LUA_T_NIL) pc -= w;1422 if (ttype(top) == LUA_T_NIL) pc -= w;
1288 }1423 }
1289 break;1424 break;
1290 1425
@@ -1309,12 +1444,12 @@ static StkId lua_execute (Byte *pc, StkId base) · 18 unmodified lines
1309 {1444 {
1310 Word line;1445 Word line;
1311 get_word(line,pc);1446 get_word(line,pc);
1312 if ((stack+base-1)->tag != LUA_T_LINE)1447 if ((stack+base-1)->ttype != LUA_T_LINE)
1313 {1448 {
1314 /* open space for LINE value */1449 /* open space for LINE value */
1315 open_stack((top-stack)-base);1450 open_stack((top-stack)-base);
1316 base++;1451 base++;
1317 (stack+base-1)->tag = LUA_T_LINE;1452 (stack+base-1)->ttype = LUA_T_LINE;
1318 }1453 }
1319 (stack+base-1)->value.i = line;1454 (stack+base-1)->value.i = line;
1320 if (lua_linehook)1455 if (lua_linehook)
@@ -1328,3 +1463,16 @@ static StkId lua_execute (Byte *pc, StkId base) · 7 unmodified lines
1328 }1463 }
1329}1464}
1330 1465
1466
1467#if COMPAT2_5
1468/*
1469** API: set a function as a fallback
1470*/
1471lua_Object lua_setfallback (char *name, lua_CFunction fallback)
1472{
1473 lua_pushstring(name);
1474 lua_pushcfunction(fallback);
1475 do_unprotectedrun(luaI_setfallback, 2, 1);
1476 return put_luaObjectonTop();
1477}
1478#endif
src/opcode.h+46−25
@@ -1,6 +1,6 @@
1/*1/*
2** TeCGraf - PUC-Rio2** TeCGraf - PUC-Rio
3** $Id: opcode.h,v 3.24 1996/11/01 12:46:59 roberto Exp $3** $Id: opcode.h,v 3.34 1997/06/16 16:50:22 roberto Exp $
4*/4*/
5 5
6#ifndef opcode_h6#ifndef opcode_h
@@ -14,6 +14,28 @@ · 7 unmodified lines
14 14
15#define FIELDS_PER_FLUSH 4015#define FIELDS_PER_FLUSH 40
16 16
17/*
18* WARNING: if you change the order of this enumeration,
19* grep "ORDER LUA_T"
20*/
21typedef enum
22{
23 LUA_T_NIL = -9,
24 LUA_T_NUMBER = -8,
25 LUA_T_STRING = -7,
26 LUA_T_ARRAY = -6, /* array==table */
27 LUA_T_FUNCTION = -5,
28 LUA_T_CFUNCTION= -4,
29 LUA_T_MARK = -3,
30 LUA_T_CMARK = -2,
31 LUA_T_LINE = -1,
32 LUA_T_USERDATA = 0
33} lua_Type;
34
35#define NUM_TYPES 10
36
37
38extern char *luaI_typenames[];
17 39
18typedef enum {40typedef enum {
19/* name parm before after side effect41/* name parm before after side effect
@@ -38,7 +60,7 @@ PUSHLOCAL6,/* - LOC[6] */ · 18 unmodified lines
38PUSHLOCAL7,/* - LOC[7] */60PUSHLOCAL7,/* - LOC[7] */
39PUSHLOCAL8,/* - LOC[8] */61PUSHLOCAL8,/* - LOC[8] */
40PUSHLOCAL9,/* - LOC[9] */62PUSHLOCAL9,/* - LOC[9] */
41PUSHLOCAL,/* w - LOC[w] */63PUSHLOCAL,/* b - LOC[b] */
42PUSHGLOBAL,/* w - VAR[w] */64PUSHGLOBAL,/* w - VAR[w] */
43PUSHINDEXED,/* i t t[i] */65PUSHINDEXED,/* i t t[i] */
44PUSHSELF,/* w t t t[STR[w]] */66PUSHSELF,/* w t t t[STR[w]] */
@@ -52,14 +74,14 @@ STORELOCAL6,/* x - LOC[6]=x */ · 7 unmodified lines
52STORELOCAL7,/* x - LOC[7]=x */74STORELOCAL7,/* x - LOC[7]=x */
53STORELOCAL8,/* x - LOC[8]=x */75STORELOCAL8,/* x - LOC[8]=x */
54STORELOCAL9,/* x - LOC[9]=x */76STORELOCAL9,/* x - LOC[9]=x */
55STORELOCAL,/* w x - LOC[w]=x */77STORELOCAL,/* b x - LOC[b]=x */
56STOREGLOBAL,/* w x - VAR[w]=x */78STOREGLOBAL,/* w x - VAR[w]=x */
57STOREINDEXED0,/* v i t - t[i]=v */79STOREINDEXED0,/* v i t - t[i]=v */
58STOREINDEXED,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */80STOREINDEXED,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
59STORELIST0,/* w v_w...v_1 t - t[i]=v_i */81STORELIST0,/* b v_b...v_1 t - t[i]=v_i */
60STORELIST,/* w n v_w...v_1 t - t[i+n*FPF]=v_i */82STORELIST,/* b c v_b...v_1 t - t[i+c*FPF]=v_i */
61STORERECORD,/* n83STORERECORD,/* b
62 w_n...w_1 v_n...v_1 t - t[STR[w_i]]=v_i */84 w_b...w_1 v_b...v_1 t - t[STR[w_i]]=v_i */
63ADJUST0,/* - - TOP=BASE */85ADJUST0,/* - - TOP=BASE */
64ADJUST,/* b - - TOP=BASE+b */86ADJUST,/* b - - TOP=BASE+b */
65CREATEARRAY,/* w - newarray(size = w) */87CREATEARRAY,/* w - newarray(size = w) */
@@ -76,19 +98,19 @@ POWOP,/* y x x^y */ · 10 unmodified lines
76CONCOP,/* y x x..y */98CONCOP,/* y x x..y */
77MINUSOP,/* x -x */99MINUSOP,/* x -x */
78NOTOP,/* x (x==nil)? 1 : nil */100NOTOP,/* x (x==nil)? 1 : nil */
79ONTJMP,/* w x - (x!=nil)? PC+=w */101ONTJMP,/* w x - (x!=nil)? PC+=w */
80ONFJMP,/* w x - (x==nil)? PC+=w */102ONFJMP,/* w x - (x==nil)? PC+=w */
81JMP,/* w - - PC+=w */103JMP,/* w - - PC+=w */
82UPJMP,/* w - - PC-=w */104UPJMP,/* w - - PC-=w */
83IFFJMP,/* w x - (x==nil)? PC+=w */105IFFJMP,/* w x - (x==nil)? PC+=w */
84IFFUPJMP,/* w x - (x==nil)? PC-=w */106IFFUPJMP,/* w x - (x==nil)? PC-=w */
85POP,/* x - */107POP,/* x - */
86CALLFUNC,/* n m v_n...v_1 f r_m...r_1 f(v1,...,v_n) */108CALLFUNC,/* b c v_b...v_1 f r_c...r_1 f(v1,...,v_b) */
87RETCODE0,109RETCODE0,
88RETCODE,/* b - - */110RETCODE,/* b - - */
89SETLINE,/* w - - LINE=w */111SETLINE,/* w - - LINE=w */
90VARARGS/* b v_n...v_1 {v_1...v_n;n=n} */112VARARGS,/* b v_b...v_1 {v_1...v_b;n=b} */
91 113STOREMAP/* b v_b k_b ...v_1 k_1 t - t[k_i]=v_i */
92} OpCode;114} OpCode;
93 115
94 116
@@ -102,29 +124,27 @@ typedef union · 7 unmodified lines
102 TaggedString *ts;124 TaggedString *ts;
103 TFunc *tf;125 TFunc *tf;
104 struct Hash *a;126 struct Hash *a;
105 void *u;
106 int i;127 int i;
107} Value;128} Value;
108 129
109typedef struct Object130typedef struct TObject
110{131{
111 lua_Type tag;132 lua_Type ttype;
112 Value value;133 Value value;
113} Object;134} TObject;
114 135
115 136
116/* Macros to access structure members */137/* Macros to access structure members */
117#define tag(o) ((o)->tag)138#define ttype(o) ((o)->ttype)
118#define nvalue(o) ((o)->value.n)139#define nvalue(o) ((o)->value.n)
119#define svalue(o) ((o)->value.ts->str)140#define svalue(o) ((o)->value.ts->str)
120#define tsvalue(o) ((o)->value.ts)141#define tsvalue(o) ((o)->value.ts)
121#define avalue(o) ((o)->value.a)142#define avalue(o) ((o)->value.a)
122#define fvalue(o) ((o)->value.f)143#define fvalue(o) ((o)->value.f)
123#define uvalue(o) ((o)->value.u)
124 144
125/* Macros to access symbol table */145/* Macros to access symbol table */
126#define s_object(i) (lua_table[i].object)146#define s_object(i) (lua_table[i].object)
127#define s_tag(i) (tag(&s_object(i)))147#define s_ttype(i) (ttype(&s_object(i)))
128#define s_nvalue(i) (nvalue(&s_object(i)))148#define s_nvalue(i) (nvalue(&s_object(i)))
129#define s_svalue(i) (svalue(&s_object(i)))149#define s_svalue(i) (svalue(&s_object(i)))
130#define s_tsvalue(i) (tsvalue(&s_object(i)))150#define s_tsvalue(i) (tsvalue(&s_object(i)))
@@ -141,10 +161,11 @@ typedef struct Object · 10 unmodified lines
141/* Exported functions */161/* Exported functions */
142void lua_parse (TFunc *tf); /* from "lua.stx" module */162void lua_parse (TFunc *tf); /* from "lua.stx" module */
143void luaI_codedebugline (int line); /* from "lua.stx" module */163void luaI_codedebugline (int line); /* from "lua.stx" module */
144void lua_travstack (int (*fn)(Object *));164void lua_travstack (int (*fn)(TObject *));
145Object *luaI_Address (lua_Object o);165TObject *luaI_Address (lua_Object o);
146void luaI_pushobject (Object *o);166void luaI_pushobject (TObject *o);
147void luaI_gcFB (Object *o);167void luaI_gcIM (TObject *o);
148int luaI_dorun (TFunc *tf);168int luaI_dorun (TFunc *tf);
169int lua_domain (void);
149 170
150#endif171#endif
src/parser.c+344−316
@@ -10,14 +10,14 @@ static char luaY_sccsid[] = "@(#)yaccpar 1.9 (Berkeley) 02/21/93"; · 9 unmodified lines
10#define YYPREFIX "luaY_"10#define YYPREFIX "luaY_"
11#line 2 "lua.stx"11#line 2 "lua.stx"
12 12
13char *rcs_luastx = "$Id: parser.c,v 1.1 1996/11/21 16:11:40 lhf Exp $";13char *rcs_luastx = "$Id: parser.c,v 1.1 1997/06/30 18:59:03 lhf Exp $";
14 14
15#include <stdio.h>15#include <stdio.h>
16#include <stdlib.h>16#include <stdlib.h>
17#include <string.h>17#include <string.h>
18 18
19#include "luadebug.h"19#include "luadebug.h"
20#include "mem.h"20#include "luamem.h"
21#include "lex.h"21#include "lex.h"
22#include "opcode.h"22#include "opcode.h"
23#include "hash.h"23#include "hash.h"
@@ -60,8 +60,6 @@ static TaggedString *localvar[MAXLOCALS]; /* store local variable names */ · 36 unmodified lines
60static int nlocalvar=0; /* number of local variables */60static int nlocalvar=0; /* number of local variables */
61 61
62#define MAXFIELDS FIELDS_PER_FLUSH*262#define MAXFIELDS FIELDS_PER_FLUSH*2
63static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
64static int nfields=0;
65 63
66int lua_debug = 0;64int lua_debug = 0;
67 65
@@ -113,22 +111,11 @@ static void code_word_at (Byte *p, int n) · 45 unmodified lines
113 memcpy(p, &w, sizeof(Word));111 memcpy(p, &w, sizeof(Word));
114}112}
115 113
116static void push_field (Word name)
117{
118 if (nfields < MAXFIELDS)
119 fields[nfields++] = name;
120 else
121 luaY_error ("too many fields in nested constructors");
122}
123
124static void flush_record (int n)114static void flush_record (int n)
125{115{
126 int i;
127 if (n == 0) return;116 if (n == 0) return;
128 code_byte(STORERECORD);117 code_byte(STOREMAP);
129 code_byte(n);118 code_byte(n);
130 for (i=0; i<n; i++)
131 code_word(fields[--nfields]);
132}119}
133 120
134static void flush_list (int m, int n)121static void flush_list (int m, int n)
@@ -171,11 +158,22 @@ static void add_varbuffer (Long var) · 36 unmodified lines
171 luaY_error ("variable buffer overflow");158 luaY_error ("variable buffer overflow");
172}159}
173 160
161static void code_string (Word w)
162{
163 code_byte(PUSHSTRING);
164 code_word(w);
165}
166
167static void code_constant (TaggedString *s)
168{
169 code_string(luaI_findconstant(s));
170}
171
174static void code_number (float f)172static void code_number (float f)
175{ 173{
176 Word i = (Word)f;174 Word i;
177 if (f == (float)i) /* f has an (short) integer value */175 if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) {
178 {176 /* f has an (short) integer value */
179 if (i <= 2) code_byte(PUSH0 + i);177 if (i <= 2) code_byte(PUSH0 + i);
180 else if (i <= 255)178 else if (i <= 255)
181 {179 {
@@ -418,7 +416,7 @@ void lua_parse (TFunc *tf) · 236 unmodified lines
418}416}
419 417
420 418
421#line 414 "lua.stx"419#line 412 "lua.stx"
422typedef union 420typedef union
423{421{
424 int vInt;422 int vInt;
@@ -429,7 +427,7 @@ typedef union · 4 unmodified lines
429 TFunc *pFunc;427 TFunc *pFunc;
430 TaggedString *pTStr;428 TaggedString *pTStr;
431} YYSTYPE;429} YYSTYPE;
432#line 433 "y.tab.c"430#line 431 "y.tab.c"
433#define WRONGTOKEN 257431#define WRONGTOKEN 257
434#define NIL 258432#define NIL 258
435#define IF 259433#define IF 259
@@ -467,8 +465,9 @@ short luaY_lhs[] = { -1, · 31 unmodified lines
467 38, 5, 39, 5, 40, 37, 4, 8, 8, 7,465 38, 5, 39, 5, 40, 37, 4, 8, 8, 7,
468 7, 2, 2, 3, 41, 3, 17, 17, 18, 18,466 7, 2, 2, 3, 41, 3, 17, 17, 18, 18,
469 19, 19, 42, 9, 9, 14, 14, 43, 43, 12,467 19, 19, 42, 9, 9, 14, 14, 43, 43, 12,
470 12, 13, 13, 44, 15, 15, 16, 16, 6, 6,468 12, 13, 13, 44, 45, 45, 15, 15, 16, 16,
471 20, 20, 20, 21, 29, 10, 10, 11, 11,469 6, 6, 20, 20, 20, 21, 29, 10, 10, 11,
470 11,
472};471};
473short luaY_len[] = { 2,472short luaY_len[] = { 2,
474 2, 0, 3, 2, 3, 1, 3, 5, 0, 3,473 2, 0, 3, 2, 3, 1, 3, 5, 0, 3,
@@ -479,113 +478,114 @@ short luaY_len[] = { 2, · 4 unmodified lines
479 0, 5, 0, 5, 0, 4, 2, 1, 3, 3,478 0, 5, 0, 5, 0, 4, 2, 1, 3, 3,
480 1, 0, 1, 1, 0, 4, 0, 1, 1, 3,479 1, 0, 1, 1, 0, 4, 0, 1, 1, 3,
481 1, 1, 0, 3, 2, 0, 2, 0, 1, 0,480 1, 1, 0, 3, 2, 0, 2, 0, 1, 0,
482 2, 1, 3, 3, 0, 2, 1, 3, 1, 3,481 2, 1, 3, 3, 3, 1, 0, 2, 1, 3,
483 1, 4, 3, 1, 1, 1, 3, 0, 2,482 1, 3, 1, 4, 3, 1, 1, 1, 3, 0,
483 2,
484};484};
485short luaY_defred[] = { 2,485short luaY_defred[] = { 2,
486 0, 0, 0, 14, 16, 0, 0, 0, 94, 19,486 0, 0, 0, 14, 16, 0, 0, 0, 96, 19,
487 0, 0, 0, 91, 1, 0, 4, 0, 48, 46,487 0, 0, 0, 93, 1, 0, 4, 0, 48, 46,
488 47, 0, 0, 0, 49, 29, 95, 0, 0, 44,488 47, 0, 0, 0, 49, 29, 97, 0, 0, 44,
489 0, 0, 24, 0, 0, 0, 0, 96, 0, 0,489 0, 0, 24, 0, 0, 0, 0, 98, 0, 0,
490 0, 0, 0, 0, 0, 57, 61, 12, 3, 0,490 0, 0, 0, 0, 0, 57, 61, 12, 3, 0,
491 0, 0, 0, 0, 0, 28, 28, 28, 0, 0,491 0, 0, 0, 0, 0, 28, 28, 28, 0, 0,
492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
493 0, 0, 0, 9, 27, 65, 0, 0, 20, 0,493 0, 0, 0, 9, 27, 65, 0, 0, 20, 0,
494 5, 0, 0, 0, 0, 0, 59, 0, 93, 30,494 5, 0, 0, 0, 0, 0, 59, 0, 95, 30,
495 24, 51, 53, 0, 0, 0, 0, 0, 0, 0,495 24, 51, 53, 0, 0, 0, 0, 0, 0, 0,
496 0, 0, 0, 0, 0, 0, 0, 0, 73, 0,496 0, 0, 0, 0, 0, 0, 0, 0, 0, 73,
497 0, 82, 28, 0, 0, 0, 0, 97, 72, 71,497 0, 0, 82, 0, 28, 0, 0, 0, 0, 99,
498 0, 0, 69, 7, 60, 92, 28, 0, 0, 0,498 72, 71, 0, 0, 69, 7, 60, 94, 28, 0,
499 56, 0, 75, 0, 0, 86, 24, 0, 25, 0,499 0, 0, 56, 0, 75, 0, 0, 88, 0, 24,
500 0, 24, 0, 0, 0, 0, 0, 0, 83, 0,500 0, 25, 0, 0, 24, 0, 0, 0, 0, 85,
501 74, 0, 28, 17, 10, 0, 70, 24, 0, 0,501 86, 83, 0, 74, 0, 0, 28, 17, 10, 0,
502 77, 0, 0, 8, 22, 0, 13, 81, 15, 28,502 70, 24, 0, 0, 77, 0, 0, 8, 22, 0,
503 24, 28, 0, 23,503 13, 81, 15, 28, 24, 28, 0, 23,
504};504};
505short luaY_dgoto[] = { 1,505short luaY_dgoto[] = { 1,
506 91, 34, 35, 25, 26, 11, 46, 12, 107, 39,506 91, 34, 35, 25, 26, 11, 46, 12, 108, 39,
507 79, 161, 108, 151, 109, 110, 121, 122, 123, 27,507 79, 165, 109, 154, 110, 111, 123, 124, 125, 27,
508 14, 41, 81, 2, 15, 16, 49, 17, 28, 73,508 14, 41, 81, 2, 15, 16, 49, 17, 28, 73,
509 115, 37, 160, 32, 33, 74, 30, 128, 129, 31,509 117, 37, 164, 32, 33, 74, 30, 130, 131, 31,
510 116, 134, 133, 112,510 118, 136, 135, 113, 114,
511};511};
512short luaY_sindex[] = { 0,512short luaY_sindex[] = { 0,
513 0, 332, -38, 0, 0, -38, -239, -223, 0, 0,513 0, 288, -34, 0, 0, -34, -238, -223, 0, 0,
514 -23, 17, 0, 0, 0, 3, 0, 137, 0, 0,514 -6, 22, 0, 0, 0, 14, 0, 150, 0, 0,
515 0, -38, -38, -38, 0, 0, 0, 137, 547, 0,515 0, -34, -34, -34, 0, 0, 0, 150, 547, 0,
516 -25, -38, 0, 3, 45, 0, 212, 0, -22, 0,516 -46, -34, 0, 14, 34, 0, 212, 0, -5, 0,
517 52, 140, -38, -223, -38, 0, 0, 0, 0, -166,517 39, 156, -34, -223, -34, 0, 0, 0, 0, -194,
518 -38, -154, 40, 40, 102, 0, 0, 0, -38, -38,518 -34, -193, -4, -4, 48, 0, 0, 0, -34, -34,
519 -38, -38, -38, -38, -38, -38, -38, -38, -38, -38,519 -34, -34, -34, -34, -34, -34, -34, -34, -34, -34,
520 -34, 481, -130, 0, 0, 0, -38, -129, 0, -194,520 -38, 481, -173, 0, 0, 0, -34, -146, 0, -234,
521 0, -123, 45, 0, -17, 103, 0, 753, 0, 0,521 0, -138, 34, 0, -15, 96, 0, 753, 0, 0,
522 0, 0, 0, -30, -30, -30, -30, -30, -30, 95,522 0, 0, 0, -30, -30, -30, -30, -30, -30, -20,
523 11, 11, 40, 40, 40, 97, 41, 123, 0, 124,523 -12, -12, -4, -4, -4, 0, -34, 16, 95, 0,
524 212, 0, 0, -38, -59, -38, 45, 0, 0, 0,524 98, 212, 0, 82, 0, -34, 328, -34, 34, 0,
525 128, 131, 0, 0, 0, 0, 0, -38, -38, -38,525 0, 0, 103, 101, 0, 0, 0, 0, 0, -34,
526 0, -97, 0, 120, -38, 0, 0, 212, 0, 3,526 -34, 1181, 0, -91, 0, 92, -34, 0, -34, 0,
527 0, 0, -194, -183, 118, 118, 212, 97, 0, -97,527 212, 0, 14, 0, 0, -234, -233, 118, 118, 0,
528 0, 212, 0, 0, 0, -85, 0, 0, -38, -82,528 0, 0, -91, 0, 212, 212, 0, 0, 0, -105,
529 0, 123, -80, 0, 0, 1105, 0, 0, 0, 0,529 0, 0, -34, -103, 0, 95, -101, 0, 0, 1105,
530 0, 0, -183, 0,530 0, 0, 0, 0, 0, 0, -233, 0,
531};531};
532short luaY_rindex[] = { 0,532short luaY_rindex[] = { 0,
533 0, 191, 69, 0, 0, 173, 0, 0, 0, 0,533 0, 169, 52, 0, 0, 173, 0, 0, 0, 0,
534 0, 69, 150, 0, 0, 146, 0, -36, 0, 0,534 0, 52, 508, 0, 0, 146, 0, -36, 0, 0,
535 0, 69, 69, 69, 0, 0, 0, 1, 0, 0,535 0, 52, 52, 52, 0, 0, 0, 1, 0, 0,
536 0, 69, 0, 47, 461, 436, 0, 0, 197, 130,536 0, 52, 0, 47, 461, 436, 0, 0, 197, 76,
537 0, 0, 69, 0, -33, 0, 0, 0, 0, 0,537 0, 0, 52, 0, -32, 0, 0, 0, 0, 0,
538 69, 0, 24, 59, 1181, 0, 0, 0, 69, 69,538 52, 0, 24, 59, 1195, 0, 0, 0, 52, 52,
539 69, 69, 69, 69, 69, 69, 69, 69, 69, 69,539 52, 52, 52, 52, 52, 52, 52, 52, 52, 52,
540 -50, 0, 0, 0, 0, 0, 69, 0, 0, 152,540 -27, 0, 0, 0, 0, 0, 52, 0, 0, 129,
541 0, 0, 311, -21, 0, 0, 0, 0, 0, 0,541 0, 0, 311, 142, 0, 0, 0, 0, 0, 0,
542 0, 0, 0, 746, 776, 799, 821, 844, 866, 505,542 0, 0, 0, 746, 776, 799, 821, 844, 866, 505,
543 383, 410, 88, 112, 359, 1174, 0, 74, 0, -40,543 383, 410, 88, 112, 359, 1174, 52, 0, 51, 0,
544 -26, 0, 0, 69, -230, 69, 921, 0, 0, 0,544 -40, -10, 0, 0, 0, 52, -153, 52, 921, 0,
545 0, 160, 0, 0, 0, 0, 0, 69, 69, 69,545 0, 0, 0, 144, 0, 0, 0, 0, 0, 52,
546 0, 77, 0, 78, -9, 0, 0, 939, 0, 289,546 52, 0, 0, 62, 0, 64, -26, 0, 52, 0,
547 474, 0, 0, -60, 894, 1137, -16, 0, 0, 89,547 939, 0, 682, 474, 0, 0, -77, 894, 1137, 0,
548 0, -10, 0, 0, 0, 0, 0, 0, 69, 0,548 0, 0, 66, 0, 13, -18, 0, 0, 0, 0,
549 0, 74, 0, 0, 0, 0, 0, 0, 0, 0,549 0, 0, 52, 0, 0, 51, 0, 0, 0, 0,
550 0, 0, -60, 0,550 0, 0, 0, 0, 0, 0, -77, 0,
551};551};
552short luaY_gindex[] = { 0,552short luaY_gindex[] = { 0,
553 -31, 171, 85, -2, 48, 0, 0, 0, 0, 0,553 53, 147, -23, 3, 134, 0, 0, 0, 0, 0,
554 0, 0, 67, 0, 0, 0, 0, 0, 75, 8,554 0, 0, 40, 0, 0, 0, 0, 0, 49, 6,
555 0, 0, 0, 0, 104, 107, -29, 0, 12, -61,555 0, 0, 0, 0, 81, 83, -16, 0, 8, -70,
556 0, 1260, 50, 0, 0, 0, 213, 0, 0, 0,556 0, 1445, 27, 0, 0, 0, 187, 0, 0, 0,
557 0, 0, -107, 94,557 0, 0, -108, 67, 0,
558};558};
559#define YYTABLESIZE 1462559#define YYTABLESIZE 1608
560short luaY_table[] = { 10,560short luaY_table[] = { 107,
561 45, 24, 136, 58, 75, 24, 22, 62, 85, 13,561 45, 24, 138, 58, 10, 24, 22, 13, 62, 18,
562 22, 68, 66, 18, 67, 40, 69, 87, 78, 42,562 22, 68, 66, 40, 67, 42, 69, 75, 78, 83,
563 44, 78, 90, 43, 95, 92, 93, 84, 52, 127,563 129, 68, 66, 43, 67, 84, 69, 162, 163, 68,
564 26, 26, 87, 88, 38, 26, 26, 43, 77, 90,564 52, 87, 79, 89, 69, 38, 121, 44, 78, 122,
565 58, 45, 45, 45, 45, 45, 11, 45, 88, 79,565 58, 45, 45, 45, 45, 45, 11, 45, 89, 84,
566 9, 84, 68, 36, 168, 85, 45, 69, 50, 45,566 9, 85, 107, 119, 43, 77, 90, 172, 50, 45,
567 45, 48, 45, 70, 43, 43, 43, 43, 43, 95,567 45, 45, 45, 70, 43, 43, 43, 43, 43, 157,
568 43, 55, 55, 51, 85, 153, 119, 158, 159, 120,568 43, 90, 48, 70, 160, 51, 71, 76, 80, 87,
569 156, 137, 43, 43, 78, 43, 58, 39, 76, 55,569 89, 70, 43, 43, 78, 43, 58, 39, 90, 70,
570 36, 80, 36, 45, 45, 144, 165, 71, 87, 50,570 55, 169, 116, 45, 45, 55, 55, 87, 79, 50,
571 50, 50, 50, 50, 70, 50, 154, 87, 84, 172,571 50, 50, 50, 50, 176, 50, 84, 26, 26, 92,
572 155, 40, 10, 55, 88, 79, 43, 50, 50, 89,572 93, 40, 26, 26, 89, 6, 43, 50, 50, 10,
573 50, 163, 13, 58, 36, 45, 18, 83, 39, 39,573 50, 97, 13, 58, 18, 45, 159, 120, 39, 39,
574 39, 39, 39, 70, 39, 114, 68, 66, 171, 67,574 39, 39, 39, 97, 39, 126, 127, 90, 134, 36,
575 173, 69, 90, 125, 118, 11, 39, 39, 43, 39,575 133, 137, 139, 145, 146, 11, 39, 39, 43, 39,
576 124, 50, 40, 40, 40, 40, 40, 130, 40, 68,576 153, 50, 40, 40, 40, 40, 40, 55, 40, 68,
577 66, 117, 67, 141, 69, 131, 132, 135, 142, 6,577 66, 168, 67, 171, 69, 173, 97, 140, 26, 67,
578 40, 40, 62, 40, 143, 95, 148, 62, 150, 61,578 40, 40, 62, 40, 55, 78, 36, 62, 36, 61,
579 39, 164, 52, 50, 167, 52, 169, 95, 70, 95,579 39, 147, 151, 50, 68, 92, 79, 97, 76, 21,
580 26, 55, 67, 89, 50, 95, 98, 82, 78, 3,580 80, 86, 166, 158, 161, 52, 100, 142, 47, 143,
581 68, 79, 76, 4, 40, 5, 21, 95, 6, 7,581 152, 52, 92, 178, 40, 0, 0, 50, 0, 167,
582 89, 70, 39, 80, 9, 86, 162, 157, 139, 19,582 36, 70, 39, 82, 0, 0, 0, 0, 0, 19,
583 95, 140, 174, 19, 47, 149, 0, 51, 0, 0,583 0, 0, 0, 19, 0, 0, 175, 0, 177, 0,
584 51, 62, 0, 20, 21, 9, 40, 20, 21, 106,584 0, 62, 97, 20, 21, 106, 40, 20, 21, 9,
585 95, 0, 0, 0, 23, 0, 0, 0, 23, 0,585 51, 0, 0, 0, 23, 0, 51, 0, 23, 0,
586 65, 0, 0, 68, 66, 98, 67, 0, 69, 45,586 65, 144, 0, 68, 66, 100, 67, 0, 69, 45,
587 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,587 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
588 45, 62, 95, 61, 45, 45, 45, 45, 45, 45,588 45, 62, 0, 61, 45, 45, 45, 45, 45, 45,
589 45, 45, 43, 43, 43, 43, 43, 43, 43, 43,589 45, 45, 43, 43, 43, 43, 43, 43, 43, 43,
590 43, 43, 43, 43, 0, 55, 0, 43, 43, 43,590 43, 43, 43, 43, 0, 55, 0, 43, 43, 43,
591 43, 43, 43, 43, 43, 70, 0, 11, 11, 0,591 43, 43, 43, 43, 43, 70, 0, 11, 11, 0,
@@ -603,25 +603,25 @@ short luaY_table[] = { 10, · 11 unmodified lines
603 41, 0, 0, 37, 0, 37, 37, 37, 0, 0,603 41, 0, 0, 37, 0, 37, 37, 37, 0, 0,
604 0, 0, 0, 62, 62, 64, 0, 0, 62, 62,604 0, 0, 0, 62, 62, 64, 0, 0, 62, 62,
605 0, 37, 37, 0, 37, 0, 0, 0, 0, 0,605 0, 37, 37, 0, 37, 0, 0, 0, 0, 0,
606 38, 41, 38, 38, 38, 98, 0, 98, 98, 98,606 38, 41, 38, 38, 38, 100, 0, 100, 100, 100,
607 63, 98, 98, 98, 98, 98, 98, 0, 38, 38,607 63, 100, 100, 100, 100, 100, 100, 0, 38, 38,
608 98, 38, 0, 66, 0, 37, 64, 29, 29, 64,608 100, 38, 0, 66, 0, 37, 64, 29, 29, 64,
609 29, 0, 29, 41, 0, 0, 57, 58, 59, 60,609 29, 0, 29, 41, 0, 0, 57, 58, 59, 60,
610 63, 64, 65, 0, 64, 29, 0, 29, 0, 0,610 63, 64, 65, 0, 64, 29, 0, 29, 0, 0,
611 0, 63, 38, 0, 42, 0, 0, 37, 0, 0,611 0, 63, 38, 0, 42, 0, 0, 37, 0, 0,
612 0, 0, 0, 0, 66, 29, 29, 66, 29, 63,612 0, 0, 0, 0, 66, 29, 29, 66, 29, 63,
613 29, 0, 68, 66, 0, 67, 0, 69, 0, 29,613 29, 0, 68, 66, 0, 67, 0, 69, 0, 29,
614 0, 0, 66, 29, 38, 29, 0, 0, 0, 0,614 0, 0, 66, 29, 38, 29, 0, 0, 0, 0,
615 62, 0, 61, 0, 0, 42, 0, 11, 42, 11,615 62, 0, 61, 0, 0, 42, 3, 97, 42, 0,
616 11, 11, 0, 11, 11, 11, 11, 11, 0, 0,616 4, 91, 5, 97, 0, 6, 7, 8, 0, 0,
617 0, 0, 11, 42, 42, 0, 42, 29, 0, 18,617 0, 9, 0, 42, 42, 97, 42, 29, 91, 18,
618 0, 18, 18, 18, 70, 18, 18, 18, 18, 18,618 0, 18, 18, 18, 70, 18, 18, 18, 18, 18,
619 18, 0, 0, 0, 18, 0, 0, 0, 68, 66,619 18, 0, 0, 0, 18, 0, 3, 0, 68, 66,
620 3, 67, 0, 69, 4, 0, 5, 42, 0, 6,620 4, 67, 5, 69, 0, 6, 7, 42, 97, 0,
621 7, 8, 0, 0, 0, 9, 62, 0, 61, 0,621 0, 9, 0, 0, 0, 0, 62, 0, 61, 0,
622 0, 0, 0, 0, 0, 0, 0, 41, 41, 41,622 0, 0, 0, 0, 0, 0, 0, 41, 41, 41,
623 41, 41, 41, 41, 41, 41, 41, 41, 41, 42,623 41, 41, 41, 41, 41, 41, 41, 41, 41, 42,
624 0, 0, 41, 41, 41, 41, 41, 41, 41, 41,624 97, 0, 41, 41, 41, 41, 41, 41, 41, 41,
625 70, 37, 37, 37, 37, 37, 37, 37, 37, 37,625 70, 37, 37, 37, 37, 37, 37, 37, 37, 37,
626 37, 37, 37, 0, 0, 0, 37, 37, 37, 37,626 37, 37, 37, 0, 0, 0, 37, 37, 37, 37,
627 37, 37, 37, 37, 0, 0, 0, 0, 38, 38,627 37, 37, 37, 37, 0, 0, 0, 0, 38, 38,
@@ -632,7 +632,7 @@ short luaY_table[] = { 10, · 4 unmodified lines
632 29, 29, 29, 29, 29, 29, 29, 0, 0, 0,632 29, 29, 29, 29, 29, 29, 29, 0, 0, 0,
633 0, 63, 63, 0, 0, 0, 63, 63, 0, 0,633 0, 63, 63, 0, 0, 0, 63, 63, 0, 0,
634 0, 0, 66, 0, 66, 66, 66, 0, 66, 66,634 0, 0, 66, 0, 66, 66, 66, 0, 66, 66,
635 66, 66, 66, 66, 113, 31, 0, 66, 29, 29,635 66, 66, 66, 66, 115, 31, 0, 66, 29, 29,
636 29, 29, 29, 29, 29, 57, 58, 59, 60, 63,636 29, 29, 29, 29, 29, 57, 58, 59, 60, 63,
637 64, 65, 0, 42, 42, 42, 42, 42, 42, 42,637 64, 65, 0, 42, 42, 42, 42, 42, 42, 42,
638 42, 42, 42, 42, 42, 34, 0, 0, 42, 42,638 42, 42, 42, 42, 42, 34, 0, 0, 42, 42,
@@ -642,7 +642,7 @@ short luaY_table[] = { 10, · 3 unmodified lines
642 0, 0, 62, 0, 61, 0, 34, 0, 0, 34,642 0, 0, 62, 0, 61, 0, 34, 0, 0, 34,
643 32, 57, 58, 59, 60, 63, 64, 65, 0, 0,643 32, 57, 58, 59, 60, 63, 64, 65, 0, 0,
644 0, 0, 0, 0, 34, 34, 0, 34, 31, 33,644 0, 0, 0, 0, 34, 34, 0, 34, 31, 33,
645 0, 0, 33, 35, 0, 126, 70, 0, 0, 0,645 0, 0, 33, 35, 0, 128, 70, 0, 0, 0,
646 0, 0, 0, 0, 0, 0, 0, 33, 33, 0,646 0, 0, 0, 0, 0, 0, 0, 33, 33, 0,
647 33, 32, 0, 0, 32, 36, 0, 0, 34, 0,647 33, 32, 0, 0, 32, 36, 0, 0, 34, 0,
648 31, 0, 0, 0, 0, 0, 0, 0, 0, 32,648 31, 0, 0, 0, 0, 0, 0, 0, 0, 32,
@@ -650,12 +650,12 @@ short luaY_table[] = { 10, · 1 unmodified line
650 0, 33, 0, 52, 0, 0, 0, 0, 0, 0,650 0, 33, 0, 52, 0, 0, 0, 0, 0, 0,
651 34, 0, 35, 35, 0, 35, 36, 0, 0, 36,651 34, 0, 35, 35, 0, 35, 36, 0, 0, 36,
652 0, 0, 0, 32, 0, 0, 0, 0, 0, 0,652 0, 0, 0, 32, 0, 0, 0, 0, 0, 0,
653 99, 0, 0, 33, 36, 36, 0, 36, 0, 0,653 101, 0, 0, 33, 36, 36, 0, 36, 0, 0,
654 0, 0, 0, 0, 52, 0, 35, 52, 28, 0,654 0, 0, 0, 0, 52, 0, 35, 52, 28, 0,
655 0, 0, 0, 0, 0, 32, 0, 0, 0, 0,655 11, 0, 11, 11, 11, 32, 11, 11, 11, 11,
656 0, 0, 52, 0, 0, 0, 0, 0, 36, 0,656 11, 0, 52, 0, 0, 11, 0, 0, 36, 0,
657 0, 0, 0, 0, 0, 0, 0, 0, 35, 0,657 0, 0, 0, 0, 0, 0, 0, 0, 35, 0,
658 0, 0, 0, 0, 0, 0, 0, 0, 0, 99,658 0, 0, 0, 0, 0, 0, 0, 0, 0, 101,
659 0, 0, 0, 0, 0, 0, 52, 0, 0, 0,659 0, 0, 0, 0, 0, 0, 52, 0, 0, 0,
660 36, 0, 0, 0, 0, 0, 0, 28, 0, 0,660 36, 0, 0, 0, 0, 0, 0, 28, 0, 0,
661 0, 0, 0, 0, 31, 31, 31, 31, 31, 31,661 0, 0, 0, 0, 31, 31, 31, 31, 31, 31,
@@ -675,66 +675,80 @@ short luaY_table[] = { 10, · 13 unmodified lines
675 36, 36, 36, 36, 36, 36, 68, 66, 0, 67,675 36, 36, 36, 36, 36, 36, 68, 66, 0, 67,
676 0, 69, 52, 52, 52, 52, 52, 52, 52, 52,676 0, 69, 52, 52, 52, 52, 52, 52, 52, 52,
677 52, 52, 52, 52, 62, 0, 61, 52, 52, 52,677 52, 52, 52, 52, 62, 0, 61, 52, 52, 52,
678 0, 0, 0, 0, 0, 0, 0, 54, 0, 99,678 0, 0, 0, 0, 0, 0, 0, 54, 0, 101,
679 54, 99, 99, 99, 0, 99, 99, 99, 99, 99,679 54, 101, 101, 101, 0, 101, 101, 101, 101, 101,
680 99, 0, 0, 0, 99, 54, 0, 28, 70, 28,680 101, 0, 0, 0, 101, 54, 0, 28, 70, 28,
681 28, 28, 0, 28, 28, 28, 28, 28, 28, 0,681 28, 28, 0, 28, 28, 28, 28, 28, 28, 0,
682 0, 0, 28, 94, 0, 94, 94, 94, 94, 94,682 0, 0, 28, 96, 0, 96, 96, 96, 96, 96,
683 94, 0, 29, 29, 0, 29, 0, 29, 0, 54,683 96, 0, 68, 66, 0, 67, 0, 69, 0, 54,
684 0, 94, 94, 94, 0, 94, 0, 0, 0, 0,684 0, 96, 96, 96, 86, 96, 29, 29, 0, 29,
685 29, 0, 29, 0, 0, 0, 0, 0, 0, 0,685 62, 29, 61, 0, 0, 0, 0, 0, 0, 0,
686 0, 0, 0, 0, 29, 0, 29, 0, 0, 0,
687 0, 54, 0, 0, 96, 0, 0, 96, 0, 0,
688 0, 0, 0, 150, 70, 0, 0, 0, 0, 0,
689 0, 0, 0, 0, 0, 0, 0, 0, 29, 0,
690 0, 0, 0, 0, 0, 0, 96, 0, 96, 0,
691 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
686 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,692 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
687 0, 54, 29, 0, 94, 0, 0, 94, 0, 0,
688 0, 0, 0, 0, 29, 0, 0, 0, 0, 0,
689 0, 53, 54, 0, 0, 0, 0, 0, 0, 0,
690 0, 72, 0, 0, 0, 0, 94, 0, 94, 0,
691 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,693 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
692 88, 0, 0, 0, 0, 0, 0, 0, 94, 95,
693 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
694 111, 0, 0, 0, 0, 0, 0, 0, 0, 0,
695 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,694 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
696 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,695 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
697 0, 0, 0, 0, 170, 0, 0, 0, 0, 0,696 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
698 0, 0, 0, 138, 0, 0, 0, 0, 0, 57,697 0, 0, 0, 0, 174, 0, 0, 0, 0, 0,
699 58, 59, 60, 63, 64, 65, 0, 145, 146, 147,698 0, 0, 0, 0, 0, 0, 0, 0, 0, 57,
700 0, 0, 0, 0, 152, 54, 54, 54, 54, 54,699 58, 59, 60, 63, 64, 65, 0, 0, 0, 0,
700 0, 0, 0, 0, 0, 54, 54, 54, 54, 54,
701 54, 54, 54, 54, 54, 54, 54, 0, 0, 0,701 54, 54, 54, 54, 54, 54, 54, 0, 0, 0,
702 54, 54, 54, 0, 0, 0, 0, 0, 166, 0,702 54, 54, 54, 0, 0, 0, 0, 0, 0, 0,
703 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
704 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
705 0, 0, 0, 0, 0, 0, 0, 29, 96, 96,
706 96, 96, 96, 96, 96, 57, 58, 59, 60, 63,
707 64, 65, 0, 0, 0, 0, 53, 54, 0, 29,
708 29, 29, 29, 29, 29, 29, 72, 0, 0, 0,
703 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,709 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
710 0, 0, 0, 0, 0, 88, 0, 0, 0, 0,
711 0, 0, 0, 94, 95, 96, 97, 98, 99, 100,
712 101, 102, 103, 104, 105, 112, 0, 0, 0, 0,
704 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,713 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
705 0, 0, 0, 0, 0, 0, 0, 0, 94, 94,714 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
706 94, 94, 94, 94, 94, 29, 29, 29, 29, 29,715 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
707 29, 29,716 0, 132, 0, 0, 0, 0, 0, 0, 0, 0,
717 141, 0, 0, 0, 0, 0, 0, 0, 0, 0,
718 0, 0, 0, 0, 148, 149, 0, 0, 0, 0,
719 0, 155, 0, 156, 0, 0, 0, 0, 0, 0,
720 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
721 0, 0, 0, 0, 0, 0, 0, 170,
708};722};
709short luaY_check[] = { 2,723short luaY_check[] = { 91,
710 0, 40, 110, 40, 34, 40, 45, 41, 59, 2,724 0, 40, 111, 40, 2, 40, 45, 2, 41, 2,
711 45, 42, 43, 2, 45, 8, 47, 44, 59, 8,725 45, 42, 43, 8, 45, 8, 47, 34, 59, 43,
712 44, 44, 44, 0, 46, 57, 58, 44, 46, 91,726 91, 42, 43, 0, 45, 44, 47, 261, 262, 42,
713 261, 262, 59, 44, 274, 266, 267, 61, 61, 61,727 46, 59, 59, 44, 47, 274, 271, 44, 44, 274,
714 40, 41, 42, 43, 44, 45, 0, 47, 59, 59,728 40, 41, 42, 43, 44, 45, 0, 47, 59, 44,
715 274, 44, 42, 6, 162, 44, 40, 47, 0, 59,729 274, 44, 91, 77, 61, 61, 44, 166, 0, 59,
716 60, 59, 62, 94, 41, 42, 43, 44, 45, 91,730 60, 40, 62, 94, 41, 42, 43, 44, 45, 140,
717 47, 24, 123, 91, 125, 137, 271, 261, 262, 274,731 47, 59, 59, 94, 145, 91, 123, 44, 40, 274,
718 142, 113, 59, 60, 125, 62, 123, 0, 44, 123,732 274, 94, 59, 60, 125, 62, 123, 0, 41, 94,
719 43, 40, 45, 93, 94, 127, 158, 123, 125, 41,733 123, 162, 266, 93, 94, 123, 123, 125, 125, 41,
720 42, 43, 44, 45, 94, 47, 138, 274, 125, 171,734 42, 43, 44, 45, 175, 47, 125, 261, 262, 57,
721 140, 0, 115, 123, 125, 125, 93, 59, 60, 274,735 58, 0, 266, 267, 125, 40, 93, 59, 60, 117,
722 62, 153, 115, 123, 77, 125, 115, 43, 41, 42,736 62, 46, 117, 123, 117, 125, 143, 274, 41, 42,
723 43, 44, 45, 94, 47, 266, 42, 43, 170, 45,737 43, 44, 45, 58, 47, 274, 41, 125, 44, 6,
724 172, 47, 41, 41, 274, 0, 59, 60, 125, 62,738 125, 44, 61, 41, 44, 0, 59, 60, 125, 62,
725 274, 93, 41, 42, 43, 44, 45, 61, 47, 42,739 59, 93, 41, 42, 43, 44, 45, 24, 47, 42,
726 43, 77, 45, 116, 47, 125, 44, 44, 41, 40,740 43, 267, 45, 267, 47, 267, 91, 115, 0, 41,
727 59, 60, 0, 62, 44, 46, 274, 60, 59, 62,741 59, 60, 0, 62, 123, 125, 43, 60, 45, 62,
728 93, 267, 46, 125, 267, 46, 267, 58, 94, 40,742 93, 129, 274, 125, 41, 44, 125, 46, 125, 267,
729 0, 123, 41, 44, 58, 46, 0, 58, 125, 259,743 125, 45, 153, 141, 146, 46, 0, 117, 12, 117,
730 41, 125, 125, 263, 93, 265, 267, 58, 268, 269,744 134, 46, 61, 177, 93, -1, -1, 58, -1, 157,
731 61, 94, 125, 125, 274, 45, 150, 143, 115, 258,745 77, 94, 125, 58, -1, -1, -1, -1, -1, 258,
732 91, 115, 173, 258, 12, 132, -1, 91, -1, -1,746 -1, -1, -1, 258, -1, -1, 174, -1, 176, -1,
733 91, 59, -1, 272, 273, 274, 125, 272, 273, 274,747 -1, 59, 91, 272, 273, 274, 125, 272, 273, 274,
734 91, -1, -1, -1, 283, -1, -1, -1, 283, -1,748 91, -1, -1, -1, 283, -1, 91, -1, 283, -1,
735 281, -1, -1, 42, 43, 59, 45, -1, 47, 259,749 281, 118, -1, 42, 43, 59, 45, -1, 47, 259,
736 260, 261, 262, 263, 264, 265, 266, 267, 268, 269,750 260, 261, 262, 263, 264, 265, 266, 267, 268, 269,
737 270, 60, 123, 62, 274, 275, 276, 277, 278, 279,751 270, 60, -1, 62, 274, 275, 276, 277, 278, 279,
738 280, 281, 259, 260, 261, 262, 263, 264, 265, 266,752 280, 281, 259, 260, 261, 262, 263, 264, 265, 266,
739 267, 268, 269, 270, -1, 123, -1, 274, 275, 276,753 267, 268, 269, 270, -1, 123, -1, 274, 275, 276,
740 277, 278, 279, 280, 281, 94, -1, 261, 262, -1,754 277, 278, 279, 280, 281, 94, -1, 261, 262, -1,
@@ -761,16 +775,16 @@ short luaY_check[] = { 2, · 20 unmodified lines
761 -1, -1, -1, -1, 41, 42, 43, 44, 45, 59,775 -1, -1, -1, -1, 41, 42, 43, 44, 45, 59,
762 47, -1, 42, 43, -1, 45, -1, 47, -1, 94,776 47, -1, 42, 43, -1, 45, -1, 47, -1, 94,
763 -1, -1, 59, 60, 125, 62, -1, -1, -1, -1,777 -1, -1, 59, 60, 125, 62, -1, -1, -1, -1,
764 60, -1, 62, -1, -1, 41, -1, 259, 44, 261,778 60, -1, 62, -1, -1, 41, 259, 40, 44, -1,
765 262, 263, -1, 265, 266, 267, 268, 269, -1, -1,779 263, 44, 265, 46, -1, 268, 269, 270, -1, -1,
766 -1, -1, 274, 59, 60, -1, 62, 94, -1, 259,780 -1, 274, -1, 59, 60, 58, 62, 94, 61, 259,
767 -1, 261, 262, 263, 94, 265, 266, 267, 268, 269,781 -1, 261, 262, 263, 94, 265, 266, 267, 268, 269,
768 270, -1, -1, -1, 274, -1, -1, -1, 42, 43,782 270, -1, -1, -1, 274, -1, 259, -1, 42, 43,
769 259, 45, -1, 47, 263, -1, 265, 93, -1, 268,783 263, 45, 265, 47, -1, 268, 269, 93, 91, -1,
770 269, 270, -1, -1, -1, 274, 60, -1, 62, -1,784 -1, 274, -1, -1, -1, -1, 60, -1, 62, -1,
771 -1, -1, -1, -1, -1, -1, -1, 259, 260, 261,785 -1, -1, -1, -1, -1, -1, -1, 259, 260, 261,
772 262, 263, 264, 265, 266, 267, 268, 269, 270, 125,786 262, 263, 264, 265, 266, 267, 268, 269, 270, 125,
773 -1, -1, 274, 275, 276, 277, 278, 279, 280, 281,787 123, -1, 274, 275, 276, 277, 278, 279, 280, 281,
774 94, 259, 260, 261, 262, 263, 264, 265, 266, 267,788 94, 259, 260, 261, 262, 263, 264, 265, 266, 267,
775 268, 269, 270, -1, -1, -1, 274, 275, 276, 277,789 268, 269, 270, -1, -1, -1, 274, 275, 276, 277,
776 278, 279, 280, 281, -1, -1, -1, -1, 259, 260,790 278, 279, 280, 281, -1, -1, -1, -1, 259, 260,
@@ -801,8 +815,8 @@ short luaY_check[] = { 2, · 24 unmodified lines
801 -1, -1, -1, 93, -1, -1, -1, -1, -1, -1,815 -1, -1, -1, 93, -1, -1, -1, -1, -1, -1,
802 0, -1, -1, 125, 59, 60, -1, 62, -1, -1,816 0, -1, -1, 125, 59, 60, -1, 62, -1, -1,
803 -1, -1, -1, -1, 41, -1, 93, 44, 0, -1,817 -1, -1, -1, -1, 41, -1, 93, 44, 0, -1,
804 -1, -1, -1, -1, -1, 125, -1, -1, -1, -1,818 259, -1, 261, 262, 263, 125, 265, 266, 267, 268,
805 -1, -1, 59, -1, -1, -1, -1, -1, 93, -1,819 269, -1, 59, -1, -1, 274, -1, -1, 93, -1,
806 -1, -1, -1, -1, -1, -1, -1, -1, 125, -1,820 -1, -1, -1, -1, -1, -1, -1, -1, 125, -1,
807 -1, -1, -1, -1, -1, -1, -1, -1, -1, 59,821 -1, -1, -1, -1, -1, -1, -1, -1, -1, 59,
808 -1, -1, -1, -1, -1, -1, 93, -1, -1, -1,822 -1, -1, -1, -1, -1, -1, 93, -1, -1, -1,
@@ -830,30 +844,44 @@ short luaY_check[] = { 2, · 21 unmodified lines
830 262, 263, -1, 265, 266, 267, 268, 269, 270, -1,844 262, 263, -1, 265, 266, 267, 268, 269, 270, -1,
831 -1, -1, 274, 40, -1, 42, 43, 44, 45, 46,845 -1, -1, 274, 40, -1, 42, 43, 44, 45, 46,
832 47, -1, 42, 43, -1, 45, -1, 47, -1, 93,846 47, -1, 42, 43, -1, 45, -1, 47, -1, 93,
833 -1, 58, 59, 60, -1, 62, -1, -1, -1, -1,847 -1, 58, 59, 60, 61, 62, 42, 43, -1, 45,
834 60, -1, 62, -1, -1, -1, -1, -1, -1, -1,848 60, 47, 62, -1, -1, -1, -1, -1, -1, -1,
849 -1, -1, -1, -1, 60, -1, 62, -1, -1, -1,
850 -1, 125, -1, -1, 91, -1, -1, 94, -1, -1,
851 -1, -1, -1, 93, 94, -1, -1, -1, -1, -1,
852 -1, -1, -1, -1, -1, -1, -1, -1, 94, -1,
853 -1, -1, -1, -1, -1, -1, 123, -1, 125, -1,
854 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
855 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
835 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,856 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
836 -1, 125, 3, -1, 91, -1, -1, 94, -1, -1,
837 -1, -1, -1, -1, 94, -1, -1, -1, -1, -1,
838 -1, 22, 23, -1, -1, -1, -1, -1, -1, -1,
839 -1, 32, -1, -1, -1, -1, 123, -1, 125, -1,
840 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,857 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
841 51, -1, -1, -1, -1, -1, -1, -1, 59, 60,
842 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
843 71, -1, -1, -1, -1, -1, -1, -1, -1, -1,
844 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,858 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
845 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,859 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
846 -1, -1, -1, -1, 260, -1, -1, -1, -1, -1,860 -1, -1, -1, -1, 260, -1, -1, -1, -1, -1,
847 -1, -1, -1, 114, -1, -1, -1, -1, -1, 275,861 -1, -1, -1, -1, -1, -1, -1, -1, -1, 275,
848 276, 277, 278, 279, 280, 281, -1, 128, 129, 130,862 276, 277, 278, 279, 280, 281, -1, -1, -1, -1,
849 -1, -1, -1, -1, 135, 259, 260, 261, 262, 263,863 -1, -1, -1, -1, -1, 259, 260, 261, 262, 263,
850 264, 265, 266, 267, 268, 269, 270, -1, -1, -1,864 264, 265, 266, 267, 268, 269, 270, -1, -1, -1,
851 274, 275, 276, -1, -1, -1, -1, -1, 159, -1,865 274, 275, 276, -1, -1, -1, -1, -1, -1, -1,
852 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,866 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
853 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,867 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
854 -1, -1, -1, -1, -1, -1, -1, -1, 275, 276,868 -1, -1, -1, -1, -1, -1, -1, 3, 275, 276,
855 277, 278, 279, 280, 281, 275, 276, 277, 278, 279,869 277, 278, 279, 280, 281, 275, 276, 277, 278, 279,
856 280, 281,870 280, 281, -1, -1, -1, -1, 22, 23, -1, 275,
871 276, 277, 278, 279, 280, 281, 32, -1, -1, -1,
872 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
873 -1, -1, -1, -1, -1, 51, -1, -1, -1, -1,
874 -1, -1, -1, 59, 60, 61, 62, 63, 64, 65,
875 66, 67, 68, 69, 70, 71, -1, -1, -1, -1,
876 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
877 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
878 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
879 -1, 107, -1, -1, -1, -1, -1, -1, -1, -1,
880 116, -1, -1, -1, -1, -1, -1, -1, -1, -1,
881 -1, -1, -1, -1, 130, 131, -1, -1, -1, -1,
882 -1, 137, -1, 139, -1, -1, -1, -1, -1, -1,
883 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
884 -1, -1, -1, -1, -1, -1, -1, 163,
857};885};
858#define YYFINAL 1886#define YYFINAL 1
859#ifndef YYDEBUG887#ifndef YYDEBUG
@@ -958,7 +986,9 @@ char *luaY_rule[] = { · 98 unmodified lines
958"ffieldlist : ffieldlist1 lastcomma",986"ffieldlist : ffieldlist1 lastcomma",
959"ffieldlist1 : ffield",987"ffieldlist1 : ffield",
960"ffieldlist1 : ffieldlist1 ',' ffield",988"ffieldlist1 : ffieldlist1 ',' ffield",
961"ffield : NAME '=' expr1",989"ffield : ffieldkey '=' expr1",
990"ffieldkey : '[' expr1 ']'",
991"ffieldkey : NAME",
962"lfieldlist :",992"lfieldlist :",
963"lfieldlist : lfieldlist1 lastcomma",993"lfieldlist : lfieldlist1 lastcomma",
964"lfieldlist1 : expr1",994"lfieldlist1 : expr1",
@@ -1139,7 +1169,7 @@ luaY_reduce: · 174 unmodified lines
1139 switch (luaY_n)1169 switch (luaY_n)
1140 {1170 {
1141case 5:1171case 5:
1142#line 470 "lua.stx"1172#line 468 "lua.stx"
1143{ 1173{
1144 code_byte(PUSHFUNCTION);1174 code_byte(PUSHFUNCTION);
1145 code_code(luaY_vsp[0].pFunc);1175 code_code(luaY_vsp[0].pFunc);
@@ -1147,28 +1177,26 @@ case 5: · 1 unmodified line
1147 }1177 }
1148break;1178break;
1149case 6:1179case 6:
1150#line 477 "lua.stx"1180#line 475 "lua.stx"
1151{ luaY_val.vLong =luaY_vsp[0].vLong; init_func(); }1181{ luaY_val.vLong =luaY_vsp[0].vLong; init_func(); }
1152break;1182break;
1153case 7:1183case 7:
1154#line 479 "lua.stx"1184#line 477 "lua.stx"
1155{1185{
1156 code_byte(PUSHSTRING);1186 code_constant(luaY_vsp[0].pTStr);
1157 code_word(luaI_findconstant(luaY_vsp[0].pTStr));
1158 luaY_val.vLong = 0; /* indexed variable */1187 luaY_val.vLong = 0; /* indexed variable */
1159 init_func();1188 init_func();
1160 add_localvar(luaI_createfixedstring("self"));1189 add_localvar(luaI_createfixedstring("self"));
1161 }1190 }
1162break;1191break;
1163case 8:1192case 8:
1164#line 489 "lua.stx"1193#line 486 "lua.stx"
1165{1194{
1166 codereturn();1195 codereturn();
1167 luaY_val.pFunc = new(TFunc);1196 luaY_val.pFunc = new(TFunc);
1168 luaI_initTFunc(luaY_val.pFunc);1197 luaI_initTFunc(luaY_val.pFunc);
1169 luaY_val.pFunc->size = pc;1198 luaY_val.pFunc->size = pc;
1170 luaY_val.pFunc->code = newvector(pc, Byte);1199 luaY_val.pFunc->code = newvector(pc, Byte);
1171 luaY_val.pFunc->fileName = lua_parsedfile;
1172 luaY_val.pFunc->lineDefined = luaY_vsp[-3].vInt;1200 luaY_val.pFunc->lineDefined = luaY_vsp[-3].vInt;
1173 memcpy(luaY_val.pFunc->code, basepc, pc*sizeof(Byte));1201 memcpy(luaY_val.pFunc->code, basepc, pc*sizeof(Byte));
1174 if (lua_debug)1202 if (lua_debug)
@@ -1182,15 +1210,15 @@ case 8: · 7 unmodified lines
1182 }1210 }
1183break;1211break;
1184case 13:1212case 13:
1185#line 516 "lua.stx"1213#line 512 "lua.stx"
1186{ codeIf(luaY_vsp[-4].vLong, luaY_vsp[-2].vLong); }1214{ codeIf(luaY_vsp[-4].vLong, luaY_vsp[-2].vLong); }
1187break;1215break;
1188case 14:1216case 14:
1189#line 518 "lua.stx"1217#line 514 "lua.stx"
1190{luaY_val.vLong=pc;}1218{luaY_val.vLong=pc;}
1191break;1219break;
1192case 15:1220case 15:
1193#line 519 "lua.stx"1221#line 515 "lua.stx"
1194{1222{
1195 basepc[luaY_vsp[-3].vLong] = IFFJMP;1223 basepc[luaY_vsp[-3].vLong] = IFFJMP;
1196 code_word_at(basepc+luaY_vsp[-3].vLong+1, pc - (luaY_vsp[-3].vLong + sizeof(Word)+1));1224 code_word_at(basepc+luaY_vsp[-3].vLong+1, pc - (luaY_vsp[-3].vLong + sizeof(Word)+1));
@@ -1199,18 +1227,18 @@ case 15: · 2 unmodified lines
1199 }1227 }
1200break;1228break;
1201case 16:1229case 16:
1202#line 526 "lua.stx"1230#line 522 "lua.stx"
1203{luaY_val.vLong=pc;}1231{luaY_val.vLong=pc;}
1204break;1232break;
1205case 17:1233case 17:
1206#line 527 "lua.stx"1234#line 523 "lua.stx"
1207{1235{
1208 basepc[luaY_vsp[0].vLong] = IFFUPJMP;1236 basepc[luaY_vsp[0].vLong] = IFFUPJMP;
1209 code_word_at(basepc+luaY_vsp[0].vLong+1, pc - (luaY_vsp[-4].vLong));1237 code_word_at(basepc+luaY_vsp[0].vLong+1, pc - (luaY_vsp[-4].vLong));
1210 }1238 }
1211break;1239break;
1212case 18:1240case 18:
1213#line 533 "lua.stx"1241#line 529 "lua.stx"
1214{1242{
1215 {1243 {
1216 int i;1244 int i;
@@ -1222,22 +1250,26 @@ case 18: · 5 unmodified lines
1222 }1250 }
1223 }1251 }
1224break;1252break;
1253case 19:
1254#line 539 "lua.stx"
1255{;}
1256break;
1225case 20:1257case 20:
1226#line 545 "lua.stx"1258#line 541 "lua.stx"
1227{ nlocalvar += luaY_vsp[-1].vInt;1259{ nlocalvar += luaY_vsp[-1].vInt;
1228 adjust_mult_assign(luaY_vsp[-1].vInt, luaY_vsp[0].vInt, 0);1260 adjust_mult_assign(luaY_vsp[-1].vInt, luaY_vsp[0].vInt, 0);
1229 }1261 }
1230break;1262break;
1231case 23:1263case 23:
1232#line 553 "lua.stx"1264#line 549 "lua.stx"
1233{ codeIf(luaY_vsp[-3].vLong, luaY_vsp[-1].vLong); }1265{ codeIf(luaY_vsp[-3].vLong, luaY_vsp[-1].vLong); }
1234break;1266break;
1235case 24:1267case 24:
1236#line 556 "lua.stx"1268#line 552 "lua.stx"
1237{luaY_val.vInt = nlocalvar;}1269{luaY_val.vInt = nlocalvar;}
1238break;1270break;
1239case 25:1271case 25:
1240#line 557 "lua.stx"1272#line 553 "lua.stx"
1241{1273{
1242 if (nlocalvar != luaY_vsp[-2].vInt)1274 if (nlocalvar != luaY_vsp[-2].vInt)
1243 {1275 {
@@ -1251,14 +1283,14 @@ case 25: · 7 unmodified lines
1251 }1283 }
1252break;1284break;
1253case 27:1285case 27:
1254#line 572 "lua.stx"1286#line 568 "lua.stx"
1255{1287{
1256 adjust_functioncall(luaY_vsp[-1].vLong, MULT_RET);1288 adjust_functioncall(luaY_vsp[-1].vLong, MULT_RET);
1257 codereturn();1289 codereturn();
1258 }1290 }
1259break;1291break;
1260case 28:1292case 28:
1261#line 579 "lua.stx"1293#line 575 "lua.stx"
1262{ 1294{
1263 luaY_val.vLong = pc;1295 luaY_val.vLong = pc;
1264 code_byte(0); /* open space */1296 code_byte(0); /* open space */
@@ -1266,103 +1298,102 @@ case 28: · 1 unmodified line
1266 }1298 }
1267break;1299break;
1268case 29:1300case 29:
1269#line 586 "lua.stx"1301#line 582 "lua.stx"
1270{ adjust_functioncall(luaY_vsp[0].vLong, 1); }1302{ adjust_functioncall(luaY_vsp[0].vLong, 1); }
1271break;1303break;
1272case 30:1304case 30:
1273#line 589 "lua.stx"1305#line 585 "lua.stx"
1274{ luaY_val.vLong = luaY_vsp[-1].vLong; }1306{ luaY_val.vLong = luaY_vsp[-1].vLong; }
1275break;1307break;
1276case 31:1308case 31:
1277#line 590 "lua.stx"1309#line 586 "lua.stx"
1278{ code_byte(EQOP); luaY_val.vLong = 0; }1310{ code_byte(EQOP); luaY_val.vLong = 0; }
1279break;1311break;
1280case 32:1312case 32:
1281#line 591 "lua.stx"1313#line 587 "lua.stx"
1282{ code_byte(LTOP); luaY_val.vLong = 0; }1314{ code_byte(LTOP); luaY_val.vLong = 0; }
1283break;1315break;
1284case 33:1316case 33:
1285#line 592 "lua.stx"1317#line 588 "lua.stx"
1286{ code_byte(GTOP); luaY_val.vLong = 0; }1318{ code_byte(GTOP); luaY_val.vLong = 0; }
1287break;1319break;
1288case 34:1320case 34:
1289#line 593 "lua.stx"1321#line 589 "lua.stx"
1290{ code_byte(EQOP); code_byte(NOTOP); luaY_val.vLong = 0; }1322{ code_byte(EQOP); code_byte(NOTOP); luaY_val.vLong = 0; }
1291break;1323break;
1292case 35:1324case 35:
1293#line 594 "lua.stx"1325#line 590 "lua.stx"
1294{ code_byte(LEOP); luaY_val.vLong = 0; }1326{ code_byte(LEOP); luaY_val.vLong = 0; }
1295break;1327break;
1296case 36:1328case 36:
1297#line 595 "lua.stx"1329#line 591 "lua.stx"
1298{ code_byte(GEOP); luaY_val.vLong = 0; }1330{ code_byte(GEOP); luaY_val.vLong = 0; }
1299break;1331break;
1300case 37:1332case 37:
1301#line 596 "lua.stx"1333#line 592 "lua.stx"
1302{ code_byte(ADDOP); luaY_val.vLong = 0; }1334{ code_byte(ADDOP); luaY_val.vLong = 0; }
1303break;1335break;
1304case 38:1336case 38:
1305#line 597 "lua.stx"1337#line 593 "lua.stx"
1306{ code_byte(SUBOP); luaY_val.vLong = 0; }1338{ code_byte(SUBOP); luaY_val.vLong = 0; }
1307break;1339break;
1308case 39:1340case 39:
1309#line 598 "lua.stx"1341#line 594 "lua.stx"
1310{ code_byte(MULTOP); luaY_val.vLong = 0; }1342{ code_byte(MULTOP); luaY_val.vLong = 0; }
1311break;1343break;
1312case 40:1344case 40:
1313#line 599 "lua.stx"1345#line 595 "lua.stx"
1314{ code_byte(DIVOP); luaY_val.vLong = 0; }1346{ code_byte(DIVOP); luaY_val.vLong = 0; }
1315break;1347break;
1316case 41:1348case 41:
1317#line 600 "lua.stx"1349#line 596 "lua.stx"
1318{ code_byte(POWOP); luaY_val.vLong = 0; }1350{ code_byte(POWOP); luaY_val.vLong = 0; }
1319break;1351break;
1320case 42:1352case 42:
1321#line 601 "lua.stx"1353#line 597 "lua.stx"
1322{ code_byte(CONCOP); luaY_val.vLong = 0; }1354{ code_byte(CONCOP); luaY_val.vLong = 0; }
1323break;1355break;
1324case 43:1356case 43:
1325#line 602 "lua.stx"1357#line 598 "lua.stx"
1326{ code_byte(MINUSOP); luaY_val.vLong = 0;}1358{ code_byte(MINUSOP); luaY_val.vLong = 0;}
1327break;1359break;
1328case 44:1360case 44:
1329#line 603 "lua.stx"1361#line 599 "lua.stx"
1330{ luaY_val.vLong = 0; }1362{ luaY_val.vLong = 0; }
1331break;1363break;
1332case 45:1364case 45:
1333#line 604 "lua.stx"1365#line 600 "lua.stx"
1334{ luaY_val.vLong = 0;}1366{ luaY_val.vLong = 0;}
1335break;1367break;
1336case 46:1368case 46:
1337#line 605 "lua.stx"1369#line 601 "lua.stx"
1338{ code_number(luaY_vsp[0].vFloat); luaY_val.vLong = 0; }1370{ code_number(luaY_vsp[0].vFloat); luaY_val.vLong = 0; }
1339break;1371break;
1340case 47:1372case 47:
1341#line 607 "lua.stx"1373#line 603 "lua.stx"
1342{1374{
1343 code_byte(PUSHSTRING);1375 code_string(luaY_vsp[0].vWord);
1344 code_word(luaY_vsp[0].vWord);1376 luaY_val.vLong = 0;
1345 luaY_val.vLong = 0;
1346 }1377 }
1347break;1378break;
1348case 48:1379case 48:
1349#line 612 "lua.stx"1380#line 607 "lua.stx"
1350{code_byte(PUSHNIL); luaY_val.vLong = 0; }1381{code_byte(PUSHNIL); luaY_val.vLong = 0; }
1351break;1382break;
1352case 49:1383case 49:
1353#line 613 "lua.stx"1384#line 608 "lua.stx"
1354{ luaY_val.vLong = luaY_vsp[0].vLong; }1385{ luaY_val.vLong = luaY_vsp[0].vLong; }
1355break;1386break;
1356case 50:1387case 50:
1357#line 614 "lua.stx"1388#line 609 "lua.stx"
1358{ code_byte(NOTOP); luaY_val.vLong = 0;}1389{ code_byte(NOTOP); luaY_val.vLong = 0;}
1359break;1390break;
1360case 51:1391case 51:
1361#line 615 "lua.stx"1392#line 610 "lua.stx"
1362{code_byte(POP); }1393{code_byte(POP); }
1363break;1394break;
1364case 52:1395case 52:
1365#line 616 "lua.stx"1396#line 611 "lua.stx"
1366{ 1397{
1367 basepc[luaY_vsp[-2].vLong] = ONFJMP;1398 basepc[luaY_vsp[-2].vLong] = ONFJMP;
1368 code_word_at(basepc+luaY_vsp[-2].vLong+1, pc - (luaY_vsp[-2].vLong + sizeof(Word)+1));1399 code_word_at(basepc+luaY_vsp[-2].vLong+1, pc - (luaY_vsp[-2].vLong + sizeof(Word)+1));
@@ -1370,11 +1401,11 @@ case 52: · 1 unmodified line
1370 }1401 }
1371break;1402break;
1372case 53:1403case 53:
1373#line 621 "lua.stx"1404#line 616 "lua.stx"
1374{code_byte(POP); }1405{code_byte(POP); }
1375break;1406break;
1376case 54:1407case 54:
1377#line 622 "lua.stx"1408#line 617 "lua.stx"
1378{ 1409{
1379 basepc[luaY_vsp[-2].vLong] = ONTJMP;1410 basepc[luaY_vsp[-2].vLong] = ONTJMP;
1380 code_word_at(basepc+luaY_vsp[-2].vLong+1, pc - (luaY_vsp[-2].vLong + sizeof(Word)+1));1411 code_word_at(basepc+luaY_vsp[-2].vLong+1, pc - (luaY_vsp[-2].vLong + sizeof(Word)+1));
@@ -1382,20 +1413,20 @@ case 54: · 1 unmodified line
1382 }1413 }
1383break;1414break;
1384case 55:1415case 55:
1385#line 630 "lua.stx"1416#line 625 "lua.stx"
1386{1417{
1387 code_byte(CREATEARRAY);1418 code_byte(CREATEARRAY);
1388 luaY_val.vLong = pc; code_word(0);1419 luaY_val.vLong = pc; code_word(0);
1389 }1420 }
1390break;1421break;
1391case 56:1422case 56:
1392#line 635 "lua.stx"1423#line 630 "lua.stx"
1393{1424{
1394 code_word_at(basepc+luaY_vsp[-3].vLong, luaY_vsp[-1].vInt);1425 code_word_at(basepc+luaY_vsp[-3].vLong, luaY_vsp[-1].vInt);
1395 }1426 }
1396break;1427break;
1397case 57:1428case 57:
1398#line 641 "lua.stx"1429#line 636 "lua.stx"
1399{1430{
1400 code_byte(CALLFUNC);1431 code_byte(CALLFUNC);
1401 code_byte(luaY_vsp[-1].vInt+luaY_vsp[0].vInt);1432 code_byte(luaY_vsp[-1].vInt+luaY_vsp[0].vInt);
@@ -1404,11 +1435,11 @@ case 57: · 2 unmodified lines
1404 }1435 }
1405break;1436break;
1406case 58:1437case 58:
1407#line 649 "lua.stx"1438#line 644 "lua.stx"
1408{ luaY_val.vInt = 0; }1439{ luaY_val.vInt = 0; }
1409break;1440break;
1410case 59:1441case 59:
1411#line 651 "lua.stx"1442#line 646 "lua.stx"
1412{ 1443{
1413 code_byte(PUSHSELF); 1444 code_byte(PUSHSELF);
1414 code_word(luaI_findconstant(luaY_vsp[0].pTStr));1445 code_word(luaI_findconstant(luaY_vsp[0].pTStr));
@@ -1416,31 +1447,31 @@ case 59: · 1 unmodified line
1416 }1447 }
1417break;1448break;
1418case 60:1449case 60:
1419#line 659 "lua.stx"1450#line 654 "lua.stx"
1420{ luaY_val.vInt = adjust_functioncall(luaY_vsp[-1].vLong, 1); }1451{ luaY_val.vInt = adjust_functioncall(luaY_vsp[-1].vLong, 1); }
1421break;1452break;
1422case 61:1453case 61:
1423#line 660 "lua.stx"1454#line 655 "lua.stx"
1424{ luaY_val.vInt = 1; }1455{ luaY_val.vInt = 1; }
1425break;1456break;
1426case 62:1457case 62:
1427#line 663 "lua.stx"1458#line 658 "lua.stx"
1428{ luaY_val.vLong = 0; }1459{ luaY_val.vLong = 0; }
1429break;1460break;
1430case 63:1461case 63:
1431#line 664 "lua.stx"1462#line 659 "lua.stx"
1432{ luaY_val.vLong = luaY_vsp[0].vLong; }1463{ luaY_val.vLong = luaY_vsp[0].vLong; }
1433break;1464break;
1434case 64:1465case 64:
1435#line 667 "lua.stx"1466#line 662 "lua.stx"
1436{ if (luaY_vsp[0].vLong != 0) luaY_val.vLong = luaY_vsp[0].vLong; else luaY_val.vLong = -1; }1467{ if (luaY_vsp[0].vLong != 0) luaY_val.vLong = luaY_vsp[0].vLong; else luaY_val.vLong = -1; }
1437break;1468break;
1438case 65:1469case 65:
1439#line 668 "lua.stx"1470#line 663 "lua.stx"
1440{ luaY_val.vLong = adjust_functioncall(luaY_vsp[-1].vLong, 1); }1471{ luaY_val.vLong = adjust_functioncall(luaY_vsp[-1].vLong, 1); }
1441break;1472break;
1442case 66:1473case 66:
1443#line 669 "lua.stx"1474#line 664 "lua.stx"
1444{1475{
1445 if (luaY_vsp[0].vLong == 0) luaY_val.vLong = -(luaY_vsp[-1].vLong + 1); /* -length */1476 if (luaY_vsp[0].vLong == 0) luaY_val.vLong = -(luaY_vsp[-1].vLong + 1); /* -length */
1446 else1477 else
@@ -1451,19 +1482,19 @@ case 66: · 4 unmodified lines
1451 }1482 }
1452break;1483break;
1453case 67:1484case 67:
1454#line 679 "lua.stx"1485#line 674 "lua.stx"
1455{ luaY_val.vInt = close_parlist(0); }1486{ luaY_val.vInt = close_parlist(0); }
1456break;1487break;
1457case 68:1488case 68:
1458#line 680 "lua.stx"1489#line 675 "lua.stx"
1459{ luaY_val.vInt = close_parlist(luaY_vsp[0].vInt); }1490{ luaY_val.vInt = close_parlist(luaY_vsp[0].vInt); }
1460break;1491break;
1461case 69:1492case 69:
1462#line 683 "lua.stx"1493#line 678 "lua.stx"
1463{ luaY_val.vInt = luaY_vsp[0].vInt; }1494{ luaY_val.vInt = luaY_vsp[0].vInt; }
1464break;1495break;
1465case 70:1496case 70:
1466#line 685 "lua.stx"1497#line 680 "lua.stx"
1467{1498{
1468 if (luaY_vsp[-2].vInt)1499 if (luaY_vsp[-2].vInt)
1469 lua_error("invalid parameter list");1500 lua_error("invalid parameter list");
@@ -1471,113 +1502,110 @@ case 70: · 1 unmodified line
1471 }1502 }
1472break;1503break;
1473case 71:1504case 71:
1474#line 692 "lua.stx"1505#line 687 "lua.stx"
1475{ add_localvar(luaY_vsp[0].pTStr); luaY_val.vInt = 0; }1506{ add_localvar(luaY_vsp[0].pTStr); luaY_val.vInt = 0; }
1476break;1507break;
1477case 72:1508case 72:
1478#line 693 "lua.stx"1509#line 688 "lua.stx"
1479{ luaY_val.vInt = 1; }1510{ luaY_val.vInt = 1; }
1480break;1511break;
1481case 73:1512case 73:
1482#line 697 "lua.stx"1513#line 692 "lua.stx"
1483{ flush_list(luaY_vsp[0].vInt/FIELDS_PER_FLUSH, luaY_vsp[0].vInt%FIELDS_PER_FLUSH); }1514{ flush_list(luaY_vsp[0].vInt/FIELDS_PER_FLUSH, luaY_vsp[0].vInt%FIELDS_PER_FLUSH); }
1484break;1515break;
1485case 74:1516case 74:
1486#line 699 "lua.stx"1517#line 694 "lua.stx"
1487{ luaY_val.vInt = luaY_vsp[-2].vInt+luaY_vsp[0].vInt; }1518{ luaY_val.vInt = luaY_vsp[-2].vInt+luaY_vsp[0].vInt; }
1488break;1519break;
1489case 75:1520case 75:
1490#line 701 "lua.stx"1521#line 696 "lua.stx"
1491{ luaY_val.vInt = luaY_vsp[-1].vInt; flush_record(luaY_vsp[-1].vInt%FIELDS_PER_FLUSH); }1522{ luaY_val.vInt = luaY_vsp[-1].vInt; flush_record(luaY_vsp[-1].vInt%FIELDS_PER_FLUSH); }
1492break;1523break;
1493case 76:1524case 76:
1494#line 705 "lua.stx"1525#line 700 "lua.stx"
1495{ luaY_val.vInt = 0; }1526{ luaY_val.vInt = 0; }
1496break;1527break;
1497case 77:1528case 77:
1498#line 707 "lua.stx"1529#line 702 "lua.stx"
1499{ luaY_val.vInt = luaY_vsp[0].vInt; flush_record(luaY_vsp[0].vInt%FIELDS_PER_FLUSH); }1530{ luaY_val.vInt = luaY_vsp[0].vInt; flush_record(luaY_vsp[0].vInt%FIELDS_PER_FLUSH); }
1500break;1531break;
1501case 80:1532case 80:
1502#line 714 "lua.stx"1533#line 709 "lua.stx"
1503{ luaY_val.vInt = 0; }1534{ luaY_val.vInt = 0; }
1504break;1535break;
1505case 81:1536case 81:
1506#line 715 "lua.stx"1537#line 710 "lua.stx"
1507{ luaY_val.vInt = luaY_vsp[-1].vInt; }1538{ luaY_val.vInt = luaY_vsp[-1].vInt; }
1508break;1539break;
1509case 82:1540case 82:
1510#line 718 "lua.stx"1541#line 713 "lua.stx"
1511{luaY_val.vInt=1;}1542{luaY_val.vInt=1;}
1512break;1543break;
1513case 83:1544case 83:
1514#line 720 "lua.stx"1545#line 715 "lua.stx"
1515{1546{
1516 luaY_val.vInt=luaY_vsp[-2].vInt+1;1547 luaY_val.vInt=luaY_vsp[-2].vInt+1;
1517 if (luaY_val.vInt%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);1548 if (luaY_val.vInt%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
1518 }1549 }
1519break;1550break;
1520case 84:1551case 86:
1521#line 727 "lua.stx"1552#line 725 "lua.stx"
1522{ 1553{ code_constant(luaY_vsp[0].pTStr); }
1523 push_field(luaI_findconstant(luaY_vsp[-2].pTStr));
1524 }
1525break;1554break;
1526case 85:1555case 87:
1527#line 732 "lua.stx"1556#line 728 "lua.stx"
1528{ luaY_val.vInt = 0; }1557{ luaY_val.vInt = 0; }
1529break;1558break;
1530case 86:1559case 88:
1531#line 733 "lua.stx"1560#line 729 "lua.stx"
1532{ luaY_val.vInt = luaY_vsp[-1].vInt; }1561{ luaY_val.vInt = luaY_vsp[-1].vInt; }
1533break;1562break;
1534case 87:1563case 89:
1535#line 736 "lua.stx"1564#line 732 "lua.stx"
1536{luaY_val.vInt=1;}1565{luaY_val.vInt=1;}
1537break;1566break;
1538case 88:1567case 90:
1539#line 738 "lua.stx"1568#line 734 "lua.stx"
1540{1569{
1541 luaY_val.vInt=luaY_vsp[-2].vInt+1;1570 luaY_val.vInt=luaY_vsp[-2].vInt+1;
1542 if (luaY_val.vInt%FIELDS_PER_FLUSH == 0) 1571 if (luaY_val.vInt%FIELDS_PER_FLUSH == 0)
1543 flush_list(luaY_val.vInt/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);1572 flush_list(luaY_val.vInt/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
1544 }1573 }
1545break;1574break;
1546case 89:1575case 91:
1547#line 746 "lua.stx"1576#line 742 "lua.stx"
1548{1577{
1549 nvarbuffer = 0; 1578 nvarbuffer = 0;
1550 add_varbuffer(luaY_vsp[0].vLong);1579 add_varbuffer(luaY_vsp[0].vLong);
1551 luaY_val.vInt = (luaY_vsp[0].vLong == 0) ? 1 : 0;1580 luaY_val.vInt = (luaY_vsp[0].vLong == 0) ? 1 : 0;
1552 }1581 }
1553break;1582break;
1554case 90:1583case 92:
1555#line 752 "lua.stx"1584#line 748 "lua.stx"
1556{ 1585{
1557 add_varbuffer(luaY_vsp[0].vLong);1586 add_varbuffer(luaY_vsp[0].vLong);
1558 luaY_val.vInt = (luaY_vsp[0].vLong == 0) ? luaY_vsp[-2].vInt + 1 : luaY_vsp[-2].vInt;1587 luaY_val.vInt = (luaY_vsp[0].vLong == 0) ? luaY_vsp[-2].vInt + 1 : luaY_vsp[-2].vInt;
1559 }1588 }
1560break;1589break;
1561case 91:1590case 93:
1562#line 758 "lua.stx"1591#line 754 "lua.stx"
1563{ luaY_val.vLong = luaY_vsp[0].vLong; }1592{ luaY_val.vLong = luaY_vsp[0].vLong; }
1564break;1593break;
1565case 92:1594case 94:
1566#line 760 "lua.stx"1595#line 756 "lua.stx"
1567{1596{
1568 luaY_val.vLong = 0; /* indexed variable */1597 luaY_val.vLong = 0; /* indexed variable */
1569 }1598 }
1570break;1599break;
1571case 93:1600case 95:
1572#line 764 "lua.stx"1601#line 760 "lua.stx"
1573{1602{
1574 code_byte(PUSHSTRING);1603 code_constant(luaY_vsp[0].pTStr);
1575 code_word(luaI_findconstant(luaY_vsp[0].pTStr));1604 luaY_val.vLong = 0; /* indexed variable */
1576 luaY_val.vLong = 0; /* indexed variable */
1577 }1605 }
1578break;1606break;
1579case 94:1607case 96:
1580#line 772 "lua.stx"1608#line 767 "lua.stx"
1581{1609{
1582 int local = lua_localname(luaY_vsp[0].pTStr);1610 int local = lua_localname(luaY_vsp[0].pTStr);
1583 if (local == -1) /* global var */1611 if (local == -1) /* global var */
@@ -1586,30 +1614,30 @@ case 94: · 2 unmodified lines
1586 luaY_val.vLong = -(local+1); /* return negative value */1614 luaY_val.vLong = -(local+1); /* return negative value */
1587 }1615 }
1588break;1616break;
1589case 95:1617case 97:
1590#line 781 "lua.stx"1618#line 776 "lua.stx"
1591{ lua_pushvar(luaY_vsp[0].vLong); }1619{ lua_pushvar(luaY_vsp[0].vLong); }
1592break;1620break;
1593case 96:1621case 98:
1594#line 784 "lua.stx"1622#line 779 "lua.stx"
1595{store_localvar(luaY_vsp[0].pTStr, 0); luaY_val.vInt = 1;}1623{store_localvar(luaY_vsp[0].pTStr, 0); luaY_val.vInt = 1;}
1596break;1624break;
1597case 97:1625case 99:
1598#line 786 "lua.stx"1626#line 781 "lua.stx"
1599{1627{
1600 store_localvar(luaY_vsp[0].pTStr, luaY_vsp[-2].vInt);1628 store_localvar(luaY_vsp[0].pTStr, luaY_vsp[-2].vInt);
1601 luaY_val.vInt = luaY_vsp[-2].vInt+1;1629 luaY_val.vInt = luaY_vsp[-2].vInt+1;
1602 }1630 }
1603break;1631break;
1604case 98:1632case 100:
1605#line 792 "lua.stx"1633#line 787 "lua.stx"
1606{ luaY_val.vInt = 0; }1634{ luaY_val.vInt = 0; }
1607break;1635break;
1608case 99:1636case 101:
1609#line 793 "lua.stx"1637#line 788 "lua.stx"
1610{ luaY_val.vInt = luaY_vsp[0].vLong; }1638{ luaY_val.vInt = luaY_vsp[0].vLong; }
1611break;1639break;
1612#line 1613 "y.tab.c"1640#line 1641 "y.tab.c"
1613 }1641 }
1614 luaY_ssp -= luaY_m;1642 luaY_ssp -= luaY_m;
1615 luaY_state = *luaY_ssp;1643 luaY_state = *luaY_ssp;
src/table.c+84−96
@@ -3,9 +3,11 @@ · 2 unmodified lines
3** Module to control static tables3** Module to control static tables
4*/4*/
5 5
6char *rcs_table="$Id: table.c,v 2.58 1996/11/01 12:47:45 roberto Exp $";6char *rcs_table="$Id: table.c,v 2.72 1997/06/17 18:09:31 roberto Exp $";
7 7
8#include "mem.h"8#include "luamem.h"
9#include "auxlib.h"
10#include "func.h"
9#include "opcode.h"11#include "opcode.h"
10#include "tree.h"12#include "tree.h"
11#include "hash.h"13#include "hash.h"
@@ -27,49 +29,14 @@ Word lua_nconstant = 0; · 15 unmodified lines
27static Long lua_maxconstant = 0;29static Long lua_maxconstant = 0;
28 30
29 31
30#define GARBAGE_BLOCK 5032#define GARBAGE_BLOCK 100
31
32static void lua_nextvar (void);
33
34/*
35** Internal functions
36*/
37static struct {
38 char *name;
39 lua_CFunction func;
40} int_funcs[] = {
41 {"assert", luaI_assert},
42 {"call", luaI_call},
43 {"dofile", lua_internaldofile},
44 {"dostring", lua_internaldostring},
45 {"error", luaI_error},
46 {"getglobal", luaI_getglobal},
47 {"next", lua_next},
48 {"nextvar", lua_nextvar},
49 {"print", luaI_print},
50 {"setfallback", luaI_setfallback},
51 {"setglobal", luaI_setglobal},
52 {"tonumber", lua_obj2number},
53 {"tostring", luaI_tostring},
54 {"type", luaI_type}
55};
56
57#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
58 33
59 34
60void luaI_initsymbol (void)35void luaI_initsymbol (void)
61{36{
62 int i;
63 Word n;
64 lua_maxsymbol = BUFFER_BLOCK;37 lua_maxsymbol = BUFFER_BLOCK;
65 lua_table = newvector(lua_maxsymbol, Symbol);38 lua_table = newvector(lua_maxsymbol, Symbol);
66 for (i=0; i<INTFUNCSIZE; i++)39 luaI_predefine();
67 {
68 n = luaI_findsymbolbyname(int_funcs[i].name);
69 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
70 }
71 n = luaI_findsymbolbyname("_VERSION_");
72 s_tag(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION);
73}40}
74 41
75 42
@@ -92,17 +59,17 @@ void luaI_initconstant (void) · 16 unmodified lines
92*/59*/
93Word luaI_findsymbol (TaggedString *t)60Word luaI_findsymbol (TaggedString *t)
94{61{
95 if (t->varindex == NOT_USED)62 if (t->u.s.varindex == NOT_USED)
96 {63 {
97 if (lua_ntable == lua_maxsymbol)64 if (lua_ntable == lua_maxsymbol)
98 lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,65 lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
99 symbolEM, MAX_WORD);66 symbolEM, MAX_WORD);
100 t->varindex = lua_ntable;67 t->u.s.varindex = lua_ntable;
101 lua_table[lua_ntable].varname = t;68 lua_table[lua_ntable].varname = t;
102 s_tag(lua_ntable) = LUA_T_NIL;69 s_ttype(lua_ntable) = LUA_T_NIL;
103 lua_ntable++;70 lua_ntable++;
104 }71 }
105 return t->varindex;72 return t->u.s.varindex;
106}73}
107 74
108 75
@@ -118,16 +85,16 @@ Word luaI_findsymbolbyname (char *name) · 9 unmodified lines
118*/85*/
119Word luaI_findconstant (TaggedString *t)86Word luaI_findconstant (TaggedString *t)
120{87{
121 if (t->constindex == NOT_USED)88 if (t->u.s.constindex == NOT_USED)
122 {89 {
123 if (lua_nconstant == lua_maxconstant)90 if (lua_nconstant == lua_maxconstant)
124 lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,91 lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
125 constantEM, MAX_WORD);92 constantEM, MAX_WORD);
126 t->constindex = lua_nconstant;93 t->u.s.constindex = lua_nconstant;
127 lua_constant[lua_nconstant] = t;94 lua_constant[lua_nconstant] = t;
128 lua_nconstant++;95 lua_nconstant++;
129 }96 }
130 return t->constindex;97 return t->u.s.constindex;
131}98}
132 99
133 100
@@ -145,10 +112,16 @@ TaggedString *luaI_createfixedstring (char *name) · 11 unmodified lines
145}112}
146 113
147 114
115int luaI_globaldefined (char *name)
116{
117 return ttype(&lua_table[luaI_findsymbolbyname(name)].object) != LUA_T_NIL;
118}
119
120
148/*121/*
149** Traverse symbol table objects122** Traverse symbol table objects
150*/123*/
151static char *lua_travsymbol (int (*fn)(Object *))124static char *lua_travsymbol (int (*fn)(TObject *))
152{125{
153 Word i;126 Word i;
154 for (i=0; i<lua_ntable; i++)127 for (i=0; i<lua_ntable; i++)
@@ -161,13 +134,14 @@ static char *lua_travsymbol (int (*fn)(Object *)) · 6 unmodified lines
161/*134/*
162** Mark an object if it is a string or a unmarked array.135** Mark an object if it is a string or a unmarked array.
163*/136*/
164int lua_markobject (Object *o)137int lua_markobject (TObject *o)
165{/* if already marked, does not change mark value */138{/* if already marked, does not change mark value */
166 if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)139 if (ttype(o) == LUA_T_USERDATA ||
140 (ttype(o) == LUA_T_STRING && !tsvalue(o)->marked))
167 tsvalue(o)->marked = 1;141 tsvalue(o)->marked = 1;
168 else if (tag(o) == LUA_T_ARRAY)142 else if (ttype(o) == LUA_T_ARRAY)
169 lua_hashmark (avalue(o));143 lua_hashmark (avalue(o));
170 else if ((o->tag == LUA_T_FUNCTION || o->tag == LUA_T_MARK)144 else if ((o->ttype == LUA_T_FUNCTION || o->ttype == LUA_T_MARK)
171 && !o->value.tf->marked)145 && !o->value.tf->marked)
172 o->value.tf->marked = 1;146 o->value.tf->marked = 1;
173 return 0;147 return 0;
@@ -176,11 +150,11 @@ int lua_markobject (Object *o) · 2 unmodified lines
176/*150/*
177* returns 0 if the object is going to be (garbage) collected151* returns 0 if the object is going to be (garbage) collected
178*/152*/
179int luaI_ismarked (Object *o)153int luaI_ismarked (TObject *o)
180{154{
181 switch (o->tag)155 switch (o->ttype)
182 {156 {
183 case LUA_T_STRING:157 case LUA_T_STRING: case LUA_T_USERDATA:
184 return o->value.ts->marked;158 return o->value.ts->marked;
185 case LUA_T_FUNCTION:159 case LUA_T_FUNCTION:
186 return o->value.tf->marked;160 return o->value.tf->marked;
@@ -192,76 +166,90 @@ int luaI_ismarked (Object *o) · 5 unmodified lines
192}166}
193 167
194 168
169static void call_nilIM (void)
170{ /* signals end of garbage collection */
171 TObject t;
172 ttype(&t) = LUA_T_NIL;
173 luaI_gcIM(&t); /* end of list */
174}
175
195/*176/*
196** Garbage collection. 177** Garbage collection.
197** Delete all unused strings and arrays.178** Delete all unused strings and arrays.
198*/179*/
199Long luaI_collectgarbage (void)180static long gc_block = GARBAGE_BLOCK;
181static long gc_nentity = 0; /* total of strings, arrays, etc */
182
183static void markall (void)
200{184{
201 Long recovered = 0;
202 lua_travstack(lua_markobject); /* mark stack objects */185 lua_travstack(lua_markobject); /* mark stack objects */
203 lua_travsymbol(lua_markobject); /* mark symbol table objects */186 lua_travsymbol(lua_markobject); /* mark symbol table objects */
204 luaI_travlock(lua_markobject); /* mark locked objects */187 luaI_travlock(lua_markobject); /* mark locked objects */
205 luaI_travfallbacks(lua_markobject); /* mark fallbacks */188 luaI_travfallbacks(lua_markobject); /* mark fallbacks */
189}
190
191
192long lua_collectgarbage (long limit)
193{
194 long recovered = 0;
195 Hash *freetable;
196 TaggedString *freestr;
197 TFunc *freefunc;
198 markall();
206 luaI_invalidaterefs();199 luaI_invalidaterefs();
207 recovered += lua_strcollector();200 freetable = luaI_hashcollector(&recovered);
208 recovered += lua_hashcollector();201 freestr = luaI_strcollector(&recovered);
209 recovered += luaI_funccollector();202 freefunc = luaI_funccollector(&recovered);
203 gc_nentity -= recovered;
204 gc_block = (limit == 0) ? 2*(gc_block-recovered) : gc_nentity+limit;
205 luaI_hashcallIM(freetable);
206 luaI_strcallIM(freestr);
207 call_nilIM();
208 luaI_hashfree(freetable);
209 luaI_strfree(freestr);
210 luaI_funcfree(freefunc);
210 return recovered;211 return recovered;
211} 212}
212 213
214
213void lua_pack (void)215void lua_pack (void)
214{216{
215 static unsigned long block = GARBAGE_BLOCK;217 if (++gc_nentity >= gc_block)
216 static unsigned long nentity = 0; /* total of strings, arrays, etc */218 lua_collectgarbage(0);
217 unsigned long recovered = 0;
218 if (nentity++ < block) return;
219 recovered = luaI_collectgarbage();
220 block = 2*(block-recovered);
221 nentity -= recovered;
222} 219}
223 220
224 221
225/*222/*
226** Internal function: return next global variable223** Internal function: return next global variable
227*/224*/
228static void lua_nextvar (void)225void luaI_nextvar (void)
229{226{
230 Word next;227 Word next;
231 lua_Object o = lua_getparam(1);228 if (lua_isnil(lua_getparam(1)))
232 if (o == LUA_NOOBJECT)229 next = 0;
233 lua_error("too few arguments to function `nextvar'");230 else
234 if (lua_getparam(2) != LUA_NOOBJECT)231 next = luaI_findsymbolbyname(luaL_check_string(1)) + 1;
235 lua_error("too many arguments to function `nextvar'");232 while (next < lua_ntable && s_ttype(next) == LUA_T_NIL)
236 if (lua_isnil(o))233 next++;
237 next = 0;234 if (next < lua_ntable) {
238 else if (!lua_isstring(o))235 lua_pushstring(lua_table[next].varname->str);
239 {236 luaI_pushobject(&s_object(next));
240 lua_error("incorrect argument to function `nextvar'"); 237 }
241 return; /* to avoid warnings */
242 }
243 else
244 next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
245 while (next < lua_ntable && s_tag(next) == LUA_T_NIL) next++;
246 if (next < lua_ntable)
247 {
248 lua_pushstring(lua_table[next].varname->str);
249 luaI_pushobject(&s_object(next));
250 }
251}238}
252 239
253 240
254static Object *functofind;241static TObject *functofind;
255static int checkfunc (Object *o)242static int checkfunc (TObject *o)
256{243{
257 if (o->tag == LUA_T_FUNCTION)244 if (o->ttype == LUA_T_FUNCTION)
258 return245 return
259 ((functofind->tag == LUA_T_FUNCTION || functofind->tag == LUA_T_MARK)246 ((functofind->ttype == LUA_T_FUNCTION || functofind->ttype == LUA_T_MARK)
260 && (functofind->value.tf == o->value.tf));247 && (functofind->value.tf == o->value.tf));
261 if (o->tag == LUA_T_CFUNCTION)248 if (o->ttype == LUA_T_CFUNCTION)
262 return249 return
263 ((functofind->tag == LUA_T_CFUNCTION || functofind->tag == LUA_T_CMARK)250 ((functofind->ttype == LUA_T_CFUNCTION ||
264 && (functofind->value.f == o->value.f));251 functofind->ttype == LUA_T_CMARK) &&
252 (functofind->value.f == o->value.f));
265 return 0;253 return 0;
266}254}
267 255
@@ -270,7 +258,7 @@ char *lua_getobjname (lua_Object o, char **name) · 2 unmodified lines
270{ /* try to find a name for given function */258{ /* try to find a name for given function */
271 functofind = luaI_Address(o);259 functofind = luaI_Address(o);
272 if ((*name = luaI_travfallbacks(checkfunc)) != NULL)260 if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
273 return "fallback";261 return "tag-method";
274 else if ((*name = lua_travsymbol(checkfunc)) != NULL)262 else if ((*name = lua_travsymbol(checkfunc)) != NULL)
275 return "global";263 return "global";
276 else return "";264 else return "";
src/table.h+6−5
@@ -1,7 +1,7 @@
1/*1/*
2** Module to control static tables2** Module to control static tables
3** TeCGraf - PUC-Rio3** TeCGraf - PUC-Rio
4** $Id: table.h,v 2.21 1996/04/22 18:00:37 roberto Exp $4** $Id: table.h,v 2.25 1997/05/26 14:42:36 roberto Exp $
5*/5*/
6 6
7#ifndef table_h7#ifndef table_h
@@ -12,7 +12,7 @@ · 4 unmodified lines
12 12
13typedef struct13typedef struct
14{14{
15 Object object;15 TObject object;
16 TaggedString *varname;16 TaggedString *varname;
17} Symbol;17} Symbol;
18 18
@@ -28,10 +28,11 @@ Word luaI_findsymbolbyname (char *name); · 9 unmodified lines
28Word luaI_findsymbol (TaggedString *t);28Word luaI_findsymbol (TaggedString *t);
29Word luaI_findconstant (TaggedString *t);29Word luaI_findconstant (TaggedString *t);
30Word luaI_findconstantbyname (char *name);30Word luaI_findconstantbyname (char *name);
31int luaI_globaldefined (char *name);
32void luaI_nextvar (void);
31TaggedString *luaI_createfixedstring (char *str);33TaggedString *luaI_createfixedstring (char *str);
32int lua_markobject (Object *o);34int lua_markobject (TObject *o);
33int luaI_ismarked (Object *o);35int luaI_ismarked (TObject *o);
34Long luaI_collectgarbage (void);
35void lua_pack (void);36void lua_pack (void);
36 37
37 38
src/tree.c+92−26
@@ -3,17 +3,18 @@ · 2 unmodified lines
3** TecCGraf - PUC-Rio3** TecCGraf - PUC-Rio
4*/4*/
5 5
6char *rcs_tree="$Id: tree.c,v 1.20 1996/03/14 15:56:26 roberto Exp $";6char *rcs_tree="$Id: tree.c,v 1.28 1997/06/11 14:24:40 roberto Exp $";
7 7
8 8
9#include <string.h>9#include <string.h>
10 10
11#include "mem.h"11#include "luamem.h"
12#include "lua.h"12#include "lua.h"
13#include "tree.h"13#include "tree.h"
14#include "lex.h"14#include "lex.h"
15#include "hash.h"15#include "hash.h"
16#include "table.h"16#include "table.h"
17#include "fallback.h"
17 18
18 19
19#define NUM_HASHS 6420#define NUM_HASHS 64
@@ -28,23 +29,43 @@ static int initialized = 0; · 8 unmodified lines
28 29
29static stringtable string_root[NUM_HASHS];30static stringtable string_root[NUM_HASHS];
30 31
31static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 2, {0}};32static TaggedString EMPTY = {LUA_T_STRING, NULL, {{NOT_USED, NOT_USED}},
33 0, 2, {0}};
32 34
33 35
34static unsigned long hash (char *str)36static unsigned long hash (char *s, int tag)
35{37{
36 unsigned long h = 0;38 unsigned long h;
37 while (*str)39 if (tag != LUA_T_STRING)
38 h = ((h<<5)-h)^(unsigned char)*(str++);40 h = (unsigned long)s;
41 else {
42 h = 0;
43 while (*s)
44 h = ((h<<5)-h)^(unsigned char)*(s++);
45 }
39 return h;46 return h;
40}47}
41 48
49
50static void luaI_inittree (void)
51{
52 int i;
53 for (i=0; i<NUM_HASHS; i++) {
54 string_root[i].size = 0;
55 string_root[i].nuse = 0;
56 string_root[i].hash = NULL;
57 }
58}
59
60
42static void initialize (void)61static void initialize (void)
43{62{
44 initialized = 1;63 initialized = 1;
64 luaI_inittree();
45 luaI_addReserved();65 luaI_addReserved();
46 luaI_initsymbol();66 luaI_initsymbol();
47 luaI_initconstant();67 luaI_initconstant();
68 luaI_initfallbacks();
48}69}
49 70
50 71
@@ -58,8 +79,7 @@ static void grow (stringtable *tb) · 7 unmodified lines
58 /* rehash */79 /* rehash */
59 tb->nuse = 0;80 tb->nuse = 0;
60 for (i=0; i<tb->size; i++)81 for (i=0; i<tb->size; i++)
61 if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)82 if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
62 {
63 int h = tb->hash[i]->hash%newsize;83 int h = tb->hash[i]->hash%newsize;
64 while (newhash[h])84 while (newhash[h])
65 h = (h+1)%newsize;85 h = (h+1)%newsize;
@@ -71,10 +91,30 @@ static void grow (stringtable *tb) · 5 unmodified lines
71 tb->hash = newhash;91 tb->hash = newhash;
72}92}
73 93
74static TaggedString *insert (char *str, stringtable *tb)94
95static TaggedString *newone(char *buff, int tag, unsigned long h)
96{
97 TaggedString *ts;
98 if (tag == LUA_T_STRING) {
99 ts = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(buff));
100 strcpy(ts->str, buff);
101 ts->u.s.varindex = ts->u.s.constindex = NOT_USED;
102 ts->tag = LUA_T_STRING;
103 }
104 else {
105 ts = (TaggedString *)luaI_malloc(sizeof(TaggedString));
106 ts->u.v = buff;
107 ts->tag = tag == LUA_ANYTAG ? 0 : tag;
108 }
109 ts->marked = 0;
110 ts->hash = h;
111 return ts;
112}
113
114static TaggedString *insert (char *buff, int tag, stringtable *tb)
75{115{
76 TaggedString *ts;116 TaggedString *ts;
77 unsigned long h = hash(str);117 unsigned long h = hash(buff, tag);
78 int i;118 int i;
79 int j = -1;119 int j = -1;
80 if ((Long)tb->nuse*3 >= (Long)tb->size*2)120 if ((Long)tb->nuse*3 >= (Long)tb->size*2)
@@ -84,12 +124,14 @@ static TaggedString *insert (char *str, stringtable *tb) · 3 unmodified lines
84 grow(tb);124 grow(tb);
85 }125 }
86 i = h%tb->size;126 i = h%tb->size;
87 while (tb->hash[i])127 while ((ts = tb->hash[i]) != NULL)
88 {128 {
89 if (tb->hash[i] == &EMPTY)129 if (ts == &EMPTY)
90 j = i;130 j = i;
91 else if (strcmp(str, tb->hash[i]->str) == 0)131 else if ((ts->tag == LUA_T_STRING) ?
92 return tb->hash[i];132 (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
133 ((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v))
134 return ts;
93 i = (i+1)%tb->size;135 i = (i+1)%tb->size;
94 }136 }
95 /* not found */137 /* not found */
@@ -98,27 +140,49 @@ static TaggedString *insert (char *str, stringtable *tb) · 2 unmodified lines
98 i = j;140 i = j;
99 else141 else
100 tb->nuse++;142 tb->nuse++;
101 ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(str));143 ts = tb->hash[i] = newone(buff, tag, h);
102 strcpy(ts->str, str);
103 ts->marked = 0;
104 ts->hash = h;
105 ts->varindex = ts->constindex = NOT_USED;
106 return ts;144 return ts;
107}145}
108 146
109TaggedString *lua_createstring (char *str) 147TaggedString *luaI_createudata (void *udata, int tag)
110{148{
111 return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]);149 return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]);
150}
151
152TaggedString *lua_createstring (char *str)
153{
154 return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]);
155}
156
157
158void luaI_strcallIM (TaggedString *l)
159{
160 TObject o;
161 ttype(&o) = LUA_T_USERDATA;
162 for (; l; l=l->next) {
163 tsvalue(&o) = l;
164 luaI_gcIM(&o);
165 }
166}
167
168
169void luaI_strfree (TaggedString *l)
170{
171 while (l) {
172 TaggedString *next = l->next;
173 luaI_free(l);
174 l = next;
175 }
112}176}
113 177
114 178
115/*179/*
116** Garbage collection function.180** Garbage collection function.
117** This function traverse the string list freeing unindexed strings
118*/181*/
119Long lua_strcollector (void)182TaggedString *luaI_strcollector (long *acum)
120{183{
121 Long counter = 0;184 Long counter = 0;
185 TaggedString *frees = NULL;
122 int i;186 int i;
123 for (i=0; i<NUM_HASHS; i++)187 for (i=0; i<NUM_HASHS; i++)
124 {188 {
@@ -133,13 +197,15 @@ Long lua_strcollector (void) · 8 unmodified lines
133 t->marked = 0;197 t->marked = 0;
134 else198 else
135 {199 {
136 luaI_free(t);200 t->next = frees;
201 frees = t;
137 tb->hash[j] = &EMPTY;202 tb->hash[j] = &EMPTY;
138 counter++;203 counter++;
139 }204 }
140 }205 }
141 }206 }
142 }207 }
143 return counter;208 *acum += counter;
209 return frees;
144}210}
145 211
src/tree.h+14−4
@@ -1,7 +1,7 @@
1/*1/*
2** tree.h2** tree.h
3** TecCGraf - PUC-Rio3** TecCGraf - PUC-Rio
4** $Id: tree.h,v 1.14 1996/02/26 17:07:49 roberto Exp $4** $Id: tree.h,v 1.18 1997/06/09 17:28:14 roberto Exp $
5*/5*/
6 6
7#ifndef tree_h7#ifndef tree_h
@@ -14,8 +14,15 @@ · 6 unmodified lines
14 14
15typedef struct TaggedString15typedef struct TaggedString
16{16{
17 Word varindex; /* != NOT_USED if this is a symbol */17 int tag; /* if != LUA_T_STRING, this is a userdata */
18 Word constindex; /* != NOT_USED if this is a constant */18 struct TaggedString *next;
19 union {
20 struct {
21 Word varindex; /* != NOT_USED if this is a symbol */
22 Word constindex; /* != NOT_USED if this is a constant */
23 } s;
24 void *v; /* if this is a userdata, here is its value */
25 } u;
19 unsigned long hash; /* 0 if not initialized */26 unsigned long hash; /* 0 if not initialized */
20 int marked; /* for garbage collection; never collect (nor change) if > 1 */27 int marked; /* for garbage collection; never collect (nor change) if > 1 */
21 char str[1]; /* \0 byte already reserved */28 char str[1]; /* \0 byte already reserved */
@@ -23,6 +30,9 @@ typedef struct TaggedString · 1 unmodified line
23 30
24 31
25TaggedString *lua_createstring (char *str);32TaggedString *lua_createstring (char *str);
26Long lua_strcollector (void);33TaggedString *luaI_createudata (void *udata, int tag);
34TaggedString *luaI_strcollector (long *cont);
35void luaI_strfree (TaggedString *l);
36void luaI_strcallIM (TaggedString *l);
27 37
28#endif38#endif
src/undump.c+71−74
@@ -3,33 +3,28 @@ · 2 unmodified lines
3** load bytecodes from files3** load bytecodes from files
4*/4*/
5 5
6char* rcs_undump="$Id: undump.c,v 1.21 1996/11/18 11:18:29 lhf Exp $";6char* rcs_undump="$Id: undump.c,v 1.24 1997/06/17 18:19:17 roberto Exp $";
7 7
8#include <stdio.h>8#include <stdio.h>
9#include <string.h>9#include <string.h>
10#include "auxlib.h"
10#include "opcode.h"11#include "opcode.h"
11#include "mem.h"12#include "luamem.h"
12#include "table.h"13#include "table.h"
13#include "undump.h"14#include "undump.h"
15#include "zio.h"
14 16
15static int swapword=0;17static int swapword=0;
16static int swapfloat=0;18static int swapfloat=0;
17static TFunc* Main=NULL; /* functions in a chunk */19static TFunc* Main=NULL; /* functions in a chunk */
18static TFunc* lastF=NULL;20static TFunc* lastF=NULL;
19 21
20static void warn(char* s) /* TODO: remove */
21{
22#if 0
23 fprintf(stderr,"undump: %s\n",s);
24#endif
25}
26
27static void FixCode(Byte* code, Byte* end) /* swap words */22static void FixCode(Byte* code, Byte* end) /* swap words */
28{23{
29 Byte* p;24 Byte* p;
30 for (p=code; p!=end;)25 for (p=code; p!=end;)
31 {26 {
32 OpCode op=(OpCode)*p;27 int op=*p;
33 switch (op)28 switch (op)
34 {29 {
35 case PUSHNIL:30 case PUSHNIL:
@@ -83,6 +78,8 @@ static void FixCode(Byte* code, Byte* end) /* swap words */ · 47 unmodified lines
83 case STORELIST0:78 case STORELIST0:
84 case ADJUST:79 case ADJUST:
85 case RETCODE:80 case RETCODE:
81 case VARARGS:
82 case STOREMAP:
86 p+=2;83 p+=2;
87 break;84 break;
88 case STORELIST:85 case STORELIST:
@@ -132,7 +129,8 @@ static void FixCode(Byte* code, Byte* end) /* swap words */ · 43 unmodified lines
132 break;129 break;
133 }130 }
134 default:131 default:
135 lua_error("corrupt binary file");132 luaL_verror("corrupt binary file: bad opcode %d at %d\n",
133 op,(int)(p-code));
136 break;134 break;
137 }135 }
138 }136 }
@@ -150,149 +148,152 @@ static void Unthread(Byte* code, int i, int v) · 11 unmodified lines
150 }148 }
151}149}
152 150
153static int LoadWord(FILE* D)151static int LoadWord(ZIO* Z)
154{152{
155 Word w;153 Word w;
156 fread(&w,sizeof(w),1,D);154 zread(Z,&w,sizeof(w));
157 if (swapword)155 if (swapword)
158 {156 {
159 Byte* p=(Byte*)&w; /* TODO: need union? */157 Byte* p=(Byte*)&w;
160 Byte t;158 Byte t;
161 t=p[0]; p[0]=p[1]; p[1]=t;159 t=p[0]; p[0]=p[1]; p[1]=t;
162 }160 }
163 return w;161 return w;
164}162}
165 163
166static int LoadSize(FILE* D)164static int LoadSize(ZIO* Z)
167{165{
168 Word hi=LoadWord(D);166 Word hi=LoadWord(Z);
169 Word lo=LoadWord(D);167 Word lo=LoadWord(Z);
170 int s=(hi<<16)|lo;168 int s=(hi<<16)|lo;
171 if ((Word)s != s) lua_error("code too long");169 if ((Word)s != s) lua_error("code too long");
172 return s;170 return s;
173}171}
174 172
175static void* LoadBlock(int size, FILE* D)173static void* LoadBlock(int size, ZIO* Z)
176{174{
177 void* b=luaI_malloc(size);175 void* b=luaI_malloc(size);
178 fread(b,size,1,D);176 zread(Z,b,size);
179 return b;177 return b;
180}178}
181 179
182static char* LoadString(FILE* D)180static char* LoadString(ZIO* Z)
183{181{
184 int size=LoadWord(D);182 int size=LoadWord(Z);
185 char *b=luaI_buffer(size);183 char *b=luaI_buffer(size);
186 fread(b,size,1,D);184 zread(Z,b,size);
187 return b;185 return b;
188}186}
189 187
190static char* LoadNewString(FILE* D)188static char* LoadNewString(ZIO* Z)
191{189{
192 return LoadBlock(LoadWord(D),D);190 return LoadBlock(LoadWord(Z),Z);
193}191}
194 192
195static void LoadFunction(FILE* D)193static void LoadFunction(ZIO* Z)
196{194{
197 TFunc* tf=new(TFunc);195 TFunc* tf=new(TFunc);
198 tf->next=NULL;196 tf->next=NULL;
199 tf->locvars=NULL;197 tf->locvars=NULL;
200 tf->size=LoadSize(D);198 tf->size=LoadSize(Z);
201 tf->lineDefined=LoadWord(D);199 tf->lineDefined=LoadWord(Z);
202 if (IsMain(tf)) /* new main */200 if (IsMain(tf)) /* new main */
203 {201 {
204 tf->fileName=LoadNewString(D);202 tf->fileName=LoadNewString(Z);
205 Main=lastF=tf;203 Main=lastF=tf;
206 }204 }
207 else /* fix PUSHFUNCTION */205 else /* fix PUSHFUNCTION */
208 {206 {
209 tf->marked=LoadWord(D);207 tf->marked=LoadWord(Z);
210 tf->fileName=Main->fileName;208 tf->fileName=Main->fileName;
211 memcpy(Main->code+tf->marked,&tf,sizeof(tf));209 memcpy(Main->code+tf->marked,&tf,sizeof(tf));
212 lastF=lastF->next=tf;210 lastF=lastF->next=tf;
213 }211 }
214 tf->code=LoadBlock(tf->size,D);212 tf->code=LoadBlock(tf->size,Z);
215 if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);213 if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);
216 while (1) /* unthread */214 while (1) /* unthread */
217 {215 {
218 int c=getc(D);216 int c=zgetc(Z);
219 if (c==ID_VAR) /* global var */217 if (c==ID_VAR) /* global var */
220 {218 {
221 int i=LoadWord(D);219 int i=LoadWord(Z);
222 char* s=LoadString(D);220 char* s=LoadString(Z);
223 int v=luaI_findsymbolbyname(s);221 int v=luaI_findsymbolbyname(s);
224 Unthread(tf->code,i,v);222 Unthread(tf->code,i,v);
225 }223 }
226 else if (c==ID_STR) /* constant string */224 else if (c==ID_STR) /* constant string */
227 {225 {
228 int i=LoadWord(D);226 int i=LoadWord(Z);
229 char* s=LoadString(D);227 char* s=LoadString(Z);
230 int v=luaI_findconstantbyname(s);228 int v=luaI_findconstantbyname(s);
231 Unthread(tf->code,i,v);229 Unthread(tf->code,i,v);
232 }230 }
233 else231 else
234 {232 {
235 ungetc(c,D);233 zungetc(Z);
236 break;234 break;
237 }235 }
238 }236 }
239}237}
240 238
241static void LoadSignature(FILE* D)239static void LoadSignature(ZIO* Z)
242{240{
243 char* s=SIGNATURE;241 char* s=SIGNATURE;
244 while (*s!=0 && getc(D)==*s)242 while (*s!=0 && zgetc(Z)==*s)
245 ++s;243 ++s;
246 if (*s!=0) lua_error("bad signature");244 if (*s!=0) lua_error("cannot load binary file: bad signature");
247}245}
248 246
249static void LoadHeader(FILE* D) /* TODO: error handling */247static void LoadHeader(ZIO* Z)
250{248{
251 Word w,tw=TEST_WORD;249 Word w,tw=TEST_WORD;
252 float f,tf=TEST_FLOAT;250 float f,tf=TEST_FLOAT;
253 int version;251 int version;
254 LoadSignature(D);252 LoadSignature(Z);
255 version=getc(D);253 version=zgetc(Z);
256 if (version>0x23) /* after 2.5 */254 if (version>0x23) /* after 2.5 */
257 {255 {
258 int oldsizeofW=getc(D);256 int oldsizeofW=zgetc(Z);
259 int oldsizeofF=getc(D);257 int oldsizeofF=zgetc(Z);
260 int oldsizeofP=getc(D);258 int oldsizeofP=zgetc(Z);
261 if (oldsizeofW!=2)259 if (oldsizeofW!=2)
262 lua_error("cannot load binary file created on machine with sizeof(Word)!=2");260 luaL_verror(
261 "cannot load binary file created on machine with sizeof(Word)=%d; "
262 "expected 2",oldsizeofW);
263 if (oldsizeofF!=4)263 if (oldsizeofF!=4)
264 lua_error("cannot load binary file created on machine with sizeof(float)!=4. not an IEEE machine?");264 luaL_verror(
265 if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack */265 "cannot load binary file created on machine with sizeof(float)=%d; "
266 lua_error("cannot load binary file: different pointer sizes");266 "expected 4\nnot an IEEE machine?",oldsizeofF);
267 if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack? */
268 luaL_verror(
269 "cannot load binary file created on machine with sizeof(TFunc*)=%d; "
270 "expected %d",oldsizeofP,(int)sizeof(TFunc*));
267 }271 }
268 fread(&w,sizeof(w),1,D); /* test word */272 zread(Z,&w,sizeof(w)); /* test word */
269 if (w!=tw)273 if (w!=tw)
270 {274 {
271 swapword=1;275 swapword=1;
272 warn("different byte order");
273 }276 }
274 fread(&f,sizeof(f),1,D); /* test float */277 zread(Z,&f,sizeof(f)); /* test float */
275 if (f!=tf)278 if (f!=tf)
276 {279 {
277 Byte* p=(Byte*)&f; /* TODO: need union? */280 Byte* p=(Byte*)&f;
278 Byte t;281 Byte t;
279 swapfloat=1;282 swapfloat=1;
280 t=p[0]; p[0]=p[3]; p[3]=t;283 t=p[0]; p[0]=p[3]; p[3]=t;
281 t=p[1]; p[1]=p[2]; p[2]=t;284 t=p[1]; p[1]=p[2]; p[2]=t;
282 if (f!=tf) /* TODO: try another perm? */285 if (f!=tf) /* TODO: try another perm? */
283 lua_error("different float representation");286 lua_error("cannot load binary file: unknown float representation");
284 else
285 warn("different byte order in floats");
286 }287 }
287}288}
288 289
289static void LoadChunk(FILE* D)290static void LoadChunk(ZIO* Z)
290{291{
291 LoadHeader(D);292 LoadHeader(Z);
292 while (1)293 while (1)
293 {294 {
294 int c=getc(D);295 int c=zgetc(Z);
295 if (c==ID_FUN) LoadFunction(D); else { ungetc(c,D); break; }296 if (c==ID_FUN) LoadFunction(Z); else { zungetc(Z); break; }
296 }297 }
297}298}
298 299
@@ -300,30 +301,26 @@ static void LoadChunk(FILE* D) · 1 unmodified line
300** load one chunk from a file.301** load one chunk from a file.
301** return list of functions found, headed by main, or NULL at EOF.302** return list of functions found, headed by main, or NULL at EOF.
302*/303*/
303TFunc* luaI_undump1(FILE* D)304TFunc* luaI_undump1(ZIO* Z)
304{305{
305 while (1)306 int c=zgetc(Z);
307 if (c==ID_CHUNK)
306 {308 {
307 int c=getc(D);309 LoadChunk(Z);
308 if (c==ID_CHUNK)310 return Main;
309 {
310 LoadChunk(D);
311 return Main;
312 }
313 else if (c==EOF)
314 return NULL;
315 else
316 lua_error("not a lua binary file");
317 }311 }
312 else if (c!=EOZ)
313 lua_error("not a lua binary file");
314 return NULL;
318}315}
319 316
320/*317/*
321** load and run all chunks in a file318** load and run all chunks in a file
322*/319*/
323int luaI_undump(FILE* D)320int luaI_undump(ZIO* Z)
324{321{
325 TFunc* m;322 TFunc* m;
326 while ((m=luaI_undump1(D)))323 while ((m=luaI_undump1(Z)))
327 {324 {
328 int status=luaI_dorun(m);325 int status=luaI_dorun(m);
329 luaI_freefunc(m);326 luaI_freefunc(m);
src/undump.h+11−4
@@ -1,10 +1,14 @@
1/*1/*
2** undump.h2** undump.h
3** definitions for lua decompiler3** definitions for lua decompiler
4** $Id: undump.h,v 1.3 1996/11/14 11:44:34 lhf Exp $4** $Id: undump.h,v 1.6 1997/06/17 18:19:17 roberto Exp $
5*/5*/
6 6
7#ifndef undump_h
8#define undump_h
9
7#include "func.h"10#include "func.h"
11#include "zio.h"
8 12
9#define IsMain(f) (f->lineDefined==0)13#define IsMain(f) (f->lineDefined==0)
10 14
@@ -15,9 +19,12 @@ · 4 unmodified lines
15#define ID_VAR 'V'19#define ID_VAR 'V'
16#define ID_STR 'S'20#define ID_STR 'S'
17#define SIGNATURE "Lua"21#define SIGNATURE "Lua"
18#define VERSION 0x25 /* 2.5 */22#define VERSION 0x25 /* last format change was in 2.5 */
19#define TEST_WORD 0x1234 /* a word for testing byte ordering */23#define TEST_WORD 0x1234 /* a word for testing byte ordering */
20#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */24#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */
21 25
22TFunc* luaI_undump1(FILE* D); /* load one chunk */26
23int luaI_undump(FILE* D); /* load all chunks */27TFunc* luaI_undump1(ZIO* Z);
28int luaI_undump(ZIO* Z); /* load all chunks */
29
30#endif
src/zio.c+79−0
@@ -0,0 +1,79 @@
1/*
2* zio.c
3* a generic input stream interface
4* $Id: zio.c,v 1.2 1997/06/20 19:25:54 roberto Exp $
5*/
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include "zio.h"
11
12
13/* ----------------------------------------------------- memory buffers --- */
14
15static int zmfilbuf(ZIO* z)
16{
17 return EOZ;
18}
19
20ZIO* zmopen(ZIO* z, char* b, int size)
21{
22 if (b==NULL) return NULL;
23 z->n=size;
24 z->p= (unsigned char *)b;
25 z->filbuf=zmfilbuf;
26 z->u=NULL;
27 return z;
28}
29
30/* ------------------------------------------------------------ strings --- */
31
32ZIO* zsopen(ZIO* z, char* s)
33{
34 if (s==NULL) return NULL;
35 return zmopen(z,s,strlen(s));
36}
37
38/* -------------------------------------------------------------- FILEs --- */
39
40static int zffilbuf(ZIO* z)
41{
42 int n=fread(z->buffer,1,ZBSIZE,z->u);
43 if (n==0) return EOZ;
44 z->n=n-1;
45 z->p=z->buffer;
46 return *(z->p++);
47}
48
49
50ZIO* zFopen(ZIO* z, FILE* f)
51{
52 if (f==NULL) return NULL;
53 z->n=0;
54 z->p=z->buffer;
55 z->filbuf=zffilbuf;
56 z->u=f;
57 return z;
58}
59
60
61/* --------------------------------------------------------------- read --- */
62int zread(ZIO *z, void *b, int n)
63{
64 while (n) {
65 int m;
66 if (z->n == 0) {
67 if (z->filbuf(z) == EOZ)
68 return n; /* retorna quantos faltaram ler */
69 zungetc(z); /* poe o resultado de filbuf no buffer */
70 }
71 m = (n <= z->n) ? n : z->n; /* minimo de n e z->n */
72 memcpy(b, z->p, m);
73 z->n -= m;
74 z->p += m;
75 b = (char *)b + m;
76 n -= m;
77 }
78 return 0;
79}
src/zio.h+48−0
@@ -0,0 +1,48 @@
1/*
2* zio.h
3* a generic input stream interface
4* $Id: zio.h,v 1.5 1997/06/20 19:25:54 roberto Exp $
5*/
6
7#ifndef zio_h
8#define zio_h
9
10#include <stdio.h>
11
12
13
14/* For Lua only */
15#define zFopen luaZ_Fopen
16#define zsopen luaZ_sopen
17#define zmopen luaZ_mopen
18#define zread luaZ_read
19
20#define EOZ (-1) /* end of stream */
21
22typedef struct zio ZIO;
23
24ZIO* zFopen(ZIO* z, FILE* f); /* open FILEs */
25ZIO* zsopen(ZIO* z, char* s); /* string */
26ZIO* zmopen(ZIO* z, char* b, int size); /* memory */
27
28int zread(ZIO* z, void* b, int n); /* read next n bytes */
29
30#define zgetc(z) (--(z)->n>=0 ? ((int)*(z)->p++): (z)->filbuf(z))
31#define zungetc(z) (++(z)->n,--(z)->p)
32
33
34
35/* --------- Private Part ------------------ */
36
37#define ZBSIZE 256 /* buffer size */
38
39struct zio {
40 int n; /* bytes still unread */
41 unsigned char* p; /* current position in buffer */
42 int (*filbuf)(ZIO* z);
43 void* u; /* additional data */
44 unsigned char buffer[ZBSIZE]; /* buffer */
45};
46
47
48#endif