summaryrefslogtreecommitdiff
path: root/lua.stx
diff options
context:
space:
mode:
authorWaldemar Celes <celes@tecgraf.puc-rio.br>1994-04-20 13:22:21 -0300
committerWaldemar Celes <celes@tecgraf.puc-rio.br>1994-04-20 13:22:21 -0300
commitf8fb7b39478c3468192c69fcb2154f9022dbab64 (patch)
tree7eeabc5be9ebc4cd7f5eda4b146a9cf4c7023c3e /lua.stx
parent14b6ab354038d408fc446188de003b405a080998 (diff)
downloadlua-f8fb7b39478c3468192c69fcb2154f9022dbab64.tar.gz
lua-f8fb7b39478c3468192c69fcb2154f9022dbab64.tar.bz2
lua-f8fb7b39478c3468192c69fcb2154f9022dbab64.zip
Alteracao do tipo da variavel "pc" na compilacao, passando
a ser indice e nao mais ponteiro.
Diffstat (limited to 'lua.stx')
-rw-r--r--lua.stx86
1 files changed, 39 insertions, 47 deletions
diff --git a/lua.stx b/lua.stx
index b37a246e..1e4b3107 100644
--- a/lua.stx
+++ b/lua.stx
@@ -1,6 +1,6 @@
1%{ 1%{
2 2
3char *rcs_luastx = "$Id: lua.stx,v 2.2 1994/04/15 21:30:12 celes Exp celes $"; 3char *rcs_luastx = "$Id: lua.stx,v 2.3 1994/04/19 19:06:15 celes Exp celes $";
4 4
5#include <stdio.h> 5#include <stdio.h>
6#include <stdlib.h> 6#include <stdlib.h>
@@ -23,10 +23,10 @@ static Word maxcode;
23static Word maxmain; 23static Word maxmain;
24static Word maxcurr ; 24static Word maxcurr ;
25static Byte *code = NULL; 25static Byte *code = NULL;
26static Byte *maincode;
27static Byte *initcode; 26static Byte *initcode;
28static Byte *basepc; 27static Byte *basepc;
29static Byte *pc; 28static Word maincode;
29static Word pc;
30 30
31#define MAXVAR 32 31#define MAXVAR 32
32static long varbuffer[MAXVAR]; /* variables in an assignment list; 32static long varbuffer[MAXVAR]; /* variables in an assignment list;
@@ -46,24 +46,17 @@ static int err; /* flag to indicate error */
46 46
47static void code_byte (Byte c) 47static void code_byte (Byte c)
48{ 48{
49 if (pc-basepc>maxcurr-2) /* 1 byte free to code HALT of main code */ 49 if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */
50 { 50 {
51 Word d = pc-basepc;
52 Byte *new = calloc(maxcurr+GAPCODE, sizeof(Byte));;
53 memcpy(new, basepc, maxcurr*sizeof(Byte));
54 maxcurr += GAPCODE; 51 maxcurr += GAPCODE;
55 free(basepc); 52 basepc = (Byte *)realloc(basepc, maxcurr*sizeof(Byte));
56 basepc=new;
57
58/* basepc = (Byte *)realloc(basepc, maxcurr*sizeof(Byte)); */
59 if (basepc == NULL) 53 if (basepc == NULL)
60 { 54 {
61 lua_error ("not enough memory"); 55 lua_error ("not enough memory");
62 err = 1; 56 err = 1;
63 } 57 }
64 pc = basepc+d;
65 } 58 }
66 *pc++ = c; 59 basepc[pc++] = c;
67} 60}
68 61
69static void code_word (Word n) 62static void code_word (Word n)
@@ -255,7 +248,7 @@ function : FUNCTION NAME
255 } 248 }
256 maxcode = GAPCODE; 249 maxcode = GAPCODE;
257 } 250 }
258 pc=basepc=code; maxcurr=maxcode; 251 pc=0; basepc=code; maxcurr=maxcode;
259 nlocalvar=0; 252 nlocalvar=0;
260 $<vWord>$ = lua_findsymbol($2); 253 $<vWord>$ = lua_findsymbol($2);
261 } 254 }
@@ -275,16 +268,16 @@ function : FUNCTION NAME
275 if (lua_debug) code_byte(RESET); 268 if (lua_debug) code_byte(RESET);
276 code_byte(RETCODE); code_byte(nlocalvar); 269 code_byte(RETCODE); code_byte(nlocalvar);
277 s_tag($<vWord>3) = T_FUNCTION; 270 s_tag($<vWord>3) = T_FUNCTION;
278 s_bvalue($<vWord>3) = calloc (pc-basepc, sizeof(Byte)); 271 s_bvalue($<vWord>3) = calloc (pc, sizeof(Byte));
279 if (s_bvalue($<vWord>3) == NULL) 272 if (s_bvalue($<vWord>3) == NULL)
280 { 273 {
281 lua_error("not enough memory"); 274 lua_error("not enough memory");
282 err = 1; 275 err = 1;
283 } 276 }
284 memcpy (s_bvalue($<vWord>3), basepc, (pc-basepc)*sizeof(Byte)); 277 memcpy (s_bvalue($<vWord>3), basepc, pc*sizeof(Byte));
285 code = basepc; maxcode=maxcurr; 278 code = basepc; maxcode=maxcurr;
286#if LISTING 279#if LISTING
287PrintCode(code,pc); 280PrintCode(code,code+pc);
288#endif 281#endif
289 } 282 }
290 ; 283 ;
@@ -308,7 +301,7 @@ sc : /* empty */ | ';' ;
308stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END 301stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
309 { 302 {
310 { 303 {
311 Byte *elseinit = basepc + $6 + sizeof(Word)+1; 304 Word elseinit = $6+sizeof(Word)+1;
312 if (pc - elseinit == 0) /* no else */ 305 if (pc - elseinit == 0) /* no else */
313 { 306 {
314 pc -= sizeof(Word)+1; 307 pc -= sizeof(Word)+1;
@@ -316,29 +309,29 @@ stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
316 } 309 }
317 else 310 else
318 { 311 {
319 *(basepc+$6) = JMP; 312 basepc[$6] = JMP;
320 code_word_at(basepc+$6+1, pc - elseinit); 313 code_word_at(basepc+$6+1, pc - elseinit);
321 } 314 }
322 *(basepc+$4) = IFFJMP; 315 basepc[$4] = IFFJMP;
323 code_word_at(basepc+$4+1,elseinit-(basepc+$4+sizeof(Word)+1)); 316 code_word_at(basepc+$4+1,elseinit-($4+sizeof(Word)+1));
324 } 317 }
325 } 318 }
326 319
327 | WHILE {$<vWord>$=pc-basepc;} expr1 DO PrepJump block PrepJump END 320 | WHILE {$<vWord>$=pc;} expr1 DO PrepJump block PrepJump END
328 321
329 { 322 {
330 *(basepc+$5) = IFFJMP; 323 basepc[$5] = IFFJMP;
331 code_word_at(basepc+$5+1, pc - (basepc+$5 + sizeof(Word)+1)); 324 code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1));
332 325
333 *(basepc+$7) = UPJMP; 326 basepc[$7] = UPJMP;
334 code_word_at(basepc+$7+1, pc - (basepc+$<vWord>2)); 327 code_word_at(basepc+$7+1, pc - ($<vWord>2));
335 } 328 }
336 329
337 | REPEAT {$<vWord>$=pc-basepc;} block UNTIL expr1 PrepJump 330 | REPEAT {$<vWord>$=pc;} block UNTIL expr1 PrepJump
338 331
339 { 332 {
340 *(basepc+$6) = IFFUPJMP; 333 basepc[$6] = IFFUPJMP;
341 code_word_at(basepc+$6+1, pc - (basepc+$<vWord>2)); 334 code_word_at(basepc+$6+1, pc - ($<vWord>2));
342 } 335 }
343 336
344 337
@@ -364,20 +357,19 @@ elsepart : /* empty */
364 | ELSEIF expr1 THEN PrepJump block PrepJump elsepart 357 | ELSEIF expr1 THEN PrepJump block PrepJump elsepart
365 { 358 {
366 { 359 {
367 Byte *elseinit = basepc + $6 + sizeof(Word)+1; 360 Word elseinit = $6+sizeof(Word)+1;
368 if (pc - elseinit == 0) /* no else */ 361 if (pc - elseinit == 0) /* no else */
369 { 362 {
370 pc -= sizeof(Word)+1; 363 pc -= sizeof(Word)+1;
371 /* if (*(pc-1) == NOP) --pc; */
372 elseinit = pc; 364 elseinit = pc;
373 } 365 }
374 else 366 else
375 { 367 {
376 *(basepc+$6) = JMP; 368 basepc[$6] = JMP;
377 code_word_at(basepc+$6+1, pc - elseinit); 369 code_word_at(basepc+$6+1, pc - elseinit);
378 } 370 }
379 *(basepc+$4) = IFFJMP; 371 basepc[$4] = IFFJMP;
380 code_word_at(basepc+$4+1, elseinit - (basepc+$4 + sizeof(Word)+1)); 372 code_word_at(basepc+$4+1, elseinit - ($4 + sizeof(Word)+1));
381 } 373 }
382 } 374 }
383 ; 375 ;
@@ -403,7 +395,7 @@ ret : /* empty */
403 395
404PrepJump : /* empty */ 396PrepJump : /* empty */
405 { 397 {
406 $$ = pc-basepc; 398 $$ = pc;
407 code_byte(0); /* open space */ 399 code_byte(0); /* open space */
408 code_word (0); 400 code_word (0);
409 } 401 }
@@ -452,14 +444,14 @@ expr : '(' expr ')' { $$ = $2; }
452 | NOT expr1 { code_byte(NOTOP); $$ = 1;} 444 | NOT expr1 { code_byte(NOTOP); $$ = 1;}
453 | expr1 AND PrepJump {code_byte(POP); ntemp--;} expr1 445 | expr1 AND PrepJump {code_byte(POP); ntemp--;} expr1
454 { 446 {
455 *(basepc+$3) = ONFJMP; 447 basepc[$3] = ONFJMP;
456 code_word_at(basepc+$3+1, pc - (basepc+$3 + sizeof(Word)+1)); 448 code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
457 $$ = 1; 449 $$ = 1;
458 } 450 }
459 | expr1 OR PrepJump {code_byte(POP); ntemp--;} expr1 451 | expr1 OR PrepJump {code_byte(POP); ntemp--;} expr1
460 { 452 {
461 *(basepc+$3) = ONTJMP; 453 basepc[$3] = ONTJMP;
462 code_word_at(basepc+$3+1, pc - (basepc+$3 + sizeof(Word)+1)); 454 code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
463 $$ = 1; 455 $$ = 1;
464 } 456 }
465 ; 457 ;
@@ -467,13 +459,13 @@ expr : '(' expr ')' { $$ = $2; }
467typeconstructor: '@' 459typeconstructor: '@'
468 { 460 {
469 code_byte(PUSHBYTE); 461 code_byte(PUSHBYTE);
470 $<vWord>$ = pc-basepc; code_byte(0); 462 $<vWord>$ = pc; code_byte(0);
471 incr_ntemp(); 463 incr_ntemp();
472 code_byte(CREATEARRAY); 464 code_byte(CREATEARRAY);
473 } 465 }
474 objectname fieldlist 466 objectname fieldlist
475 { 467 {
476 *(basepc+$<vWord>2) = $4; 468 basepc[$<vWord>2] = $4;
477 if ($3 < 0) /* there is no function to be called */ 469 if ($3 < 0) /* there is no function to be called */
478 { 470 {
479 $$ = 1; 471 $$ = 1;
@@ -733,20 +725,20 @@ int yywrap (void)
733*/ 725*/
734int lua_parse (void) 726int lua_parse (void)
735{ 727{
736 Byte *init = maincode = (Byte *) calloc(GAPCODE, sizeof(Byte)); 728 Byte *init = initcode = (Byte *) calloc(GAPCODE, sizeof(Byte));
737 if (init== NULL) 729 maincode = 0;
730 maxmain = GAPCODE;
731 if (init == NULL)
738 { 732 {
739 lua_error("not enough memory"); 733 lua_error("not enough memory");
740 return 1; 734 return 1;
741 } 735 }
742 initcode = init;
743 maxmain = GAPCODE;
744 err = 0; 736 err = 0;
745 if (yyparse () || (err==1)) return 1; 737 if (yyparse () || (err==1)) return 1;
746 *maincode++ = HALT; 738 initcode[maincode++] = HALT;
747 init = initcode; 739 init = initcode;
748#if LISTING 740#if LISTING
749 PrintCode(init,maincode); 741 PrintCode(init,init+maincode);
750#endif 742#endif
751 if (lua_execute (init)) return 1; 743 if (lua_execute (init)) return 1;
752 free(init); 744 free(init);