diff options
Diffstat (limited to '')
| -rw-r--r-- | lua.stx | 147 |
1 files changed, 95 insertions, 52 deletions
| @@ -1,6 +1,6 @@ | |||
| 1 | %{ | 1 | %{ |
| 2 | 2 | ||
| 3 | char *rcs_luastx = "$Id: lua.stx,v 2.1 1994/04/15 19:02:04 celes Exp celes $"; | 3 | char *rcs_luastx = "$Id: lua.stx,v 2.2 1994/04/15 21:30:12 celes Exp celes $"; |
| 4 | 4 | ||
| 5 | #include <stdio.h> | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| @@ -14,15 +14,17 @@ char *rcs_luastx = "$Id: lua.stx,v 2.1 1994/04/15 19:02:04 celes Exp celes $"; | |||
| 14 | #include "table.h" | 14 | #include "table.h" |
| 15 | #include "lua.h" | 15 | #include "lua.h" |
| 16 | 16 | ||
| 17 | #define LISTING 1 | 17 | #define LISTING 0 |
| 18 | 18 | ||
| 19 | #ifndef MAXCODE | 19 | #ifndef GAPCODE |
| 20 | #define MAXCODE 1024 | 20 | #define GAPCODE 50 |
| 21 | #endif | 21 | #endif |
| 22 | static long buffer[MAXCODE*sizeof(Byte)/sizeof(long)]; | 22 | static Word maxcode; |
| 23 | static Byte *code = (Byte *)buffer; | 23 | static Word maxmain; |
| 24 | static long mainbuffer[MAXCODE]; | 24 | static Word maxcurr ; |
| 25 | static Byte *maincode = (Byte *)mainbuffer; | 25 | static Byte *code = NULL; |
| 26 | static Byte *maincode; | ||
| 27 | static Byte *initcode; | ||
| 26 | static Byte *basepc; | 28 | static Byte *basepc; |
| 27 | static Byte *pc; | 29 | static Byte *pc; |
| 28 | 30 | ||
| @@ -44,10 +46,22 @@ static int err; /* flag to indicate error */ | |||
| 44 | 46 | ||
| 45 | static void code_byte (Byte c) | 47 | static void code_byte (Byte c) |
| 46 | { | 48 | { |
| 47 | if (pc-basepc>MAXCODE-1) | 49 | if (pc-basepc>maxcurr-2) /* 1 byte free to code HALT of main code */ |
| 48 | { | 50 | { |
| 49 | lua_error ("code buffer overflow"); | 51 | Word d = pc-basepc; |
| 50 | err = 1; | 52 | Byte *new = calloc(maxcurr+GAPCODE, sizeof(Byte));; |
| 53 | memcpy(new, basepc, maxcurr*sizeof(Byte)); | ||
| 54 | maxcurr += GAPCODE; | ||
| 55 | free(basepc); | ||
| 56 | basepc=new; | ||
| 57 | |||
| 58 | /* basepc = (Byte *)realloc(basepc, maxcurr*sizeof(Byte)); */ | ||
| 59 | if (basepc == NULL) | ||
| 60 | { | ||
| 61 | lua_error ("not enough memory"); | ||
| 62 | err = 1; | ||
| 63 | } | ||
| 64 | pc = basepc+d; | ||
| 51 | } | 65 | } |
| 52 | *pc++ = c; | 66 | *pc++ = c; |
| 53 | } | 67 | } |
| @@ -196,7 +210,7 @@ static void code_number (float f) | |||
| 196 | %token <pChar> NAME | 210 | %token <pChar> NAME |
| 197 | %token <vInt> DEBUG | 211 | %token <vInt> DEBUG |
| 198 | 212 | ||
| 199 | %type <pByte> PrepJump | 213 | %type <vWord> PrepJump |
| 200 | %type <vInt> expr, exprlist, exprlist1, varlist1, typeconstructor | 214 | %type <vInt> expr, exprlist, exprlist1, varlist1, typeconstructor |
| 201 | %type <vInt> fieldlist, localdeclist | 215 | %type <vInt> fieldlist, localdeclist |
| 202 | %type <vInt> ffieldlist, ffieldlist1 | 216 | %type <vInt> ffieldlist, ffieldlist1 |
| @@ -216,9 +230,14 @@ static void code_number (float f) | |||
| 216 | 230 | ||
| 217 | 231 | ||
| 218 | functionlist : /* empty */ | 232 | functionlist : /* empty */ |
| 219 | | functionlist { pc=basepc=maincode; nlocalvar=0;} stat sc | 233 | | functionlist |
| 234 | { | ||
| 235 | pc=maincode; basepc=initcode; maxcurr=maxmain; | ||
| 236 | nlocalvar=0; | ||
| 237 | } | ||
| 238 | stat sc | ||
| 220 | { | 239 | { |
| 221 | maincode=pc; | 240 | maincode=pc; initcode=basepc; maxmain=maxcurr; |
| 222 | } | 241 | } |
| 223 | | functionlist function | 242 | | functionlist function |
| 224 | | functionlist setdebug | 243 | | functionlist setdebug |
| @@ -226,9 +245,19 @@ functionlist : /* empty */ | |||
| 226 | 245 | ||
| 227 | function : FUNCTION NAME | 246 | function : FUNCTION NAME |
| 228 | { | 247 | { |
| 229 | $<vWord>$ = lua_findsymbol($2); | 248 | if (code == NULL) /* first function */ |
| 230 | pc=basepc=code; | 249 | { |
| 250 | code = (Byte *) calloc(GAPCODE, sizeof(Byte)); | ||
| 251 | if (code == NULL) | ||
| 252 | { | ||
| 253 | lua_error("not enough memory"); | ||
| 254 | err = 1; | ||
| 255 | } | ||
| 256 | maxcode = GAPCODE; | ||
| 257 | } | ||
| 258 | pc=basepc=code; maxcurr=maxcode; | ||
| 231 | nlocalvar=0; | 259 | nlocalvar=0; |
| 260 | $<vWord>$ = lua_findsymbol($2); | ||
| 232 | } | 261 | } |
| 233 | '(' parlist ')' | 262 | '(' parlist ')' |
| 234 | { | 263 | { |
| @@ -246,10 +275,16 @@ function : FUNCTION NAME | |||
| 246 | if (lua_debug) code_byte(RESET); | 275 | if (lua_debug) code_byte(RESET); |
| 247 | code_byte(RETCODE); code_byte(nlocalvar); | 276 | code_byte(RETCODE); code_byte(nlocalvar); |
| 248 | s_tag($<vWord>3) = T_FUNCTION; | 277 | s_tag($<vWord>3) = T_FUNCTION; |
| 249 | s_bvalue($<vWord>3) = calloc (pc-code, sizeof(Byte)); | 278 | s_bvalue($<vWord>3) = calloc (pc-basepc, sizeof(Byte)); |
| 250 | memcpy (s_bvalue($<vWord>3), code, (pc-code)*sizeof(Byte)); | 279 | if (s_bvalue($<vWord>3) == NULL) |
| 280 | { | ||
| 281 | lua_error("not enough memory"); | ||
| 282 | err = 1; | ||
| 283 | } | ||
| 284 | memcpy (s_bvalue($<vWord>3), basepc, (pc-basepc)*sizeof(Byte)); | ||
| 285 | code = basepc; maxcode=maxcurr; | ||
| 251 | #if LISTING | 286 | #if LISTING |
| 252 | PrintCode(code,pc,(Byte*)buffer); | 287 | PrintCode(code,pc); |
| 253 | #endif | 288 | #endif |
| 254 | } | 289 | } |
| 255 | ; | 290 | ; |
| @@ -273,7 +308,7 @@ sc : /* empty */ | ';' ; | |||
| 273 | stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END | 308 | stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END |
| 274 | { | 309 | { |
| 275 | { | 310 | { |
| 276 | Byte *elseinit = $6 + sizeof(Word)+1; | 311 | Byte *elseinit = basepc + $6 + sizeof(Word)+1; |
| 277 | if (pc - elseinit == 0) /* no else */ | 312 | if (pc - elseinit == 0) /* no else */ |
| 278 | { | 313 | { |
| 279 | pc -= sizeof(Word)+1; | 314 | pc -= sizeof(Word)+1; |
| @@ -281,29 +316,29 @@ stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END | |||
| 281 | } | 316 | } |
| 282 | else | 317 | else |
| 283 | { | 318 | { |
| 284 | *($6) = JMP; | 319 | *(basepc+$6) = JMP; |
| 285 | code_word_at($6+1, pc - elseinit); | 320 | code_word_at(basepc+$6+1, pc - elseinit); |
| 286 | } | 321 | } |
| 287 | *($4) = IFFJMP; | 322 | *(basepc+$4) = IFFJMP; |
| 288 | code_word_at($4+1, elseinit - ($4 + sizeof(Word)+1)); | 323 | code_word_at(basepc+$4+1,elseinit-(basepc+$4+sizeof(Word)+1)); |
| 289 | } | 324 | } |
| 290 | } | 325 | } |
| 291 | 326 | ||
| 292 | | WHILE {$<pByte>$ = pc;} expr1 DO PrepJump block PrepJump END | 327 | | WHILE {$<vWord>$=pc-basepc;} expr1 DO PrepJump block PrepJump END |
| 293 | 328 | ||
| 294 | { | 329 | { |
| 295 | *($5) = IFFJMP; | 330 | *(basepc+$5) = IFFJMP; |
| 296 | code_word_at($5+1, pc - ($5 + sizeof(Word)+1)); | 331 | code_word_at(basepc+$5+1, pc - (basepc+$5 + sizeof(Word)+1)); |
| 297 | 332 | ||
| 298 | *($7) = UPJMP; | 333 | *(basepc+$7) = UPJMP; |
| 299 | code_word_at($7+1, pc - $<pByte>2); | 334 | code_word_at(basepc+$7+1, pc - (basepc+$<vWord>2)); |
| 300 | } | 335 | } |
| 301 | 336 | ||
| 302 | | REPEAT {$<pByte>$ = pc;} block UNTIL expr1 PrepJump | 337 | | REPEAT {$<vWord>$=pc-basepc;} block UNTIL expr1 PrepJump |
| 303 | 338 | ||
| 304 | { | 339 | { |
| 305 | *($6) = IFFUPJMP; | 340 | *(basepc+$6) = IFFUPJMP; |
| 306 | code_word_at($6+1, pc - $<pByte>2); | 341 | code_word_at(basepc+$6+1, pc - (basepc+$<vWord>2)); |
| 307 | } | 342 | } |
| 308 | 343 | ||
| 309 | 344 | ||
| @@ -329,7 +364,7 @@ elsepart : /* empty */ | |||
| 329 | | ELSEIF expr1 THEN PrepJump block PrepJump elsepart | 364 | | ELSEIF expr1 THEN PrepJump block PrepJump elsepart |
| 330 | { | 365 | { |
| 331 | { | 366 | { |
| 332 | Byte *elseinit = $6 + sizeof(Word)+1; | 367 | Byte *elseinit = basepc + $6 + sizeof(Word)+1; |
| 333 | if (pc - elseinit == 0) /* no else */ | 368 | if (pc - elseinit == 0) /* no else */ |
| 334 | { | 369 | { |
| 335 | pc -= sizeof(Word)+1; | 370 | pc -= sizeof(Word)+1; |
| @@ -338,11 +373,11 @@ elsepart : /* empty */ | |||
| 338 | } | 373 | } |
| 339 | else | 374 | else |
| 340 | { | 375 | { |
| 341 | *($6) = JMP; | 376 | *(basepc+$6) = JMP; |
| 342 | code_word_at($6+1, pc - elseinit); | 377 | code_word_at(basepc+$6+1, pc - elseinit); |
| 343 | } | 378 | } |
| 344 | *($4) = IFFJMP; | 379 | *(basepc+$4) = IFFJMP; |
| 345 | code_word_at($4+1, elseinit - ($4 + sizeof(Word)+1)); | 380 | code_word_at(basepc+$4+1, elseinit - (basepc+$4 + sizeof(Word)+1)); |
| 346 | } | 381 | } |
| 347 | } | 382 | } |
| 348 | ; | 383 | ; |
| @@ -368,7 +403,7 @@ ret : /* empty */ | |||
| 368 | 403 | ||
| 369 | PrepJump : /* empty */ | 404 | PrepJump : /* empty */ |
| 370 | { | 405 | { |
| 371 | $$ = pc; | 406 | $$ = pc-basepc; |
| 372 | code_byte(0); /* open space */ | 407 | code_byte(0); /* open space */ |
| 373 | code_word (0); | 408 | code_word (0); |
| 374 | } | 409 | } |
| @@ -417,14 +452,14 @@ expr : '(' expr ')' { $$ = $2; } | |||
| 417 | | NOT expr1 { code_byte(NOTOP); $$ = 1;} | 452 | | NOT expr1 { code_byte(NOTOP); $$ = 1;} |
| 418 | | expr1 AND PrepJump {code_byte(POP); ntemp--;} expr1 | 453 | | expr1 AND PrepJump {code_byte(POP); ntemp--;} expr1 |
| 419 | { | 454 | { |
| 420 | *($3) = ONFJMP; | 455 | *(basepc+$3) = ONFJMP; |
| 421 | code_word_at($3+1, pc - ($3 + sizeof(Word)+1)); | 456 | code_word_at(basepc+$3+1, pc - (basepc+$3 + sizeof(Word)+1)); |
| 422 | $$ = 1; | 457 | $$ = 1; |
| 423 | } | 458 | } |
| 424 | | expr1 OR PrepJump {code_byte(POP); ntemp--;} expr1 | 459 | | expr1 OR PrepJump {code_byte(POP); ntemp--;} expr1 |
| 425 | { | 460 | { |
| 426 | *($3) = ONTJMP; | 461 | *(basepc+$3) = ONTJMP; |
| 427 | code_word_at($3+1, pc - ($3 + sizeof(Word)+1)); | 462 | code_word_at(basepc+$3+1, pc - (basepc+$3 + sizeof(Word)+1)); |
| 428 | $$ = 1; | 463 | $$ = 1; |
| 429 | } | 464 | } |
| 430 | ; | 465 | ; |
| @@ -432,13 +467,13 @@ expr : '(' expr ')' { $$ = $2; } | |||
| 432 | typeconstructor: '@' | 467 | typeconstructor: '@' |
| 433 | { | 468 | { |
| 434 | code_byte(PUSHBYTE); | 469 | code_byte(PUSHBYTE); |
| 435 | $<pByte>$ = pc; code_byte(0); | 470 | $<vWord>$ = pc-basepc; code_byte(0); |
| 436 | incr_ntemp(); | 471 | incr_ntemp(); |
| 437 | code_byte(CREATEARRAY); | 472 | code_byte(CREATEARRAY); |
| 438 | } | 473 | } |
| 439 | objectname fieldlist | 474 | objectname fieldlist |
| 440 | { | 475 | { |
| 441 | *($<pByte>2) = $4; | 476 | *(basepc+$<vWord>2) = $4; |
| 442 | if ($3 < 0) /* there is no function to be called */ | 477 | if ($3 < 0) /* there is no function to be called */ |
| 443 | { | 478 | { |
| 444 | $$ = 1; | 479 | $$ = 1; |
| @@ -698,23 +733,32 @@ int yywrap (void) | |||
| 698 | */ | 733 | */ |
| 699 | int lua_parse (void) | 734 | int lua_parse (void) |
| 700 | { | 735 | { |
| 701 | Byte *initcode = maincode; | 736 | Byte *init = maincode = (Byte *) calloc(GAPCODE, sizeof(Byte)); |
| 737 | if (init== NULL) | ||
| 738 | { | ||
| 739 | lua_error("not enough memory"); | ||
| 740 | return 1; | ||
| 741 | } | ||
| 742 | initcode = init; | ||
| 743 | maxmain = GAPCODE; | ||
| 702 | err = 0; | 744 | err = 0; |
| 703 | if (yyparse () || (err==1)) return 1; | 745 | if (yyparse () || (err==1)) return 1; |
| 704 | *maincode++ = HALT; | 746 | *maincode++ = HALT; |
| 747 | init = initcode; | ||
| 705 | #if LISTING | 748 | #if LISTING |
| 706 | PrintCode(basepc,maincode,(Byte*)mainbuffer); | 749 | PrintCode(init,maincode); |
| 707 | #endif | 750 | #endif |
| 708 | if (lua_execute (initcode)) return 1; | 751 | if (lua_execute (init)) return 1; |
| 709 | maincode = initcode; | 752 | free(init); |
| 710 | return 0; | 753 | return 0; |
| 711 | } | 754 | } |
| 712 | 755 | ||
| 713 | 756 | ||
| 714 | #if LISTING | 757 | #if LISTING |
| 715 | 758 | ||
| 716 | static void PrintCode (Byte *p, Byte *end, Byte *code) | 759 | static void PrintCode (Byte *code, Byte *end) |
| 717 | { | 760 | { |
| 761 | Byte *p = code; | ||
| 718 | printf ("\n\nCODE\n"); | 762 | printf ("\n\nCODE\n"); |
| 719 | while (p != end) | 763 | while (p != end) |
| 720 | { | 764 | { |
| @@ -887,7 +931,7 @@ static void PrintCode (Byte *p, Byte *end, Byte *code) | |||
| 887 | printf ("%d RETCODE %d\n", p-code, *(++p)); | 931 | printf ("%d RETCODE %d\n", p-code, *(++p)); |
| 888 | p++; | 932 | p++; |
| 889 | break; | 933 | break; |
| 890 | case HALT: printf ("%d HALT\n", (p++)-code); break; | 934 | case HALT: printf ("%d HALT\n", (p++)-code); break; |
| 891 | case SETFUNCTION: | 935 | case SETFUNCTION: |
| 892 | { | 936 | { |
| 893 | CodeWord c1, c2; | 937 | CodeWord c1, c2; |
| @@ -907,9 +951,8 @@ static void PrintCode (Byte *p, Byte *end, Byte *code) | |||
| 907 | printf ("%d SETLINE %d\n", n, c.w); | 951 | printf ("%d SETLINE %d\n", n, c.w); |
| 908 | } | 952 | } |
| 909 | break; | 953 | break; |
| 910 | |||
| 911 | case RESET: printf ("%d RESET\n", (p++)-code); break; | ||
| 912 | 954 | ||
| 955 | case RESET: printf ("%d RESET\n", (p++)-code); break; | ||
| 913 | default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break; | 956 | default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break; |
| 914 | } | 957 | } |
| 915 | } | 958 | } |
