History

Lua 2.4

Lua Team committed 30 years ago

Commit ff7c16d1 parent 0a2b61162 files · +6886 −4759Browse files

Showing the changes to src (32 of 62 files). Show all

Unified · Split

32 files
  1. src/Makefile +47 −21
  2. src/fallback.c +53 −39
  3. src/fallback.h +6 −3
  4. src/func.c +80 −3
  5. src/func.h +22 −2
  6. src/hash.c +29 −41
  7. src/hash.h +8 −7
  8. src/inout.c +78 −52
  9. src/inout.h +8 −4
  10. src/lex.c +30 −61
  11. src/lex.h +19 −0
  12. src/lua.stx +47 −273
  13. src/luac/Makefile +24 −0
  14. src/luac/dump.c +247 −0
  15. src/luac/luac.c +104 −0
  16. src/luac/luac.h +24 −0
  17. src/luac/print.c +189 −0
  18. src/luac/print.h +76 −0
  19. src/mem.c +34 −11
  20. src/mem.h +18 −4
  21. src/opcode.c +202 −89
  22. src/opcode.h +6 −20
  23. src/parser.c +439 −663
  24. src/parser.h +3 −2
  25. src/table.c +94 −97
  26. src/table.h +17 −8
  27. src/tree.c +100 −78
  28. src/tree.h +4 −13
  29. src/types.h +1 −3
  30. src/ugly.h +0 −36
  31. src/undump.c +326 −0
  32. src/undump.h +23 −0
