diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-14 19:40:14 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-14 19:40:14 -0200 |
commit | 86b35cf4f6a824880239069d0afe282e95806aaa (patch) | |
tree | 78352c354fc6befe1af900606cb84b23a40235e0 /lua.stx | |
parent | 3b7a36653b5da227502ec5a3c677b6a351af67be (diff) | |
download | lua-86b35cf4f6a824880239069d0afe282e95806aaa.tar.gz lua-86b35cf4f6a824880239069d0afe282e95806aaa.tar.bz2 lua-86b35cf4f6a824880239069d0afe282e95806aaa.zip |
unification of symbol tree and constant tree
Diffstat (limited to 'lua.stx')
-rw-r--r-- | lua.stx | 141 |
1 files changed, 61 insertions, 80 deletions
@@ -1,6 +1,6 @@ | |||
1 | %{ | 1 | %{ |
2 | 2 | ||
3 | char *rcs_luastx = "$Id: lua.stx,v 3.4 1994/11/09 18:07:38 roberto Exp roberto $"; | 3 | char *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 */ | |||
40 | static Word fields[MAXFIELDS]; /* fieldnames to be flushed */ | 41 | static Word fields[MAXFIELDS]; /* fieldnames to be flushed */ |
41 | static int nfields=0; | 42 | static int nfields=0; |
42 | 43 | ||
44 | |||
43 | /* Internal functions */ | 45 | /* Internal functions */ |
44 | 46 | ||
45 | static void code_byte (Byte c) | 47 | static void code_byte (Byte c) |
@@ -164,17 +166,6 @@ static void code_number (float f) | |||
164 | } | 166 | } |
165 | } | 167 | } |
166 | 168 | ||
167 | static 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 | ||
240 | function : FUNCTION NAME | 232 | function : 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 | ||
273 | method : FUNCTION NAME { $<vWord>$ = lua_findsymbol($2); } ':' NAME | 247 | method : 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 | ||
271 | body : '(' 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 | |||
320 | statlist : /* empty */ | 282 | statlist : /* 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 | ||
519 | parlist : /* empty */ | 481 | parlist : /* empty */ { lua_codeadjust(0); } |
520 | | parlist1 | 482 | | parlist1 { lua_codeadjust(0); } |
521 | ; | 483 | ; |
522 | 484 | ||
523 | parlist1 : NAME | 485 | parlist1 : 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 | ||
558 | ffield : NAME {$<vWord>$ = lua_findconstant($1);} '=' expr1 | 520 | ffield : 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 | ||
599 | singlevar : NAME | 561 | singlevar : 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 | |||
610 | varexp : var { lua_pushvar($1); } | 572 | varexp : var { lua_pushvar($1); } |
611 | ; | 573 | ; |
612 | 574 | ||
613 | localdeclist : NAME {localvar[nlocalvar]=lua_findsymbol($1); $$ = 1;} | 575 | localdeclist : 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 | ||
641 | static 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 | |||
679 | static void codereturn (void) | 660 | static void codereturn (void) |
680 | { | 661 | { |
681 | if (lua_debug) code_byte(RESET); | 662 | if (lua_debug) code_byte(RESET); |