diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-17 16:59:06 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-17 16:59:06 -0200 |
| commit | 3bd0f9e21149e7ea30c8ca3c710700410da37ae7 (patch) | |
| tree | a1e241d452a1bd775f17e4f91ee3e7e9acd36332 | |
| parent | 5406d391cd8c51d56b92e5c390afd60f4c006922 (diff) | |
| download | lua-3bd0f9e21149e7ea30c8ca3c710700410da37ae7.tar.gz lua-3bd0f9e21149e7ea30c8ca3c710700410da37ae7.tar.bz2 lua-3bd0f9e21149e7ea30c8ca3c710700410da37ae7.zip | |
uses new memory module (mem.c).
small changes in seting debug line.
if and elseif unified in a outine 'codeIf'
| -rw-r--r-- | lua.stx | 101 |
1 files changed, 36 insertions, 65 deletions
| @@ -1,11 +1,12 @@ | |||
| 1 | %{ | 1 | %{ |
| 2 | 2 | ||
| 3 | char *rcs_luastx = "$Id: lua.stx,v 3.5 1994/11/13 14:54:18 roberto Exp roberto $"; | 3 | char *rcs_luastx = "$Id: lua.stx,v 3.6 1994/11/14 21:40:14 roberto Exp $"; |
| 4 | 4 | ||
| 5 | #include <stdio.h> | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| 7 | #include <string.h> | 7 | #include <string.h> |
| 8 | 8 | ||
| 9 | #include "mem.h" | ||
| 9 | #include "opcode.h" | 10 | #include "opcode.h" |
| 10 | #include "hash.h" | 11 | #include "hash.h" |
| 11 | #include "inout.h" | 12 | #include "inout.h" |
| @@ -14,7 +15,7 @@ char *rcs_luastx = "$Id: lua.stx,v 3.5 1994/11/13 14:54:18 roberto Exp roberto $ | |||
| 14 | #include "lua.h" | 15 | #include "lua.h" |
| 15 | 16 | ||
| 16 | #ifndef LISTING | 17 | #ifndef LISTING |
| 17 | #define LISTING 0 | 18 | #define LISTING 1 |
| 18 | #endif | 19 | #endif |
| 19 | 20 | ||
| 20 | #ifndef CODE_BLOCK | 21 | #ifndef CODE_BLOCK |
| @@ -49,9 +50,7 @@ static void code_byte (Byte c) | |||
| 49 | if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */ | 50 | if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */ |
| 50 | { | 51 | { |
| 51 | maxcurr *= 2; | 52 | maxcurr *= 2; |
| 52 | basepc = (Byte *)realloc(basepc, maxcurr*sizeof(Byte)); | 53 | basepc = growvector(basepc, maxcurr, Byte); |
| 53 | if (basepc == NULL) | ||
| 54 | lua_error ("not enough memory"); | ||
| 55 | } | 54 | } |
| 56 | basepc[pc++] = c; | 55 | basepc[pc++] = c; |
| 57 | } | 56 | } |
| @@ -271,10 +270,8 @@ method : FUNCTION NAME ':' NAME | |||
| 271 | body : '(' parlist ')' block END | 270 | body : '(' parlist ')' block END |
| 272 | { | 271 | { |
| 273 | codereturn(); | 272 | codereturn(); |
| 274 | $$ = calloc (pc, sizeof(Byte)); | 273 | $$ = newvector(pc, Byte); |
| 275 | if ($$ == NULL) | 274 | memcpy($$, basepc, pc*sizeof(Byte)); |
| 276 | lua_error("not enough memory"); | ||
| 277 | memcpy ($$, basepc, pc*sizeof(Byte)); | ||
| 278 | funcCode = basepc; maxcode=maxcurr; | 275 | funcCode = basepc; maxcode=maxcurr; |
| 279 | } | 276 | } |
| 280 | ; | 277 | ; |
| @@ -283,49 +280,29 @@ statlist : /* empty */ | |||
| 283 | | statlist stat sc | 280 | | statlist stat sc |
| 284 | ; | 281 | ; |
| 285 | 282 | ||
| 286 | stat : { codedebugline(); } stat1 ; | ||
| 287 | |||
| 288 | sc : /* empty */ | ';' ; | 283 | sc : /* empty */ | ';' ; |
| 289 | 284 | ||
| 290 | cond : { codedebugline(); } expr1 ; | 285 | stat : { codedebugline(); } stat1 ; |
| 291 | 286 | ||
| 292 | stat1 : IF cond THEN PrepJump block PrepJump elsepart END | 287 | cond : { codedebugline(); } expr1 ; |
| 293 | { | 288 | |
| 294 | { | 289 | stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END |
| 295 | Long elseinit = $6+sizeof(Word)+1; | 290 | { codeIf($4, $6); } |
| 296 | if (pc - elseinit == 0) /* no else */ | 291 | |
| 297 | { | 292 | | WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END |
| 298 | pc -= sizeof(Word)+1; | ||
| 299 | elseinit = pc; | ||
| 300 | } | ||
| 301 | else | ||
| 302 | { | ||
| 303 | basepc[$6] = JMP; | ||
| 304 | code_word_at(basepc+$6+1, pc - elseinit); | ||
| 305 | } | ||
| 306 | basepc[$4] = IFFJMP; | ||
| 307 | code_word_at(basepc+$4+1,elseinit-($4+sizeof(Word)+1)); | ||
| 308 | } | ||
| 309 | } | ||
| 310 | |||
| 311 | | WHILE {$<vLong>$=pc;} cond DO PrepJump block PrepJump END | ||
| 312 | |||
| 313 | { | 293 | { |
| 314 | basepc[$5] = IFFJMP; | 294 | basepc[$5] = IFFJMP; |
| 315 | code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1)); | 295 | code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1)); |
| 316 | |||
| 317 | basepc[$7] = UPJMP; | 296 | basepc[$7] = UPJMP; |
| 318 | code_word_at(basepc+$7+1, pc - ($<vLong>2)); | 297 | code_word_at(basepc+$7+1, pc - ($<vLong>2)); |
| 319 | } | 298 | } |
| 320 | 299 | ||
| 321 | | REPEAT {$<vLong>$=pc;} block UNTIL expr1 PrepJump | 300 | | REPEAT {$<vLong>$=pc;} block UNTIL cond PrepJump |
| 322 | |||
| 323 | { | 301 | { |
| 324 | basepc[$6] = IFFUPJMP; | 302 | basepc[$6] = IFFUPJMP; |
| 325 | code_word_at(basepc+$6+1, pc - ($<vLong>2)); | 303 | code_word_at(basepc+$6+1, pc - ($<vLong>2)); |
| 326 | } | 304 | } |
| 327 | 305 | ||
| 328 | |||
| 329 | | varlist1 '=' exprlist1 | 306 | | varlist1 '=' exprlist1 |
| 330 | { | 307 | { |
| 331 | { | 308 | { |
| @@ -338,7 +315,7 @@ stat1 : IF cond THEN PrepJump block PrepJump elsepart END | |||
| 338 | } | 315 | } |
| 339 | } | 316 | } |
| 340 | | functioncall { code_byte(0); } | 317 | | functioncall { code_byte(0); } |
| 341 | | LOCAL localdeclist decinit | 318 | | LOCAL localdeclist decinit |
| 342 | { add_nlocalvar($2); | 319 | { add_nlocalvar($2); |
| 343 | adjust_mult_assign($2, $3, 0); | 320 | adjust_mult_assign($2, $3, 0); |
| 344 | } | 321 | } |
| @@ -346,24 +323,8 @@ stat1 : IF cond THEN PrepJump block PrepJump elsepart END | |||
| 346 | 323 | ||
| 347 | elsepart : /* empty */ | 324 | elsepart : /* empty */ |
| 348 | | ELSE block | 325 | | ELSE block |
| 349 | | ELSEIF expr1 THEN PrepJump block PrepJump elsepart | 326 | | ELSEIF cond THEN PrepJump block PrepJump elsepart |
| 350 | { | 327 | { codeIf($4, $6); } |
| 351 | { | ||
| 352 | Long elseinit = $6+sizeof(Word)+1; | ||
| 353 | if (pc - elseinit == 0) /* no else */ | ||
| 354 | { | ||
| 355 | pc -= sizeof(Word)+1; | ||
| 356 | elseinit = pc; | ||
| 357 | } | ||
| 358 | else | ||
| 359 | { | ||
| 360 | basepc[$6] = JMP; | ||
| 361 | code_word_at(basepc+$6+1, pc - elseinit); | ||
| 362 | } | ||
| 363 | basepc[$4] = IFFJMP; | ||
| 364 | code_word_at(basepc+$4+1, elseinit - ($4 + sizeof(Word)+1)); | ||
| 365 | } | ||
| 366 | } | ||
| 367 | ; | 328 | ; |
| 368 | 329 | ||
| 369 | block : {$<vInt>$ = nlocalvar;} statlist ret | 330 | block : {$<vInt>$ = nlocalvar;} statlist ret |
| @@ -377,9 +338,8 @@ block : {$<vInt>$ = nlocalvar;} statlist ret | |||
| 377 | ; | 338 | ; |
| 378 | 339 | ||
| 379 | ret : /* empty */ | 340 | ret : /* empty */ |
| 380 | | { codedebugline(); } | 341 | | RETURN { codedebugline(); } exprlist sc |
| 381 | RETURN exprlist sc | 342 | { |
| 382 | { | ||
| 383 | if ($3 < 0) code_byte(MULT_RET); | 343 | if ($3 < 0) code_byte(MULT_RET); |
| 384 | codereturn(); | 344 | codereturn(); |
| 385 | } | 345 | } |
| @@ -642,9 +602,7 @@ static void init_function (TreeNode *func) | |||
| 642 | { | 602 | { |
| 643 | if (funcCode == NULL) /* first function */ | 603 | if (funcCode == NULL) /* first function */ |
| 644 | { | 604 | { |
| 645 | funcCode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte)); | 605 | funcCode = newvector(CODE_BLOCK, Byte); |
| 646 | if (funcCode == NULL) | ||
| 647 | lua_error("not enough memory"); | ||
| 648 | maxcode = CODE_BLOCK; | 606 | maxcode = CODE_BLOCK; |
| 649 | } | 607 | } |
| 650 | pc=0; basepc=funcCode; maxcurr=maxcode; | 608 | pc=0; basepc=funcCode; maxcurr=maxcode; |
| @@ -730,6 +688,20 @@ static void lua_codestore (int i) | |||
| 730 | } | 688 | } |
| 731 | } | 689 | } |
| 732 | 690 | ||
| 691 | static void codeIf (Long thenAdd, Long elseAdd) | ||
| 692 | { | ||
| 693 | Long elseinit = elseAdd+sizeof(Word)+1; | ||
| 694 | if (pc == elseinit) /* no else */ | ||
| 695 | pc -= sizeof(Word)+1; | ||
| 696 | else | ||
| 697 | { | ||
| 698 | basepc[elseAdd] = JMP; | ||
| 699 | code_word_at(basepc+elseAdd+1, pc-elseinit); | ||
| 700 | } | ||
| 701 | basepc[thenAdd] = IFFJMP; | ||
| 702 | code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1)); | ||
| 703 | } | ||
| 704 | |||
| 733 | void yyerror (char *s) | 705 | void yyerror (char *s) |
| 734 | { | 706 | { |
| 735 | static char msg[256]; | 707 | static char msg[256]; |
| @@ -750,14 +722,13 @@ int yywrap (void) | |||
| 750 | void lua_parse (Byte **code) | 722 | void lua_parse (Byte **code) |
| 751 | { | 723 | { |
| 752 | initcode = code; | 724 | initcode = code; |
| 753 | *initcode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte)); | 725 | *initcode = newvector(CODE_BLOCK, Byte); |
| 754 | maincode = 0; | 726 | maincode = 0; |
| 755 | maxmain = CODE_BLOCK; | 727 | maxmain = CODE_BLOCK; |
| 756 | if (*initcode == NULL) lua_error("not enough memory"); | ||
| 757 | if (yyparse ()) lua_error("parse error"); | 728 | if (yyparse ()) lua_error("parse error"); |
| 758 | (*initcode)[maincode++] = RETCODE0; | 729 | (*initcode)[maincode++] = RETCODE0; |
| 759 | #if LISTING | 730 | #if LISTING |
| 760 | { static void PrintCode (Byte *code, Byte *end); | 731 | { static void PrintCode (Byte *c, Byte *end); |
| 761 | PrintCode(*initcode,*initcode+maincode); } | 732 | PrintCode(*initcode,*initcode+maincode); } |
| 762 | #endif | 733 | #endif |
| 763 | } | 734 | } |