src/Makefile+47−21
@@ -1,40 +1,66 @@
1−# makefile for lua distribution
1+# makefile for lua distribution (main library)
22
3−INC= $(LUA)/include
4−LIB= $(LUA)/lib
3+LUA= ..
54
6−# in SunOs /usr/5include contains prototypes for standard lib
7−INCS= -I/usr/5include -I$(INC)
8−WARN= -Wall -Wmissing-prototypes -Wshadow -ansi
5+include $(LUA)/config
96
10−CC= gcc
11−CFLAGS= $(INCS) $(DEFS) $(WARN) -O2
7+OBJS= fallback.o\
8+ func.o\
9+ hash.o\
10+ inout.o\
11+ lex.o\
12+ mem.o\
13+ opcode.o\
14+ parser.o\
15+ table.o\
16+ tree.o\
17+ undump.o
1218
13−OBJS= fallback.o func.o hash.o inout.o lex.o mem.o opcode.o parser.o table.o tree.o
14−SRCS= fallback.c func.c hash.c inout.c lex.c mem.c opcode.c table.c tree.c fallback.h func.h hash.h inout.h mem.h opcode.h table.h tree.h types.h ugly.h lua.stx
19+SRCS= fallback.c\
20+ fallback.h\
21+ func.c\
22+ func.h\
23+ hash.c\
24+ hash.h\
25+ inout.c\
26+ inout.h\
27+ lex.c\
28+ lex.h\
29+ mem.c\
30+ mem.h\
31+ opcode.c\
32+ opcode.h\
33+ parser.c\
34+ parser.h\
35+ table.c\
36+ table.h\
37+ tree.c\
38+ tree.h\
39+ types.h\
40+ undump.c\
41+ undump.h\
42+ lua.stx
1543
1644 SLIB= $(LIB)/liblua.a
1745
18−# dynamic libraries only work for SunOs
19−DLIB= $(LIB)/liblua.so.$(VERSION)
20−
2146 all: $(SLIB)
2247
23−dynamic: $(DLIB)
24−
2548 $(SLIB): $(OBJS)
2649 ar rcuv $@ $(OBJS)
2750 ranlib $@
2851
29−$(DLIB): $(OBJS)
30− ld -o $@ $(OBJS)
31−
3252 clean:
33− rm -f $(OBJS) $(SLIB) $(DLIB)
53+ rm -f $(OBJS) $(SLIB)
3454
3555 co:
3656 co -f -M $(SRCS)
37− yacc -d lua.stx ; mv -f y.tab.c parser.c ; mv -f y.tab.h parser.h
57+
58+parser:
59+ yacc -d lua.stx
60+ rm -f parser.c parser.h
61+ sed -e 's/yy/luaY_/g' y.tab.c > parser.c
62+ sed -e 's/yy/luaY_/g' y.tab.h > parser.h
63+ rm y.tab.c y.tab.h
3864
3965 klean: clean
40− rm -f $(SRCS) parser.c parser.h
66+ rm -f $(SRCS)
src/fallback.c+53−39
@@ -3,7 +3,7 @@ · 2 unmodified lines
33 ** TecCGraf - PUC-Rio
44 */
55
6−char *rcs_fallback="$Id: fallback.c,v 1.17 1995/10/17 14:30:05 roberto Exp $";
6+char *rcs_fallback="$Id: fallback.c,v 1.25 1996/04/25 14:10:00 roberto Exp $";
77
88 #include <stdio.h>
99 #include <string.h>
@@ -12,6 +12,7 @@ char *rcs_fallback="$Id: fallback.c,v 1.17 1995/10/17 14:30:05 roberto Exp $"; · 2 unmodified lines
1212 #include "fallback.h"
1313 #include "opcode.h"
1414 #include "lua.h"
15+#include "table.h"
1516
1617
1718 static void errorFB (void);
@@ -36,8 +37,10 @@ struct FB luaI_fallBacks[] = { · 18 unmodified lines
3637 {"concat", {LUA_T_CFUNCTION, {concatFB}}, 2, 1},
3738 {"settable", {LUA_T_CFUNCTION, {gettableFB}}, 3, 0},
3839 {"gc", {LUA_T_CFUNCTION, {GDFB}}, 1, 0},
39−{"function", {LUA_T_CFUNCTION, {funcFB}}, -1, -1}
40+{"function", {LUA_T_CFUNCTION, {funcFB}}, -1, -1},
4041 /* no fixed number of params or results */
42+{"getglobal", {LUA_T_CFUNCTION, {indexFB}}, 1, 1}
43+ /* same default behavior of index FB */
4144 };
4245
4346 #define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
@@ -47,7 +50,7 @@ void luaI_setfallback (void) · 3 unmodified lines
4750 int i;
4851 char *name = lua_getstring(lua_getparam(1));
4952 lua_Object func = lua_getparam(2);
50− if (name == NULL || !(lua_isfunction(func) || lua_iscfunction(func)))
53+ if (name == NULL || !lua_isfunction(func))
5154 lua_error("incorrect argument to function `setfallback'");
5255 for (i=0; i<N_FB; i++)
5356 {
@@ -110,66 +113,77 @@ static void funcFB (void) · 56 unmodified lines
110113
111114
112115 /*
113−** Lock routines
116+** Reference routines
114117 */
115118
116−static Object *lockArray = NULL;
117−static Word lockSize = 0;
119+static struct ref {
120+ Object o;
121+ enum {LOCK, HOLD, FREE, COLLECTED} status;
122+} *refArray = NULL;
123+static int refSize = 0;
118124
119−int luaI_lock (Object *object)
125+int luaI_ref (Object *object, int lock)
120126 {
121− Word i;
122− Word oldSize;
127+ int i;
128+ int oldSize;
123129 if (tag(object) == LUA_T_NIL)
124− return -1;
125− for (i=0; i<lockSize; i++)
126− if (tag(&lockArray[i]) == LUA_T_NIL)
127− {
128− lockArray[i] = *object;
129− return i;
130− }
130+ return -1; /* special ref for nil */
131+ for (i=0; i<refSize; i++)
132+ if (refArray[i].status == FREE)
133+ goto found;
131134 /* no more empty spaces */
132− oldSize = lockSize;
133− if (lockArray == NULL)
134− {
135− lockSize = 10;
136− lockArray = newvector(lockSize, Object);
137− }
138− else
139− {
140− lockSize = 3*oldSize/2 + 5;
141− lockArray = growvector(lockArray, lockSize, Object);
142− }
143− for (i=oldSize; i<lockSize; i++)
144− tag(&lockArray[i]) = LUA_T_NIL;
145− lockArray[oldSize] = *object;
146− return oldSize;
135+ oldSize = refSize;
136+ refSize = growvector(&refArray, refSize, struct ref, refEM, MAX_WORD);
137+ for (i=oldSize; i<refSize; i++)
138+ refArray[i].status = FREE;
139+ i = oldSize;
140+ found:
141+ refArray[i].o = *object;
142+ refArray[i].status = lock ? LOCK : HOLD;
143+ return i;
147144 }
148145
149146
150−void lua_unlock (int ref)
147+void lua_unref (int ref)
151148 {
152− tag(&lockArray[ref]) = LUA_T_NIL;
149+ if (ref >= 0 && ref < refSize)
150+ refArray[ref].status = FREE;
153151 }
154152
155153
156−Object *luaI_getlocked (int ref)
154+Object *luaI_getref (int ref)
157155 {
158− return &lockArray[ref];
156+ static Object nul = {LUA_T_NIL, {0}};
157+ if (ref == -1)
158+ return &nul;
159+ if (ref >= 0 && ref < refSize &&
160+ (refArray[ref].status == LOCK || refArray[ref].status == HOLD))
161+ return &refArray[ref].o;
162+ else
163+ return NULL;
159164 }
160165
161166
162167 void luaI_travlock (int (*fn)(Object *))
163168 {
164− Word i;
165− for (i=0; i<lockSize; i++)
166− fn(&lockArray[i]);
169+ int i;
170+ for (i=0; i<refSize; i++)
171+ if (refArray[i].status == LOCK)
172+ fn(&refArray[i].o);
167173 }
168174
169175
176+void luaI_invalidaterefs (void)
177+{
178+ int i;
179+ for (i=0; i<refSize; i++)
180+ if (refArray[i].status == HOLD && !luaI_ismarked(&refArray[i].o))
181+ refArray[i].status = COLLECTED;
182+}
183+
170184 char *luaI_travfallbacks (int (*fn)(Object *))
171185 {
172− Word i;
186+ int i;
173187 for (i=0; i<N_FB; i++)
174188 if (fn(&luaI_fallBacks[i].function))
175189 return luaI_fallBacks[i].kind;
src/fallback.h+6−3
@@ -1,10 +1,11 @@
11 /*
2−** $Id: fallback.h,v 1.10 1995/10/17 11:52:38 roberto Exp $
2+** $Id: fallback.h,v 1.13 1996/04/25 14:10:00 roberto Exp $
33 */
44
55 #ifndef fallback_h
66 #define fallback_h
77
8+#include "lua.h"
89 #include "opcode.h"
910
1011 extern struct FB {
@@ -23,11 +24,13 @@ extern struct FB { · 12 unmodified lines
2324 #define FB_SETTABLE 6
2425 #define FB_GC 7
2526 #define FB_FUNCTION 8
27+#define FB_GETGLOBAL 9
2628
2729 void luaI_setfallback (void);
28−int luaI_lock (Object *object);
29−Object *luaI_getlocked (int ref);
30+int luaI_ref (Object *object, int lock);
31+Object *luaI_getref (int ref);
3032 void luaI_travlock (int (*fn)(Object *));
33+void luaI_invalidaterefs (void);
3134 char *luaI_travfallbacks (int (*fn)(Object *));
3235
3336 #endif
src/func.c+80−3
@@ -1,4 +1,4 @@
1−#include <stdio.h>
1+#include <stdlib.h>
22
33 #include "luadebug.h"
44 #include "table.h"
@@ -6,8 +6,26 @@ · 1 unmodified line
66 #include "func.h"
77 #include "opcode.h"
88
9+
910 static TFunc *function_root = NULL;
11+static LocVar *currvars = NULL;
12+static int numcurrvars = 0;
13+static int maxcurrvars = 0;
14+
1015
16+/*
17+** Initialize TFunc struct
18+*/
19+void luaI_initTFunc (TFunc *f)
20+{
21+ f->next = NULL;
22+ f->marked = 0;
23+ f->size = 0;
24+ f->code = NULL;
25+ f->lineDefined = 0;
26+ f->fileName = NULL;
27+ f->locvars = NULL;
28+}
1129
1230 /*
1331 ** Insert function in list for GC
@@ -24,9 +42,10 @@ void luaI_insertfunction (TFunc *f) · 10 unmodified lines
2442 /*
2543 ** Free function
2644 */
27−static void freefunc (TFunc *f)
45+void luaI_freefunc (TFunc *f)
2846 {
2947 luaI_free (f->code);
48+ luaI_free (f->locvars);
3049 luaI_free (f);
3150 }
3251
@@ -48,7 +67,7 @@ Long luaI_funccollector (void) · 15 unmodified lines
4867 function_root = next;
4968 else
5069 prev->next = next;
51− freefunc (curr);
70+ luaI_freefunc (curr);
5271 ++counter;
5372 }
5473 else
@@ -77,3 +96,61 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined) · 22 unmodified lines
7796 }
7897 }
7998
99+/*
100+** Stores information to know that variable has been declared in given line
101+*/
102+void luaI_registerlocalvar (TaggedString *varname, int line)
103+{
104+ if (numcurrvars >= maxcurrvars)
105+ maxcurrvars = growvector(&currvars, maxcurrvars, LocVar, "", MAX_WORD);
106+ currvars[numcurrvars].varname = varname;
107+ currvars[numcurrvars].line = line;
108+ numcurrvars++;
109+}
110+
111+/*
112+** Stores information to know that variable has been out of scope in given line
113+*/
114+void luaI_unregisterlocalvar (int line)
115+{
116+ luaI_registerlocalvar(NULL, line);
117+}
118+
119+/*
120+** Copies "currvars" into a new area and store it in function header.
121+** The values (varname = NULL, line = -1) signal the end of vector.
122+*/
123+void luaI_closelocalvars (TFunc *func)
124+{
125+ func->locvars = newvector (numcurrvars+1, LocVar);
126+ memcpy (func->locvars, currvars, numcurrvars*sizeof(LocVar));
127+ func->locvars[numcurrvars].varname = NULL;
128+ func->locvars[numcurrvars].line = -1;
129+ numcurrvars = 0; /* prepares for next function */
130+}
131+
132+/*
133+** Look for n-esim local variable at line "line" in function "func".
134+** Returns NULL if not found.
135+*/
136+char *luaI_getlocalname (TFunc *func, int local_number, int line)
137+{
138+ int count = 0;
139+ char *varname = NULL;
140+ LocVar *lv = func->locvars;
141+ if (lv == NULL)
142+ return NULL;
143+ for (; lv->line != -1 && lv->line < line; lv++)
144+ {
145+ if (lv->varname) /* register */
146+ {
147+ if (++count == local_number)
148+ varname = lv->varname->str;
149+ }
150+ else /* unregister */
151+ if (--count < local_number)
152+ varname = NULL;
153+ }
154+ return varname;
155+}
156+
src/func.h+22−2
@@ -1,24 +1,44 @@
1+/*
2+** $Id: func.h,v 1.8 1996/03/14 15:54:20 roberto Exp $
3+*/
4+
15 #ifndef func_h
26 #define func_h
37
48 #include "types.h"
59 #include "lua.h"
10+#include "tree.h"
11+
12+typedef struct LocVar
13+{
14+ TaggedString *varname; /* NULL signals end of scope */
15+ int line;
16+} LocVar;
17+
618
719 /*
8−** Header para funcoes.
20+** Function Headers
921 */
1022 typedef struct TFunc
1123 {
1224 struct TFunc *next;
13− char marked;
25+ int marked;
1426 int size;
1527 Byte *code;
1628 int lineDefined;
1729 char *fileName;
30+ LocVar *locvars;
1831 } TFunc;
1932
2033 Long luaI_funccollector (void);
2134 void luaI_insertfunction (TFunc *f);
2235
36+void luaI_initTFunc (TFunc *f);
37+void luaI_freefunc (TFunc *f);
38+
39+void luaI_registerlocalvar (TaggedString *varname, int line);
40+void luaI_unregisterlocalvar (int line);
41+void luaI_closelocalvars (TFunc *func);
42+char *luaI_getlocalname (TFunc *func, int local_number, int line);
2343
2444 #endif
src/hash.c+29−41
@@ -3,9 +3,8 @@ · 2 unmodified lines
33 ** hash manager for lua
44 */
55
6−char *rcs_hash="$Id: hash.c,v 2.26 1995/10/04 14:20:26 roberto Exp $";
6+char *rcs_hash="$Id: hash.c,v 2.30 1996/05/06 14:30:27 roberto Exp $";
77
8−#include <string.h>
98
109 #include "mem.h"
1110 #include "opcode.h"
@@ -13,7 +12,6 @@ char *rcs_hash="$Id: hash.c,v 2.26 1995/10/04 14:20:26 roberto Exp $"; · 1 unmodified line
1312 #include "table.h"
1413 #include "lua.h"
1514
16−#define streq(s1,s2) (s1 == s2 || (*(s1) == *(s2) && strcmp(s1,s2)==0))
1715
1816 #define nhash(t) ((t)->nhash)
1917 #define nuse(t) ((t)->nuse)
@@ -24,22 +22,22 @@ char *rcs_hash="$Id: hash.c,v 2.26 1995/10/04 14:20:26 roberto Exp $"; · 4 unmodified lines
2422 #define val(n) (&(n)->val)
2523
2624
27−#define REHASH_LIMIT 0.70 /* avoid more than this % full */
25+#define REHASH_LIMIT 0.70 /* avoid more than this % full */
2826
2927
3028 static Hash *listhead = NULL;
3129
3230
33−
3431 /* hash dimensions values */
35−static Word dimensions[] =
36− {3, 5, 7, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421,
37− 12853, 25717, 51437, 65521, 0}; /* 65521 == last prime < MAX_WORD */
32+static Long dimensions[] =
33+ {3L, 5L, 7L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
34+ 12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
35+ 1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
3836
39−static Word redimension (Word nhash)
37+int luaI_redimension (int nhash)
4038 {
41− Word i;
42− for (i=0; dimensions[i]!=0; i++)
39+ int i;
40+ for (i=0; dimensions[i]<MAX_INT; i++)
4341 {
4442 if (dimensions[i] > nhash)
4543 return dimensions[i];
@@ -48,7 +46,7 @@ static Word redimension (Word nhash) · 2 unmodified lines
4846 return 0; /* to avoid warnings */
4947 }
5048
51−static Word hashindex (Hash *t, Object *ref) /* hash function */
49+static int hashindex (Hash *t, Object *ref) /* hash function */
5250 {
5351 switch (tag(ref))
5452 {
@@ -56,19 +54,9 @@ static Word hashindex (Hash *t, Object *ref) /* hash function */ · 1 unmodified line
5654 lua_error ("unexpected type to index table");
5755 return -1; /* UNREACHEABLE */
5856 case LUA_T_NUMBER:
59− return (((Word)nvalue(ref))%nhash(t));
57+ return (((int)nvalue(ref))%nhash(t));
6058 case LUA_T_STRING:
61− {
62− unsigned long h = tsvalue(ref)->hash;
63− if (h == 0)
64− {
65− char *name = svalue(ref);
66− while (*name)
67− h = ((h<<5)-h)^(unsigned char)*(name++);
68− tsvalue(ref)->hash = h;
69− }
70− return (Word)h%nhash(t); /* make it a valid index */
71− }
59+ return (int)((tsvalue(ref)->hash)%nhash(t)); /* make it a valid index */
7260 case LUA_T_FUNCTION:
7361 return (((IntPoint)ref->value.tf)%nhash(t));
7462 case LUA_T_CFUNCTION:
@@ -80,14 +68,14 @@ static Word hashindex (Hash *t, Object *ref) /* hash function */ · 5 unmodified lines
8068 }
8169 }
8270
83−Bool lua_equalObj (Object *t1, Object *t2)
71+int lua_equalObj (Object *t1, Object *t2)
8472 {
8573 if (tag(t1) != tag(t2)) return 0;
8674 switch (tag(t1))
8775 {
8876 case LUA_T_NIL: return 1;
8977 case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
90− case LUA_T_STRING: return streq(svalue(t1), svalue(t2));
78+ case LUA_T_STRING: return svalue(t1) == svalue(t2);
9179 case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
9280 case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf;
9381 case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
@@ -95,9 +83,9 @@ Bool lua_equalObj (Object *t1, Object *t2) · 1 unmodified line
9583 }
9684 }
9785
98−static Word present (Hash *t, Object *ref)
86+static int present (Hash *t, Object *ref)
9987 {
100− Word h = hashindex(t, ref);
88+ int h = hashindex(t, ref);
10189 while (tag(ref(node(t, h))) != LUA_T_NIL)
10290 {
10391 if (lua_equalObj(ref, ref(node(t, h))))
@@ -111,9 +99,9 @@ static Word present (Hash *t, Object *ref) · 7 unmodified lines
11199 /*
112100 ** Alloc a vector node
113101 */
114−static Node *hashnodecreate (Word nhash)
102+static Node *hashnodecreate (int nhash)
115103 {
116− Word i;
104+ int i;
117105 Node *v = newvector (nhash, Node);
118106 for (i=0; i<nhash; i++)
119107 tag(ref(&v[i])) = LUA_T_NIL;
@@ -123,10 +111,10 @@ static Node *hashnodecreate (Word nhash) · 3 unmodified lines
123111 /*
124112 ** Create a new hash. Return the hash pointer or NULL on error.
125113 */
126−static Hash *hashcreate (Word nhash)
114+static Hash *hashcreate (int nhash)
127115 {
128116 Hash *t = new(Hash);
129− nhash = redimension((Word)((float)nhash/REHASH_LIMIT));
117+ nhash = luaI_redimension((int)((float)nhash/REHASH_LIMIT));
130118 nodevector(t) = hashnodecreate(nhash);
131119 nhash(t) = nhash;
132120 nuse(t) = 0;
@@ -151,7 +139,7 @@ void lua_hashmark (Hash *h) · 18 unmodified lines
151139 {
152140 if (markarray(h) == 0)
153141 {
154− Word i;
142+ int i;
155143 markarray(h) = 1;
156144 for (i=0; i<nhash(h); i++)
157145 {
@@ -218,7 +206,7 @@ Long lua_hashcollector (void) · 60 unmodified lines
218206 ** executes garbage collection if the number of arrays created
219207 ** exceed a pre-defined range.
220208 */
221−Hash *lua_createarray (Word nhash)
209+Hash *lua_createarray (int nhash)
222210 {
223211 Hash *array;
224212 lua_pack();
@@ -234,10 +222,10 @@ Hash *lua_createarray (Word nhash) · 9 unmodified lines
234222 */
235223 static void rehash (Hash *t)
236224 {
237− Word i;
238− Word nold = nhash(t);
225+ int i;
226+ int nold = nhash(t);
239227 Node *vold = nodevector(t);
240− nhash(t) = redimension(nhash(t));
228+ nhash(t) = luaI_redimension(nhash(t));
241229 nodevector(t) = hashnodecreate(nhash(t));
242230 for (i=0; i<nold; i++)
243231 {
@@ -254,7 +242,7 @@ static void rehash (Hash *t) · 10 unmodified lines
254242 */
255243 Object *lua_hashget (Hash *t, Object *ref)
256244 {
257− Word h = present(t, ref);
245+ int h = present(t, ref);
258246 if (tag(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
259247 else return NULL;
260248 }
@@ -266,7 +254,7 @@ Object *lua_hashget (Hash *t, Object *ref) · 5 unmodified lines
266254 */
267255 Object *lua_hashdefine (Hash *t, Object *ref)
268256 {
269− Word h;
257+ int h;
270258 Node *n;
271259 h = present(t, ref);
272260 n = node(t, h);
@@ -292,7 +280,7 @@ Object *lua_hashdefine (Hash *t, Object *ref) · 19 unmodified lines
292280 ** in the hash.
293281 ** This function pushs the element value and its reference to the stack.
294282 */
295−static void hashnext (Hash *t, Word i)
283+static void hashnext (Hash *t, int i)
296284 {
297285 if (i >= nhash(t))
298286 {
@@ -329,7 +317,7 @@ void lua_next (void) · 30 unmodified lines
329317 }
330318 else
331319 {
332− Word h = present (t, luaI_Address(r));
320+ int h = present (t, luaI_Address(r));
333321 hashnext(t, h+1);
334322 }
335323 }
src/hash.h+8−7
@@ -1,14 +1,14 @@
11 /*
22 ** hash.h
33 ** hash manager for lua
4−** Luiz Henrique de Figueiredo - 17 Aug 90
5−** $Id: hash.h,v 2.8 1995/01/12 14:19:04 roberto Exp $
4+** $Id: hash.h,v 2.12 1996/05/06 14:30:27 roberto Exp $
65 */
76
87 #ifndef hash_h
98 #define hash_h
109
1110 #include "types.h"
11+#include "opcode.h"
1212
1313 typedef struct node
1414 {
@@ -19,15 +19,16 @@ typedef struct node · 4 unmodified lines
1919 typedef struct Hash
2020 {
2121 struct Hash *next;
22− char mark;
23− Word nhash;
24− Word nuse;
2522 Node *node;
23+ int nhash;
24+ int nuse;
25+ char mark;
2626 } Hash;
2727
2828
29−Bool lua_equalObj (Object *t1, Object *t2);
30−Hash *lua_createarray (Word nhash);
29+int lua_equalObj (Object *t1, Object *t2);
30+int luaI_redimension (int nhash);
31+Hash *lua_createarray (int nhash);
3132 void lua_hashmark (Hash *h);
3233 Long lua_hashcollector (void);
3334 Object *lua_hashget (Hash *t, Object *ref);
src/inout.c+78−52
@@ -5,31 +5,21 @@ · 4 unmodified lines
55 ** Also provides some predefined lua functions.
66 */
77
8−char *rcs_inout="$Id: inout.c,v 2.25 1995/10/25 13:05:51 roberto Exp $";
8+char *rcs_inout="$Id: inout.c,v 2.36 1996/03/19 22:28:37 roberto Exp $";
99
1010 #include <stdio.h>
11−#include <stdlib.h>
12−#include <string.h>
1311
14−#include "mem.h"
12+#include "lex.h"
1513 #include "opcode.h"
16−#include "hash.h"
1714 #include "inout.h"
1815 #include "table.h"
1916 #include "tree.h"
2017 #include "lua.h"
21−
22−
23−#ifndef MAXFUNCSTACK
24−#define MAXFUNCSTACK 100
25−#endif
26−
27−#define MAXMESSAGE MAXFUNCSTACK*80
18+#include "mem.h"
2819
2920
3021 /* Exported variables */
3122 Word lua_linenumber;
32−Bool lua_debug = 0;
3323 char *lua_parsedfile;
3424
3525
@@ -54,9 +44,9 @@ static int stringinput (void) · 18 unmodified lines
5444
5545 /*
5646 ** Function to open a file to be input unit.
57−** Return 0 on success or error message on error.
47+** Return the file.
5848 */
59−char *lua_openfile (char *fn)
49+FILE *lua_openfile (char *fn)
6050 {
6151 lua_setinput (fileinput);
6252 if (fn == NULL)
@@ -67,14 +57,10 @@ char *lua_openfile (char *fn) · 4 unmodified lines
6757 else
6858 fp = fopen (fn, "r");
6959 if (fp == NULL)
70− {
71− static char buff[255];
72− sprintf(buff, "unable to open file `%.200s'", fn);
73− return buff;
74− }
60+ return NULL;
7561 lua_linenumber = 1;
76− lua_parsedfile = lua_constcreate(fn)->ts.str;
77− return NULL;
62+ lua_parsedfile = luaI_createfixedstring(fn)->str;
63+ return fp;
7864 }
7965
8066 /*
@@ -97,7 +83,7 @@ void lua_openstring (char *s) · 16 unmodified lines
9783 lua_setinput (stringinput);
9884 st = s;
9985 lua_linenumber = 1;
100− lua_parsedfile = lua_constcreate("(string)")->ts.str;
86+ lua_parsedfile = luaI_createfixedstring("(string)")->str;
10187 }
10288
10389 /*
@@ -126,33 +112,57 @@ void lua_internaldostring (void) · 22 unmodified lines
126112 void lua_internaldofile (void)
127113 {
128114 lua_Object obj = lua_getparam (1);
129− if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj)))
115+ char *fname = NULL;
116+ if (lua_isstring(obj))
117+ fname = lua_getstring(obj);
118+ else if (obj != LUA_NOOBJECT)
119+ lua_error("invalid argument to function `dofile'");
120+ /* else fname = NULL */
121+ if (!lua_dofile(fname))
130122 lua_pushnumber(1);
131123 else
132124 lua_pushnil();
133125 }
134126
135−/*
136−** Internal function: print object values
137−*/
138−void lua_print (void)
127+
128+static char *tostring (lua_Object obj)
139129 {
140− int i=1;
141− lua_Object obj;
142− while ((obj=lua_getparam (i++)) != LUA_NOOBJECT)
143− {
144− if (lua_isnumber(obj)) printf("%g\n",lua_getnumber(obj));
145− else if (lua_isstring(obj)) printf("%s\n",lua_getstring(obj));
146− else if (lua_isfunction(obj)) printf("function: %p\n",(luaI_Address(obj))->value.tf);
147− else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj)
148−);
149− else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata(obj));
150− else if (lua_istable(obj)) printf("table: %p\n",avalue(luaI_Address(obj)));
151− else if (lua_isnil(obj)) printf("nil\n");
152− else printf("invalid value to print\n");
153− }
130+ char *buff = luaI_buffer(20);
131+ if (lua_isstring(obj)) /* get strings and numbers */
132+ return lua_getstring(obj);
133+ else switch(lua_type(obj))
134+ {
135+ case LUA_T_FUNCTION:
136+ sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);
137+ break;
138+ case LUA_T_CFUNCTION:
139+ sprintf(buff, "cfunction: %p", lua_getcfunction(obj));
140+ break;
141+ case LUA_T_ARRAY:
142+ sprintf(buff, "table: %p", avalue(luaI_Address(obj)));
143+ break;
144+ case LUA_T_NIL:
145+ sprintf(buff, "nil");
146+ break;
147+ default:
148+ sprintf(buff, "userdata: %p", lua_getuserdata(obj));
149+ break;
150+ }
151+ return buff;
154152 }
155153
154+void luaI_tostring (void)
155+{
156+ lua_pushstring(tostring(lua_getparam(1)));
157+}
158+
159+void luaI_print (void)
160+{
161+ int i = 1;
162+ lua_Object obj;
163+ while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
164+ printf("%s\n", tostring(obj));
165+}
156166
157167 /*
158168 ** Internal function: return an object type.
@@ -196,16 +206,7 @@ void lua_obj2number (void) · 37 unmodified lines
196206 {
197207 lua_Object o = lua_getparam(1);
198208 if (lua_isnumber(o))
199− lua_pushobject(o);
200− else if (lua_isstring(o))
201− {
202− char c;
203− float f;
204− if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1)
205− lua_pushnumber(f);
206− else
207− lua_pushnil();
208− }
209+ lua_pushnumber(lua_getnumber(o));
209210 else
210211 lua_pushnil();
211212 }
@@ -218,3 +219,28 @@ void luaI_error (void) · 6 unmodified lines
218219 lua_error(s);
219220 }
220221
222+void luaI_assert (void)
223+{
224+ lua_Object p = lua_getparam(1);
225+ if (p == LUA_NOOBJECT || lua_isnil(p))
226+ lua_error("assertion failed!");
227+}
228+
229+void luaI_setglobal (void)
230+{
231+ lua_Object name = lua_getparam(1);
232+ lua_Object value = lua_getparam(2);
233+ if (!lua_isstring(name))
234+ lua_error("incorrect argument to function `setglobal'");
235+ lua_pushobject(value);
236+ lua_storeglobal(lua_getstring(name));
237+ lua_pushobject(value); /* return given value */
238+}
239+
240+void luaI_getglobal (void)
241+{
242+ lua_Object name = lua_getparam(1);
243+ if (!lua_isstring(name))
244+ lua_error("incorrect argument to function `getglobal'");
245+ lua_pushobject(lua_getglobal(lua_getstring(name)));
246+}
src/inout.h+8−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: inout.h,v 1.10 1995/10/17 11:58:41 roberto Exp $
2+** $Id: inout.h,v 1.15 1996/03/15 18:21:58 roberto Exp $
33 */
44
55
@@ -7,23 +7,27 @@ · 1 unmodified line
77 #define inout_h
88
99 #include "types.h"
10+#include <stdio.h>
1011
1112
1213 extern Word lua_linenumber;
13−extern Bool lua_debug;
1414 extern Word lua_debugline;
1515 extern char *lua_parsedfile;
1616
17−char *lua_openfile (char *fn);
17+FILE *lua_openfile (char *fn);
1818 void lua_closefile (void);
1919 void lua_openstring (char *s);
2020 void lua_closestring (void);
2121
2222 void lua_internaldofile (void);
2323 void lua_internaldostring (void);
24−void lua_print (void);
24+void luaI_tostring (void);
25+void luaI_print (void);
2526 void luaI_type (void);
2627 void lua_obj2number (void);
2728 void luaI_error (void);
29+void luaI_assert (void);
30+void luaI_setglobal (void);
31+void luaI_getglobal (void);
2832
2933 #endif
src/lex.c+30−61
@@ -1,23 +1,19 @@
1−char *rcs_lex = "$Id: lex.c,v 2.21 1995/11/16 20:46:24 roberto Exp $";
1+char *rcs_lex = "$Id: lex.c,v 2.32 1996/03/21 16:33:47 roberto Exp $";
22
33
44 #include <ctype.h>
5−#include <stdio.h>
6−#include <stdlib.h>
75 #include <string.h>
86
97 #include "mem.h"
108 #include "tree.h"
119 #include "table.h"
12−#include "opcode.h"
10+#include "lex.h"
1311 #include "inout.h"
12+#include "luadebug.h"
1413 #include "parser.h"
15−#include "ugly.h"
1614
1715 #define MINBUFF 260
1816
19−#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
20−
2117 #define next() { current = input(); }
2218 #define save(x) { *yytextLast++ = (x); }
2319 #define save_and_next() { save(current); next(); }
@@ -47,7 +43,6 @@ char *lua_lasttext (void) · 23 unmodified lines
4743 }
4844
4945
50−/* The reserved words must be listed in lexicographic order */
5146 static struct
5247 {
5348 char *name;
@@ -74,30 +69,21 @@ static struct · 20 unmodified lines
7469 #define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
7570
7671
77−static int findReserved (char *name)
72+void luaI_addReserved (void)
7873 {
79− int l = 0;
80− int h = RESERVEDSIZE - 1;
81− while (l <= h)
74+ int i;
75+ for (i=0; i<RESERVEDSIZE; i++)
8276 {
83− int m = (l+h)/2;
84− int comp = lua_strcmp(name, reserved[m].name);
85− if (comp < 0)
86− h = m-1;
87− else if (comp == 0)
88− return reserved[m].token;
89− else
90− l = m+1;
77+ TaggedString *ts = lua_createstring(reserved[i].name);
78+ ts->marked = reserved[i].token; /* reserved word (always > 255) */
9179 }
92− return 0;
9380 }
9481
9582
9683 static void growtext (void)
9784 {
9885 int size = yytextLast - yytext;
99− textsize *= 2;
100− yytext = growvector(yytext, textsize, char);
86+ textsize = growvector(&yytext, textsize, char, lexEM, MAX_WORD);
10187 yytextLast = yytext + size;
10288 }
10389
@@ -144,9 +130,9 @@ static int read_long_string (void) · 40 unmodified lines
144130 }
145131
146132
147−int yylex (void)
133+int luaY_lex (void)
148134 {
149− float a;
135+ double a;
150136 static int linelasttoken = 0;
151137 if (lua_debug)
152138 luaI_codedebugline(linelasttoken);
@@ -154,9 +140,6 @@ int yylex (void) · 1 unmodified line
154140 while (1)
155141 {
156142 yytextLast = yytext;
157−#if 0
158− fprintf(stderr,"'%c' %d\n",current,current);
159−#endif
160143 switch (current)
161144 {
162145 case EOF:
@@ -174,22 +157,21 @@ int yylex (void) · 11 unmodified lines
174157 while (isalnum(current) || current == '_')
175158 save_and_next();
176159 *yytextLast = 0;
177− if (lua_strcmp(yytext, "debug") == 0)
160+ if (strcmp(yytext, "debug") == 0)
178161 {
179− yylval.vInt = 1;
162+ luaY_lval.vInt = 1;
180163 return DEBUG;
181164 }
182− else if (lua_strcmp(yytext, "nodebug") == 0)
165+ else if (strcmp(yytext, "nodebug") == 0)
183166 {
184− yylval.vInt = 0;
167+ luaY_lval.vInt = 0;
185168 return DEBUG;
186169 }
187170 return WRONGTOKEN;
188171
189172 case '-':
190173 save_and_next();
191− if (current != '-') return '-'; /* else goes through */
192− case '#':
174+ if (current != '-') return '-';
193175 do { next(); } while (current != '\n' && current != 0);
194176 continue;
195177
@@ -203,7 +185,7 @@ int yylex (void) · 7 unmodified lines
203185 return WRONGTOKEN;
204186 save_and_next(); /* pass the second ']' */
205187 *(yytextLast-2) = 0; /* erases ']]' */
206− yylval.vWord = luaI_findconstantbyname(yytext+2);
188+ luaY_lval.vWord = luaI_findconstantbyname(yytext+2);
207189 return STRING;
208190 }
209191
@@ -254,6 +236,7 @@ int yylex (void) · 44 unmodified lines
254236 case 'n': save('\n'); next(); break;
255237 case 't': save('\t'); next(); break;
256238 case 'r': save('\r'); next(); break;
239+ case '\n': lua_linenumber++; /* goes through */
257240 default : save(current); next(); break;
258241 }
259242 break;
@@ -263,7 +246,7 @@ int yylex (void) · 3 unmodified lines
263246 }
264247 next(); /* skip the delimiter */
265248 *yytextLast = 0;
266− yylval.vWord = luaI_findconstantbyname(yytext);
249+ luaY_lval.vWord = luaI_findconstantbyname(yytext);
267250 return STRING;
268251 }
269252
@@ -281,12 +264,14 @@ int yylex (void) · 11 unmodified lines
281264 case 'Z':
282265 case '_':
283266 {
284− Word res;
267+ TaggedString *ts;
285268 do { save_and_next(); } while (isalnum(current) || current == '_');
286269 *yytextLast = 0;
287− res = findReserved(yytext);
288− if (res) return res;
289− yylval.pNode = lua_constcreate(yytext);
270+ ts = lua_createstring(yytext);
271+ if (ts->marked > 2)
272+ return ts->marked; /* reserved word */
273+ luaY_lval.pTStr = ts;
274+ ts->marked = 2; /* avoid GC */
290275 return NAME;
291276 }
292277
@@ -305,48 +290,32 @@ int yylex (void) · 12 unmodified lines
305290 case '0': case '1': case '2': case '3': case '4':
306291 case '5': case '6': case '7': case '8': case '9':
307292 a=0.0;
308− do { a=10*a+current-'0'; save_and_next(); } while (isdigit(current));
293+ do { a=10.0*a+(current-'0'); save_and_next(); } while (isdigit(current));
309294 if (current == '.') save_and_next();
310295 fraction:
311− { float da=0.1;
296+ { double da=0.1;
312297 while (isdigit(current))
313298 {a+=(current-'0')*da; da/=10.0; save_and_next()};
314299 if (current == 'e' || current == 'E')
315300 {
316301 int e=0;
317302 int neg;
318− float ea;
303+ double ea;
319304 save_and_next();
320305 neg=(current=='-');
321306 if (current == '+' || current == '-') save_and_next();
322307 if (!isdigit(current)) return WRONGTOKEN;
323− do { e=10*e+current-'0'; save_and_next(); } while (isdigit(current));
308+ do { e=10.0*e+(current-'0'); save_and_next(); } while (isdigit(current));
324309 for (ea=neg?0.1:10.0; e>0; e>>=1)
325310 {
326311 if (e & 1) a*=ea;
327312 ea*=ea;
328313 }
329314 }
330− yylval.vFloat = a;
315+ luaY_lval.vFloat = a;
331316 return NUMBER;
332317 }
333318
334− case U_and: case U_do: case U_else: case U_elseif: case U_end:
335− case U_function: case U_if: case U_local: case U_nil: case U_not:
336− case U_or: case U_repeat: case U_return: case U_then:
337− case U_until: case U_while:
338− {
339− int old = current;
340− next();
341− return reserved[old-U_and].token;
342− }
343−
344− case U_eq: next(); return EQ;
345− case U_le: next(); return LE;
346− case U_ge: next(); return GE;
347− case U_ne: next(); return NE;
348− case U_sc: next(); return CONC;
349−
350319 default: /* also end of file */
351320 {
352321 save_and_next();
src/lex.h+19−0
@@ -0,0 +1,19 @@
1+/*
2+** lex.h
3+** TecCGraf - PUC-Rio
4+** $Id: lex.h,v 1.2 1996/02/14 13:35:51 roberto Exp $
5+*/
6+
7+#ifndef lex_h
8+#define lex_h
9+
10+
11+typedef int (*Input) (void);
12+
13+void lua_setinput (Input fn);
14+char *lua_lasttext (void);
15+int luaY_lex (void);
16+void luaI_addReserved (void);
17+
18+
19+#endif
src/lua.stx+47−273
@@ -1,12 +1,13 @@
11 %{
22
3−char *rcs_luastx = "$Id: lua.stx,v 3.25 1995/10/26 17:02:50 roberto Exp $";
3+char *rcs_luastx = "$Id: lua.stx,v 3.36 1996/03/21 16:31:32 roberto Exp $";
44
55 #include <stdio.h>
66 #include <stdlib.h>
7−#include <string.h>
87
8+#include "luadebug.h"
99 #include "mem.h"
10+#include "lex.h"
1011 #include "opcode.h"
1112 #include "hash.h"
1213 #include "inout.h"
@@ -30,7 +31,7 @@ int yyparse (void); · 17 unmodified lines
3031 #endif
3132 static int maxcode;
3233 static int maxmain;
33−static Long maxcurr; /* to allow maxcurr *= 2 without overflow */
34+static int maxcurr;
3435 static Byte *funcCode = NULL;
3536 static Byte **initcode;
3637 static Byte *basepc;
@@ -44,35 +45,32 @@ static Long varbuffer[MAXVAR]; /* variables in an assignment list; · 7 unmodified lines
4445 static int nvarbuffer=0; /* number of variables at a list */
4546
4647 #define MAXLOCALS 32
47−static Word localvar[MAXLOCALS]; /* store local variable names */
48+static TaggedString *localvar[MAXLOCALS]; /* store local variable names */
4849 static int nlocalvar=0; /* number of local variables */
4950
5051 #define MAXFIELDS FIELDS_PER_FLUSH*2
5152 static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
5253 static int nfields=0;
5354
55+int lua_debug = 0;
5456
5557 /* Internal functions */
5658
5759 static void yyerror (char *s)
5860 {
59− static char msg[256];
60− sprintf (msg,"%s near \"%s\" at line %d in file `%s'",
61− s, lua_lasttext (), lua_linenumber, lua_parsedfile);
61+ char msg[256];
62+ char *token = lua_lasttext();
63+ if (token[0] == 0)
64+ token = "<eof>";
65+ sprintf (msg,"%s; last token read: \"%s\" at line %d in file `%s'",
66+ s, token, lua_linenumber, lua_parsedfile);
6267 lua_error (msg);
6368 }
6469
6570 static void code_byte (Byte c)
6671 {
6772 if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */
68− {
69− if (maxcurr >= MAX_INT)
70− lua_error("code size overflow");
71− maxcurr *= 2;
72− if (maxcurr >= MAX_INT)
73− maxcurr = MAX_INT;
74− basepc = growvector(basepc, maxcurr, Byte);
75− }
73+ maxcurr = growvector(&basepc, maxcurr, Byte, codeEM, MAX_INT);
7674 basepc[pc++] = c;
7775 }
7876
@@ -104,10 +102,12 @@ static void code_code (TFunc *tf) · 25 unmodified lines
104102 code_byte(code.m.c4);
105103 }
106104
107−static void code_word_at (Byte *p, Word n)
105+static void code_word_at (Byte *p, int n)
108106 {
109107 CodeWord code;
110− code.w = n;
108+ if ((Word)n != n)
109+ yyerror("block too big");
110+ code.w = (Word)n;
111111 *p++ = code.m.c1;
112112 *p++ = code.m.c2;
113113 }
@@ -117,7 +117,7 @@ static void push_field (Word name) · 3 unmodified lines
117117 if (nfields < MAXFIELDS)
118118 fields[nfields++] = name;
119119 else
120− lua_error ("too many fields in nested constructors");
120+ yyerror ("too many fields in nested constructors");
121121 }
122122
123123 static void flush_record (int n)
@@ -142,24 +142,24 @@ static void flush_list (int m, int n) · 18 unmodified lines
142142 code_byte(m);
143143 }
144144 else
145− lua_error ("list constructor too long");
145+ yyerror ("list constructor too long");
146146 code_byte(n);
147147 }
148148
149−static void add_localvar (Word name)
149+static void store_localvar (TaggedString *name, int n)
150150 {
151− if (nlocalvar < MAXLOCALS)
152− localvar[nlocalvar++] = name;
151+ if (nlocalvar+n < MAXLOCALS)
152+ localvar[nlocalvar+n] = name;
153153 else
154− lua_error ("too many local variables");
154+ yyerror ("too many local variables");
155+ if (lua_debug)
156+ luaI_registerlocalvar(name, lua_linenumber);
155157 }
156158
157−static void store_localvar (Word name, int n)
159+static void add_localvar (TaggedString *name)
158160 {
159− if (nlocalvar+n < MAXLOCALS)
160− localvar[nlocalvar+n] = name;
161− else
162− lua_error ("too many local variables");
161+ store_localvar(name, 0);
162+ nlocalvar++;
163163 }
164164
165165 static void add_varbuffer (Long var)
@@ -167,7 +167,7 @@ static void add_varbuffer (Long var) · 1 unmodified line
167167 if (nvarbuffer < MAXVAR)
168168 varbuffer[nvarbuffer++] = var;
169169 else
170− lua_error ("variable buffer overflow");
170+ yyerror ("variable buffer overflow");
171171 }
172172
173173 static void code_number (float f)
@@ -197,7 +197,7 @@ static void code_number (float f) · 23 unmodified lines
197197 /*
198198 ** Search a local name and if find return its index. If do not find return -1
199199 */
200−static int lua_localname (Word n)
200+static int lua_localname (TaggedString *n)
201201 {
202202 int i;
203203 for (i=nlocalvar-1; i >= 0; i--)
@@ -388,7 +388,6 @@ static void codeIf (Long thenAdd, Long elseAdd) · 184 unmodified lines
388388 */
389389 void lua_parse (TFunc *tf)
390390 {
391− lua_debug = 0;
392391 initcode = &(tf->code);
393392 *initcode = newvector(CODE_BLOCK, Byte);
394393 maincode = 0;
@@ -416,7 +415,7 @@ void lua_parse (TFunc *tf) · 21 unmodified lines
416415 Word vWord;
417416 Long vLong;
418417 TFunc *pFunc;
419− TreeNode *pNode;
418+ TaggedString *pTStr;
420419 }
421420
422421 %start functionlist
@@ -429,7 +428,7 @@ void lua_parse (TFunc *tf) · 6 unmodified lines
429428 %token FUNCTION
430429 %token <vFloat> NUMBER
431430 %token <vWord> STRING
432−%token <pNode> NAME
431+%token <pTStr> NAME
433432 %token <vInt> DEBUG
434433
435434 %type <vLong> PrepJump
@@ -481,7 +480,7 @@ funcname : var { $$ =$1; init_func(); } · 45 unmodified lines
481480 code_word(luaI_findconstant($3));
482481 $$ = 0; /* indexed variable */
483482 init_func();
484− add_localvar(luaI_findsymbolbyname("self"));
483+ add_localvar(luaI_createfixedstring("self"));
485484 }
486485 ;
487486
@@ -489,11 +488,14 @@ body : '(' parlist ')' block END · 1 unmodified line
489488 {
490489 codereturn();
491490 $$ = new(TFunc);
491+ luaI_initTFunc($$);
492492 $$->size = pc;
493493 $$->code = newvector(pc, Byte);
494494 $$->fileName = lua_parsedfile;
495495 $$->lineDefined = $2;
496496 memcpy($$->code, basepc, pc*sizeof(Byte));
497+ if (lua_debug)
498+ luaI_closelocalvars($$);
497499 /* save func values */
498500 funcCode = basepc; maxcode=maxcurr;
499501 #if LISTING
@@ -554,7 +556,11 @@ block : {$<vInt>$ = nlocalvar;} statlist ret · 54 unmodified lines
554556 {
555557 if (nlocalvar != $<vInt>1)
556558 {
557− nlocalvar = $<vInt>1;
559+ if (lua_debug)
560+ for (; nlocalvar > $<vInt>1; nlocalvar--)
561+ luaI_unregisterlocalvar(lua_linenumber);
562+ else
563+ nlocalvar = $<vInt>1;
558564 lua_codeadjust (0);
559565 }
560566 }
@@ -672,14 +678,8 @@ parlist : /* empty */ { lua_codeadjust(0); $$ = lua_linenumber; } · 111 unmodified lines
672678 | parlist1 { lua_codeadjust(0); $$ = lua_linenumber; }
673679 ;
674680
675−parlist1 : NAME
676− {
677− add_localvar(luaI_findsymbol($1));
678− }
679− | parlist1 ',' NAME
680− {
681− add_localvar(luaI_findsymbol($3));
682− }
681+parlist1 : NAME { add_localvar($1); }
682+ | parlist1 ',' NAME { add_localvar($3); }
683683 ;
684684
685685 fieldlist : lfieldlist
@@ -759,10 +759,9 @@ var : singlevar { $$ = $1; } · 73 unmodified lines
759759
760760 singlevar : NAME
761761 {
762− Word s = luaI_findsymbol($1);
763− int local = lua_localname (s);
762+ int local = lua_localname($1);
764763 if (local == -1) /* global var */
765− $$ = s + 1; /* return positive value */
764+ $$ = luaI_findsymbol($1)+1; /* return positive value */
766765 else
767766 $$ = -(local+1); /* return negative value */
768767 }
@@ -771,10 +770,10 @@ singlevar : NAME · 2 unmodified lines
771770 varexp : var { lua_pushvar($1); }
772771 ;
773772
774−localdeclist : NAME {store_localvar(luaI_findsymbol($1), 0); $$ = 1;}
773+localdeclist : NAME {store_localvar($1, 0); $$ = 1;}
775774 | localdeclist ',' NAME
776775 {
777− store_localvar(luaI_findsymbol($3), $1);
776+ store_localvar($3, $1);
778777 $$ = $1+1;
779778 }
780779 ;
@@ -787,228 +786,3 @@ setdebug : DEBUG { lua_debug = $1; } · 6 unmodified lines
787786 ;
788787
789788 %%
790−
791−#if LISTING
792−
793−static void PrintCode (Byte *code, Byte *end)
794−{
795− Byte *p = code;
796− printf ("\n\nCODE\n");
797− while (p != end)
798− {
799− switch ((OpCode)*p)
800− {
801− case PUSHNIL: printf ("%d PUSHNIL\n", (p++)-code); break;
802− case PUSH0: case PUSH1: case PUSH2:
803− printf ("%d PUSH%c\n", p-code, *p-PUSH0+'0');
804− p++;
805− break;
806− case PUSHBYTE:
807− printf ("%d PUSHBYTE %d\n", p-code, *(++p));
808− p++;
809− break;
810− case PUSHWORD:
811− {
812− CodeWord c;
813− int n = p-code;
814− p++;
815− get_word(c,p);
816− printf ("%d PUSHWORD %d\n", n, c.w);
817− }
818− break;
819− case PUSHFLOAT:
820− {
821− CodeFloat c;
822− int n = p-code;
823− p++;
824− get_float(c,p);
825− printf ("%d PUSHFLOAT %f\n", n, c.f);
826− }
827− break;
828− case PUSHSTRING:
829− {
830− CodeWord c;
831− int n = p-code;
832− p++;
833− get_word(c,p);
834− printf ("%d PUSHSTRING %d\n", n, c.w);
835− }
836− break;
837− case PUSHFUNCTION:
838− {
839− CodeCode c;
840− int n = p-code;
841− p++;
842− get_code(c,p);
843− printf ("%d PUSHFUNCTION %p\n", n, c.tf);
844− }
845− break;
846−
847− case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
848− case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
849− case PUSHLOCAL8: case PUSHLOCAL9:
850− printf ("%d PUSHLOCAL%c\n", p-code, *p-PUSHLOCAL0+'0');
851− p++;
852− break;
853− case PUSHLOCAL: printf ("%d PUSHLOCAL %d\n", p-code, *(++p));
854− p++;
855− break;
856− case PUSHGLOBAL:
857− {
858− CodeWord c;
859− int n = p-code;
860− p++;
861− get_word(c,p);
862− printf ("%d PUSHGLOBAL %d\n", n, c.w);
863− }
864− break;
865− case PUSHINDEXED: printf ("%d PUSHINDEXED\n", (p++)-code); break;
866− case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: case STORELOCAL3:
867− case STORELOCAL4: case STORELOCAL5: case STORELOCAL6: case STORELOCAL7:
868− case STORELOCAL8: case STORELOCAL9:
869− printf ("%d STORELOCAL%c\n", p-code, *p-STORELOCAL0+'0');
870− p++;
871− break;
872− case STORELOCAL:
873− printf ("%d STORELOCAL %d\n", p-code, *(++p));
874− p++;
875− break;
876− case STOREGLOBAL:
877− {
878− CodeWord c;
879− int n = p-code;
880− p++;
881− get_word(c,p);
882− printf ("%d STOREGLOBAL %d\n", n, c.w);
883− }
884− break;
885− case PUSHSELF:
886− {
887− CodeWord c;
888− int n = p-code;
889− p++;
890− get_word(c,p);
891− printf ("%d PUSHSELF %d\n", n, c.w);
892− }
893− break;
894− case STOREINDEXED0: printf ("%d STOREINDEXED0\n", (p++)-code); break;
895− case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p));
896− p++;
897− break;
898− case STORELIST0:
899− printf("%d STORELIST0 %d\n", p-code, *(++p));
900− p++;
901− break;
902− case STORELIST:
903− printf("%d STORELIST %d %d\n", p-code, *(p+1), *(p+2));
904− p+=3;
905− break;
906− case STORERECORD:
907− printf("%d STORERECORD %d\n", p-code, *(++p));
908− p += *p*sizeof(Word) + 1;
909− break;
910− case ADJUST0: printf ("%d ADJUST0\n", (p++)-code); break;
911− case ADJUST:
912− printf ("%d ADJUST %d\n", p-code, *(++p));
913− p++;
914− break;
915− case CREATEARRAY:
916− {
917− CodeWord c;
918− int n = p-code;
919− p++;
920− get_word(c,p);
921− printf ("%d CREATEARRAY %d\n", n, c.w);
922− break;
923− }
924− case EQOP: printf ("%d EQOP\n", (p++)-code); break;
925− case LTOP: printf ("%d LTOP\n", (p++)-code); break;
926− case LEOP: printf ("%d LEOP\n", (p++)-code); break;
927− case ADDOP: printf ("%d ADDOP\n", (p++)-code); break;
928− case SUBOP: printf ("%d SUBOP\n", (p++)-code); break;
929− case MULTOP: printf ("%d MULTOP\n", (p++)-code); break;
930− case DIVOP: printf ("%d DIVOP\n", (p++)-code); break;
931− case POWOP: printf ("%d POWOP\n", (p++)-code); break;
932− case CONCOP: printf ("%d CONCOP\n", (p++)-code); break;
933− case MINUSOP: printf ("%d MINUSOP\n", (p++)-code); break;
934− case NOTOP: printf ("%d NOTOP\n", (p++)-code); break;
935− case ONTJMP:
936− {
937− CodeWord c;
938− int n = p-code;
939− p++;
940− get_word(c,p);
941− printf ("%d ONTJMP %d\n", n, c.w);
942− }
943− break;
944− case ONFJMP:
945− {
946− CodeWord c;
947− int n = p-code;
948− p++;
949− get_word(c,p);
950− printf ("%d ONFJMP %d\n", n, c.w);
951− }
952− break;
953− case JMP:
954− {
955− CodeWord c;
956− int n = p-code;
957− p++;
958− get_word(c,p);
959− printf ("%d JMP %d\n", n, c.w);
960− }
961− break;
962− case UPJMP:
963− {
964− CodeWord c;
965− int n = p-code;
966− p++;
967− get_word(c,p);
968− printf ("%d UPJMP %d\n", n, c.w);
969− }
970− break;
971− case IFFJMP:
972− {
973− CodeWord c;
974− int n = p-code;
975− p++;
976− get_word(c,p);
977− printf ("%d IFFJMP %d\n", n, c.w);
978− }
979− break;
980− case IFFUPJMP:
981− {
982− CodeWord c;
983− int n = p-code;
984− p++;
985− get_word(c,p);
986− printf ("%d IFFUPJMP %d\n", n, c.w);
987− }
988− break;
989− case POP: printf ("%d POP\n", (p++)-code); break;
990− case CALLFUNC:
991− printf ("%d CALLFUNC %d %d\n", p-code, *(p+1), *(p+2));
992− p+=3;
993− break;
994− case RETCODE0: printf ("%d RETCODE0\n", (p++)-code); break;
995− case RETCODE:
996− printf ("%d RETCODE %d\n", p-code, *(++p));
997− p++;
998− break;
999− case SETLINE:
1000− {
1001− CodeWord c;
1002− int n = p-code;
1003− p++;
1004− get_word(c,p);
1005− printf ("%d SETLINE %d\n", n, c.w);
1006− }
1007− break;
1008−
1009− default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break;
1010− }
1011− }
1012−}
1013−#endif
1014−
src/luac/Makefile+24−0
@@ -0,0 +1,24 @@
1+# makefile for lua distribution (compiler)
2+
3+LUA= ../..
4+
5+include $(LUA)/config
6+
7+INCS= -I$(INC) $(EXTRA_INCS) -I..
8+OBJS= dump.o luac.o print.o
9+SRCS= dump.c luac.c print.c luac.h print.h
10+T=$(BIN)/luac
11+
12+all: $T
13+
14+$T: $(OBJS)
15+ $(CC) -o $@ $(OBJS) -L$(LIB) -llua
16+
17+clean:
18+ rm -f $(OBJS) $T
19+
20+co:
21+ co -f -M $(SRCS)
22+
23+klean: clean
24+ rm -f $(SRCS)
src/luac/dump.c+247−0
@@ -0,0 +1,247 @@
1+/*
2+** dump.c
3+** thread and save bytecodes to file
4+*/
5+
6+char* rcs_dump="$Id: dump.c,v 1.12 1996/03/12 20:00:03 lhf Exp $";
7+
8+#include <stdio.h>
9+#include <string.h>
10+#include "luac.h"
11+
12+static TFunc* lastF=NULL; /* list of functions seen in code */
13+
14+static int SawVar(int i, int at)
15+{
16+ int old=VarLoc(i);
17+ VarLoc(i)=at;
18+ return old;
19+}
20+
21+static int SawStr(int i, int at)
22+{
23+ int old=StrLoc(i);
24+ StrLoc(i)=at;
25+ return old;
26+}
27+
28+static void ThreadCode(Byte* code, Byte* end)
29+{
30+ Byte* p;
31+ int i;
32+ for (i=0; i<lua_ntable; i++) VarLoc(i)=0;
33+ for (i=0; i<lua_nconstant; i++) StrLoc(i)=0;
34+ for (p=code; p!=end;)
35+ {
36+ OpCode op=(OpCode)*p;
37+ int at=p-code+1;
38+ switch (op)
39+ {
40+ case PUSHNIL:
41+ case PUSH0:
42+ case PUSH1:
43+ case PUSH2:
44+ case PUSHLOCAL0:
45+ case PUSHLOCAL1:
46+ case PUSHLOCAL2:
47+ case PUSHLOCAL3:
48+ case PUSHLOCAL4:
49+ case PUSHLOCAL5:
50+ case PUSHLOCAL6:
51+ case PUSHLOCAL7:
52+ case PUSHLOCAL8:
53+ case PUSHLOCAL9:
54+ case PUSHINDEXED:
55+ case STORELOCAL0:
56+ case STORELOCAL1:
57+ case STORELOCAL2:
58+ case STORELOCAL3:
59+ case STORELOCAL4:
60+ case STORELOCAL5:
61+ case STORELOCAL6:
62+ case STORELOCAL7:
63+ case STORELOCAL8:
64+ case STORELOCAL9:
65+ case STOREINDEXED0:
66+ case ADJUST0:
67+ case EQOP:
68+ case LTOP:
69+ case LEOP:
70+ case GTOP:
71+ case GEOP:
72+ case ADDOP:
73+ case SUBOP:
74+ case MULTOP:
75+ case DIVOP:
76+ case POWOP:
77+ case CONCOP:
78+ case MINUSOP:
79+ case NOTOP:
80+ case POP:
81+ case RETCODE0:
82+ p++;
83+ break;
84+ case PUSHBYTE:
85+ case PUSHLOCAL:
86+ case STORELOCAL:
87+ case STOREINDEXED:
88+ case STORELIST0:
89+ case ADJUST:
90+ case RETCODE:
91+ p+=2;
92+ break;
93+ case PUSHWORD:
94+ case CREATEARRAY:
95+ case ONTJMP:
96+ case ONFJMP:
97+ case JMP:
98+ case UPJMP:
99+ case IFFJMP:
100+ case IFFUPJMP:
101+ case SETLINE:
102+ case STORELIST:
103+ case CALLFUNC:
104+ p+=3;
105+ break;
106+ case PUSHFLOAT:
107+ p+=5;
108+ break;
109+ case PUSHSELF:
110+ case PUSHSTRING:
111+ {
112+ CodeWord c;
113+ p++;
114+ get_word(c,p);
115+ c.w=SawStr(c.w,at);
116+ p[-2]=c.m.c1;
117+ p[-1]=c.m.c2;
118+ break;
119+ }
120+ case PUSHFUNCTION:
121+ {
122+ CodeCode c;
123+ p++;
124+ get_code(c,p);
125+ c.tf->marked=at;
126+ c.tf->next=NULL; /* TODO: remove? */
127+ lastF=lastF->next=c.tf;
128+ break;
129+ }
130+ case PUSHGLOBAL:
131+ case STOREGLOBAL:
132+ {
133+ CodeWord c;
134+ p++;
135+ get_word(c,p);
136+ c.w=SawVar(c.w,at);
137+ p[-2]=c.m.c1;
138+ p[-1]=c.m.c2;
139+ break;
140+ }
141+ case STORERECORD:
142+ {
143+ int n=*++p;
144+ p++;
145+ while (n--)
146+ {
147+ CodeWord c;
148+ at=p-code;
149+ get_word(c,p);
150+ c.w=SawStr(c.w,at);
151+ p[-2]=c.m.c1;
152+ p[-1]=c.m.c2;
153+ }
154+ break;
155+ }
156+ default:
157+ fprintf(stderr,"luac: cannot happen: opcode=%d",*p);
158+ exit(1);
159+ break;
160+ }
161+ }
162+}
163+
164+static void DumpWord(int i, FILE* D)
165+{
166+ Word w=i;
167+ fwrite(&w,sizeof(w),1,D);
168+}
169+
170+static void DumpBlock(char* b, int size, FILE* D)
171+{
172+ fwrite(b,size,1,D);
173+}
174+
175+static void DumpSize(int i, FILE* D)
176+{
177+ Word lo=i&0x0FFFF;
178+ Word hi=(i>>16)&0x0FFFF;
179+ fwrite(&hi,sizeof(hi),1,D);
180+ fwrite(&lo,sizeof(lo),1,D);
181+ if (hi!=0)
182+ fprintf(stderr,
183+ "luac: warning: code too long for 16-bit machines (%d bytes)\n",i);
184+}
185+
186+static void DumpString(char* s, FILE* D)
187+{
188+ int n=strlen(s)+1;
189+ if ((Word)n != n)
190+ {
191+ fprintf(stderr,"luac: string too long: \"%.32s...\"\n",s);
192+ exit(1);
193+ }
194+ DumpWord(n,D);
195+ DumpBlock(s,n,D);
196+}
197+
198+static void DumpStrings(FILE* D)
199+{
200+ int i;
201+ for (i=0; i<lua_ntable; i++)
202+ {
203+ if (VarLoc(i)!=0)
204+ {
205+ fputc(ID_VAR,D);
206+ DumpWord(VarLoc(i),D);
207+ DumpString(VarStr(i),D);
208+ }
209+ VarLoc(i)=i;
210+ }
211+ for (i=0; i<lua_nconstant; i++)
212+ {
213+ if (StrLoc(i)!=0)
214+ {
215+ fputc(ID_STR,D);
216+ DumpWord(StrLoc(i),D);
217+ DumpString(StrStr(i),D);
218+ }
219+ StrLoc(i)=i;
220+ }
221+}
222+
223+void DumpFunction(TFunc* tf, FILE* D)
224+{
225+ lastF=tf;
226+ ThreadCode(tf->code,tf->code+tf->size);
227+ fputc(ID_FUN,D);
228+ DumpSize(tf->size,D);
229+ DumpWord(tf->lineDefined,D);
230+ if (IsMain(tf))
231+ DumpString(tf->fileName,D);
232+ else
233+ DumpWord(tf->marked,D);
234+ DumpBlock(tf->code,tf->size,D);
235+ DumpStrings(D);
236+}
237+
238+void DumpHeader(FILE* D)
239+{
240+ Word w=TEST_WORD;
241+ float f=TEST_FLOAT;
242+ fputc(ID_CHUNK,D);
243+ fputs(SIGNATURE,D);
244+ fputc(VERSION,D);
245+ fwrite(&w,sizeof(w),1,D);
246+ fwrite(&f,sizeof(f),1,D);
247+}
src/luac/luac.c+104−0
@@ -0,0 +1,104 @@
1+/*
2+** luac.c
3+** lua compiler (saves bytecodes to files)
4+*/
5+
6+char* rcs_luac="$Id: luac.c,v 1.16 1996/03/13 17:33:03 lhf Exp $";
7+
8+#include <stdio.h>
9+#include <string.h>
10+#include "luac.h"
11+
12+static void compile(char* filename);
13+
14+static int listing=0; /* list bytecodes? */
15+static int dumping=1; /* dump bytecodes? */
16+static FILE* D; /* output file */
17+
18+static void usage(void)
19+{
20+ fprintf(stderr,"usage: luac [-dlpv] [-o output] file ...\n");
21+ exit(0);
22+}
23+
24+#define IS(s) (strcmp(argv[i],s)==0)
25+
26+int main(int argc, char* argv[])
27+{
28+ char* d="luac.out"; /* default output file */
29+ int i;
30+ for (i=1; i<argc; i++)
31+ {
32+ if (argv[i][0]!='-') /* end of options */
33+ break;
34+ else if (IS("-")) /* use stdin */
35+ break;
36+ else if (IS("-d")) /* debug */
37+ lua_debug=1;
38+ else if (IS("-l")) /* list */
39+ listing=1;
40+ else if (IS("-o")) /* output file */
41+ d=argv[++i];
42+ else if (IS("-p")) /* parse only (for timing purposes) */
43+ dumping=0;
44+ else if (IS("-v")) /* show version */
45+ printf("%s %s\n(written by %s)\n\n",LUA_VERSION,LUA_COPYRIGHT,LUA_AUTHORS);
46+ else /* unknown option */
47+ usage();
48+ }
49+ --i; /* fake new argv[0] */
50+ argc-=i;
51+ argv+=i;
52+ if (argc<2) usage();
53+ for (i=1; i<argc; i++)
54+ if (IS(d))
55+ {
56+ fprintf(stderr,"luac: will not overwrite input file \"%s\"\n",d);
57+ exit(1);
58+ }
59+ D=(dumping) ? fopen(d,"wb") : stdout; /* must open in binary mode */
60+ if (D==NULL)
61+ {
62+ fprintf(stderr,"luac: cannot open ");
63+ perror(d);
64+ exit(1);
65+ }
66+ for (i=1; i<argc; i++) compile(IS("-")? NULL : argv[i]);
67+ fclose(D);
68+ return 0;
69+}
70+
71+static void do_dump(TFunc* tf) /* only for tf==main */
72+{
73+ if (dumping) DumpHeader(D);
74+ while (tf!=NULL)
75+ {
76+ TFunc* nf;
77+ if (listing) PrintFunction(tf);
78+ if (dumping) DumpFunction(tf,D);
79+ nf=tf->next; /* list only built after first main */
80+ luaI_freefunc(tf);
81+ tf=nf;
82+ }
83+}
84+
85+static void do_compile(void)
86+{
87+ TFunc* tf=new(TFunc);
88+ luaI_initTFunc(tf);
89+ tf->fileName = lua_parsedfile;
90+ lua_parse(tf);
91+ do_dump(tf);
92+}
93+
94+static void compile(char* filename)
95+{
96+ if (lua_openfile(filename)==NULL)
97+ {
98+ fprintf(stderr,"luac: cannot open ");
99+ perror(filename);
100+ exit(1);
101+ }
102+ do_compile();
103+ lua_closefile();
104+}
src/luac/luac.h+24−0
@@ -0,0 +1,24 @@
1+/*
2+** luac.h
3+** definitions for luac compiler
4+** $Id: luac.h,v 1.5 1996/03/08 21:41:47 lhf Exp $
5+*/
6+
7+#include "inout.h"
8+#include "mem.h"
9+#include "opcode.h"
10+#include "table.h"
11+#include "undump.h"
12+
13+#define VarStr(i) (lua_table[i].varname->str)
14+#define VarLoc(i) (lua_table[i].varname->varindex)
15+#define StrStr(i) (lua_constant[i]->str)
16+#define StrLoc(i) (lua_constant[i]->constindex)
17+
18+extern Word lua_ntable;
19+extern Word lua_nconstant;
20+extern int lua_debug;
21+
22+void DumpHeader(FILE* D);
23+void DumpFunction(TFunc* tf, FILE* D);
24+void PrintFunction(TFunc* tf);
src/luac/print.c+189−0
@@ -0,0 +1,189 @@
1+/*
2+** print.c
3+** print bytecodes
4+*/
5+
6+char* rcs_print="$Id: print.c,v 1.6 1996/03/12 20:00:24 lhf Exp $";
7+
8+#include <stdio.h>
9+#include <string.h>
10+#include "luac.h"
11+#include "print.h"
12+
13+static LocVar* V=NULL;
14+
15+static char* LocStr(int i)
16+{
17+ if (V==NULL) return ""; else return V[i].varname->str;
18+}
19+
20+static void PrintCode(Byte* code, Byte* end)
21+{
22+ Byte* p;
23+ for (p=code; p!=end;)
24+ {
25+ OpCode op=(OpCode)*p;
26+ if (op>SETLINE) op=SETLINE+1;
27+ printf("%6d\t%s",p-code,OpCodeName[op]);
28+ switch (op)
29+ {
30+ case PUSHNIL:
31+ case PUSH0:
32+ case PUSH1:
33+ case PUSH2:
34+ case PUSHINDEXED:
35+ case STOREINDEXED0:
36+ case ADJUST0:
37+ case EQOP:
38+ case LTOP:
39+ case LEOP:
40+ case GTOP:
41+ case GEOP:
42+ case ADDOP:
43+ case SUBOP:
44+ case MULTOP:
45+ case DIVOP:
46+ case POWOP:
47+ case CONCOP:
48+ case MINUSOP:
49+ case NOTOP:
50+ case POP:
51+ case RETCODE0:
52+ p++;
53+ break;
54+ case PUSHLOCAL0:
55+ case PUSHLOCAL1:
56+ case PUSHLOCAL2:
57+ case PUSHLOCAL3:
58+ case PUSHLOCAL4:
59+ case PUSHLOCAL5:
60+ case PUSHLOCAL6:
61+ case PUSHLOCAL7:
62+ case PUSHLOCAL8:
63+ case PUSHLOCAL9:
64+ {
65+ int i=op-PUSHLOCAL0;
66+ printf("\t%d\t; %s",i,LocStr(i));
67+ p++;
68+ break;
69+ }
70+ case STORELOCAL0:
71+ case STORELOCAL1:
72+ case STORELOCAL2:
73+ case STORELOCAL3:
74+ case STORELOCAL4:
75+ case STORELOCAL5:
76+ case STORELOCAL6:
77+ case STORELOCAL7:
78+ case STORELOCAL8:
79+ case STORELOCAL9:
80+ {
81+ int i=op-STORELOCAL0;
82+ printf("\t%d\t; %s",i,LocStr(i));
83+ p++;
84+ break;
85+ }
86+ case PUSHLOCAL:
87+ case STORELOCAL:
88+ {
89+ int i=*(p+1);
90+ printf("\t%d\t; %s",i,LocStr(i));
91+ p+=2;
92+ break;
93+ }
94+ case PUSHBYTE:
95+ case STOREINDEXED:
96+ case STORELIST0:
97+ case ADJUST:
98+ case RETCODE:
99+ printf("\t%d",*(p+1));
100+ p+=2;
101+ break;
102+ case PUSHWORD:
103+ case CREATEARRAY:
104+ case ONTJMP:
105+ case ONFJMP:
106+ case JMP:
107+ case UPJMP:
108+ case IFFJMP:
109+ case IFFUPJMP:
110+ case SETLINE:
111+ {
112+ CodeWord c;
113+ p++;
114+ get_word(c,p);
115+ printf("\t%d",c.w);
116+ break;
117+ }
118+ case PUSHFLOAT:
119+ {
120+ CodeFloat c;
121+ p++;
122+ get_float(c,p);
123+ printf("\t%g",c.f);
124+ break;
125+ }
126+ case PUSHSELF:
127+ case PUSHSTRING:
128+ {
129+ CodeWord c;
130+ p++;
131+ get_word(c,p);
132+ printf("\t%d\t; \"%s\"",c.w,StrStr(c.w));
133+ break;
134+ }
135+ case PUSHFUNCTION:
136+ {
137+ CodeCode c;
138+ p++;
139+ get_code(c,p);
140+ printf("\t%p\t; \"%s\":%d",c.tf,c.tf->fileName,c.tf->lineDefined);
141+ break;
142+ }
143+ case PUSHGLOBAL:
144+ case STOREGLOBAL:
145+ {
146+ CodeWord c;
147+ p++;
148+ get_word(c,p);
149+ printf("\t%d\t; %s",c.w,VarStr(c.w));
150+ break;
151+ }
152+ case STORELIST:
153+ case CALLFUNC:
154+ printf("\t%d %d",*(p+1),*(p+2));
155+ p+=3;
156+ break;
157+ case STORERECORD:
158+ {
159+ int n=*++p;
160+ printf("\t%d",n);
161+ p++;
162+ while (n--)
163+ {
164+ CodeWord c;
165+ printf("\n%6d\t FIELD",p-code);
166+ get_word(c,p);
167+ printf("\t%d\t; \"%s\"",c.w,StrStr(c.w));
168+ }
169+ break;
170+ }
171+ default:
172+ printf("\tcannot happen: opcode=%d",*p);
173+ exit(1);
174+ break;
175+ }
176+ printf("\n");
177+ }
178+}
179+
180+void PrintFunction(TFunc* tf)
181+{
182+ if (IsMain(tf))
183+ printf("\nmain of \"%s\" (%d bytes at %p)\n",tf->fileName,tf->size,tf);
184+ else
185+ printf("\nfunction \"%s\":%d (%d bytes at %p); used at main+%d\n",
186+ tf->fileName,tf->lineDefined,tf->size,tf,tf->marked);
187+ V=tf->locvars;
188+ PrintCode(tf->code,tf->code+tf->size);
189+}
src/luac/print.h+76−0
@@ -0,0 +1,76 @@
1+/*
2+** print.h
3+** opcode names
4+** $Id: print.h,v 1.1 1996/02/23 19:04:13 lhf Exp $
5+*/
6+
7+static char* OpCodeName[]={ /* attention: same order as enum in opcode.h */
8+ "PUSHNIL",
9+ "PUSH0",
10+ "PUSH1",
11+ "PUSH2",
12+ "PUSHBYTE",
13+ "PUSHWORD",
14+ "PUSHFLOAT",
15+ "PUSHSTRING",
16+ "PUSHFUNCTION",
17+ "PUSHLOCAL0",
18+ "PUSHLOCAL1",
19+ "PUSHLOCAL2",
20+ "PUSHLOCAL3",
21+ "PUSHLOCAL4",
22+ "PUSHLOCAL5",
23+ "PUSHLOCAL6",
24+ "PUSHLOCAL7",
25+ "PUSHLOCAL8",
26+ "PUSHLOCAL9",
27+ "PUSHLOCAL",
28+ "PUSHGLOBAL",
29+ "PUSHINDEXED",
30+ "PUSHSELF",
31+ "STORELOCAL0",
32+ "STORELOCAL1",
33+ "STORELOCAL2",
34+ "STORELOCAL3",
35+ "STORELOCAL4",
36+ "STORELOCAL5",
37+ "STORELOCAL6",
38+ "STORELOCAL7",
39+ "STORELOCAL8",
40+ "STORELOCAL9",
41+ "STORELOCAL",
42+ "STOREGLOBAL",
43+ "STOREINDEXED0",
44+ "STOREINDEXED",
45+ "STORELIST0",
46+ "STORELIST",
47+ "STORERECORD",
48+ "ADJUST0",
49+ "ADJUST",
50+ "CREATEARRAY",
51+ "EQOP",
52+ "LTOP",
53+ "LEOP",
54+ "GTOP",
55+ "GEOP",
56+ "ADDOP",
57+ "SUBOP",
58+ "MULTOP",
59+ "DIVOP",
60+ "POWOP",
61+ "CONCOP",
62+ "MINUSOP",
63+ "NOTOP",
64+ "ONTJMP",
65+ "ONFJMP",
66+ "JMP",
67+ "UPJMP",
68+ "IFFJMP",
69+ "IFFUPJMP",
70+ "POP",
71+ "CALLFUNC",
72+ "RETCODE0",
73+ "RETCODE",
74+ "SETLINE",
75+ ""
76+};
src/mem.c+34−11
@@ -3,18 +3,24 @@ · 2 unmodified lines
33 ** TecCGraf - PUC-Rio
44 */
55
6−char *rcs_mem = "$Id: mem.c,v 1.5 1995/02/06 19:34:03 roberto Exp $";
6+char *rcs_mem = "$Id: mem.c,v 1.12 1996/05/06 16:59:00 roberto Exp $";
77
88 #include <stdlib.h>
9−#include <string.h>
109
1110 #include "mem.h"
1211 #include "lua.h"
1312
13+
14+#define mem_error() lua_error(memEM)
15+
16+
1417 void luaI_free (void *block)
1518 {
16− *((int *)block) = -1; /* to catch errors */
17− free(block);
19+ if (block)
20+ {
21+ *((int *)block) = -1; /* to catch errors */
22+ free(block);
23+ }
1824 }
1925
2026
@@ -22,23 +28,40 @@ void *luaI_malloc (unsigned long size) · 1 unmodified line
2228 {
2329 void *block = malloc((size_t)size);
2430 if (block == NULL)
25− lua_error("not enough memory");
31+ mem_error();
2632 return block;
2733 }
2834
2935
3036 void *luaI_realloc (void *oldblock, unsigned long size)
3137 {
32− void *block = realloc(oldblock, (size_t)size);
38+ void *block = oldblock ? realloc(oldblock, (size_t)size) :
39+ malloc((size_t)size);
3340 if (block == NULL)
34− lua_error("not enough memory");
41+ mem_error();
3542 return block;
3643 }
3744
3845
39−char *luaI_strdup (char *str)
46+int luaI_growvector (void **block, unsigned long nelems, int size,
47+ char *errormsg, unsigned long limit)
4048 {
41− char *newstr = luaI_malloc(strlen(str)+1);
42− strcpy(newstr, str);
43− return newstr;
49+ if (nelems >= limit)
50+ lua_error(errormsg);
51+ nelems = (nelems == 0) ? 20 : nelems*2;
52+ if (nelems > limit)
53+ nelems = limit;
54+ *block = luaI_realloc(*block, nelems*size);
55+ return (int) nelems;
4456 }
57+
58+
59+void* luaI_buffer (unsigned long size)
60+{
61+ static unsigned long buffsize = 0;
62+ static char* buffer = NULL;
63+ if (size > buffsize)
64+ buffer = luaI_realloc(buffer, buffsize=size);
65+ return buffer;
66+}
67+
src/mem.h+18−4
@@ -1,7 +1,7 @@
11 /*
22 ** mem.c
33 ** memory manager for lua
4−** $Id: mem.h,v 1.2 1995/01/13 22:11:12 roberto Exp $
4+** $Id: mem.h,v 1.7 1996/04/22 18:00:37 roberto Exp $
55 */
66
77 #ifndef mem_h
@@ -11,15 +11,29 @@ · 3 unmodified lines
1111 #define NULL 0
1212 #endif
1313
14+
15+/* memory error messages */
16+#define codeEM "code size overflow"
17+#define symbolEM "symbol table overflow"
18+#define constantEM "constant table overflow"
19+#define stackEM "stack size overflow"
20+#define lexEM "lex buffer overflow"
21+#define refEM "reference table overflow"
22+#define tableEM "table overflow"
23+#define memEM "not enough memory"
24+
25+
1426 void luaI_free (void *block);
1527 void *luaI_malloc (unsigned long size);
1628 void *luaI_realloc (void *oldblock, unsigned long size);
17−
18−char *luaI_strdup (char *str);
29+void *luaI_buffer (unsigned long size);
30+int luaI_growvector (void **block, unsigned long nelems, int size,
31+ char *errormsg, unsigned long limit);
1932
2033 #define new(s) ((s *)luaI_malloc(sizeof(s)))
2134 #define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s)))
22−#define growvector(old,n,s) ((s *)luaI_realloc(old,(n)*sizeof(s)))
35+#define growvector(old,n,s,e,l) \
36+ (luaI_growvector((void**)old,n,sizeof(s),e,l))
2337
2438 #endif
2539
src/opcode.c+202−89
@@ -3,10 +3,9 @@ · 2 unmodified lines
33 ** TecCGraf - PUC-Rio
44 */
55
6−char *rcs_opcode="$Id: opcode.c,v 3.50 1995/11/16 20:46:24 roberto Exp $";
6+char *rcs_opcode="$Id: opcode.c,v 3.68 1996/04/25 14:10:00 roberto Exp $";
77
88 #include <setjmp.h>
9−#include <stdlib.h>
109 #include <stdio.h>
1110 #include <string.h>
1211
@@ -18,6 +17,7 @@ char *rcs_opcode="$Id: opcode.c,v 3.50 1995/11/16 20:46:24 roberto Exp $"; · 5 unmodified lines
1817 #include "table.h"
1918 #include "lua.h"
2019 #include "fallback.h"
20+#include "undump.h"
2121
2222 #define tonumber(o) ((tag(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
2323 #define tostring(o) ((tag(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
@@ -25,6 +25,10 @@ char *rcs_opcode="$Id: opcode.c,v 3.50 1995/11/16 20:46:24 roberto Exp $"; · 1 unmodified line
2525
2626 #define STACK_SIZE 128
2727
28+#ifndef STACK_LIMIT
29+#define STACK_LIMIT 6000
30+#endif
31+
2832 typedef int StkId; /* index to stack elements */
2933
3034 static Object initial_stack;
@@ -53,6 +57,11 @@ static int CnResults = 0; /* when Lua calls C, has the number of parameters; */ · 22 unmodified lines
5357 static jmp_buf *errorJmp = NULL; /* current error recover point */
5458
5559
60+/* Hooks */
61+lua_LHFunction lua_linehook = NULL;
62+lua_CHFunction lua_callhook = NULL;
63+
64+
5665 static StkId lua_execute (Byte *pc, StkId base);
5766 static void do_call (StkId base, int nResults);
5867
@@ -64,7 +73,6 @@ Object *luaI_Address (lua_Object o) · 5 unmodified lines
6473 }
6574
6675
67−
6876 /*
6977 ** Init stack
7078 */
@@ -89,14 +97,17 @@ static void growstack (void) · 18 unmodified lines
8997 lua_initstack();
9098 else
9199 {
100+ static int limit = STACK_LIMIT;
92101 StkId t = top-stack;
93− Long maxstack = stackLimit - stack;
94− maxstack *= 2;
95− stack = growvector(stack, maxstack, Object);
96− stackLimit = stack+maxstack;
102+ Long stacksize = stackLimit - stack;
103+ stacksize = growvector(&stack, stacksize, Object, stackEM, limit+100);
104+ stackLimit = stack+stacksize;
97105 top = stack + t;
98− if (maxstack >= MAX_WORD/2)
99− lua_error("stack size overflow");
106+ if (stacksize >= limit)
107+ {
108+ limit = stacksize;
109+ lua_error(stackEM);
110+ }
100111 }
101112 }
102113
@@ -106,18 +117,9 @@ static void growstack (void) · 3 unmodified lines
106117 */
107118 static char *lua_strconc (char *l, char *r)
108119 {
109− static char *buffer = NULL;
110− static int buffer_size = 0;
111− int nl = strlen(l);
112− int n = nl+strlen(r)+1;
113− if (n > buffer_size)
114− {
115− buffer_size = n;
116− if (buffer != NULL)
117− luaI_free(buffer);
118− buffer = newvector(buffer_size, char);
119− }
120− strcpy(buffer,l);
120+ int nl = strlen(l);
121+ char *buffer = luaI_buffer(nl+strlen(r)+1);
122+ strcpy(buffer, l);
121123 strcpy(buffer+nl, r);
122124 return buffer;
123125 }
@@ -150,7 +152,7 @@ static int lua_tonumber (Object *obj) · 26 unmodified lines
150152 */
151153 static int lua_tostring (Object *obj)
152154 {
153− static char s[256];
155+ char s[256];
154156 if (tag(obj) != LUA_T_NUMBER)
155157 return 1;
156158 if ((int) nvalue(obj) == nvalue(obj))
@@ -158,8 +160,6 @@ static int lua_tostring (Object *obj) · 1 unmodified line
158160 else
159161 sprintf (s, "%g", nvalue(obj));
160162 tsvalue(obj) = lua_createstring(s);
161− if (tsvalue(obj) == NULL)
162− return 1;
163163 tag(obj) = LUA_T_STRING;
164164 return 0;
165165 }
@@ -192,6 +192,48 @@ static void open_stack (int nelems) · 26 unmodified lines
192192 }
193193
194194
195+/*
196+** call Line hook
197+*/
198+static void lineHook (int line)
199+{
200+ StkId oldBase = CBase;
201+ int oldCnResults = CnResults;
202+ StkId old_top = CBase = top-stack;
203+ CnResults = 0;
204+ (*lua_linehook)(line);
205+ top = stack+old_top;
206+ CnResults = oldCnResults;
207+ CBase = oldBase;
208+}
209+
210+
211+/*
212+** Call hook
213+** The function being called is in [stack+base-1]
214+*/
215+static void callHook (StkId base, lua_Type type, int isreturn)
216+{
217+ StkId oldBase = CBase;
218+ int oldCnResults = CnResults;
219+ StkId old_top = CBase = top-stack;
220+ CnResults = 0;
221+ if (isreturn)
222+ (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
223+ else
224+ {
225+ Object *f = stack+base-1;
226+ if (type == LUA_T_MARK)
227+ (*lua_callhook)(Ref(f), f->value.tf->fileName, f->value.tf->lineDefined);
228+ else
229+ (*lua_callhook)(Ref(f), "(C)", -1);
230+ }
231+ top = stack+old_top;
232+ CnResults = oldCnResults;
233+ CBase = oldBase;
234+}
235+
236+
195237 /*
196238 ** Call a C function. CBase will point to the top of the stack,
197239 ** and CnResults is the number of parameters. Returns an index
@@ -204,8 +246,12 @@ static StkId callC (lua_CFunction func, StkId base) · 6 unmodified lines
204246 StkId firstResult;
205247 CnResults = (top-stack) - base;
206248 /* incorporate parameters on the stack */
207− CBase = base+CnResults;
249+ CBase = base+CnResults; /* == top-stack */
250+ if (lua_callhook)
251+ callHook(base, LUA_T_CMARK, 0);
208252 (*func)();
253+ if (lua_callhook) /* func may have changed lua_callhook */
254+ callHook(base, LUA_T_CMARK, 1);
209255 firstResult = CBase;
210256 CBase = oldBase;
211257 CnResults = oldCnResults;
@@ -303,6 +349,18 @@ static void storesubscript (void) · 91 unmodified lines
303349 }
304350
305351
352+static void getglobal (Word n)
353+{
354+ *top = lua_table[n].object;
355+ incr_top;
356+ if (tag(top-1) == LUA_T_NIL)
357+ { /* must call getglobal fallback */
358+ tag(top-1) = LUA_T_STRING;
359+ tsvalue(top-1) = lua_table[n].varname;
360+ callFB(FB_GETGLOBAL);
361+ }
362+}
363+
306364 /*
307365 ** Traverse all objects on stack
308366 */
@@ -340,7 +398,7 @@ void lua_error (char *s) · 31 unmodified lines
340398 }
341399
342400
343−lua_Object lua_stackedfunction (int level)
401+lua_Function lua_stackedfunction (int level)
344402 {
345403 Object *p = top;
346404 while (--p >= stack)
@@ -351,13 +409,45 @@ lua_Object lua_stackedfunction (int level) · 4 unmodified lines
351409 }
352410
353411
354−int lua_currentline (lua_Object func)
412+int lua_currentline (lua_Function func)
355413 {
356414 Object *f = Address(func);
357415 return (f+1 < top && (f+1)->tag == LUA_T_LINE) ? (f+1)->value.i : -1;
358416 }
359417
360418
419+lua_Object lua_getlocal (lua_Function func, int local_number, char **name)
420+{
421+ Object *f = luaI_Address(func);
422+ *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
423+ if (*name)
424+ {
425+ /* if "*name", there must be a LUA_T_LINE */
426+ /* therefore, f+2 points to function base */
427+ return Ref((f+2)+(local_number-1));
428+ }
429+ else
430+ return LUA_NOOBJECT;
431+}
432+
433+int lua_setlocal (lua_Function func, int local_number)
434+{
435+ Object *f = Address(func);
436+ char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
437+ adjustC(1);
438+ --top;
439+ if (name)
440+ {
441+ /* if "name", there must be a LUA_T_LINE */
442+ /* therefore, f+2 points to function base */
443+ *((f+2)+(local_number-1)) = *top;
444+ return 1;
445+ }
446+ else
447+ return 0;
448+}
449+
450+
361451 /*
362452 ** Execute a protected call. Assumes that function is at CBase and
363453 ** parameters are on top of it. Leave nResults on the stack.
@@ -386,6 +476,16 @@ static int do_protectedrun (int nResults) · 22 unmodified lines
386476 return status;
387477 }
388478
479+int luaI_dorun (TFunc *tf)
480+{
481+ int status;
482+ adjustC(1); /* one slot for the pseudo-function */
483+ stack[CBase].tag = LUA_T_FUNCTION;
484+ stack[CBase].value.tf = tf;
485+ status = do_protectedrun(0);
486+ adjustC(0);
487+ return status;
488+}
389489
390490 static int do_protectedmain (void)
391491 {
@@ -394,16 +494,12 @@ static int do_protectedmain (void) · 2 unmodified lines
394494 jmp_buf myErrorJmp;
395495 jmp_buf *oldErr = errorJmp;
396496 errorJmp = &myErrorJmp;
397− adjustC(1); /* one slot for the pseudo-function */
398− stack[CBase].tag = LUA_T_FUNCTION;
399− stack[CBase].value.tf = &tf;
400− tf.lineDefined = 0;
497+ luaI_initTFunc(&tf);
401498 tf.fileName = lua_parsedfile;
402− tf.code = NULL;
403499 if (setjmp(myErrorJmp) == 0)
404500 {
405501 lua_parse(&tf);
406− status = do_protectedrun(0);
502+ status = luaI_dorun(&tf);
407503 }
408504 else
409505 {
@@ -411,8 +507,7 @@ static int do_protectedmain (void) · 1 unmodified line
411507 adjustC(0); /* erase extra slot */
412508 }
413509 errorJmp = oldErr;
414− if (tf.code)
415− luaI_free(tf.code);
510+ luaI_free(tf.code);
416511 return status;
417512 }
418513
@@ -449,13 +544,13 @@ int lua_call (char *funcname) · 30 unmodified lines
449544 int lua_dofile (char *filename)
450545 {
451546 int status;
452− char *message = lua_openfile (filename);
453− if (message)
454− {
455− lua_message(message);
547+ int c;
548+ FILE *f = lua_openfile(filename);
549+ if (f == NULL)
456550 return 1;
457− }
458− status = do_protectedmain();
551+ c = fgetc(f);
552+ ungetc(c, f);
553+ status = (c == ID_CHUNK) ? luaI_undump(f) : do_protectedmain();
459554 lua_closefile();
460555 return status;
461556 }
@@ -514,8 +609,9 @@ static StkId Cblocks[MAX_C_BLOCKS]; · 52 unmodified lines
514609 */
515610 void lua_beginblock (void)
516611 {
517− if (numCblocks < MAX_C_BLOCKS)
518− Cblocks[numCblocks] = CBase;
612+ if (numCblocks >= MAX_C_BLOCKS)
613+ lua_error("`lua_beginblock': too many nested blocks");
614+ Cblocks[numCblocks] = CBase;
519615 numCblocks++;
520616 }
521617
@@ -525,11 +621,8 @@ void lua_beginblock (void) · 3 unmodified lines
525621 void lua_endblock (void)
526622 {
527623 --numCblocks;
528− if (numCblocks < MAX_C_BLOCKS)
529− {
530− CBase = Cblocks[numCblocks];
531− adjustC(0);
532− }
624+ CBase = Cblocks[numCblocks];
625+ adjustC(0);
533626 }
534627
535628 /*
@@ -566,6 +659,23 @@ lua_Object lua_getparam (int number) · 30 unmodified lines
566659 return CBase-CnResults+number;
567660 }
568661
662+int lua_isnumber (lua_Object object)
663+{
664+ return (object != LUA_NOOBJECT) && (tonumber(Address(object)) == 0);
665+}
666+
667+int lua_isstring (lua_Object object)
668+{
669+ int t = lua_type(object);
670+ return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
671+}
672+
673+int lua_isfunction (lua_Object object)
674+{
675+ int t = lua_type(object);
676+ return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION);
677+}
678+
569679 /*
570680 ** Given an object handle, return its number value. On error, return 0.0.
571681 */
@@ -607,27 +717,31 @@ void *lua_getuserdata (lua_Object object) · 35 unmodified lines
607717 }
608718
609719
610−lua_Object lua_getlocked (int ref)
720+lua_Object lua_getref (int ref)
611721 {
612− adjustC(0);
613− *top = *luaI_getlocked(ref);
614− incr_top;
615− CBase++; /* incorporate object in the stack */
616− return Ref(top-1);
722+ Object *o = luaI_getref(ref);
723+ if (o == NULL)
724+ return LUA_NOOBJECT;
725+ adjustC(0);
726+ luaI_pushobject(o);
727+ CBase++; /* incorporate object in the stack */
728+ return Ref(top-1);
617729 }
618730
619731
620−void lua_pushlocked (int ref)
732+void lua_pushref (int ref)
621733 {
622− *top = *luaI_getlocked(ref);
623− incr_top;
734+ Object *o = luaI_getref(ref);
735+ if (o == NULL)
736+ lua_error("access to invalid (possibly garbage collected) reference");
737+ luaI_pushobject(o);
624738 }
625739
626740
627−int lua_lock (void)
741+int lua_ref (int lock)
628742 {
629743 adjustC(1);
630− return luaI_lock(--top);
744+ return luaI_ref(--top, lock);
631745 }
632746
633747
@@ -637,10 +751,8 @@ int lua_lock (void) · 3 unmodified lines
637751 */
638752 lua_Object lua_getglobal (char *name)
639753 {
640− Word n = luaI_findsymbolbyname(name);
641754 adjustC(0);
642− *top = s_object(n);
643− incr_top;
755+ getglobal(luaI_findsymbolbyname(name));
644756 CBase++; /* incorporate object in the stack */
645757 return Ref(top-1);
646758 }
@@ -678,20 +790,17 @@ void lua_pushnumber (real n) · 31 unmodified lines
678790 */
679791 void lua_pushstring (char *s)
680792 {
681− tsvalue(top) = lua_createstring(s);
682− tag(top) = LUA_T_STRING;
683− incr_top;
684−}
685−
686−/*
687−** Push an object (tag=string) on stack and register it on the constant table.
688−*/
689−void lua_pushliteral (char *s)
690−{
691− tsvalue(top) = lua_constant[luaI_findconstantbyname(s)];
692− tag(top) = LUA_T_STRING;
693− incr_top;
793+ if (s == NULL)
794+ tag(top) = LUA_T_NIL;
795+ else
796+ {
797+ tsvalue(top) = lua_createstring(s);
798+ tag(top) = LUA_T_STRING;
799+ }
800+ incr_top;
694801 }
802+/*>>>>>>>>>#undef lua_pushliteral
803+void lua_pushliteral(char *s) { lua_pushstring(s); }*/
695804
696805 /*
697806 ** Push an object (tag=cfunction) to stack.
@@ -707,27 +816,29 @@ void lua_pushcfunction (lua_CFunction fn) · 9 unmodified lines
707816 */
708817 void lua_pushusertag (void *u, int tag)
709818 {
710− if (tag < LUA_T_USERDATA) return;
819+ if (tag < LUA_T_USERDATA)
820+ lua_error("invalid tag in `lua_pushusertag'");
711821 tag(top) = tag; uvalue(top) = u;
712822 incr_top;
713823 }
714824
715825 /*
716−** Push a lua_Object to stack.
826+** Push an object on the stack.
717827 */
718−void lua_pushobject (lua_Object o)
828+void luaI_pushobject (Object *o)
719829 {
720− *top = *Address(o);
830+ *top = *o;
721831 incr_top;
722832 }
723833
724834 /*
725−** Push an object on the stack.
835+** Push a lua_Object on stack.
726836 */
727−void luaI_pushobject (Object *o)
837+void lua_pushobject (lua_Object o)
728838 {
729− *top = *o;
730− incr_top;
839+ if (o == LUA_NOOBJECT)
840+ lua_error("attempt to push a NOOBJECT");
841+ luaI_pushobject(Address(o));
731842 }
732843
733844 int lua_type (lua_Object o)
@@ -783,6 +894,8 @@ static void comparison (lua_Type tag_less, lua_Type tag_equal, · 49 unmodified lines
783894 */
784895 static StkId lua_execute (Byte *pc, StkId base)
785896 {
897+ if (lua_callhook)
898+ callHook (base, LUA_T_MARK, 0);
786899 while (1)
787900 {
788901 OpCode opcode;
@@ -849,8 +962,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 60 unmodified lines
849962 {
850963 CodeWord code;
851964 get_word(code,pc);
852− *top = s_object(code.w);
853− incr_top;
965+ getglobal(code.w);
854966 }
855967 break;
856968
@@ -1143,10 +1255,10 @@ static StkId lua_execute (Byte *pc, StkId base) · 286 unmodified lines
11431255 break;
11441256
11451257 case RETCODE0:
1146− return base;
1147−
11481258 case RETCODE:
1149− return base+*pc;
1259+ if (lua_callhook)
1260+ callHook (base, LUA_T_MARK, 1);
1261+ return (base + ((opcode==RETCODE0) ? 0 : *pc));
11501262
11511263 case SETLINE:
11521264 {
@@ -1160,6 +1272,8 @@ static StkId lua_execute (Byte *pc, StkId base) · 7 unmodified lines
11601272 (stack+base-1)->tag = LUA_T_LINE;
11611273 }
11621274 (stack+base-1)->value.i = code.w;
1275+ if (lua_linehook)
1276+ lineHook (code.w);
11631277 break;
11641278 }
11651279
@@ -1169,4 +1283,3 @@ static StkId lua_execute (Byte *pc, StkId base) · 3 unmodified lines
11691283 }
11701284 }
11711285
1172−
src/opcode.h+6−20
@@ -1,6 +1,6 @@
11 /*
22 ** TeCGraf - PUC-Rio
3−** $Id: opcode.h,v 3.14 1995/10/25 13:05:51 roberto Exp $
3+** $Id: opcode.h,v 3.20 1996/03/15 13:13:13 roberto Exp $
44 */
55
66 #ifndef opcode_h
@@ -11,9 +11,6 @@ · 4 unmodified lines
1111 #include "tree.h"
1212 #include "func.h"
1313
14−#ifndef real
15−#define real float
16−#endif
1714
1815 #define FIELDS_PER_FLUSH 40
1916
@@ -74,12 +71,9 @@ typedef enum · 54 unmodified lines
7471 #define MULT_RET 255
7572
7673
77−typedef void (*Cfunction) (void);
78−typedef int (*Input) (void);
79−
8074 typedef union
8175 {
82− Cfunction f;
76+ lua_CFunction f;
8377 real n;
8478 TaggedString *ts;
8579 TFunc *tf;
@@ -94,10 +88,6 @@ typedef struct Object · 8 unmodified lines
9488 Value value;
9589 } Object;
9690
97−typedef struct
98−{
99− Object object;
100−} Symbol;
10191
10292 /* Macros to access structure members */
10393 #define tag(o) ((o)->tag)
@@ -119,14 +109,14 @@ typedef struct · 15 unmodified lines
119109
120110 typedef union
121111 {
122− struct {char c1; char c2;} m;
112+ struct {Byte c1; Byte c2;} m;
123113 Word w;
124114 } CodeWord;
125115 #define get_word(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;}
126116
127117 typedef union
128118 {
129− struct {char c1; char c2; char c3; char c4;} m;
119+ struct {Byte c1; Byte c2; Byte c3; Byte c4;} m;
130120 float f;
131121 } CodeFloat;
132122 #define get_float(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
@@ -134,7 +124,7 @@ typedef union · 1 unmodified line
134124
135125 typedef union
136126 {
137− struct {char c1; char c2; char c3; char c4;} m;
127+ struct {Byte c1; Byte c2; Byte c3; Byte c4;} m;
138128 TFunc *tf;
139129 } CodeCode;
140130 #define get_code(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
@@ -142,16 +132,12 @@ typedef union · 1 unmodified line
142132
143133
144134 /* Exported functions */
145−char *lua_strdup (char *l);
146−
147−void lua_setinput (Input fn); /* from "lex.c" module */
148−char *lua_lasttext (void); /* from "lex.c" module */
149−int yylex (void); /* from "lex.c" module */
150135 void lua_parse (TFunc *tf); /* from "lua.stx" module */
151136 void luaI_codedebugline (int line); /* from "lua.stx" module */
152137 void lua_travstack (int (*fn)(Object *));
153138 Object *luaI_Address (lua_Object o);
154139 void luaI_pushobject (Object *o);
155140 void luaI_gcFB (Object *o);
141+int luaI_dorun (TFunc *tf);
156142
157143 #endif
src/parser.c+439−663
@@ -1,25 +1,26 @@
11 #if defined (__cplusplus) || defined (c_plusplus)
22 #include <c_varieties.h>
33 #ifdef __EXTERN_C__
4− EXTERN_FUNCTION ( extern int yylex, ());
4+ EXTERN_FUNCTION ( extern int luaY_lex, ());
55 #else
6− extern int yylex();
6+ extern int luaY_lex();
77 #endif
8− extern void yyerror(char *);
9− extern int yyparse();
8+ extern void luaY_error(char *);
9+ extern int luaY_parse();
1010 #endif
1111
12−#include <malloc.h>
12+#include <stdlib.h>
1313
1414 # line 2 "lua.stx"
1515
16−char *rcs_luastx = "$Id: lua.stx,v 3.25 1995/10/26 17:02:50 roberto Exp $";
16+char *rcs_luastx = "$Id: parser.c,v 1.1 1996/05/14 19:44:57 lhf Exp $";
1717
1818 #include <stdio.h>
1919 #include <stdlib.h>
20−#include <string.h>
2120
21+#include "luadebug.h"
2222 #include "mem.h"
23+#include "lex.h"
2324 #include "opcode.h"
2425 #include "hash.h"
2526 #include "inout.h"
@@ -29,7 +30,7 @@ char *rcs_luastx = "$Id: lua.stx,v 3.25 1995/10/26 17:02:50 roberto Exp $"; · 3 unmodified lines
2930 #include "func.h"
3031
3132 /* to avoid warnings generated by yacc */
32−int yyparse (void);
33+int luaY_parse (void);
3334 #define malloc luaI_malloc
3435 #define realloc luaI_realloc
3536 #define free luaI_free
@@ -43,7 +44,7 @@ int yyparse (void); · 7 unmodified lines
4344 #endif
4445 static int maxcode;
4546 static int maxmain;
46−static Long maxcurr; /* to allow maxcurr *= 2 without overflow */
47+static int maxcurr;
4748 static Byte *funcCode = NULL;
4849 static Byte **initcode;
4950 static Byte *basepc;
@@ -57,35 +58,32 @@ static Long varbuffer[MAXVAR]; /* variables in an assignment list; · 7 unmodified lines
5758 static int nvarbuffer=0; /* number of variables at a list */
5859
5960 #define MAXLOCALS 32
60−static Word localvar[MAXLOCALS]; /* store local variable names */
61+static TaggedString *localvar[MAXLOCALS]; /* store local variable names */
6162 static int nlocalvar=0; /* number of local variables */
6263
6364 #define MAXFIELDS FIELDS_PER_FLUSH*2
6465 static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
6566 static int nfields=0;
6667
68+int lua_debug = 0;
6769
6870 /* Internal functions */
6971
70−static void yyerror (char *s)
72+static void luaY_error (char *s)
7173 {
72− static char msg[256];
73− sprintf (msg,"%s near \"%s\" at line %d in file `%s'",
74− s, lua_lasttext (), lua_linenumber, lua_parsedfile);
74+ char msg[256];
75+ char *token = lua_lasttext();
76+ if (token[0] == 0)
77+ token = "<eof>";
78+ sprintf (msg,"%s; last token read: \"%s\" at line %d in file `%s'",
79+ s, token, lua_linenumber, lua_parsedfile);
7580 lua_error (msg);
7681 }
7782
7883 static void code_byte (Byte c)
7984 {
8085 if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */
81− {
82− if (maxcurr >= MAX_INT)
83− lua_error("code size overflow");
84− maxcurr *= 2;
85− if (maxcurr >= MAX_INT)
86− maxcurr = MAX_INT;
87− basepc = growvector(basepc, maxcurr, Byte);
88− }
86+ maxcurr = growvector(&basepc, maxcurr, Byte, codeEM, MAX_INT);
8987 basepc[pc++] = c;
9088 }
9189
@@ -117,10 +115,12 @@ static void code_code (TFunc *tf) · 25 unmodified lines
117115 code_byte(code.m.c4);
118116 }
119117
120−static void code_word_at (Byte *p, Word n)
118+static void code_word_at (Byte *p, int n)
121119 {
122120 CodeWord code;
123− code.w = n;
121+ if ((Word)n != n)
122+ luaY_error("block too big");
123+ code.w = (Word)n;
124124 *p++ = code.m.c1;
125125 *p++ = code.m.c2;
126126 }
@@ -130,7 +130,7 @@ static void push_field (Word name) · 3 unmodified lines
130130 if (nfields < MAXFIELDS)
131131 fields[nfields++] = name;
132132 else
133− lua_error ("too many fields in nested constructors");
133+ luaY_error ("too many fields in nested constructors");
134134 }
135135
136136 static void flush_record (int n)
@@ -155,24 +155,24 @@ static void flush_list (int m, int n) · 18 unmodified lines
155155 code_byte(m);
156156 }
157157 else
158− lua_error ("list constructor too long");
158+ luaY_error ("list constructor too long");
159159 code_byte(n);
160160 }
161161
162−static void add_localvar (Word name)
162+static void store_localvar (TaggedString *name, int n)
163163 {
164− if (nlocalvar < MAXLOCALS)
165− localvar[nlocalvar++] = name;
164+ if (nlocalvar+n < MAXLOCALS)
165+ localvar[nlocalvar+n] = name;
166166 else
167− lua_error ("too many local variables");
167+ luaY_error ("too many local variables");
168+ if (lua_debug)
169+ luaI_registerlocalvar(name, lua_linenumber);
168170 }
169171
170−static void store_localvar (Word name, int n)
172+static void add_localvar (TaggedString *name)
171173 {
172− if (nlocalvar+n < MAXLOCALS)
173− localvar[nlocalvar+n] = name;
174− else
175− lua_error ("too many local variables");
174+ store_localvar(name, 0);
175+ nlocalvar++;
176176 }
177177
178178 static void add_varbuffer (Long var)
@@ -180,7 +180,7 @@ static void add_varbuffer (Long var) · 1 unmodified line
180180 if (nvarbuffer < MAXVAR)
181181 varbuffer[nvarbuffer++] = var;
182182 else
183− lua_error ("variable buffer overflow");
183+ luaY_error ("variable buffer overflow");
184184 }
185185
186186 static void code_number (float f)
@@ -210,7 +210,7 @@ static void code_number (float f) · 23 unmodified lines
210210 /*
211211 ** Search a local name and if find return its index. If do not find return -1
212212 */
213−static int lua_localname (Word n)
213+static int lua_localname (TaggedString *n)
214214 {
215215 int i;
216216 for (i=nlocalvar-1; i >= 0; i--)
@@ -401,13 +401,12 @@ static void codeIf (Long thenAdd, Long elseAdd) · 184 unmodified lines
401401 */
402402 void lua_parse (TFunc *tf)
403403 {
404− lua_debug = 0;
405404 initcode = &(tf->code);
406405 *initcode = newvector(CODE_BLOCK, Byte);
407406 maincode = 0;
408407 maxmain = CODE_BLOCK;
409408 change2main();
410− if (yyparse ()) lua_error("parse error");
409+ if (luaY_parse ()) lua_error("parse error");
411410 savemain();
412411 (*initcode)[maincode++] = RETCODE0;
413412 tf->size = maincode;
@@ -419,7 +418,7 @@ void lua_parse (TFunc *tf) · 5 unmodified lines
419418
420419
421420
422−# line 411 "lua.stx"
421+# line 410 "lua.stx"
423422 typedef union
424423 {
425424 int vInt;
@@ -428,7 +427,7 @@ typedef union · 2 unmodified lines
428427 Word vWord;
429428 Long vLong;
430429 TFunc *pFunc;
431− TreeNode *pNode;
430+ TaggedString *pTStr;
432431 } YYSTYPE;
433432 # define WRONGTOKEN 257
434433 # define NIL 258
@@ -457,244 +456,19 @@ typedef union · 22 unmodified lines
457456 # define CONC 281
458457 # define UNARY 282
459458 # define NOT 283
460−#define yyclearin yychar = -1
461−#define yyerrok yyerrflag = 0
462−extern int yychar;
463−extern int yyerrflag;
459+#define luaY_clearin luaY_char = -1
460+#define luaY_errok luaY_errflag = 0
461+extern int luaY_char;
462+extern int luaY_errflag;
464463 #ifndef YYMAXDEPTH
465464 #define YYMAXDEPTH 150
466465 #endif
467−YYSTYPE yylval, yyval;
466+YYSTYPE luaY_lval, luaY_val;
468467 # define YYERRCODE 256
469468
470−# line 789 "lua.stx"
471−
472−
473−#if LISTING
474−
475−static void PrintCode (Byte *code, Byte *end)
476−{
477− Byte *p = code;
478− printf ("\n\nCODE\n");
479− while (p != end)
480− {
481− switch ((OpCode)*p)
482− {
483− case PUSHNIL: printf ("%d PUSHNIL\n", (p++)-code); break;
484− case PUSH0: case PUSH1: case PUSH2:
485− printf ("%d PUSH%c\n", p-code, *p-PUSH0+'0');
486− p++;
487− break;
488− case PUSHBYTE:
489− printf ("%d PUSHBYTE %d\n", p-code, *(++p));
490− p++;
491− break;
492− case PUSHWORD:
493− {
494− CodeWord c;
495− int n = p-code;
496− p++;
497− get_word(c,p);
498− printf ("%d PUSHWORD %d\n", n, c.w);
499− }
500− break;
501− case PUSHFLOAT:
502− {
503− CodeFloat c;
504− int n = p-code;
505− p++;
506− get_float(c,p);
507− printf ("%d PUSHFLOAT %f\n", n, c.f);
508− }
509− break;
510− case PUSHSTRING:
511− {
512− CodeWord c;
513− int n = p-code;
514− p++;
515− get_word(c,p);
516− printf ("%d PUSHSTRING %d\n", n, c.w);
517− }
518− break;
519− case PUSHFUNCTION:
520− {
521− CodeCode c;
522− int n = p-code;
523− p++;
524− get_code(c,p);
525− printf ("%d PUSHFUNCTION %p\n", n, c.tf);
526− }
527− break;
528−
529− case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
530− case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
531− case PUSHLOCAL8: case PUSHLOCAL9:
532− printf ("%d PUSHLOCAL%c\n", p-code, *p-PUSHLOCAL0+'0');
533− p++;
534− break;
535− case PUSHLOCAL: printf ("%d PUSHLOCAL %d\n", p-code, *(++p));
536− p++;
537− break;
538− case PUSHGLOBAL:
539− {
540− CodeWord c;
541− int n = p-code;
542− p++;
543− get_word(c,p);
544− printf ("%d PUSHGLOBAL %d\n", n, c.w);
545− }
546− break;
547− case PUSHINDEXED: printf ("%d PUSHINDEXED\n", (p++)-code); break;
548− case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: case STORELOCAL3:
549− case STORELOCAL4: case STORELOCAL5: case STORELOCAL6: case STORELOCAL7:
550− case STORELOCAL8: case STORELOCAL9:
551− printf ("%d STORELOCAL%c\n", p-code, *p-STORELOCAL0+'0');
552− p++;
553− break;
554− case STORELOCAL:
555− printf ("%d STORELOCAL %d\n", p-code, *(++p));
556− p++;
557− break;
558− case STOREGLOBAL:
559− {
560− CodeWord c;
561− int n = p-code;
562− p++;
563− get_word(c,p);
564− printf ("%d STOREGLOBAL %d\n", n, c.w);
565− }
566− break;
567− case PUSHSELF:
568− {
569− CodeWord c;
570− int n = p-code;
571− p++;
572− get_word(c,p);
573− printf ("%d PUSHSELF %d\n", n, c.w);
574− }
575− break;
576− case STOREINDEXED0: printf ("%d STOREINDEXED0\n", (p++)-code); break;
577− case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p));
578− p++;
579− break;
580− case STORELIST0:
581− printf("%d STORELIST0 %d\n", p-code, *(++p));
582− p++;
583− break;
584− case STORELIST:
585− printf("%d STORELIST %d %d\n", p-code, *(p+1), *(p+2));
586− p+=3;
587− break;
588− case STORERECORD:
589− printf("%d STORERECORD %d\n", p-code, *(++p));
590− p += *p*sizeof(Word) + 1;
591− break;
592− case ADJUST0: printf ("%d ADJUST0\n", (p++)-code); break;
593− case ADJUST:
594− printf ("%d ADJUST %d\n", p-code, *(++p));
595− p++;
596− break;
597− case CREATEARRAY:
598− {
599− CodeWord c;
600− int n = p-code;
601− p++;
602− get_word(c,p);
603− printf ("%d CREATEARRAY %d\n", n, c.w);
604− break;
605− }
606− case EQOP: printf ("%d EQOP\n", (p++)-code); break;
607− case LTOP: printf ("%d LTOP\n", (p++)-code); break;
608− case LEOP: printf ("%d LEOP\n", (p++)-code); break;
609− case ADDOP: printf ("%d ADDOP\n", (p++)-code); break;
610− case SUBOP: printf ("%d SUBOP\n", (p++)-code); break;
611− case MULTOP: printf ("%d MULTOP\n", (p++)-code); break;
612− case DIVOP: printf ("%d DIVOP\n", (p++)-code); break;
613− case POWOP: printf ("%d POWOP\n", (p++)-code); break;
614− case CONCOP: printf ("%d CONCOP\n", (p++)-code); break;
615− case MINUSOP: printf ("%d MINUSOP\n", (p++)-code); break;
616− case NOTOP: printf ("%d NOTOP\n", (p++)-code); break;
617− case ONTJMP:
618− {
619− CodeWord c;
620− int n = p-code;
621− p++;
622− get_word(c,p);
623− printf ("%d ONTJMP %d\n", n, c.w);
624− }
625− break;
626− case ONFJMP:
627− {
628− CodeWord c;
629− int n = p-code;
630− p++;
631− get_word(c,p);
632− printf ("%d ONFJMP %d\n", n, c.w);
633− }
634− break;
635− case JMP:
636− {
637− CodeWord c;
638− int n = p-code;
639− p++;
640− get_word(c,p);
641− printf ("%d JMP %d\n", n, c.w);
642− }
643− break;
644− case UPJMP:
645− {
646− CodeWord c;
647− int n = p-code;
648− p++;
649− get_word(c,p);
650− printf ("%d UPJMP %d\n", n, c.w);
651− }
652− break;
653− case IFFJMP:
654− {
655− CodeWord c;
656− int n = p-code;
657− p++;
658− get_word(c,p);
659− printf ("%d IFFJMP %d\n", n, c.w);
660− }
661− break;
662− case IFFUPJMP:
663− {
664− CodeWord c;
665− int n = p-code;
666− p++;
667− get_word(c,p);
668− printf ("%d IFFUPJMP %d\n", n, c.w);
669− }
670− break;
671− case POP: printf ("%d POP\n", (p++)-code); break;
672− case CALLFUNC:
673− printf ("%d CALLFUNC %d %d\n", p-code, *(p+1), *(p+2));
674− p+=3;
675− break;
676− case RETCODE0: printf ("%d RETCODE0\n", (p++)-code); break;
677− case RETCODE:
678− printf ("%d RETCODE %d\n", p-code, *(++p));
679− p++;
680− break;
681− case SETLINE:
682− {
683− CodeWord c;
684− int n = p-code;
685− p++;
686− get_word(c,p);
687− printf ("%d SETLINE %d\n", n, c.w);
688− }
689− break;
690−
691− default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break;
692− }
693− }
694−}
695−#endif
469+# line 788 "lua.stx"
696470
697−int yyexca[] ={
471+int luaY_exca[] ={
698472 -1, 1,
699473 0, -1,
700474 -2, 0,
@@ -760,7 +534,7 @@ int yyexca[] ={ · 59 unmodified lines
760534 };
761535 # define YYNPROD 100
762536 # define YYLAST 311
763−int yyact[]={
537+int luaY_act[]={
764538
765539 61, 59, 148, 60, 141, 62, 118, 61, 59, 90,
766540 60, 89, 62, 86, 84, 18, 42, 168, 54, 164,
@@ -794,7 +568,7 @@ int yyact[]={ · 27 unmodified lines
794568 0, 0, 0, 0, 0, 30, 31, 18, 30, 31,
795569 113, 0, 0, 0, 64, 0, 0, 34, 0, 0,
796570 34 };
797−int yypact[]={
571+int luaY_pact[]={
798572
799573 -1000, -188, -1000, -1000, 51, -1000, -258, 24, -1000, -1000,
800574 47, -1000, -257, -1000, -1000, 93, -1000, 73, -1000, -1000,
@@ -814,14 +588,14 @@ int yypact[]={ · 13 unmodified lines
814588 -21, -1000, -1000, -1000, 51, -1000, -1000, -248, -1000, 24,
815589 -1000, 69, -250, -1000, -1000, -1000, -42, -1000, -1000, -1000,
816590 -1000, -1000, -212, -1000 };
817−int yypgo[]={
591+int luaY_pgo[]={
818592
819593 0, 216, 54, 44, 138, 76, 57, 212, 211, 210,
820594 205, 202, 201, 199, 61, 198, 195, 194, 189, 159,
821595 188, 186, 185, 184, 182, 92, 37, 181, 130, 32,
822596 180, 88, 34, 177, 176, 175, 172, 141, 170, 168,
823597 165, 163, 154, 134, 51, 56 };
824−int yyr1[]={
598+int luaY_r1[]={
825599
826600 0, 1, 1, 1, 23, 23, 24, 21, 21, 22,
827601 30, 30, 26, 26, 25, 33, 25, 34, 25, 25,
@@ -833,7 +607,7 @@ int yyr1[]={ · 5 unmodified lines
833607 42, 42, 43, 10, 10, 15, 15, 44, 44, 13,
834608 13, 14, 14, 45, 16, 16, 17, 17, 7, 7,
835609 19, 19, 19, 20, 28, 11, 11, 12, 12, 27 };
836−int yyr2[]={
610+int luaY_r2[]={
837611
838612 0, 0, 4, 4, 4, 2, 7, 3, 7, 11,
839613 0, 6, 0, 2, 17, 1, 17, 1, 13, 7,
@@ -845,7 +619,7 @@ int yyr2[]={ · 5 unmodified lines
845619 3, 7, 1, 7, 5, 1, 5, 0, 2, 1,
846620 5, 3, 7, 7, 1, 5, 3, 7, 3, 7,
847621 3, 9, 7, 3, 3, 3, 7, 1, 5, 3 };
848−int yychk[]={
622+int luaY_chk[]={
849623
850624 -1000, -1, -23, -24, -25, -27, 270, 259, 263, 265,
851625 -7, -5, 269, 274, -19, -9, -20, -28, 273, -26,
@@ -865,7 +639,7 @@ int yychk[]={ · 13 unmodified lines
865639 -31, -29, -2, -26, -3, -6, 267, -32, 261, 262,
866640 -13, -14, -2, -26, 267, -29, -31, -44, 267, 260,
867641 -2, -29, -2, -32 };
868−int yydef[]={
642+int luaY_def[]={
869643
870644 1, -2, 2, 3, 12, 5, 0, 56, 15, 17,
871645 0, 20, 0, 99, -2, 56, 90, 59, 93, 4,
@@ -885,14 +659,14 @@ int yydef[]={ · 13 unmodified lines
885659 83, 29, 18, 11, 12, -2, 9, 0, 25, 56,
886660 76, 77, 0, 28, 14, 23, 0, 80, 16, 29,
887661 25, 29, 22, 24 };
888−typedef struct { char *t_name; int t_val; } yytoktype;
662+typedef struct { char *t_name; int t_val; } luaY_toktype;
889663 #ifndef YYDEBUG
890664 # define YYDEBUG 0 /* don't allow debugging */
891665 #endif
892666
893667 #if YYDEBUG
894668
895−yytoktype yytoks[] =
669+luaY_toktype luaY_toks[] =
896670 {
897671 "WRONGTOKEN", 257,
898672 "NIL", 258,
@@ -931,7 +705,7 @@ yytoktype yytoks[] = · 32 unmodified lines
931705 "-unknown-", -1 /* ends search */
932706 };
933707
934−char * yyreds[] =
708+char * luaY_reds[] =
935709 {
936710 "-no such reduction-",
937711 "functionlist : /* empty */",
@@ -1048,24 +822,24 @@ char * yyreds[] = · 110 unmodified lines
1048822 /*
1049823 ** yacc user known macros and defines
1050824 */
1051−#define YYERROR goto yyerrlab
825+#define YYERROR goto luaY_errlab
1052826 #define YYACCEPT return(0)
1053827 #define YYABORT return(1)
1054828 #define YYBACKUP( newtoken, newvalue )\
1055829 {\
1056− if ( yychar >= 0 || ( yyr2[ yytmp ] >> 1 ) != 1 )\
830+ if ( luaY_char >= 0 || ( luaY_r2[ luaY_tmp ] >> 1 ) != 1 )\
1057831 {\
1058− yyerror( "syntax error - cannot backup" );\
1059− goto yyerrlab;\
832+ luaY_error( "syntax error - cannot backup" );\
833+ goto luaY_errlab;\
1060834 }\
1061− yychar = newtoken;\
1062− yystate = *yyps;\
1063− yylval = newvalue;\
1064− goto yynewstate;\
835+ luaY_char = newtoken;\
836+ luaY_state = *luaY_ps;\
837+ luaY_lval = newvalue;\
838+ goto luaY_newstate;\
1065839 }
1066−#define YYRECOVERING() (!!yyerrflag)
840+#define YYRECOVERING() (!!luaY_errflag)
1067841 #define YYCOPY(to, from, type) \
1068− (type *) memcpy(to, (char *) from, yynewmax * sizeof(type))
842+ (type *) memcpy(to, (char *) from, luaY_newmax * sizeof(type))
1069843 #ifndef YYDEBUG
1070844 # define YYDEBUG 1 /* make debugging available */
1071845 #endif
@@ -1085,7 +859,7 @@ EXTERN_FUNCTION ( extern void *memcpy, (void *, const void *, int) ); · 13 unmodified lines
1085859 /*
1086860 ** user known globals
1087861 */
1088−int yydebug; /* set to 1 to get debugging */
862+int luaY_debug; /* set to 1 to get debugging */
1089863
1090864 /*
1091865 ** driver internal defines
@@ -1095,50 +869,50 @@ int yydebug; /* set to 1 to get debugging */ · 3 unmodified lines
1095869 /*
1096870 ** static variables used by the parser
1097871 */
1098−static YYSTYPE yy_yyv[YYMAXDEPTH], *yyv = yy_yyv; /* value stack */
1099−static int yy_yys[YYMAXDEPTH], *yys = yy_yys; /* state stack */
872+static YYSTYPE luaY__luaY_v[YYMAXDEPTH], *luaY_v = luaY__luaY_v; /* value stack */
873+static int luaY__luaY_s[YYMAXDEPTH], *luaY_s = luaY__luaY_s; /* state stack */
1100874
1101−static YYSTYPE *yypv; /* top of value stack */
1102−static int *yyps; /* top of state stack */
875+static YYSTYPE *luaY_pv; /* top of value stack */
876+static int *luaY_ps; /* top of state stack */
1103877
1104−static int yystate; /* current state */
1105−static int yytmp; /* extra var (lasts between blocks) */
878+static int luaY_state; /* current state */
879+static int luaY_tmp; /* extra var (lasts between blocks) */
1106880
1107881 #if defined(__cplusplus) || defined(__STDC__) || defined(lint)
1108882 static int __yaccpar_lint_hack__ = 0;
1109883 /* if you change the value from 0 to
1110884 something else, make sure you know
1111− what to do with yyerrlab reference.
885+ what to do with luaY_errlab reference.
1112886 This is a hack - to make sure C++ and
1113887 lint are happy with the 4.1 yacc code. */
1114888 #endif
1115889
1116−int yynerrs; /* number of errors */
890+int luaY_nerrs; /* number of errors */
1117891
1118−int yyerrflag; /* error recovery flag */
1119−int yychar; /* current input token number */
1120−static unsigned yymaxdepth = YYMAXDEPTH;
892+int luaY_errflag; /* error recovery flag */
893+int luaY_char; /* current input token number */
894+static unsigned luaY_maxdepth = YYMAXDEPTH;
1121895
1122896
1123897 /*
1124−** yyparse - return 0 if worked, 1 if syntax error not recovered from
898+** luaY_parse - return 0 if worked, 1 if syntax error not recovered from
1125899 */
1126900 int
1127−yyparse()
901+luaY_parse()
1128902 {
1129− register YYSTYPE *yypvt = (YYSTYPE*)0 ; /* top of value stack for
903+ register YYSTYPE *luaY_pvt = (YYSTYPE*)0 ; /* top of value stack for
1130904 $vars */
1131905
1132906 /*
1133− ** Initialize externals - yyparse may be called more than once
907+ ** Initialize externals - luaY_parse may be called more than once
1134908 */
1135− yypv = &yyv[-1];
1136− yyps = &yys[-1];
1137− yystate = 0;
1138− yytmp = 0;
1139− yynerrs = 0;
1140− yyerrflag = 0;
1141− yychar = -1;
909+ luaY_pv = &luaY_v[-1];
910+ luaY_ps = &luaY_s[-1];
911+ luaY_state = 0;
912+ luaY_tmp = 0;
913+ luaY_nerrs = 0;
914+ luaY_errflag = 0;
915+ luaY_char = -1;
1142916
1143917 #if defined(__cplusplus) || defined(__STDC__) || defined(lint)
1144918 /*
@@ -1147,42 +921,42 @@ $vars */ · 2 unmodified lines
1147921 */
1148922 switch (__yaccpar_lint_hack__)
1149923 {
1150− case 1: goto yyerrlab;
1151− case 2: goto yynewstate;
924+ case 1: goto luaY_errlab;
925+ case 2: goto luaY_newstate;
1152926 }
1153927 #endif
1154928
1155929 {
1156− register YYSTYPE *yy_pv; /* top of value stack */
1157− register int *yy_ps; /* top of state stack */
1158− register int yy_state; /* current state */
1159− register int yy_n; /* internal state number info */
930+ register YYSTYPE *luaY__pv; /* top of value stack */
931+ register int *luaY__ps; /* top of state stack */
932+ register int luaY__state; /* current state */
933+ register int luaY__n; /* internal state number info */
1160934
1161− goto yystack;
935+ goto luaY_stack;
1162936
1163937 /*
1164938 ** get globals into registers.
1165939 ** branch to here only if YYBACKUP was called.
1166940 */
1167− yynewstate:
1168− yy_pv = yypv;
1169− yy_ps = yyps;
1170− yy_state = yystate;
1171− goto yy_newstate;
941+ luaY_newstate:
942+ luaY__pv = luaY_pv;
943+ luaY__ps = luaY_ps;
944+ luaY__state = luaY_state;
945+ goto luaY__newstate;
1172946
1173947 /*
1174948 ** get globals into registers.
1175949 ** either we just started, or we just finished a reduction
1176950 */
1177− yystack:
1178− yy_pv = yypv;
1179− yy_ps = yyps;
1180− yy_state = yystate;
951+ luaY_stack:
952+ luaY__pv = luaY_pv;
953+ luaY__ps = luaY_ps;
954+ luaY__state = luaY_state;
1181955
1182956 /*
1183957 ** top of for (;;) loop while no reductions done
1184958 */
1185− yy_stack:
959+ luaY__stack:
1186960 /*
1187961 ** put a state and value onto the stacks
1188962 */
@@ -1193,153 +967,153 @@ $vars */ · 4 unmodified lines
1193967 ** Note: linear search is used since time is not a real
1194968 ** consideration while debugging.
1195969 */
1196− if ( yydebug )
970+ if ( luaY_debug )
1197971 {
1198− register int yy_i;
972+ register int luaY__i;
1199973
1200− (void)printf( "State %d, token ", yy_state );
1201− if ( yychar == 0 )
974+ (void)printf( "State %d, token ", luaY__state );
975+ if ( luaY_char == 0 )
1202976 (void)printf( "end-of-file\n" );
1203− else if ( yychar < 0 )
977+ else if ( luaY_char < 0 )
1204978 (void)printf( "-none-\n" );
1205979 else
1206980 {
1207− for ( yy_i = 0; yytoks[yy_i].t_val >= 0;
1208− yy_i++ )
981+ for ( luaY__i = 0; luaY_toks[luaY__i].t_val >= 0;
982+ luaY__i++ )
1209983 {
1210− if ( yytoks[yy_i].t_val == yychar )
984+ if ( luaY_toks[luaY__i].t_val == luaY_char )
1211985 break;
1212986 }
1213− (void)printf( "%s\n", yytoks[yy_i].t_name );
987+ (void)printf( "%s\n", luaY_toks[luaY__i].t_name );
1214988 }
1215989 }
1216990 #endif /* YYDEBUG */
1217− if ( ++yy_ps >= &yys[ yymaxdepth ] ) /* room on stack? */
991+ if ( ++luaY__ps >= &luaY_s[ luaY_maxdepth ] ) /* room on stack? */
1218992 {
1219993 /*
1220994 ** reallocate and recover. Note that pointers
1221995 ** have to be reset, or bad things will happen
1222996 */
1223− int yyps_index = (yy_ps - yys);
1224− int yypv_index = (yy_pv - yyv);
1225− int yypvt_index = (yypvt - yyv);
1226− int yynewmax;
997+ int luaY_ps_index = (luaY__ps - luaY_s);
998+ int luaY_pv_index = (luaY__pv - luaY_v);
999+ int luaY_pvt_index = (luaY_pvt - luaY_v);
1000+ int luaY_newmax;
12271001
1228− yynewmax = yymaxdepth + YYMAXDEPTH;
1229− if (yymaxdepth == YYMAXDEPTH) /* first time growth */
1002+ luaY_newmax = luaY_maxdepth + YYMAXDEPTH;
1003+ if (luaY_maxdepth == YYMAXDEPTH) /* first time growth */
12301004 {
1231− YYSTYPE *newyyv = (YYSTYPE*)malloc(yynewmax*sizeof(YYSTYPE));
1232− int *newyys = (int*)malloc(yynewmax*sizeof(int));
1233− if (newyys != 0 && newyyv != 0)
1005+ YYSTYPE *newluaY_v = (YYSTYPE*)malloc(luaY_newmax*sizeof(YYSTYPE));
1006+ int *newluaY_s = (int*)malloc(luaY_newmax*sizeof(int));
1007+ if (newluaY_s != 0 && newluaY_v != 0)
12341008 {
1235− yys = YYCOPY(newyys, yys, int);
1236− yyv = YYCOPY(newyyv, yyv, YYSTYPE);
1009+ luaY_s = YYCOPY(newluaY_s, luaY_s, int);
1010+ luaY_v = YYCOPY(newluaY_v, luaY_v, YYSTYPE);
12371011 }
12381012 else
1239− yynewmax = 0; /* failed */
1013+ luaY_newmax = 0; /* failed */
12401014 }
12411015 else /* not first time */
12421016 {
1243− yyv = (YYSTYPE*)realloc((char*)yyv,
1244− yynewmax * sizeof(YYSTYPE));
1245− yys = (int*)realloc((char*)yys,
1246− yynewmax * sizeof(int));
1247− if (yys == 0 || yyv == 0)
1248− yynewmax = 0; /* failed */
1017+ luaY_v = (YYSTYPE*)realloc((char*)luaY_v,
1018+ luaY_newmax * sizeof(YYSTYPE));
1019+ luaY_s = (int*)realloc((char*)luaY_s,
1020+ luaY_newmax * sizeof(int));
1021+ if (luaY_s == 0 || luaY_v == 0)
1022+ luaY_newmax = 0; /* failed */
12491023 }
1250− if (yynewmax <= yymaxdepth) /* tables not expanded */
1024+ if (luaY_newmax <= luaY_maxdepth) /* tables not expanded */
12511025 {
1252− yyerror( "yacc stack overflow" );
1026+ luaY_error( "yacc stack overflow" );
12531027 YYABORT;
12541028 }
1255− yymaxdepth = yynewmax;
1029+ luaY_maxdepth = luaY_newmax;
12561030
1257− yy_ps = yys + yyps_index;
1258− yy_pv = yyv + yypv_index;
1259− yypvt = yyv + yypvt_index;
1031+ luaY__ps = luaY_s + luaY_ps_index;
1032+ luaY__pv = luaY_v + luaY_pv_index;
1033+ luaY_pvt = luaY_v + luaY_pvt_index;
12601034 }
1261− *yy_ps = yy_state;
1262− *++yy_pv = yyval;
1035+ *luaY__ps = luaY__state;
1036+ *++luaY__pv = luaY_val;
12631037
12641038 /*
12651039 ** we have a new state - find out what to do
12661040 */
1267− yy_newstate:
1268− if ( ( yy_n = yypact[ yy_state ] ) <= YYFLAG )
1269− goto yydefault; /* simple state */
1041+ luaY__newstate:
1042+ if ( ( luaY__n = luaY_pact[ luaY__state ] ) <= YYFLAG )
1043+ goto luaY_default; /* simple state */
12701044 #if YYDEBUG
12711045 /*
12721046 ** if debugging, need to mark whether new token grabbed
12731047 */
1274− yytmp = yychar < 0;
1048+ luaY_tmp = luaY_char < 0;
12751049 #endif
1276− if ( ( yychar < 0 ) && ( ( yychar = yylex() ) < 0 ) )
1277− yychar = 0; /* reached EOF */
1050+ if ( ( luaY_char < 0 ) && ( ( luaY_char = luaY_lex() ) < 0 ) )
1051+ luaY_char = 0; /* reached EOF */
12781052 #if YYDEBUG
1279− if ( yydebug && yytmp )
1053+ if ( luaY_debug && luaY_tmp )
12801054 {
1281− register int yy_i;
1055+ register int luaY__i;
12821056
12831057 (void)printf( "Received token " );
1284− if ( yychar == 0 )
1058+ if ( luaY_char == 0 )
12851059 (void)printf( "end-of-file\n" );
1286− else if ( yychar < 0 )
1060+ else if ( luaY_char < 0 )
12871061 (void)printf( "-none-\n" );
12881062 else
12891063 {
1290− for ( yy_i = 0; yytoks[yy_i].t_val >= 0;
1291− yy_i++ )
1064+ for ( luaY__i = 0; luaY_toks[luaY__i].t_val >= 0;
1065+ luaY__i++ )
12921066 {
1293− if ( yytoks[yy_i].t_val == yychar )
1067+ if ( luaY_toks[luaY__i].t_val == luaY_char )
12941068 break;
12951069 }
1296− (void)printf( "%s\n", yytoks[yy_i].t_name );
1070+ (void)printf( "%s\n", luaY_toks[luaY__i].t_name );
12971071 }
12981072 }
12991073 #endif /* YYDEBUG */
1300− if ( ( ( yy_n += yychar ) < 0 ) || ( yy_n >= YYLAST ) )
1301− goto yydefault;
1302− if ( yychk[ yy_n = yyact[ yy_n ] ] == yychar ) /*valid shift*/
1074+ if ( ( ( luaY__n += luaY_char ) < 0 ) || ( luaY__n >= YYLAST ) )
1075+ goto luaY_default;
1076+ if ( luaY_chk[ luaY__n = luaY_act[ luaY__n ] ] == luaY_char ) /*valid shift*/
13031077 {
1304− yychar = -1;
1305− yyval = yylval;
1306− yy_state = yy_n;
1307− if ( yyerrflag > 0 )
1308− yyerrflag--;
1309− goto yy_stack;
1078+ luaY_char = -1;
1079+ luaY_val = luaY_lval;
1080+ luaY__state = luaY__n;
1081+ if ( luaY_errflag > 0 )
1082+ luaY_errflag--;
1083+ goto luaY__stack;
13101084 }
13111085
1312− yydefault:
1313− if ( ( yy_n = yydef[ yy_state ] ) == -2 )
1086+ luaY_default:
1087+ if ( ( luaY__n = luaY_def[ luaY__state ] ) == -2 )
13141088 {
13151089 #if YYDEBUG
1316− yytmp = yychar < 0;
1090+ luaY_tmp = luaY_char < 0;
13171091 #endif
1318− if ( ( yychar < 0 ) && ( ( yychar = yylex() ) < 0 ) )
1319− yychar = 0; /* reached EOF */
1092+ if ( ( luaY_char < 0 ) && ( ( luaY_char = luaY_lex() ) < 0 ) )
1093+ luaY_char = 0; /* reached EOF */
13201094 #if YYDEBUG
1321− if ( yydebug && yytmp )
1095+ if ( luaY_debug && luaY_tmp )
13221096 {
1323− register int yy_i;
1097+ register int luaY__i;
13241098
13251099 (void)printf( "Received token " );
1326− if ( yychar == 0 )
1100+ if ( luaY_char == 0 )
13271101 (void)printf( "end-of-file\n" );
1328− else if ( yychar < 0 )
1102+ else if ( luaY_char < 0 )
13291103 (void)printf( "-none-\n" );
13301104 else
13311105 {
1332− for ( yy_i = 0;
1333− yytoks[yy_i].t_val >= 0;
1334− yy_i++ )
1106+ for ( luaY__i = 0;
1107+ luaY_toks[luaY__i].t_val >= 0;
1108+ luaY__i++ )
13351109 {
1336− if ( yytoks[yy_i].t_val
1337− == yychar )
1110+ if ( luaY_toks[luaY__i].t_val
1111+ == luaY_char )
13381112 {
13391113 break;
13401114 }
13411115 }
1342− (void)printf( "%s\n", yytoks[yy_i].t_name );
1116+ (void)printf( "%s\n", luaY_toks[luaY__i].t_name );
13431117 }
13441118 }
13451119 #endif /* YYDEBUG */
@@ -1347,17 +1121,17 @@ $vars */ · 1 unmodified line
13471121 ** look through exception table
13481122 */
13491123 {
1350− register int *yyxi = yyexca;
1124+ register int *luaY_xi = luaY_exca;
13511125
1352− while ( ( *yyxi != -1 ) ||
1353− ( yyxi[1] != yy_state ) )
1126+ while ( ( *luaY_xi != -1 ) ||
1127+ ( luaY_xi[1] != luaY__state ) )
13541128 {
1355− yyxi += 2;
1129+ luaY_xi += 2;
13561130 }
1357− while ( ( *(yyxi += 2) >= 0 ) &&
1358− ( *yyxi != yychar ) )
1131+ while ( ( *(luaY_xi += 2) >= 0 ) &&
1132+ ( *luaY_xi != luaY_char ) )
13591133 ;
1360− if ( ( yy_n = yyxi[1] ) < 0 )
1134+ if ( ( luaY__n = luaY_xi[1] ) < 0 )
13611135 YYACCEPT;
13621136 }
13631137 }
@@ -1365,42 +1139,42 @@ $vars */ · 1 unmodified line
13651139 /*
13661140 ** check for syntax error
13671141 */
1368− if ( yy_n == 0 ) /* have an error */
1142+ if ( luaY__n == 0 ) /* have an error */
13691143 {
13701144 /* no worry about speed here! */
1371− switch ( yyerrflag )
1145+ switch ( luaY_errflag )
13721146 {
13731147 case 0: /* new error */
1374− yyerror( "syntax error" );
1148+ luaY_error( "syntax error" );
13751149 goto skip_init;
1376− yyerrlab:
1150+ luaY_errlab:
13771151 /*
13781152 ** get globals into registers.
13791153 ** we have a user generated syntax type error
13801154 */
1381− yy_pv = yypv;
1382− yy_ps = yyps;
1383− yy_state = yystate;
1384− yynerrs++;
1155+ luaY__pv = luaY_pv;
1156+ luaY__ps = luaY_ps;
1157+ luaY__state = luaY_state;
1158+ luaY_nerrs++;
13851159 skip_init:
13861160 case 1:
13871161 case 2: /* incompletely recovered error */
13881162 /* try again... */
1389− yyerrflag = 3;
1163+ luaY_errflag = 3;
13901164 /*
13911165 ** find state where "error" is a legal
13921166 ** shift action
13931167 */
1394− while ( yy_ps >= yys )
1168+ while ( luaY__ps >= luaY_s )
13951169 {
1396− yy_n = yypact[ *yy_ps ] + YYERRCODE;
1397− if ( yy_n >= 0 && yy_n < YYLAST &&
1398− yychk[yyact[yy_n]] == YYERRCODE) {
1170+ luaY__n = luaY_pact[ *luaY__ps ] + YYERRCODE;
1171+ if ( luaY__n >= 0 && luaY__n < YYLAST &&
1172+ luaY_chk[luaY_act[luaY__n]] == YYERRCODE) {
13991173 /*
14001174 ** simulate shift of "error"
14011175 */
1402− yy_state = yyact[ yy_n ];
1403− goto yy_stack;
1176+ luaY__state = luaY_act[ luaY__n ];
1177+ goto luaY__stack;
14041178 }
14051179 /*
14061180 ** current state has no shift on
@@ -1408,13 +1182,13 @@ $vars */ · 1 unmodified line
14081182 */
14091183 #if YYDEBUG
14101184 # define _POP_ "Error recovery pops state %d, uncovers state %d\n"
1411− if ( yydebug )
1412− (void)printf( _POP_, *yy_ps,
1413− yy_ps[-1] );
1185+ if ( luaY_debug )
1186+ (void)printf( _POP_, *luaY__ps,
1187+ luaY__ps[-1] );
14141188 # undef _POP_
14151189 #endif
1416− yy_ps--;
1417− yy_pv--;
1190+ luaY__ps--;
1191+ luaY__pv--;
14181192 }
14191193 /*
14201194 ** there is no state on stack with "error" as
@@ -1430,40 +1204,40 @@ $vars */ · 9 unmodified lines
14301204 ** debugging, it doesn't hurt to leave the
14311205 ** tests here.
14321206 */
1433− if ( yydebug )
1207+ if ( luaY_debug )
14341208 {
1435− register int yy_i;
1209+ register int luaY__i;
14361210
14371211 (void)printf( "Error recovery discards " );
1438− if ( yychar == 0 )
1212+ if ( luaY_char == 0 )
14391213 (void)printf( "token end-of-file\n" );
1440− else if ( yychar < 0 )
1214+ else if ( luaY_char < 0 )
14411215 (void)printf( "token -none-\n" );
14421216 else
14431217 {
1444− for ( yy_i = 0;
1445− yytoks[yy_i].t_val >= 0;
1446− yy_i++ )
1218+ for ( luaY__i = 0;
1219+ luaY_toks[luaY__i].t_val >= 0;
1220+ luaY__i++ )
14471221 {
1448− if ( yytoks[yy_i].t_val
1449− == yychar )
1222+ if ( luaY_toks[luaY__i].t_val
1223+ == luaY_char )
14501224 {
14511225 break;
14521226 }
14531227 }
14541228 (void)printf( "token %s\n",
1455− yytoks[yy_i].t_name );
1229+ luaY_toks[luaY__i].t_name );
14561230 }
14571231 }
14581232 #endif /* YYDEBUG */
1459− if ( yychar == 0 ) /* reached EOF. quit */
1233+ if ( luaY_char == 0 ) /* reached EOF. quit */
14601234 YYABORT;
1461− yychar = -1;
1462− goto yy_newstate;
1235+ luaY_char = -1;
1236+ goto luaY__newstate;
14631237 }
1464− }/* end if ( yy_n == 0 ) */
1238+ }/* end if ( luaY__n == 0 ) */
14651239 /*
1466− ** reduction by production yy_n
1240+ ** reduction by production luaY__n
14671241 ** put stack tops, etc. so things right after switch
14681242 */
14691243 #if YYDEBUG
@@ -1472,92 +1246,95 @@ $vars */ · 2 unmodified lines
14721246 ** specification of the reduction which is just about
14731247 ** to be done.
14741248 */
1475− if ( yydebug )
1249+ if ( luaY_debug )
14761250 (void)printf( "Reduce by (%d) \"%s\"\n",
1477− yy_n, yyreds[ yy_n ] );
1251+ luaY__n, luaY_reds[ luaY__n ] );
14781252 #endif
1479− yytmp = yy_n; /* value to switch over */
1480− yypvt = yy_pv; /* $vars top of value stack */
1253+ luaY_tmp = luaY__n; /* value to switch over */
1254+ luaY_pvt = luaY__pv; /* $vars top of value stack */
14811255 /*
14821256 ** Look in goto table for next state
1483− ** Sorry about using yy_state here as temporary
1257+ ** Sorry about using luaY__state here as temporary
14841258 ** register variable, but why not, if it works...
1485− ** If yyr2[ yy_n ] doesn't have the low order bit
1259+ ** If luaY_r2[ luaY__n ] doesn't have the low order bit
14861260 ** set, then there is no action to be done for
14871261 ** this reduction. So, no saving & unsaving of
14881262 ** registers done. The only difference between the
14891263 ** code just after the if and the body of the if is
1490− ** the goto yy_stack in the body. This way the test
1264+ ** the goto luaY__stack in the body. This way the test
14911265 ** can be made before the choice of what to do is needed.
14921266 */
14931267 {
14941268 /* length of production doubled with extra bit */
1495− register int yy_len = yyr2[ yy_n ];
1269+ register int luaY__len = luaY_r2[ luaY__n ];
14961270
1497− if ( !( yy_len & 01 ) )
1271+ if ( !( luaY__len & 01 ) )
14981272 {
1499− yy_len >>= 1;
1500− yyval = ( yy_pv -= yy_len )[1]; /* $$ = $1 */
1501− yy_state = yypgo[ yy_n = yyr1[ yy_n ] ] +
1502− *( yy_ps -= yy_len ) + 1;
1503− if ( yy_state >= YYLAST ||
1504− yychk[ yy_state =
1505− yyact[ yy_state ] ] != -yy_n )
1273+ luaY__len >>= 1;
1274+ luaY_val = ( luaY__pv -= luaY__len )[1]; /* $$ = $1 */
1275+ luaY__state = luaY_pgo[ luaY__n = luaY_r1[ luaY__n ] ] +
1276+ *( luaY__ps -= luaY__len ) + 1;
1277+ if ( luaY__state >= YYLAST ||
1278+ luaY_chk[ luaY__state =
1279+ luaY_act[ luaY__state ] ] != -luaY__n )
15061280 {
1507− yy_state = yyact[ yypgo[ yy_n ] ];
1281+ luaY__state = luaY_act[ luaY_pgo[ luaY__n ] ];
15081282 }
1509− goto yy_stack;
1283+ goto luaY__stack;
15101284 }
1511− yy_len >>= 1;
1512− yyval = ( yy_pv -= yy_len )[1]; /* $$ = $1 */
1513− yy_state = yypgo[ yy_n = yyr1[ yy_n ] ] +
1514− *( yy_ps -= yy_len ) + 1;
1515− if ( yy_state >= YYLAST ||
1516− yychk[ yy_state = yyact[ yy_state ] ] != -yy_n )
1285+ luaY__len >>= 1;
1286+ luaY_val = ( luaY__pv -= luaY__len )[1]; /* $$ = $1 */
1287+ luaY__state = luaY_pgo[ luaY__n = luaY_r1[ luaY__n ] ] +
1288+ *( luaY__ps -= luaY__len ) + 1;
1289+ if ( luaY__state >= YYLAST ||
1290+ luaY_chk[ luaY__state = luaY_act[ luaY__state ] ] != -luaY__n )
15171291 {
1518− yy_state = yyact[ yypgo[ yy_n ] ];
1292+ luaY__state = luaY_act[ luaY_pgo[ luaY__n ] ];
15191293 }
15201294 }
15211295 /* save until reenter driver code */
1522− yystate = yy_state;
1523− yyps = yy_ps;
1524− yypv = yy_pv;
1296+ luaY_state = luaY__state;
1297+ luaY_ps = luaY__ps;
1298+ luaY_pv = luaY__pv;
15251299 }
15261300 /*
15271301 ** code supplied by user is placed in this switch
15281302 */
1529− switch( yytmp )
1303+ switch( luaY_tmp )
15301304 {
15311305
15321306 case 6:
1533−# line 470 "lua.stx"
1307+# line 469 "lua.stx"
15341308 {
15351309 code_byte(PUSHFUNCTION);
1536− code_code(yypvt[-0].pFunc);
1537− storesinglevar(yypvt[-1].vLong);
1310+ code_code(luaY_pvt[-0].pFunc);
1311+ storesinglevar(luaY_pvt[-1].vLong);
15381312 } break;
15391313 case 7:
1540−# line 477 "lua.stx"
1541−{ yyval.vLong =yypvt[-0].vLong; init_func(); } break;
1314+# line 476 "lua.stx"
1315+{ luaY_val.vLong =luaY_pvt[-0].vLong; init_func(); } break;
15421316 case 8:
1543−# line 479 "lua.stx"
1317+# line 478 "lua.stx"
15441318 {
15451319 code_byte(PUSHSTRING);
1546− code_word(luaI_findconstant(yypvt[-0].pNode));
1547− yyval.vLong = 0; /* indexed variable */
1320+ code_word(luaI_findconstant(luaY_pvt[-0].pTStr));
1321+ luaY_val.vLong = 0; /* indexed variable */
15481322 init_func();
1549− add_localvar(luaI_findsymbolbyname("self"));
1323+ add_localvar(luaI_createfixedstring("self"));
15501324 } break;
15511325 case 9:
1552−# line 489 "lua.stx"
1326+# line 488 "lua.stx"
15531327 {
15541328 codereturn();
1555− yyval.pFunc = new(TFunc);
1556− yyval.pFunc->size = pc;
1557− yyval.pFunc->code = newvector(pc, Byte);
1558− yyval.pFunc->fileName = lua_parsedfile;
1559− yyval.pFunc->lineDefined = yypvt[-3].vInt;
1560− memcpy(yyval.pFunc->code, basepc, pc*sizeof(Byte));
1329+ luaY_val.pFunc = new(TFunc);
1330+ luaI_initTFunc(luaY_val.pFunc);
1331+ luaY_val.pFunc->size = pc;
1332+ luaY_val.pFunc->code = newvector(pc, Byte);
1333+ luaY_val.pFunc->fileName = lua_parsedfile;
1334+ luaY_val.pFunc->lineDefined = luaY_pvt[-3].vInt;
1335+ memcpy(luaY_val.pFunc->code, basepc, pc*sizeof(Byte));
1336+ if (lua_debug)
1337+ luaI_closelocalvars(luaY_val.pFunc);
15611338 /* save func values */
15621339 funcCode = basepc; maxcode=maxcurr;
15631340 #if LISTING
@@ -1566,346 +1343,345 @@ case 9: · 2 unmodified lines
15661343 change2main(); /* change back to main code */
15671344 } break;
15681345 case 14:
1569−# line 513 "lua.stx"
1570−{ codeIf(yypvt[-4].vLong, yypvt[-2].vLong); } break;
1571−case 15:
15721346 # line 515 "lua.stx"
1573−{yyval.vLong=pc;} break;
1347+{ codeIf(luaY_pvt[-4].vLong, luaY_pvt[-2].vLong); } break;
1348+case 15:
1349+# line 517 "lua.stx"
1350+{luaY_val.vLong=pc;} break;
15741351 case 16:
1575−# line 516 "lua.stx"
1352+# line 518 "lua.stx"
15761353 {
1577− basepc[yypvt[-3].vLong] = IFFJMP;
1578− code_word_at(basepc+yypvt[-3].vLong+1, pc - (yypvt[-3].vLong + sizeof(Word)+1));
1579− basepc[yypvt[-1].vLong] = UPJMP;
1580− code_word_at(basepc+yypvt[-1].vLong+1, pc - (yypvt[-6].vLong));
1354+ basepc[luaY_pvt[-3].vLong] = IFFJMP;
1355+ code_word_at(basepc+luaY_pvt[-3].vLong+1, pc - (luaY_pvt[-3].vLong + sizeof(Word)+1));
1356+ basepc[luaY_pvt[-1].vLong] = UPJMP;
1357+ code_word_at(basepc+luaY_pvt[-1].vLong+1, pc - (luaY_pvt[-6].vLong));
15811358 } break;
15821359 case 17:
1583−# line 523 "lua.stx"
1584−{yyval.vLong=pc;} break;
1360+# line 525 "lua.stx"
1361+{luaY_val.vLong=pc;} break;
15851362 case 18:
1586−# line 524 "lua.stx"
1363+# line 526 "lua.stx"
15871364 {
1588− basepc[yypvt[-0].vLong] = IFFUPJMP;
1589− code_word_at(basepc+yypvt[-0].vLong+1, pc - (yypvt[-4].vLong));
1365+ basepc[luaY_pvt[-0].vLong] = IFFUPJMP;
1366+ code_word_at(basepc+luaY_pvt[-0].vLong+1, pc - (luaY_pvt[-4].vLong));
15901367 } break;
15911368 case 19:
1592−# line 530 "lua.stx"
1369+# line 532 "lua.stx"
15931370 {
15941371 {
15951372 int i;
1596− adjust_mult_assign(nvarbuffer, yypvt[-0].vLong, yypvt[-2].vInt * 2 + nvarbuffer);
1373+ adjust_mult_assign(nvarbuffer, luaY_pvt[-0].vLong, luaY_pvt[-2].vInt * 2 + nvarbuffer);
15971374 for (i=nvarbuffer-1; i>=0; i--)
15981375 lua_codestore (i);
1599− if (yypvt[-2].vInt > 1 || (yypvt[-2].vInt == 1 && varbuffer[0] != 0))
1376+ if (luaY_pvt[-2].vInt > 1 || (luaY_pvt[-2].vInt == 1 && varbuffer[0] != 0))
16001377 lua_codeadjust (0);
16011378 }
16021379 } break;
16031380 case 21:
1604−# line 542 "lua.stx"
1605−{ nlocalvar += yypvt[-1].vInt;
1606− adjust_mult_assign(yypvt[-1].vInt, yypvt[-0].vInt, 0);
1381+# line 544 "lua.stx"
1382+{ nlocalvar += luaY_pvt[-1].vInt;
1383+ adjust_mult_assign(luaY_pvt[-1].vInt, luaY_pvt[-0].vInt, 0);
16071384 } break;
16081385 case 24:
1609−# line 550 "lua.stx"
1610−{ codeIf(yypvt[-3].vLong, yypvt[-1].vLong); } break;
1386+# line 552 "lua.stx"
1387+{ codeIf(luaY_pvt[-3].vLong, luaY_pvt[-1].vLong); } break;
16111388 case 25:
1612−# line 553 "lua.stx"
1613−{yyval.vInt = nlocalvar;} break;
1389+# line 555 "lua.stx"
1390+{luaY_val.vInt = nlocalvar;} break;
16141391 case 26:
1615−# line 554 "lua.stx"
1392+# line 556 "lua.stx"
16161393 {
1617− if (nlocalvar != yypvt[-2].vInt)
1394+ if (nlocalvar != luaY_pvt[-2].vInt)
16181395 {
1619− nlocalvar = yypvt[-2].vInt;
1396+ if (lua_debug)
1397+ for (; nlocalvar > luaY_pvt[-2].vInt; nlocalvar--)
1398+ luaI_unregisterlocalvar(lua_linenumber);
1399+ else
1400+ nlocalvar = luaY_pvt[-2].vInt;
16201401 lua_codeadjust (0);
16211402 }
16221403 } break;
16231404 case 28:
1624−# line 565 "lua.stx"
1405+# line 571 "lua.stx"
16251406 {
1626− adjust_functioncall(yypvt[-1].vLong, MULT_RET);
1407+ adjust_functioncall(luaY_pvt[-1].vLong, MULT_RET);
16271408 codereturn();
16281409 } break;
16291410 case 29:
1630−# line 572 "lua.stx"
1411+# line 578 "lua.stx"
16311412 {
1632− yyval.vLong = pc;
1413+ luaY_val.vLong = pc;
16331414 code_byte(0); /* open space */
16341415 code_word (0);
16351416 } break;
16361417 case 30:
1637−# line 578 "lua.stx"
1638−{ adjust_functioncall(yypvt[-0].vLong, 1); } break;
1418+# line 584 "lua.stx"
1419+{ adjust_functioncall(luaY_pvt[-0].vLong, 1); } break;
16391420 case 31:
1640−# line 581 "lua.stx"
1641−{ yyval.vLong = yypvt[-1].vLong; } break;
1421+# line 587 "lua.stx"
1422+{ luaY_val.vLong = luaY_pvt[-1].vLong; } break;
16421423 case 32:
1643−# line 582 "lua.stx"
1644−{ code_byte(EQOP); yyval.vLong = 0; } break;
1424+# line 588 "lua.stx"
1425+{ code_byte(EQOP); luaY_val.vLong = 0; } break;
16451426 case 33:
1646−# line 583 "lua.stx"
1647−{ code_byte(LTOP); yyval.vLong = 0; } break;
1427+# line 589 "lua.stx"
1428+{ code_byte(LTOP); luaY_val.vLong = 0; } break;
16481429 case 34:
1649−# line 584 "lua.stx"
1650−{ code_byte(GTOP); yyval.vLong = 0; } break;
1430+# line 590 "lua.stx"
1431+{ code_byte(GTOP); luaY_val.vLong = 0; } break;
16511432 case 35:
1652−# line 585 "lua.stx"
1653−{ code_byte(EQOP); code_byte(NOTOP); yyval.vLong = 0; } break;
1433+# line 591 "lua.stx"
1434+{ code_byte(EQOP); code_byte(NOTOP); luaY_val.vLong = 0; } break;
16541435 case 36:
1655−# line 586 "lua.stx"
1656−{ code_byte(LEOP); yyval.vLong = 0; } break;
1436+# line 592 "lua.stx"
1437+{ code_byte(LEOP); luaY_val.vLong = 0; } break;
16571438 case 37:
1658−# line 587 "lua.stx"
1659−{ code_byte(GEOP); yyval.vLong = 0; } break;
1439+# line 593 "lua.stx"
1440+{ code_byte(GEOP); luaY_val.vLong = 0; } break;
16601441 case 38:
1661−# line 588 "lua.stx"
1662−{ code_byte(ADDOP); yyval.vLong = 0; } break;
1442+# line 594 "lua.stx"
1443+{ code_byte(ADDOP); luaY_val.vLong = 0; } break;
16631444 case 39:
1664−# line 589 "lua.stx"
1665−{ code_byte(SUBOP); yyval.vLong = 0; } break;
1445+# line 595 "lua.stx"
1446+{ code_byte(SUBOP); luaY_val.vLong = 0; } break;
16661447 case 40:
1667−# line 590 "lua.stx"
1668−{ code_byte(MULTOP); yyval.vLong = 0; } break;
1448+# line 596 "lua.stx"
1449+{ code_byte(MULTOP); luaY_val.vLong = 0; } break;
16691450 case 41:
1670−# line 591 "lua.stx"
1671−{ code_byte(DIVOP); yyval.vLong = 0; } break;
1451+# line 597 "lua.stx"
1452+{ code_byte(DIVOP); luaY_val.vLong = 0; } break;
16721453 case 42:
1673−# line 592 "lua.stx"
1674−{ code_byte(POWOP); yyval.vLong = 0; } break;
1454+# line 598 "lua.stx"
1455+{ code_byte(POWOP); luaY_val.vLong = 0; } break;
16751456 case 43:
1676−# line 593 "lua.stx"
1677−{ code_byte(CONCOP); yyval.vLong = 0; } break;
1457+# line 599 "lua.stx"
1458+{ code_byte(CONCOP); luaY_val.vLong = 0; } break;
16781459 case 44:
1679−# line 594 "lua.stx"
1680−{ code_byte(MINUSOP); yyval.vLong = 0;} break;
1460+# line 600 "lua.stx"
1461+{ code_byte(MINUSOP); luaY_val.vLong = 0;} break;
16811462 case 45:
1682−# line 595 "lua.stx"
1683−{ yyval.vLong = 0; } break;
1463+# line 601 "lua.stx"
1464+{ luaY_val.vLong = 0; } break;
16841465 case 46:
1685−# line 596 "lua.stx"
1686−{ yyval.vLong = 0;} break;
1466+# line 602 "lua.stx"
1467+{ luaY_val.vLong = 0;} break;
16871468 case 47:
1688−# line 597 "lua.stx"
1689−{ code_number(yypvt[-0].vFloat); yyval.vLong = 0; } break;
1469+# line 603 "lua.stx"
1470+{ code_number(luaY_pvt[-0].vFloat); luaY_val.vLong = 0; } break;
16901471 case 48:
1691−# line 599 "lua.stx"
1472+# line 605 "lua.stx"
16921473 {
16931474 code_byte(PUSHSTRING);
1694− code_word(yypvt[-0].vWord);
1695− yyval.vLong = 0;
1475+ code_word(luaY_pvt[-0].vWord);
1476+ luaY_val.vLong = 0;
16961477 } break;
16971478 case 49:
1698−# line 604 "lua.stx"
1699−{code_byte(PUSHNIL); yyval.vLong = 0; } break;
1479+# line 610 "lua.stx"
1480+{code_byte(PUSHNIL); luaY_val.vLong = 0; } break;
17001481 case 50:
1701−# line 605 "lua.stx"
1702−{ yyval.vLong = yypvt[-0].vLong; } break;
1482+# line 611 "lua.stx"
1483+{ luaY_val.vLong = luaY_pvt[-0].vLong; } break;
17031484 case 51:
1704−# line 606 "lua.stx"
1705−{ code_byte(NOTOP); yyval.vLong = 0;} break;
1485+# line 612 "lua.stx"
1486+{ code_byte(NOTOP); luaY_val.vLong = 0;} break;
17061487 case 52:
1707−# line 607 "lua.stx"
1488+# line 613 "lua.stx"
17081489 {code_byte(POP); } break;
17091490 case 53:
1710−# line 608 "lua.stx"
1491+# line 614 "lua.stx"
17111492 {
1712− basepc[yypvt[-2].vLong] = ONFJMP;
1713− code_word_at(basepc+yypvt[-2].vLong+1, pc - (yypvt[-2].vLong + sizeof(Word)+1));
1714− yyval.vLong = 0;
1493+ basepc[luaY_pvt[-2].vLong] = ONFJMP;
1494+ code_word_at(basepc+luaY_pvt[-2].vLong+1, pc - (luaY_pvt[-2].vLong + sizeof(Word)+1));
1495+ luaY_val.vLong = 0;
17151496 } break;
17161497 case 54:
1717−# line 613 "lua.stx"
1498+# line 619 "lua.stx"
17181499 {code_byte(POP); } break;
17191500 case 55:
1720−# line 614 "lua.stx"
1501+# line 620 "lua.stx"
17211502 {
1722− basepc[yypvt[-2].vLong] = ONTJMP;
1723− code_word_at(basepc+yypvt[-2].vLong+1, pc - (yypvt[-2].vLong + sizeof(Word)+1));
1724− yyval.vLong = 0;
1503+ basepc[luaY_pvt[-2].vLong] = ONTJMP;
1504+ code_word_at(basepc+luaY_pvt[-2].vLong+1, pc - (luaY_pvt[-2].vLong + sizeof(Word)+1));
1505+ luaY_val.vLong = 0;
17251506 } break;
17261507 case 56:
1727−# line 622 "lua.stx"
1508+# line 628 "lua.stx"
17281509 {
17291510 code_byte(CREATEARRAY);
1730− yyval.vLong = pc; code_word(0);
1511+ luaY_val.vLong = pc; code_word(0);
17311512 } break;
17321513 case 57:
1733−# line 627 "lua.stx"
1514+# line 633 "lua.stx"
17341515 {
1735− code_word_at(basepc+yypvt[-3].vLong, yypvt[-1].vInt);
1516+ code_word_at(basepc+luaY_pvt[-3].vLong, luaY_pvt[-1].vInt);
17361517 } break;
17371518 case 58:
1738−# line 633 "lua.stx"
1519+# line 639 "lua.stx"
17391520 {
17401521 code_byte(CALLFUNC);
1741− code_byte(yypvt[-1].vInt+yypvt[-0].vInt);
1742− yyval.vLong = pc;
1522+ code_byte(luaY_pvt[-1].vInt+luaY_pvt[-0].vInt);
1523+ luaY_val.vLong = pc;
17431524 code_byte(0); /* may be modified by other rules */
17441525 } break;
17451526 case 59:
1746−# line 641 "lua.stx"
1747−{ yyval.vInt = 0; } break;
1527+# line 647 "lua.stx"
1528+{ luaY_val.vInt = 0; } break;
17481529 case 60:
1749−# line 643 "lua.stx"
1530+# line 649 "lua.stx"
17501531 {
17511532 code_byte(PUSHSELF);
1752− code_word(luaI_findconstant(yypvt[-0].pNode));
1753− yyval.vInt = 1;
1533+ code_word(luaI_findconstant(luaY_pvt[-0].pTStr));
1534+ luaY_val.vInt = 1;
17541535 } break;
17551536 case 61:
1756−# line 651 "lua.stx"
1757−{ yyval.vInt = adjust_functioncall(yypvt[-1].vLong, 1); } break;
1537+# line 657 "lua.stx"
1538+{ luaY_val.vInt = adjust_functioncall(luaY_pvt[-1].vLong, 1); } break;
17581539 case 62:
1759−# line 652 "lua.stx"
1760−{ yyval.vInt = 1; } break;
1540+# line 658 "lua.stx"
1541+{ luaY_val.vInt = 1; } break;
17611542 case 63:
1762−# line 655 "lua.stx"
1763−{ yyval.vLong = 0; } break;
1543+# line 661 "lua.stx"
1544+{ luaY_val.vLong = 0; } break;
17641545 case 64:
1765−# line 656 "lua.stx"
1766−{ yyval.vLong = yypvt[-0].vLong; } break;
1546+# line 662 "lua.stx"
1547+{ luaY_val.vLong = luaY_pvt[-0].vLong; } break;
17671548 case 65:
1768−# line 659 "lua.stx"
1769−{ if (yypvt[-0].vLong != 0) yyval.vLong = yypvt[-0].vLong; else yyval.vLong = -1; } break;
1549+# line 665 "lua.stx"
1550+{ if (luaY_pvt[-0].vLong != 0) luaY_val.vLong = luaY_pvt[-0].vLong; else luaY_val.vLong = -1; } break;
17701551 case 66:
1771−# line 660 "lua.stx"
1772−{ yyval.vLong = adjust_functioncall(yypvt[-1].vLong, 1); } break;
1552+# line 666 "lua.stx"
1553+{ luaY_val.vLong = adjust_functioncall(luaY_pvt[-1].vLong, 1); } break;
17731554 case 67:
1774−# line 661 "lua.stx"
1555+# line 667 "lua.stx"
17751556 {
1776− if (yypvt[-0].vLong == 0) yyval.vLong = -(yypvt[-1].vLong + 1); /* -length */
1557+ if (luaY_pvt[-0].vLong == 0) luaY_val.vLong = -(luaY_pvt[-1].vLong + 1); /* -length */
17771558 else
17781559 {
1779− adjust_functioncall(yypvt[-0].vLong, yypvt[-1].vLong);
1780− yyval.vLong = yypvt[-0].vLong;
1560+ adjust_functioncall(luaY_pvt[-0].vLong, luaY_pvt[-1].vLong);
1561+ luaY_val.vLong = luaY_pvt[-0].vLong;
17811562 }
17821563 } break;
17831564 case 68:
1784−# line 671 "lua.stx"
1785−{ lua_codeadjust(0); yyval.vInt = lua_linenumber; } break;
1565+# line 677 "lua.stx"
1566+{ lua_codeadjust(0); luaY_val.vInt = lua_linenumber; } break;
17861567 case 69:
1787−# line 672 "lua.stx"
1788−{ lua_codeadjust(0); yyval.vInt = lua_linenumber; } break;
1568+# line 678 "lua.stx"
1569+{ lua_codeadjust(0); luaY_val.vInt = lua_linenumber; } break;
17891570 case 70:
1790−# line 676 "lua.stx"
1791−{
1792− add_localvar(luaI_findsymbol(yypvt[-0].pNode));
1793− } break;
1571+# line 681 "lua.stx"
1572+{ add_localvar(luaY_pvt[-0].pTStr); } break;
17941573 case 71:
1795−# line 680 "lua.stx"
1796−{
1797− add_localvar(luaI_findsymbol(yypvt[-0].pNode));
1798− } break;
1574+# line 682 "lua.stx"
1575+{ add_localvar(luaY_pvt[-0].pTStr); } break;
17991576 case 72:
18001577 # line 686 "lua.stx"
1801−{ flush_list(yypvt[-0].vInt/FIELDS_PER_FLUSH, yypvt[-0].vInt%FIELDS_PER_FLUSH); } break;
1578+{ flush_list(luaY_pvt[-0].vInt/FIELDS_PER_FLUSH, luaY_pvt[-0].vInt%FIELDS_PER_FLUSH); } break;
18021579 case 73:
18031580 # line 688 "lua.stx"
1804−{ yyval.vInt = yypvt[-2].vInt+yypvt[-0].vInt; } break;
1581+{ luaY_val.vInt = luaY_pvt[-2].vInt+luaY_pvt[-0].vInt; } break;
18051582 case 74:
18061583 # line 690 "lua.stx"
1807−{ yyval.vInt = yypvt[-1].vInt; flush_record(yypvt[-1].vInt%FIELDS_PER_FLUSH); } break;
1584+{ luaY_val.vInt = luaY_pvt[-1].vInt; flush_record(luaY_pvt[-1].vInt%FIELDS_PER_FLUSH); } break;
18081585 case 75:
18091586 # line 694 "lua.stx"
1810−{ yyval.vInt = 0; } break;
1587+{ luaY_val.vInt = 0; } break;
18111588 case 76:
18121589 # line 696 "lua.stx"
1813−{ yyval.vInt = yypvt[-0].vInt; flush_record(yypvt[-0].vInt%FIELDS_PER_FLUSH); } break;
1590+{ luaY_val.vInt = luaY_pvt[-0].vInt; flush_record(luaY_pvt[-0].vInt%FIELDS_PER_FLUSH); } break;
18141591 case 79:
18151592 # line 703 "lua.stx"
1816−{ yyval.vInt = 0; } break;
1593+{ luaY_val.vInt = 0; } break;
18171594 case 80:
18181595 # line 704 "lua.stx"
1819−{ yyval.vInt = yypvt[-1].vInt; } break;
1596+{ luaY_val.vInt = luaY_pvt[-1].vInt; } break;
18201597 case 81:
18211598 # line 707 "lua.stx"
1822−{yyval.vInt=1;} break;
1599+{luaY_val.vInt=1;} break;
18231600 case 82:
18241601 # line 709 "lua.stx"
18251602 {
1826− yyval.vInt=yypvt[-2].vInt+1;
1827− if (yyval.vInt%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
1603+ luaY_val.vInt=luaY_pvt[-2].vInt+1;
1604+ if (luaY_val.vInt%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
18281605 } break;
18291606 case 83:
18301607 # line 716 "lua.stx"
18311608 {
1832− push_field(luaI_findconstant(yypvt[-2].pNode));
1609+ push_field(luaI_findconstant(luaY_pvt[-2].pTStr));
18331610 } break;
18341611 case 84:
18351612 # line 721 "lua.stx"
1836−{ yyval.vInt = 0; } break;
1613+{ luaY_val.vInt = 0; } break;
18371614 case 85:
18381615 # line 722 "lua.stx"
1839−{ yyval.vInt = yypvt[-1].vInt; } break;
1616+{ luaY_val.vInt = luaY_pvt[-1].vInt; } break;
18401617 case 86:
18411618 # line 725 "lua.stx"
1842−{yyval.vInt=1;} break;
1619+{luaY_val.vInt=1;} break;
18431620 case 87:
18441621 # line 727 "lua.stx"
18451622 {
1846− yyval.vInt=yypvt[-2].vInt+1;
1847− if (yyval.vInt%FIELDS_PER_FLUSH == 0)
1848− flush_list(yyval.vInt/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
1623+ luaY_val.vInt=luaY_pvt[-2].vInt+1;
1624+ if (luaY_val.vInt%FIELDS_PER_FLUSH == 0)
1625+ flush_list(luaY_val.vInt/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
18491626 } break;
18501627 case 88:
18511628 # line 735 "lua.stx"
18521629 {
18531630 nvarbuffer = 0;
1854− add_varbuffer(yypvt[-0].vLong);
1855− yyval.vInt = (yypvt[-0].vLong == 0) ? 1 : 0;
1631+ add_varbuffer(luaY_pvt[-0].vLong);
1632+ luaY_val.vInt = (luaY_pvt[-0].vLong == 0) ? 1 : 0;
18561633 } break;
18571634 case 89:
18581635 # line 741 "lua.stx"
18591636 {
1860− add_varbuffer(yypvt[-0].vLong);
1861− yyval.vInt = (yypvt[-0].vLong == 0) ? yypvt[-2].vInt + 1 : yypvt[-2].vInt;
1637+ add_varbuffer(luaY_pvt[-0].vLong);
1638+ luaY_val.vInt = (luaY_pvt[-0].vLong == 0) ? luaY_pvt[-2].vInt + 1 : luaY_pvt[-2].vInt;
18621639 } break;
18631640 case 90:
18641641 # line 747 "lua.stx"
1865−{ yyval.vLong = yypvt[-0].vLong; } break;
1642+{ luaY_val.vLong = luaY_pvt[-0].vLong; } break;
18661643 case 91:
18671644 # line 749 "lua.stx"
18681645 {
1869− yyval.vLong = 0; /* indexed variable */
1646+ luaY_val.vLong = 0; /* indexed variable */
18701647 } break;
18711648 case 92:
18721649 # line 753 "lua.stx"
18731650 {
18741651 code_byte(PUSHSTRING);
1875− code_word(luaI_findconstant(yypvt[-0].pNode));
1876− yyval.vLong = 0; /* indexed variable */
1652+ code_word(luaI_findconstant(luaY_pvt[-0].pTStr));
1653+ luaY_val.vLong = 0; /* indexed variable */
18771654 } break;
18781655 case 93:
18791656 # line 761 "lua.stx"
18801657 {
1881− Word s = luaI_findsymbol(yypvt[-0].pNode);
1882− int local = lua_localname (s);
1658+ int local = lua_localname(luaY_pvt[-0].pTStr);
18831659 if (local == -1) /* global var */
1884− yyval.vLong = s + 1; /* return positive value */
1660+ luaY_val.vLong = luaI_findsymbol(luaY_pvt[-0].pTStr)+1; /* return positive value */
18851661 else
1886− yyval.vLong = -(local+1); /* return negative value */
1662+ luaY_val.vLong = -(local+1); /* return negative value */
18871663 } break;
18881664 case 94:
1889−# line 771 "lua.stx"
1890−{ lua_pushvar(yypvt[-0].vLong); } break;
1665+# line 770 "lua.stx"
1666+{ lua_pushvar(luaY_pvt[-0].vLong); } break;
18911667 case 95:
1892−# line 774 "lua.stx"
1893−{store_localvar(luaI_findsymbol(yypvt[-0].pNode), 0); yyval.vInt = 1;} break;
1668+# line 773 "lua.stx"
1669+{store_localvar(luaY_pvt[-0].pTStr, 0); luaY_val.vInt = 1;} break;
18941670 case 96:
1895−# line 776 "lua.stx"
1671+# line 775 "lua.stx"
18961672 {
1897− store_localvar(luaI_findsymbol(yypvt[-0].pNode), yypvt[-2].vInt);
1898− yyval.vInt = yypvt[-2].vInt+1;
1673+ store_localvar(luaY_pvt[-0].pTStr, luaY_pvt[-2].vInt);
1674+ luaY_val.vInt = luaY_pvt[-2].vInt+1;
18991675 } break;
19001676 case 97:
1901−# line 782 "lua.stx"
1902−{ yyval.vInt = 0; } break;
1677+# line 781 "lua.stx"
1678+{ luaY_val.vInt = 0; } break;
19031679 case 98:
1904−# line 783 "lua.stx"
1905−{ yyval.vInt = yypvt[-0].vLong; } break;
1680+# line 782 "lua.stx"
1681+{ luaY_val.vInt = luaY_pvt[-0].vLong; } break;
19061682 case 99:
1907−# line 786 "lua.stx"
1908−{ lua_debug = yypvt[-0].vInt; } break;
1683+# line 785 "lua.stx"
1684+{ lua_debug = luaY_pvt[-0].vInt; } break;
19091685 }
1910− goto yystack; /* reset registers in driver code */
1686+ goto luaY_stack; /* reset registers in driver code */
19111687 }
src/parser.h+3−2
@@ -1,3 +1,4 @@
1+
12 typedef union
23 {
34 int vInt;
@@ -6,9 +7,9 @@ typedef union · 2 unmodified lines
67 Word vWord;
78 Long vLong;
89 TFunc *pFunc;
9− TreeNode *pNode;
10+ TaggedString *pTStr;
1011 } YYSTYPE;
11−extern YYSTYPE yylval;
12+extern YYSTYPE luaY_lval;
1213 # define WRONGTOKEN 257
1314 # define NIL 258
1415 # define IF 259
src/table.c+94−97
@@ -3,9 +3,7 @@ · 2 unmodified lines
33 ** Module to control static tables
44 */
55
6−char *rcs_table="$Id: table.c,v 2.38 1995/11/03 15:30:50 roberto Exp $";
7−
8−/*#include <string.h>*/
6+char *rcs_table="$Id: table.c,v 2.54 1996/05/06 14:29:35 roberto Exp $";
97
108 #include "mem.h"
119 #include "opcode.h"
@@ -21,61 +19,66 @@ char *rcs_table="$Id: table.c,v 2.38 1995/11/03 15:30:50 roberto Exp $"; · 9 unmodified lines
2119 #define BUFFER_BLOCK 256
2220
2321 Symbol *lua_table = NULL;
24−static Word lua_ntable = 0;
22+Word lua_ntable = 0;
2523 static Long lua_maxsymbol = 0;
2624
2725 TaggedString **lua_constant = NULL;
28−static Word lua_nconstant = 0;
26+Word lua_nconstant = 0;
2927 static Long lua_maxconstant = 0;
3028
3129
32−#define GARBAGE_BLOCK 1024
33−#define MIN_GARBAGE_BLOCK (GARBAGE_BLOCK/2)
30+#define GARBAGE_BLOCK 50
3431
3532 static void lua_nextvar (void);
36−static void setglobal (void);
37−static void getglobal (void);
3833
3934 /*
40−** Initialise symbol table with internal functions
35+** Internal functions
4136 */
42−static void lua_initsymbol (void)
37+static struct {
38+ char *name;
39+ lua_CFunction func;
40+} int_funcs[] = {
41+ {"assert", luaI_assert},
42+ {"dofile", lua_internaldofile},
43+ {"dostring", lua_internaldostring},
44+ {"error", luaI_error},
45+ {"getglobal", luaI_getglobal},
46+ {"next", lua_next},
47+ {"nextvar", lua_nextvar},
48+ {"print", luaI_print},
49+ {"setfallback", luaI_setfallback},
50+ {"setglobal", luaI_setglobal},
51+ {"tonumber", lua_obj2number},
52+ {"tostring", luaI_tostring},
53+ {"type", luaI_type}
54+};
55+
56+#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
57+
58+
59+void luaI_initsymbol (void)
4360 {
44− Word n;
45− lua_maxsymbol = BUFFER_BLOCK;
46− lua_table = newvector(lua_maxsymbol, Symbol);
47− n = luaI_findsymbolbyname("next");
48− s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
49− n = luaI_findsymbolbyname("dofile");
50− s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
51− n = luaI_findsymbolbyname("setglobal");
52− s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = setglobal;
53− n = luaI_findsymbolbyname("getglobal");
54− s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = getglobal;
55− n = luaI_findsymbolbyname("nextvar");
56− s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar;
57− n = luaI_findsymbolbyname("type");
58− s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type;
59− n = luaI_findsymbolbyname("tonumber");
60− s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
61− n = luaI_findsymbolbyname("print");
62− s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print;
63− n = luaI_findsymbolbyname("dostring");
64− s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
65− n = luaI_findsymbolbyname("setfallback");
66− s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback;
67− n = luaI_findsymbolbyname("error");
68− s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error;
61+ int i;
62+ lua_maxsymbol = BUFFER_BLOCK;
63+ lua_table = newvector(lua_maxsymbol, Symbol);
64+ for (i=0; i<INTFUNCSIZE; i++)
65+ {
66+ Word n = luaI_findsymbolbyname(int_funcs[i].name);
67+ s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
68+ }
6969 }
7070
7171
7272 /*
7373 ** Initialise constant table with pre-defined constants
7474 */
75−void lua_initconstant (void)
75+void luaI_initconstant (void)
7676 {
7777 lua_maxconstant = BUFFER_BLOCK;
7878 lua_constant = newvector(lua_maxconstant, TaggedString *);
79+ /* pre-register mem error messages, to avoid loop when error arises */
80+ luaI_findconstantbyname(tableEM);
81+ luaI_findconstantbyname(memEM);
7982 }
8083
8184
@@ -83,22 +86,15 @@ void lua_initconstant (void) · 1 unmodified line
8386 ** Given a name, search it at symbol table and return its index. If not
8487 ** found, allocate it.
8588 */
86−Word luaI_findsymbol (TreeNode *t)
89+Word luaI_findsymbol (TaggedString *t)
8790 {
88− if (lua_table == NULL)
89− lua_initsymbol();
9091 if (t->varindex == NOT_USED)
9192 {
9293 if (lua_ntable == lua_maxsymbol)
93− {
94− if (lua_maxsymbol >= MAX_WORD)
95− lua_error("symbol table overflow");
96− lua_maxsymbol *= 2;
97− if (lua_maxsymbol >= MAX_WORD)
98− lua_maxsymbol = MAX_WORD;
99− lua_table = growvector(lua_table, lua_maxsymbol, Symbol);
100− }
94+ lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
95+ symbolEM, MAX_WORD);
10196 t->varindex = lua_ntable;
97+ lua_table[lua_ntable].varname = t;
10298 s_tag(lua_ntable) = LUA_T_NIL;
10399 lua_ntable++;
104100 }
@@ -108,7 +104,7 @@ Word luaI_findsymbol (TreeNode *t) · 3 unmodified lines
108104
109105 Word luaI_findsymbolbyname (char *name)
110106 {
111− return luaI_findsymbol(lua_constcreate(name));
107+ return luaI_findsymbol(luaI_createfixedstring(name));
112108 }
113109
114110
@@ -116,23 +112,15 @@ Word luaI_findsymbolbyname (char *name) · 1 unmodified line
116112 ** Given a tree node, check it is has a correspondent constant index. If not,
117113 ** allocate it.
118114 */
119−Word luaI_findconstant (TreeNode *t)
115+Word luaI_findconstant (TaggedString *t)
120116 {
121− if (lua_constant == NULL)
122− lua_initconstant();
123117 if (t->constindex == NOT_USED)
124118 {
125119 if (lua_nconstant == lua_maxconstant)
126− {
127− if (lua_maxconstant >= MAX_WORD)
128− lua_error("constant table overflow");
129− lua_maxconstant *= 2;
130− if (lua_maxconstant >= MAX_WORD)
131− lua_maxconstant = MAX_WORD;
132− lua_constant = growvector(lua_constant, lua_maxconstant, TaggedString *);
133− }
120+ lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
121+ constantEM, MAX_WORD);
134122 t->constindex = lua_nconstant;
135− lua_constant[lua_nconstant] = &(t->ts);
123+ lua_constant[lua_nconstant] = t;
136124 lua_nconstant++;
137125 }
138126 return t->constindex;
@@ -141,7 +129,15 @@ Word luaI_findconstant (TreeNode *t) · 2 unmodified lines
141129
142130 Word luaI_findconstantbyname (char *name)
143131 {
144− return luaI_findconstant(lua_constcreate(name));
132+ return luaI_findconstant(luaI_createfixedstring(name));
133+}
134+
135+TaggedString *luaI_createfixedstring (char *name)
136+{
137+ TaggedString *ts = lua_createstring(name);
138+ if (!ts->marked)
139+ ts->marked = 2; /* avoid GC */
140+ return ts;
145141 }
146142
147143
@@ -153,7 +149,7 @@ static char *lua_travsymbol (int (*fn)(Object *)) · 5 unmodified lines
153149 Word i;
154150 for (i=0; i<lua_ntable; i++)
155151 if (fn(&s_object(i)))
156− return luaI_nodebysymbol(i)->ts.str;
152+ return lua_table[i].varname->str;
157153 return NULL;
158154 }
159155
@@ -162,7 +158,7 @@ static char *lua_travsymbol (int (*fn)(Object *)) · 2 unmodified lines
162158 ** Mark an object if it is a string or a unmarked array.
163159 */
164160 int lua_markobject (Object *o)
165−{
161+{/* if already marked, does not change mark value */
166162 if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
167163 tsvalue(o)->marked = 1;
168164 else if (tag(o) == LUA_T_ARRAY)
@@ -173,27 +169,52 @@ int lua_markobject (Object *o) · 4 unmodified lines
173169 return 0;
174170 }
175171
172+/*
173+* returns 0 if the object is going to be (garbage) collected
174+*/
175+int luaI_ismarked (Object *o)
176+{
177+ switch (o->tag)
178+ {
179+ case LUA_T_STRING:
180+ return o->value.ts->marked;
181+ case LUA_T_FUNCTION:
182+ return o->value.tf->marked;
183+ case LUA_T_ARRAY:
184+ return o->value.a->mark;
185+ default: /* nil, number, cfunction, or user data */
186+ return 1;
187+ }
188+}
189+
176190
177191 /*
178192 ** Garbage collection.
179193 ** Delete all unused strings and arrays.
180194 */
181−void lua_pack (void)
195+Long luaI_collectgarbage (void)
182196 {
183− static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */
184− static Long nentity = 0; /* counter of new entities (strings and arrays) */
185197 Long recovered = 0;
186− if (nentity++ < block) return;
187198 lua_travstack(lua_markobject); /* mark stack objects */
188199 lua_travsymbol(lua_markobject); /* mark symbol table objects */
189200 luaI_travlock(lua_markobject); /* mark locked objects */
190201 luaI_travfallbacks(lua_markobject); /* mark fallbacks */
202+ luaI_invalidaterefs();
191203 recovered += lua_strcollector();
192204 recovered += lua_hashcollector();
193205 recovered += luaI_funccollector();
194− nentity = 0; /* reset counter */
195− block=(16*block-7*recovered)/12; /* adapt block size */
196− if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;
206+ return recovered;
207+}
208+
209+void lua_pack (void)
210+{
211+ static unsigned long block = GARBAGE_BLOCK;
212+ static unsigned long nentity = 0; /* total of strings, arrays, etc */
213+ unsigned long recovered = 0;
214+ if (nentity++ < block) return;
215+ recovered = luaI_collectgarbage();
216+ block = block*2*(1.0 - (float)recovered/nentity);
217+ nentity -= recovered;
197218 }
198219
199220
@@ -225,36 +246,12 @@ static void lua_nextvar (void) · 25 unmodified lines
225246 }
226247 else
227248 {
228− TreeNode *t = luaI_nodebysymbol(next);
229− Object name;
230− tag(&name) = LUA_T_STRING;
231− tsvalue(&name) = &(t->ts);
232− luaI_pushobject(&name);
249+ lua_pushstring(lua_table[next].varname->str);
233250 luaI_pushobject(&s_object(next));
234251 }
235252 }
236253
237254
238−static void setglobal (void)
239−{
240− lua_Object name = lua_getparam(1);
241− lua_Object value = lua_getparam(2);
242− if (!lua_isstring(name))
243− lua_error("incorrect argument to function `setglobal'");
244− lua_pushobject(value);
245− lua_storeglobal(lua_getstring(name));
246−}
247−
248−
249−static void getglobal (void)
250−{
251− lua_Object name = lua_getparam(1);
252− if (!lua_isstring(name))
253− lua_error("incorrect argument to function `getglobal'");
254− lua_pushobject(lua_getglobal(lua_getstring(name)));
255−}
256−
257−
258255 static Object *functofind;
259256 static int checkfunc (Object *o)
260257 {
@@ -270,7 +267,7 @@ static int checkfunc (Object *o) · 9 unmodified lines
270267 }
271268
272269
273−char *getobjname (lua_Object o, char **name)
270+char *lua_getobjname (lua_Object o, char **name)
274271 { /* try to find a name for given function */
275272 functofind = luaI_Address(o);
276273 if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
src/table.h+17−8
@@ -1,7 +1,7 @@
11 /*
22 ** Module to control static tables
33 ** TeCGraf - PUC-Rio
4−** $Id: table.h,v 2.13 1995/10/26 14:21:56 roberto Exp $
4+** $Id: table.h,v 2.21 1996/04/22 18:00:37 roberto Exp $
55 */
66
77 #ifndef table_h
@@ -10,19 +10,28 @@ · 2 unmodified lines
1010 #include "tree.h"
1111 #include "opcode.h"
1212
13−extern Symbol *lua_table;
14−extern TaggedString **lua_constant;
13+typedef struct
14+{
15+ Object object;
16+ TaggedString *varname;
17+} Symbol;
1518
16−extern char *lua_file[];
17−extern int lua_nfile;
1819
20+extern Symbol *lua_table;
21+extern Word lua_ntable;
22+extern TaggedString **lua_constant;
23+extern Word lua_nconstant;
1924
20−void lua_initconstant (void);
25+void luaI_initsymbol (void);
26+void luaI_initconstant (void);
2127 Word luaI_findsymbolbyname (char *name);
22−Word luaI_findsymbol (TreeNode *t);
23−Word luaI_findconstant (TreeNode *t);
28+Word luaI_findsymbol (TaggedString *t);
29+Word luaI_findconstant (TaggedString *t);
2430 Word luaI_findconstantbyname (char *name);
31+TaggedString *luaI_createfixedstring (char *str);
2532 int lua_markobject (Object *o);
33+int luaI_ismarked (Object *o);
34+Long luaI_collectgarbage (void);
2635 void lua_pack (void);
2736
2837
src/tree.c+100−78
@@ -3,7 +3,7 @@ · 2 unmodified lines
33 ** TecCGraf - PUC-Rio
44 */
55
6−char *rcs_tree="$Id: tree.c,v 1.14 1995/10/17 11:53:53 roberto Exp $";
6+char *rcs_tree="$Id: tree.c,v 1.20 1996/03/14 15:56:26 roberto Exp $";
77
88
99 #include <string.h>
@@ -11,66 +11,104 @@ char *rcs_tree="$Id: tree.c,v 1.14 1995/10/17 11:53:53 roberto Exp $"; · 1 unmodified line
1111 #include "mem.h"
1212 #include "lua.h"
1313 #include "tree.h"
14+#include "lex.h"
15+#include "hash.h"
1416 #include "table.h"
1517
1618
17−#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
19+#define NUM_HASHS 64
1820
21+typedef struct {
22+ int size;
23+ int nuse; /* number of elements (including EMPTYs) */
24+ TaggedString **hash;
25+} stringtable;
1926
20−typedef struct StringNode {
21− struct StringNode *next;
22− TaggedString ts;
23−} StringNode;
27+static int initialized = 0;
2428
25−static StringNode *string_root = NULL;
29+static stringtable string_root[NUM_HASHS];
2630
27−static TreeNode *constant_root = NULL;
31+static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 2, {0}};
2832
29−/*
30−** Insert a new constant/variable at the tree.
31−*/
32−static TreeNode *tree_create (TreeNode **node, char *str)
33+
34+static unsigned long hash (char *str)
3335 {
34− if (*node == NULL)
35− {
36− *node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str));
37− (*node)->left = (*node)->right = NULL;
38− strcpy((*node)->ts.str, str);
39− (*node)->ts.marked = 0;
40− (*node)->ts.hash = 0;
41− (*node)->varindex = (*node)->constindex = NOT_USED;
42− return *node;
43− }
44− else
45− {
46− int c = lua_strcmp(str, (*node)->ts.str);
47− if (c < 0)
48− return tree_create(&(*node)->left, str);
49− else if (c > 0)
50− return tree_create(&(*node)->right, str);
51− else
52− return *node;
53− }
36+ unsigned long h = 0;
37+ while (*str)
38+ h = ((h<<5)-h)^(unsigned char)*(str++);
39+ return h;
5440 }
5541
56−TaggedString *lua_createstring (char *str)
42+static void initialize (void)
5743 {
58− StringNode *newString;
59− if (str == NULL) return NULL;
60− lua_pack();
61− newString = (StringNode *)luaI_malloc(sizeof(StringNode)+strlen(str));
62− newString->ts.marked = 0;
63− newString->ts.hash = 0;
64− strcpy(newString->ts.str, str);
65− newString->next = string_root;
66− string_root = newString;
67− return &(newString->ts);
44+ initialized = 1;
45+ luaI_addReserved();
46+ luaI_initsymbol();
47+ luaI_initconstant();
48+}
49+
50+
51+static void grow (stringtable *tb)
52+{
53+ int newsize = luaI_redimension(tb->size);
54+ TaggedString **newhash = newvector(newsize, TaggedString *);
55+ int i;
56+ for (i=0; i<newsize; i++)
57+ newhash[i] = NULL;
58+ /* rehash */
59+ tb->nuse = 0;
60+ for (i=0; i<tb->size; i++)
61+ if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
62+ {
63+ int h = tb->hash[i]->hash%newsize;
64+ while (newhash[h])
65+ h = (h+1)%newsize;
66+ newhash[h] = tb->hash[i];
67+ tb->nuse++;
68+ }
69+ luaI_free(tb->hash);
70+ tb->size = newsize;
71+ tb->hash = newhash;
6872 }
6973
74+static TaggedString *insert (char *str, stringtable *tb)
75+{
76+ TaggedString *ts;
77+ unsigned long h = hash(str);
78+ int i;
79+ int j = -1;
80+ if ((Long)tb->nuse*3 >= (Long)tb->size*2)
81+ {
82+ if (!initialized)
83+ initialize();
84+ grow(tb);
85+ }
86+ i = h%tb->size;
87+ while (tb->hash[i])
88+ {
89+ if (tb->hash[i] == &EMPTY)
90+ j = i;
91+ else if (strcmp(str, tb->hash[i]->str) == 0)
92+ return tb->hash[i];
93+ i = (i+1)%tb->size;
94+ }
95+ /* not found */
96+ lua_pack();
97+ if (j != -1) /* is there an EMPTY space? */
98+ i = j;
99+ else
100+ tb->nuse++;
101+ ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(str));
102+ strcpy(ts->str, str);
103+ ts->marked = 0;
104+ ts->hash = h;
105+ ts->varindex = ts->constindex = NOT_USED;
106+ return ts;
107+}
70108
71−TreeNode *lua_constcreate (char *str)
109+TaggedString *lua_createstring (char *str)
72110 {
73− return tree_create(&constant_root, str);
111+ return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]);
74112 }
75113
76114
@@ -80,44 +118,28 @@ TreeNode *lua_constcreate (char *str) · 3 unmodified lines
80118 */
81119 Long lua_strcollector (void)
82120 {
83− StringNode *curr = string_root, *prev = NULL;
84121 Long counter = 0;
85− while (curr)
122+ int i;
123+ for (i=0; i<NUM_HASHS; i++)
86124 {
87− StringNode *next = curr->next;
88− if (!curr->ts.marked)
89− {
90− if (prev == NULL) string_root = next;
91− else prev->next = next;
92− luaI_free(curr);
93− ++counter;
94− }
95− else
125+ stringtable *tb = &string_root[i];
126+ int j;
127+ for (j=0; j<tb->size; j++)
96128 {
97− curr->ts.marked = 0;
98− prev = curr;
129+ TaggedString *t = tb->hash[j];
130+ if (t != NULL && t->marked <= 1)
131+ {
132+ if (t->marked)
133+ t->marked = 0;
134+ else
135+ {
136+ luaI_free(t);
137+ tb->hash[j] = &EMPTY;
138+ counter++;
139+ }
140+ }
99141 }
100− curr = next;
101142 }
102143 return counter;
103144 }
104145
105−
106−/*
107−** Traverse the constant tree looking for a specific symbol number
108−*/
109−static TreeNode *nodebysymbol (TreeNode *root, Word symbol)
110−{
111− TreeNode *t;
112− if (root == NULL) return NULL;
113− if (root->varindex == symbol) return root;
114− t = nodebysymbol(root->left, symbol);
115− if (t) return t;
116− return nodebysymbol(root->right, symbol);
117−}
118−
119−TreeNode *luaI_nodebysymbol (Word symbol)
120−{
121− return nodebysymbol(constant_root, symbol);
122−}
123−
src/tree.h+4−13
@@ -1,7 +1,7 @@
11 /*
22 ** tree.h
33 ** TecCGraf - PUC-Rio
4−** $Id: tree.h,v 1.10 1995/10/17 11:53:53 roberto Exp $
4+** $Id: tree.h,v 1.14 1996/02/26 17:07:49 roberto Exp $
55 */
66
77 #ifndef tree_h
@@ -14,24 +14,15 @@ · 6 unmodified lines
1414
1515 typedef struct TaggedString
1616 {
17+ Word varindex; /* != NOT_USED if this is a symbol */
18+ Word constindex; /* != NOT_USED if this is a constant */
1719 unsigned long hash; /* 0 if not initialized */
18− char marked; /* for garbage collection */
20+ int marked; /* for garbage collection; never collect (nor change) if > 1 */
1921 char str[1]; /* \0 byte already reserved */
2022 } TaggedString;
2123
22−typedef struct TreeNode
23−{
24− struct TreeNode *right;
25− struct TreeNode *left;
26− unsigned short varindex; /* != NOT_USED if this is a symbol */
27− unsigned short constindex; /* != NOT_USED if this is a constant */
28− TaggedString ts;
29−} TreeNode;
30−
3124
3225 TaggedString *lua_createstring (char *str);
33−TreeNode *lua_constcreate (char *str);
3426 Long lua_strcollector (void);
35−TreeNode *luaI_nodebysymbol (Word symbol);
3627
3728 #endif
src/types.h+1−3
@@ -1,6 +1,6 @@
11 /*
22 ** TeCGraf - PUC-Rio
3−** $Id: types.h,v 1.3 1995/02/06 19:32:43 roberto Exp $
3+** $Id: types.h,v 1.4 1996/02/07 14:13:17 roberto Exp $
44 */
55
66 #ifndef types_h
@@ -12,8 +12,6 @@ · 5 unmodified lines
1212 #define real float
1313 #endif
1414
15−typedef int Bool; /* boolean values */
16−
1715 #define Byte lua_Byte /* some systems have Byte as a predefined type */
1816 typedef unsigned char Byte; /* unsigned 8 bits */
1917
src/ugly.h+0−36
@@ -1,36 +0,0 @@
1−/*
2−** ugly.h
3−** TecCGraf - PUC-Rio
4−** $Id: ugly.h,v 1.2 1994/11/13 14:39:04 roberto Stab $
5−*/
6−
7−#ifndef ugly_h
8−#define ugly_h
9−
10−/* This enum must have the same order of the array 'reserved' in lex.c */
11−
12−enum {
13− U_and=128,
14− U_do,
15− U_else,
16− U_elseif,
17− U_end,
18− U_function,
19− U_if,
20− U_local,
21− U_nil,
22− U_not,
23− U_or,
24− U_repeat,
25− U_return,
26− U_then,
27− U_until,
28− U_while,
29− U_eq = '='+128,
30− U_le = '<'+128,
31− U_ge = '>'+128,
32− U_ne = '~'+128,
33− U_sc = '.'+128
34−};
35−
36−#endif
src/undump.c+326−0
@@ -0,0 +1,326 @@
1+/*
2+** undump.c
3+** load bytecodes from files
4+*/
5+
6+char* rcs_undump="$Id: undump.c,v 1.14 1996/03/14 17:31:15 lhf Exp $";
7+
8+#include <stdio.h>
9+#include <string.h>
10+#include "opcode.h"
11+#include "mem.h"
12+#include "table.h"
13+#include "undump.h"
14+
15+static int swapword=0;
16+static int swapfloat=0;
17+static TFunc* Main=NULL; /* functions in a chunk */
18+static TFunc* lastF=NULL;
19+
20+static void warn(char* s) /* TODO: remove */
21+{
22+#if 0
23+ fprintf(stderr,"undump: %s\n",s);
24+#endif
25+}
26+
27+static void FixCode(Byte* code, Byte* end) /* swap words */
28+{
29+ Byte* p;
30+ for (p=code; p!=end;)
31+ {
32+ OpCode op=(OpCode)*p;
33+ switch (op)
34+ {
35+ case PUSHNIL:
36+ case PUSH0:
37+ case PUSH1:
38+ case PUSH2:
39+ case PUSHLOCAL0:
40+ case PUSHLOCAL1:
41+ case PUSHLOCAL2:
42+ case PUSHLOCAL3:
43+ case PUSHLOCAL4:
44+ case PUSHLOCAL5:
45+ case PUSHLOCAL6:
46+ case PUSHLOCAL7:
47+ case PUSHLOCAL8:
48+ case PUSHLOCAL9:
49+ case PUSHINDEXED:
50+ case STORELOCAL0:
51+ case STORELOCAL1:
52+ case STORELOCAL2:
53+ case STORELOCAL3:
54+ case STORELOCAL4:
55+ case STORELOCAL5:
56+ case STORELOCAL6:
57+ case STORELOCAL7:
58+ case STORELOCAL8:
59+ case STORELOCAL9:
60+ case STOREINDEXED0:
61+ case ADJUST0:
62+ case EQOP:
63+ case LTOP:
64+ case LEOP:
65+ case GTOP:
66+ case GEOP:
67+ case ADDOP:
68+ case SUBOP:
69+ case MULTOP:
70+ case DIVOP:
71+ case POWOP:
72+ case CONCOP:
73+ case MINUSOP:
74+ case NOTOP:
75+ case POP:
76+ case RETCODE0:
77+ p++;
78+ break;
79+ case PUSHBYTE:
80+ case PUSHLOCAL:
81+ case STORELOCAL:
82+ case STOREINDEXED:
83+ case STORELIST0:
84+ case ADJUST:
85+ case RETCODE:
86+ p+=2;
87+ break;
88+ case STORELIST:
89+ case CALLFUNC:
90+ p+=3;
91+ break;
92+ case PUSHFUNCTION:
93+ p+=5;
94+ break;
95+ case PUSHWORD:
96+ case PUSHSELF:
97+ case CREATEARRAY:
98+ case ONTJMP:
99+ case ONFJMP:
100+ case JMP:
101+ case UPJMP:
102+ case IFFJMP:
103+ case IFFUPJMP:
104+ case SETLINE:
105+ case PUSHSTRING:
106+ case PUSHGLOBAL:
107+ case STOREGLOBAL:
108+ {
109+ Byte t;
110+ t=p[1]; p[1]=p[2]; p[2]=t;
111+ p+=3;
112+ break;
113+ }
114+ case PUSHFLOAT:
115+ {
116+ Byte t;
117+ t=p[1]; p[1]=p[4]; p[4]=t;
118+ t=p[2]; p[2]=p[3]; p[3]=t;
119+ p+=5;
120+ break;
121+ }
122+ case STORERECORD:
123+ {
124+ int n=*++p;
125+ p++;
126+ while (n--)
127+ {
128+ Byte t;
129+ t=p[0]; p[0]=p[1]; p[1]=t;
130+ p+=2;
131+ }
132+ break;
133+ }
134+ default:
135+ lua_error("corrupt binary file");
136+ break;
137+ }
138+ }
139+}
140+
141+static void Unthread(Byte* code, int i, int v)
142+{
143+ while (i!=0)
144+ {
145+ CodeWord c;
146+ Byte* p=code+i;
147+ get_word(c,p);
148+ i=c.w;
149+ c.w=v;
150+ p[-2]=c.m.c1;
151+ p[-1]=c.m.c2;
152+ }
153+}
154+
155+static int LoadWord(FILE* D)
156+{
157+ Word w;
158+ fread(&w,sizeof(w),1,D);
159+ if (swapword)
160+ {
161+ Byte* p=(Byte*)&w; /* TODO: need union? */
162+ Byte t;
163+ t=p[0]; p[0]=p[1]; p[1]=t;
164+ }
165+ return w;
166+}
167+
168+static int LoadSize(FILE* D)
169+{
170+ Word hi=LoadWord(D);
171+ Word lo=LoadWord(D);
172+ int s=(hi<<16)|lo;
173+ if ((Word)s != s) lua_error("code too long");
174+ return s;
175+}
176+
177+static char* LoadBlock(int size, FILE* D)
178+{
179+ char* b=luaI_malloc(size);
180+ fread(b,size,1,D);
181+ return b;
182+}
183+
184+static char* LoadString(FILE* D)
185+{
186+ int size=LoadWord(D);
187+ char *b=luaI_buffer(size);
188+ fread(b,size,1,D);
189+ return b;
190+}
191+
192+static char* LoadNewString(FILE* D)
193+{
194+ return LoadBlock(LoadWord(D),D);
195+}
196+
197+static void LoadFunction(FILE* D)
198+{
199+ TFunc* tf=new(TFunc);
200+ tf->next=NULL;
201+ tf->locvars=NULL;
202+ tf->size=LoadSize(D);
203+ tf->lineDefined=LoadWord(D);
204+ if (IsMain(tf)) /* new main */
205+ {
206+ tf->fileName=LoadNewString(D);
207+ Main=lastF=tf;
208+ }
209+ else /* fix PUSHFUNCTION */
210+ {
211+ CodeCode c;
212+ Byte* p;
213+ tf->marked=LoadWord(D);
214+ tf->fileName=Main->fileName;
215+ p=Main->code+tf->marked;
216+ c.tf=tf;
217+ *p++=c.m.c1; *p++=c.m.c2; *p++=c.m.c3; *p++=c.m.c4;
218+ lastF=lastF->next=tf;
219+ }
220+ tf->code=LoadBlock(tf->size,D);
221+ if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);
222+ while (1) /* unthread */
223+ {
224+ int c=getc(D);
225+ if (c==ID_VAR) /* global var */
226+ {
227+ int i=LoadWord(D);
228+ char* s=LoadString(D);
229+ int v=luaI_findsymbolbyname(s);
230+ Unthread(tf->code,i,v);
231+ }
232+ else if (c==ID_STR) /* constant string */
233+ {
234+ int i=LoadWord(D);
235+ char* s=LoadString(D);
236+ int v=luaI_findconstantbyname(s);
237+ Unthread(tf->code,i,v);
238+ }
239+ else
240+ {
241+ ungetc(c,D);
242+ break;
243+ }
244+ }
245+}
246+
247+static void LoadSignature(FILE* D)
248+{
249+ char* s=SIGNATURE;
250+ while (*s!=0 && getc(D)==*s)
251+ ++s;
252+ if (*s!=0) lua_error("bad signature");
253+}
254+
255+static void LoadHeader(FILE* D) /* TODO: error handling */
256+{
257+ Word w,tw=TEST_WORD;
258+ float f,tf=TEST_FLOAT;
259+ LoadSignature(D);
260+ getc(D); /* skip version */
261+ fread(&w,sizeof(w),1,D); /* test word */
262+ if (w!=tw)
263+ {
264+ swapword=1;
265+ warn("different byte order");
266+ }
267+ fread(&f,sizeof(f),1,D); /* test float */
268+ if (f!=tf)
269+ {
270+ Byte* p=(Byte*)&f; /* TODO: need union? */
271+ Byte t;
272+ swapfloat=1;
273+ t=p[0]; p[0]=p[3]; p[3]=t;
274+ t=p[1]; p[1]=p[2]; p[2]=t;
275+ if (f!=tf) /* TODO: try another perm? */
276+ lua_error("different float representation");
277+ else
278+ warn("different byte order in floats");
279+ }
280+}
281+
282+static void LoadChunk(FILE* D)
283+{
284+ LoadHeader(D);
285+ while (1)
286+ {
287+ int c=getc(D);
288+ if (c==ID_FUN) LoadFunction(D); else { ungetc(c,D); break; }
289+ }
290+}
291+
292+/*
293+** load one chunk from a file.
294+** return list of functions found, headed by main, or NULL at EOF.
295+*/
296+TFunc* luaI_undump1(FILE* D)
297+{
298+ while (1)
299+ {
300+ int c=getc(D);
301+ if (c==ID_CHUNK)
302+ {
303+ LoadChunk(D);
304+ return Main;
305+ }
306+ else if (c==EOF)
307+ return NULL;
308+ else
309+ lua_error("not a lua binary file");
310+ }
311+}
312+
313+/*
314+** load and run all chunks in a file
315+*/
316+int luaI_undump(FILE* D)
317+{
318+ TFunc* m;
319+ while ((m=luaI_undump1(D)))
320+ {
321+ int status=luaI_dorun(m);
322+ luaI_freefunc(m);
323+ if (status!=0) return status;
324+ }
325+ return 0;
326+}
src/undump.h+23−0
@@ -0,0 +1,23 @@
1+/*
2+** undump.h
3+** definitions for lua decompiler
4+** $Id: undump.h,v 1.2 1996/03/11 21:59:41 lhf Exp $
5+*/
6+
7+#include "func.h"
8+
9+#define IsMain(f) (f->lineDefined==0)
10+
11+/* definitions for chunk headers */
12+
13+#define ID_CHUNK 27 /* ESC */
14+#define ID_FUN 'F'
15+#define ID_VAR 'V'
16+#define ID_STR 'S'
17+#define SIGNATURE "Lua"
18+#define VERSION 0x23 /* 2.3 */
19+#define TEST_WORD 0x1234 /* a word for testing byte ordering */
20+#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */
21+
22+TFunc* luaI_undump1(FILE* D); /* load one chunk */
23+int luaI_undump(FILE* D); /* load all chunks */