aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-14 19:40:14 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-14 19:40:14 -0200
commit86b35cf4f6a824880239069d0afe282e95806aaa (patch)
tree78352c354fc6befe1af900606cb84b23a40235e0
parent3b7a36653b5da227502ec5a3c677b6a351af67be (diff)
downloadlua-86b35cf4f6a824880239069d0afe282e95806aaa.tar.gz
lua-86b35cf4f6a824880239069d0afe282e95806aaa.tar.bz2
lua-86b35cf4f6a824880239069d0afe282e95806aaa.zip
unification of symbol tree and constant tree
-rw-r--r--inout.c6
-rw-r--r--lex.c24
-rw-r--r--lua.stx141
-rw-r--r--table.c48
-rw-r--r--table.h9
-rw-r--r--tree.c69
-rw-r--r--tree.h17
7 files changed, 131 insertions, 183 deletions
diff --git a/inout.c b/inout.c
index 9a54f4fc..a7dc7413 100644
--- a/inout.c
+++ b/inout.c
@@ -5,7 +5,7 @@
5** Also provides some predefined lua functions. 5** Also provides some predefined lua functions.
6*/ 6*/
7 7
8char *rcs_inout="$Id: inout.c,v 2.9 1994/11/08 20:06:15 roberto Exp roberto $"; 8char *rcs_inout="$Id: inout.c,v 2.10 1994/11/09 18:09:22 roberto Exp roberto $";
9 9
10#include <stdio.h> 10#include <stdio.h>
11#include <stdlib.h> 11#include <stdlib.h>
@@ -150,12 +150,12 @@ void lua_reportbug (char *s)
150 { 150 {
151 sprintf (strchr(msg,0), 151 sprintf (strchr(msg,0),
152 "\n\tin statement begining at line %d in function \"%s\" of file \"%s\"", 152 "\n\tin statement begining at line %d in function \"%s\" of file \"%s\"",
153 lua_debugline, lua_varname(funcstack[nfuncstack-1].function), 153 lua_debugline, lua_constant[funcstack[nfuncstack-1].function],
154 funcstack[nfuncstack-1].file); 154 funcstack[nfuncstack-1].file);
155 sprintf (strchr(msg,0), "\n\tactive stack\n"); 155 sprintf (strchr(msg,0), "\n\tactive stack\n");
156 for (i=nfuncstack-1; i>=0; i--) 156 for (i=nfuncstack-1; i>=0; i--)
157 sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n", 157 sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n",
158 lua_varname(funcstack[i].function), 158 lua_constant[funcstack[i].function],
159 funcstack[i].file); 159 funcstack[i].file);
160 } 160 }
161 else 161 else
diff --git a/lex.c b/lex.c
index 7aad1d0d..d0075a53 100644
--- a/lex.c
+++ b/lex.c
@@ -1,4 +1,4 @@
1char *rcs_lex = "$Id: lex.c,v 2.9 1994/11/03 17:09:20 roberto Exp roberto $"; 1char *rcs_lex = "$Id: lex.c,v 2.10 1994/11/13 14:39:04 roberto Exp roberto $";
2 2
3 3
4#include <ctype.h> 4#include <ctype.h>
@@ -7,6 +7,8 @@ char *rcs_lex = "$Id: lex.c,v 2.9 1994/11/03 17:09:20 roberto Exp roberto $";
7#include <stdlib.h> 7#include <stdlib.h>
8#include <string.h> 8#include <string.h>
9 9
10#include "tree.h"
11#include "table.h"
10#include "opcode.h" 12#include "opcode.h"
11#include "inout.h" 13#include "inout.h"
12#include "y.tab.h" 14#include "y.tab.h"
@@ -19,9 +21,8 @@ char *rcs_lex = "$Id: lex.c,v 2.9 1994/11/03 17:09:20 roberto Exp roberto $";
19#define save_and_next() { save(current); next(); } 21#define save_and_next() { save(current); next(); }
20 22
21static int current; 23static int current;
22static char yytext[2][256]; 24static char yytext[256];
23static char *yytextLast; 25static char *yytextLast;
24static int currentText = 0;
25 26
26static Input input; 27static Input input;
27 28
@@ -34,7 +35,7 @@ void lua_setinput (Input fn)
34char *lua_lasttext (void) 35char *lua_lasttext (void)
35{ 36{
36 *yytextLast = 0; 37 *yytextLast = 0;
37 return yytext[currentText]; 38 return yytext;
38} 39}
39 40
40 41
@@ -87,10 +88,9 @@ static int findReserved (char *name)
87int yylex (void) 88int yylex (void)
88{ 89{
89 float a; 90 float a;
90 currentText = !currentText;
91 while (1) 91 while (1)
92 { 92 {
93 yytextLast = yytext[currentText]; 93 yytextLast = yytext;
94#if 0 94#if 0
95 fprintf(stderr,"'%c' %d\n",current,current); 95 fprintf(stderr,"'%c' %d\n",current,current);
96#endif 96#endif
@@ -110,12 +110,12 @@ int yylex (void)
110 while (isalnum(current) || current == '_') 110 while (isalnum(current) || current == '_')
111 save_and_next(); 111 save_and_next();
112 *yytextLast = 0; 112 *yytextLast = 0;
113 if (lua_strcmp(yytext[currentText], "debug") == 0) 113 if (lua_strcmp(yytext, "debug") == 0)
114 { 114 {
115 yylval.vInt = 1; 115 yylval.vInt = 1;
116 return DEBUG; 116 return DEBUG;
117 } 117 }
118 else if (lua_strcmp(yytext[currentText], "nodebug") == 0) 118 else if (lua_strcmp(yytext, "nodebug") == 0)
119 { 119 {
120 yylval.vInt = 0; 120 yylval.vInt = 0;
121 return DEBUG; 121 return DEBUG;
@@ -179,7 +179,7 @@ int yylex (void)
179 } 179 }
180 next(); /* skip the delimiter */ 180 next(); /* skip the delimiter */
181 *yytextLast = 0; 181 *yytextLast = 0;
182 yylval.pChar = yytext[currentText]; 182 yylval.vWord = luaI_findconstant(lua_constcreate(yytext));
183 return STRING; 183 return STRING;
184 } 184 }
185 185
@@ -200,9 +200,9 @@ int yylex (void)
200 int res; 200 int res;
201 do { save_and_next(); } while (isalnum(current) || current == '_'); 201 do { save_and_next(); } while (isalnum(current) || current == '_');
202 *yytextLast = 0; 202 *yytextLast = 0;
203 res = findReserved(yytext[currentText]); 203 res = findReserved(yytext);
204 if (res) return res; 204 if (res) return res;
205 yylval.pChar = yytext[currentText]; 205 yylval.pNode = lua_constcreate(yytext);
206 return NAME; 206 return NAME;
207 } 207 }
208 208
@@ -266,7 +266,7 @@ fraction:
266 default: /* also end of file */ 266 default: /* also end of file */
267 { 267 {
268 save_and_next(); 268 save_and_next();
269 return yytext[currentText][0]; 269 return yytext[0];
270 } 270 }
271 } 271 }
272 } 272 }
diff --git a/lua.stx b/lua.stx
index 177e60ed..1b365942 100644
--- a/lua.stx
+++ b/lua.stx
@@ -1,6 +1,6 @@
1%{ 1%{
2 2
3char *rcs_luastx = "$Id: lua.stx,v 3.4 1994/11/09 18:07:38 roberto Exp roberto $"; 3char *rcs_luastx = "$Id: lua.stx,v 3.5 1994/11/13 14:54:18 roberto Exp roberto $";
4 4
5#include <stdio.h> 5#include <stdio.h>
6#include <stdlib.h> 6#include <stdlib.h>
@@ -9,6 +9,7 @@ char *rcs_luastx = "$Id: lua.stx,v 3.4 1994/11/09 18:07:38 roberto Exp roberto $
9#include "opcode.h" 9#include "opcode.h"
10#include "hash.h" 10#include "hash.h"
11#include "inout.h" 11#include "inout.h"
12#include "tree.h"
12#include "table.h" 13#include "table.h"
13#include "lua.h" 14#include "lua.h"
14 15
@@ -40,6 +41,7 @@ static int nlocalvar=0; /* number of local variables */
40static Word fields[MAXFIELDS]; /* fieldnames to be flushed */ 41static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
41static int nfields=0; 42static int nfields=0;
42 43
44
43/* Internal functions */ 45/* Internal functions */
44 46
45static void code_byte (Byte c) 47static void code_byte (Byte c)
@@ -164,17 +166,6 @@ static void code_number (float f)
164 } 166 }
165} 167}
166 168
167static void init_function (void)
168{
169 if (funcCode == NULL) /* first function */
170 {
171 funcCode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
172 if (funcCode == NULL)
173 lua_error("not enough memory");
174 maxcode = CODE_BLOCK;
175 }
176}
177
178 169
179%} 170%}
180 171
@@ -187,6 +178,7 @@ static void init_function (void)
187 Word vWord; 178 Word vWord;
188 Long vLong; 179 Long vLong;
189 Byte *pByte; 180 Byte *pByte;
181 TreeNode *pNode;
190} 182}
191 183
192%start functionlist 184%start functionlist
@@ -198,8 +190,8 @@ static void init_function (void)
198%token LOCAL 190%token LOCAL
199%token FUNCTION 191%token FUNCTION
200%token <vFloat> NUMBER 192%token <vFloat> NUMBER
201%token <pChar> STRING 193%token <vWord> STRING
202%token <pChar> NAME 194%token <pNode> NAME
203%token <vInt> DEBUG 195%token <vInt> DEBUG
204 196
205%type <vLong> PrepJump 197%type <vLong> PrepJump
@@ -208,7 +200,7 @@ static void init_function (void)
208%type <vInt> ffieldlist1 200%type <vInt> ffieldlist1
209%type <vInt> lfieldlist1 201%type <vInt> lfieldlist1
210%type <vLong> var, singlevar 202%type <vLong> var, singlevar
211 203%type <pByte> body
212 204
213%left AND OR 205%left AND OR
214%left EQ NE '>' '<' LE GE 206%left EQ NE '>' '<' LE GE
@@ -239,84 +231,54 @@ functionlist : /* empty */
239 231
240function : FUNCTION NAME 232function : FUNCTION NAME
241 { 233 {
242 init_function(); 234 init_function($2);
243 pc=0; basepc=funcCode; maxcurr=maxcode;
244 nlocalvar=0;
245 $<vWord>$ = lua_findsymbol($2);
246 }
247 '(' parlist ')'
248 {
249 if (lua_debug)
250 {
251 code_byte(SETFUNCTION);
252 code_code((Byte *)strdup(lua_file[lua_nfile-1]));
253 code_word($<vWord>3);
254 }
255 lua_codeadjust (0);
256 } 235 }
257 block 236 body
258 END
259 { 237 {
260 codereturn(); 238 Word func = luaI_findsymbol($2);
261 s_tag($<vWord>3) = LUA_T_FUNCTION; 239 s_tag(func) = LUA_T_FUNCTION;
262 s_bvalue($<vWord>3) = calloc (pc, sizeof(Byte)); 240 s_bvalue(func) = $4;
263 if (s_bvalue($<vWord>3) == NULL)
264 lua_error("not enough memory");
265 memcpy (s_bvalue($<vWord>3), basepc, pc*sizeof(Byte));
266 funcCode = basepc; maxcode=maxcurr;
267#if LISTING 241#if LISTING
268 PrintCode(funcCode,funcCode+pc); 242 PrintCode(funcCode,funcCode+pc);
269#endif 243#endif
270 } 244 }
271 ; 245 ;
272 246
273method : FUNCTION NAME { $<vWord>$ = lua_findsymbol($2); } ':' NAME 247method : FUNCTION NAME ':' NAME
274 { 248 {
275 init_function(); 249 init_function($4);
276 pc=0; basepc=funcCode; maxcurr=maxcode; 250 localvar[nlocalvar]=luaI_findsymbolbyname("self");
277 nlocalvar=0;
278 localvar[nlocalvar]=lua_findsymbol("self"); /* self param. */
279 add_nlocalvar(1); 251 add_nlocalvar(1);
280 $<vWord>$ = lua_findconstant($5);
281 }
282 '(' parlist ')'
283 {
284 if (lua_debug)
285 {
286 code_byte(SETFUNCTION);
287 code_code((Byte *)strdup(lua_file[lua_nfile-1]));
288 code_word($<vWord>6);
289 }
290 lua_codeadjust (0);
291 } 252 }
292 block 253 body
293 END
294 { 254 {
295 Byte *b;
296 codereturn();
297 b = calloc (pc, sizeof(Byte));
298 if (b == NULL)
299 lua_error("not enough memory");
300 memcpy (b, basepc, pc*sizeof(Byte));
301 funcCode = basepc; maxcode=maxcurr;
302#if LISTING 255#if LISTING
303 PrintCode(funcCode,funcCode+pc); 256 PrintCode(funcCode,funcCode+pc);
304#endif 257#endif
305 /* assign function to table field */ 258 /* assign function to table field */
306 pc=maincode; basepc=*initcode; maxcurr=maxmain; 259 pc=maincode; basepc=*initcode; maxcurr=maxmain;
307 nlocalvar=0; 260 nlocalvar=0;
308 261 lua_pushvar(luaI_findsymbol($2)+1);
309 lua_pushvar($<vWord>3+1);
310 code_byte(PUSHSTRING); 262 code_byte(PUSHSTRING);
311 code_word($<vWord>6); 263 code_word(luaI_findconstant($4));
312 code_byte(PUSHFUNCTION); 264 code_byte(PUSHFUNCTION);
313 code_code(b); 265 code_code($6);
314 code_byte(STOREINDEXED0); 266 code_byte(STOREINDEXED0);
315
316 maincode=pc; *initcode=basepc; maxmain=maxcurr; 267 maincode=pc; *initcode=basepc; maxmain=maxcurr;
317 } 268 }
318 ; 269 ;
319 270
271body : '(' parlist ')' block END
272 {
273 codereturn();
274 $$ = calloc (pc, sizeof(Byte));
275 if ($$ == NULL)
276 lua_error("not enough memory");
277 memcpy ($$, basepc, pc*sizeof(Byte));
278 funcCode = basepc; maxcode=maxcurr;
279 }
280 ;
281
320statlist : /* empty */ 282statlist : /* empty */
321 | statlist stat sc 283 | statlist stat sc
322 ; 284 ;
@@ -454,7 +416,7 @@ expr : '(' expr ')' { $$ = $2; }
454 | STRING 416 | STRING
455 { 417 {
456 code_byte(PUSHSTRING); 418 code_byte(PUSHSTRING);
457 code_word(lua_findconstant($1)); 419 code_word($1);
458 $$ = 1; 420 $$ = 1;
459 } 421 }
460 | NIL {code_byte(PUSHNIL); $$ = 1; } 422 | NIL {code_byte(PUSHNIL); $$ = 1; }
@@ -493,7 +455,7 @@ funcvalue : varexp { $$ = 0; }
493 | varexp ':' NAME 455 | varexp ':' NAME
494 { 456 {
495 code_byte(PUSHSTRING); 457 code_byte(PUSHSTRING);
496 code_word(lua_findconstant($3)); 458 code_word(luaI_findconstant($3));
497 code_byte(PUSHSELF); 459 code_byte(PUSHSELF);
498 $$ = 1; 460 $$ = 1;
499 } 461 }
@@ -516,18 +478,18 @@ exprlist1 : expr { if ($1 == 0) $$ = -1; else $$ = 1; }
516 } 478 }
517 ; 479 ;
518 480
519parlist : /* empty */ 481parlist : /* empty */ { lua_codeadjust(0); }
520 | parlist1 482 | parlist1 { lua_codeadjust(0); }
521 ; 483 ;
522 484
523parlist1 : NAME 485parlist1 : NAME
524 { 486 {
525 localvar[nlocalvar]=lua_findsymbol($1); 487 localvar[nlocalvar]=luaI_findsymbol($1);
526 add_nlocalvar(1); 488 add_nlocalvar(1);
527 } 489 }
528 | parlist1 ',' NAME 490 | parlist1 ',' NAME
529 { 491 {
530 localvar[nlocalvar]=lua_findsymbol($3); 492 localvar[nlocalvar]=luaI_findsymbol($3);
531 add_nlocalvar(1); 493 add_nlocalvar(1);
532 } 494 }
533 ; 495 ;
@@ -555,9 +517,9 @@ ffieldlist1 : ffield {$$=1;}
555 } 517 }
556 ; 518 ;
557 519
558ffield : NAME {$<vWord>$ = lua_findconstant($1);} '=' expr1 520ffield : NAME '=' expr1
559 { 521 {
560 push_field($<vWord>2); 522 push_field(luaI_findconstant($1));
561 } 523 }
562 ; 524 ;
563 525
@@ -591,14 +553,14 @@ var : singlevar { $$ = $1; }
591 | varexp '.' NAME 553 | varexp '.' NAME
592 { 554 {
593 code_byte(PUSHSTRING); 555 code_byte(PUSHSTRING);
594 code_word(lua_findconstant($3)); 556 code_word(luaI_findconstant($3));
595 $$ = 0; /* indexed variable */ 557 $$ = 0; /* indexed variable */
596 } 558 }
597 ; 559 ;
598 560
599singlevar : NAME 561singlevar : NAME
600 { 562 {
601 Word s = lua_findsymbol($1); 563 Word s = luaI_findsymbol($1);
602 int local = lua_localname (s); 564 int local = lua_localname (s);
603 if (local == -1) /* global var */ 565 if (local == -1) /* global var */
604 $$ = s + 1; /* return positive value */ 566 $$ = s + 1; /* return positive value */
@@ -610,10 +572,10 @@ singlevar : NAME
610varexp : var { lua_pushvar($1); } 572varexp : var { lua_pushvar($1); }
611 ; 573 ;
612 574
613localdeclist : NAME {localvar[nlocalvar]=lua_findsymbol($1); $$ = 1;} 575localdeclist : NAME {localvar[nlocalvar]=luaI_findsymbol($1); $$ = 1;}
614 | localdeclist ',' NAME 576 | localdeclist ',' NAME
615 { 577 {
616 localvar[nlocalvar+$1]=lua_findsymbol($3); 578 localvar[nlocalvar+$1]=luaI_findsymbol($3);
617 $$ = $1+1; 579 $$ = $1+1;
618 } 580 }
619 ; 581 ;
@@ -676,6 +638,25 @@ static void lua_codeadjust (int n)
676 } 638 }
677} 639}
678 640
641static void init_function (TreeNode *func)
642{
643 if (funcCode == NULL) /* first function */
644 {
645 funcCode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
646 if (funcCode == NULL)
647 lua_error("not enough memory");
648 maxcode = CODE_BLOCK;
649 }
650 pc=0; basepc=funcCode; maxcurr=maxcode;
651 nlocalvar=0;
652 if (lua_debug)
653 {
654 code_byte(SETFUNCTION);
655 code_code((Byte *)strdup(lua_file[lua_nfile-1]));
656 code_word(luaI_findconstant(func));
657 }
658}
659
679static void codereturn (void) 660static void codereturn (void)
680{ 661{
681 if (lua_debug) code_byte(RESET); 662 if (lua_debug) code_byte(RESET);
diff --git a/table.c b/table.c
index 8047b14a..20ab6046 100644
--- a/table.c
+++ b/table.c
@@ -3,7 +3,7 @@
3** Module to control static tables 3** Module to control static tables
4*/ 4*/
5 5
6char *rcs_table="$Id: table.c,v 2.15 1994/11/10 20:41:37 roberto Exp roberto $"; 6char *rcs_table="$Id: table.c,v 2.16 1994/11/11 14:00:08 roberto Exp roberto $";
7 7
8#include <stdlib.h> 8#include <stdlib.h>
9#include <string.h> 9#include <string.h>
@@ -51,23 +51,23 @@ static void lua_initsymbol (void)
51 lua_table = (Symbol *) calloc(lua_maxsymbol, sizeof(Symbol)); 51 lua_table = (Symbol *) calloc(lua_maxsymbol, sizeof(Symbol));
52 if (lua_table == NULL) 52 if (lua_table == NULL)
53 lua_error ("symbol table: not enough memory"); 53 lua_error ("symbol table: not enough memory");
54 n = lua_findsymbol("next"); 54 n = luaI_findsymbolbyname("next");
55 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next; 55 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
56 n = lua_findsymbol("nextvar"); 56 n = luaI_findsymbolbyname("nextvar");
57 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar; 57 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar;
58 n = lua_findsymbol("type"); 58 n = luaI_findsymbolbyname("type");
59 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type; 59 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type;
60 n = lua_findsymbol("tonumber"); 60 n = luaI_findsymbolbyname("tonumber");
61 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number; 61 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
62 n = lua_findsymbol("print"); 62 n = luaI_findsymbolbyname("print");
63 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print; 63 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print;
64 n = lua_findsymbol("dofile"); 64 n = luaI_findsymbolbyname("dofile");
65 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile; 65 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
66 n = lua_findsymbol("dostring"); 66 n = luaI_findsymbolbyname("dostring");
67 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring; 67 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
68 n = lua_findsymbol("setfallback"); 68 n = luaI_findsymbolbyname("setfallback");
69 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback; 69 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback;
70 n = lua_findsymbol("error"); 70 n = luaI_findsymbolbyname("error");
71 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error; 71 s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error;
72} 72}
73 73
@@ -89,13 +89,11 @@ void lua_initconstant (void)
89** found, allocate it. 89** found, allocate it.
90** On error, return -1. 90** On error, return -1.
91*/ 91*/
92int lua_findsymbol (char *s) 92int luaI_findsymbol (TreeNode *t)
93{ 93{
94 char *n;
95 if (lua_table == NULL) 94 if (lua_table == NULL)
96 lua_initsymbol(); 95 lua_initsymbol();
97 n = lua_varcreate(s); 96 if (t->varindex == UNMARKED_STRING)
98 if (indexstring(n) == UNMARKED_STRING)
99 { 97 {
100 if (lua_ntable == lua_maxsymbol) 98 if (lua_ntable == lua_maxsymbol)
101 { 99 {
@@ -106,11 +104,17 @@ int lua_findsymbol (char *s)
106 if (lua_table == NULL) 104 if (lua_table == NULL)
107 lua_error ("symbol table: not enough memory"); 105 lua_error ("symbol table: not enough memory");
108 } 106 }
109 indexstring(n) = lua_ntable; 107 t->varindex = lua_ntable;
110 s_tag(lua_ntable) = LUA_T_NIL; 108 s_tag(lua_ntable) = LUA_T_NIL;
111 lua_ntable++; 109 lua_ntable++;
112 } 110 }
113 return indexstring(n); 111 return t->varindex;
112}
113
114
115int luaI_findsymbolbyname (char *name)
116{
117 return luaI_findsymbol(lua_constcreate(name));
114} 118}
115 119
116 120
@@ -119,13 +123,11 @@ int lua_findsymbol (char *s)
119** found, allocate it. 123** found, allocate it.
120** On error, return -1. 124** On error, return -1.
121*/ 125*/
122int lua_findconstant (char *s) 126int luaI_findconstant (TreeNode *t)
123{ 127{
124 char *n;
125 if (lua_constant == NULL) 128 if (lua_constant == NULL)
126 lua_initconstant(); 129 lua_initconstant();
127 n = lua_constcreate(s); 130 if (t->constindex == UNMARKED_STRING)
128 if (indexstring(n) == UNMARKED_STRING)
129 { 131 {
130 if (lua_nconstant == lua_maxconstant) 132 if (lua_nconstant == lua_maxconstant)
131 { 133 {
@@ -136,11 +138,11 @@ int lua_findconstant (char *s)
136 if (lua_constant == NULL) 138 if (lua_constant == NULL)
137 lua_error ("constant table: not enough memory"); 139 lua_error ("constant table: not enough memory");
138 } 140 }
139 indexstring(n) = lua_nconstant; 141 t->constindex = lua_nconstant;
140 lua_constant[lua_nconstant] = n; 142 lua_constant[lua_nconstant] = t->str;
141 lua_nconstant++; 143 lua_nconstant++;
142 } 144 }
143 return indexstring(n); 145 return t->constindex;
144} 146}
145 147
146 148
diff --git a/table.h b/table.h
index a885f201..423ba3dd 100644
--- a/table.h
+++ b/table.h
@@ -1,12 +1,14 @@
1/* 1/*
2** Module to control static tables 2** Module to control static tables
3** TeCGraf - PUC-Rio 3** TeCGraf - PUC-Rio
4** $Id: table.h,v 2.3 1994/10/17 19:03:23 celes Exp roberto $ 4** $Id: table.h,v 2.4 1994/11/03 21:48:36 roberto Exp roberto $
5*/ 5*/
6 6
7#ifndef table_h 7#ifndef table_h
8#define table_h 8#define table_h
9 9
10#include "tree.h"
11
10extern Symbol *lua_table; 12extern Symbol *lua_table;
11extern char **lua_constant; 13extern char **lua_constant;
12 14
@@ -19,8 +21,9 @@ extern Word lua_recovered;
19 21
20 22
21void lua_initconstant (void); 23void lua_initconstant (void);
22int lua_findsymbol (char *s); 24int luaI_findsymbolbyname (char *name);
23int lua_findconstant (char *s); 25int luaI_findsymbol (TreeNode *t);
26int luaI_findconstant (TreeNode *t);
24void lua_travsymbol (void (*fn)(Object *)); 27void lua_travsymbol (void (*fn)(Object *));
25void lua_markobject (Object *o); 28void lua_markobject (Object *o);
26void lua_pack (void); 29void lua_pack (void);
diff --git a/tree.c b/tree.c
index ae5a19de..bca29994 100644
--- a/tree.c
+++ b/tree.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_tree="$Id: tree.c,v 1.2 1994/10/18 17:36:11 celes Exp roberto $"; 6char *rcs_tree="$Id: tree.c,v 1.3 1994/11/10 20:41:37 roberto Exp roberto $";
7 7
8 8
9#include <stdlib.h> 9#include <stdlib.h>
@@ -17,22 +17,13 @@ char *rcs_tree="$Id: tree.c,v 1.2 1994/10/18 17:36:11 celes Exp roberto $";
17#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b))) 17#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
18 18
19 19
20typedef struct TreeNode
21{
22 struct TreeNode *right;
23 struct TreeNode *left;
24 Word index;
25 char str[1]; /* \0 byte already reserved */
26} TreeNode;
27
28static TreeNode *string_root = NULL; 20static TreeNode *string_root = NULL;
29static TreeNode *constant_root = NULL; 21static TreeNode *constant_root = NULL;
30static TreeNode *variable_root = NULL;
31 22
32/* 23/*
33** Insert a new string/constant/variable at the tree. 24** Insert a new string/constant/variable at the tree.
34*/ 25*/
35static char *tree_create (TreeNode **node, char *str, int *created) 26static TreeNode *tree_create (TreeNode **node, char *str, int *created)
36{ 27{
37 if (*node == NULL) 28 if (*node == NULL)
38 { 29 {
@@ -41,9 +32,9 @@ static char *tree_create (TreeNode **node, char *str, int *created)
41 lua_error("not enough memory"); 32 lua_error("not enough memory");
42 (*node)->left = (*node)->right = NULL; 33 (*node)->left = (*node)->right = NULL;
43 strcpy((*node)->str, str); 34 strcpy((*node)->str, str);
44 (*node)->index = UNMARKED_STRING; 35 (*node)->varindex = (*node)->constindex = UNMARKED_STRING;
45 *created = 1; 36 *created = 1;
46 return (*node)->str; 37 return *node;
47 } 38 }
48 else 39 else
49 { 40 {
@@ -53,35 +44,28 @@ static char *tree_create (TreeNode **node, char *str, int *created)
53 else if (c > 0) 44 else if (c > 0)
54 return tree_create(&(*node)->right, str, created); 45 return tree_create(&(*node)->right, str, created);
55 else 46 else
56 return (*node)->str; 47 return *node;
57 } 48 }
58} 49}
59 50
60char *lua_strcreate (char *str) 51char *lua_strcreate (char *str)
61{ 52{
62 int created=0; 53 int created=0;
63 char *s = tree_create(&string_root, str, &created); 54 TreeNode *t = tree_create(&string_root, str, &created);
64 if (created) 55 if (created)
65 { 56 {
66 if (lua_nentity == lua_block) lua_pack (); 57 if (lua_nentity == lua_block) lua_pack ();
67 lua_nentity++; 58 lua_nentity++;
68 } 59 }
69 return s; 60 return t->str;
70} 61}
71 62
72char *lua_constcreate (char *str) 63TreeNode *lua_constcreate (char *str)
73{ 64{
74 int created; 65 int created;
75 return tree_create(&constant_root, str, &created); 66 return tree_create(&constant_root, str, &created);
76} 67}
77 68
78char *lua_varcreate (char *str)
79{
80 int created;
81 return tree_create(&variable_root, str, &created);
82}
83
84
85 69
86/* 70/*
87** Free a node of the tree 71** Free a node of the tree
@@ -141,14 +125,14 @@ static TreeNode *lua_travcollector (TreeNode *r)
141 if (r == NULL) return NULL; 125 if (r == NULL) return NULL;
142 r->right = lua_travcollector(r->right); 126 r->right = lua_travcollector(r->right);
143 r->left = lua_travcollector(r->left); 127 r->left = lua_travcollector(r->left);
144 if (r->index == UNMARKED_STRING) 128 if (r->constindex == UNMARKED_STRING)
145 { 129 {
146 ++lua_recovered; 130 ++lua_recovered;
147 return lua_strfree(r); 131 return lua_strfree(r);
148 } 132 }
149 else 133 else
150 { 134 {
151 r->index = UNMARKED_STRING; 135 r->constindex = UNMARKED_STRING;
152 return r; 136 return r;
153 } 137 }
154} 138}
@@ -168,18 +152,6 @@ void lua_strcollector (void)
168*/ 152*/
169static TreeNode *tree_next (TreeNode *node, char *str) 153static TreeNode *tree_next (TreeNode *node, char *str)
170{ 154{
171#if 0
172 if (node == NULL) return NULL;
173 if (str == NULL || lua_strcmp(str, node->str) < 0)
174 {
175 TreeNode *result = tree_next(node->left, str);
176 return result == NULL ? node : result;
177 }
178 else
179 {
180 return tree_next(node->right, str);
181 }
182#else
183 if (node == NULL) return NULL; 155 if (node == NULL) return NULL;
184 else if (str == NULL) return node; 156 else if (str == NULL) return node;
185 else 157 else
@@ -195,30 +167,11 @@ static TreeNode *tree_next (TreeNode *node, char *str)
195 else 167 else
196 return tree_next(node->right, str); 168 return tree_next(node->right, str);
197 } 169 }
198#endif
199} 170}
200 171
201char *lua_varnext (char *n) 172char *lua_varnext (char *n)
202{ 173{
203 TreeNode *result = tree_next(variable_root, n); 174 TreeNode *result = tree_next(constant_root, n);
204 return result != NULL ? result->str : NULL; 175 return result != NULL ? result->str : NULL;
205} 176}
206 177
207
208/*
209** Given an id, find the string with exaustive search
210*/
211static char *tree_name (TreeNode *node, Word index)
212{
213 if (node == NULL) return NULL;
214 if (node->index == index) return node->str;
215 else
216 {
217 char *result = tree_name(node->left, index);
218 return result != NULL ? result : tree_name(node->right, index);
219 }
220}
221char *lua_varname (Word index)
222{
223 return tree_name(variable_root, index);
224}
diff --git a/tree.h b/tree.h
index b1099e4e..0e7c27c9 100644
--- a/tree.h
+++ b/tree.h
@@ -1,7 +1,7 @@
1/* 1/*
2** tree.h 2** tree.h
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4** $Id: $ 4** $Id: tree.h,v 1.1 1994/07/19 21:24:17 celes Exp roberto $
5*/ 5*/
6 6
7#ifndef tree_h 7#ifndef tree_h
@@ -14,14 +14,23 @@
14#define MARKED_STRING 0xFFFE 14#define MARKED_STRING 0xFFFE
15#define MAX_WORD 0xFFFD 15#define MAX_WORD 0xFFFD
16 16
17
18typedef struct TreeNode
19{
20 struct TreeNode *right;
21 struct TreeNode *left;
22 Word varindex; /* if this is a symbol */
23 Word constindex; /* if this is a constant; also used for garbage collection */
24 char str[1]; /* \0 byte already reserved */
25} TreeNode;
26
27
17#define indexstring(s) (*(((Word *)s)-1)) 28#define indexstring(s) (*(((Word *)s)-1))
18 29
19 30
20char *lua_strcreate (char *str); 31char *lua_strcreate (char *str);
21char *lua_constcreate (char *str); 32TreeNode *lua_constcreate (char *str);
22char *lua_varcreate (char *str);
23void lua_strcollector (void); 33void lua_strcollector (void);
24char *lua_varnext (char *n); 34char *lua_varnext (char *n);
25char *lua_varname (Word index);
26 35
27#endif 36#endif