diff options
| -rw-r--r-- | lua.stx | 105 |
1 files changed, 36 insertions, 69 deletions
| @@ -1,6 +1,6 @@ | |||
| 1 | %{ | 1 | %{ |
| 2 | 2 | ||
| 3 | char *rcs_luastx = "$Id: lua.stx,v 3.2 1994/11/03 22:32:42 roberto Exp $"; | 3 | char *rcs_luastx = "$Id: lua.stx,v 3.3 1994/11/06 15:35:04 roberto Exp roberto $"; |
| 4 | 4 | ||
| 5 | #include <stdio.h> | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| @@ -12,7 +12,9 @@ char *rcs_luastx = "$Id: lua.stx,v 3.2 1994/11/03 22:32:42 roberto Exp $"; | |||
| 12 | #include "table.h" | 12 | #include "table.h" |
| 13 | #include "lua.h" | 13 | #include "lua.h" |
| 14 | 14 | ||
| 15 | #ifndef LISTING | ||
| 15 | #define LISTING 0 | 16 | #define LISTING 0 |
| 17 | #endif | ||
| 16 | 18 | ||
| 17 | #ifndef CODE_BLOCK | 19 | #ifndef CODE_BLOCK |
| 18 | #define CODE_BLOCK 256 | 20 | #define CODE_BLOCK 256 |
| @@ -21,7 +23,7 @@ static Long maxcode; | |||
| 21 | static Long maxmain; | 23 | static Long maxmain; |
| 22 | static Long maxcurr ; | 24 | static Long maxcurr ; |
| 23 | static Byte *code = NULL; | 25 | static Byte *code = NULL; |
| 24 | static Byte *initcode; | 26 | static Byte **initcode; |
| 25 | static Byte *basepc; | 27 | static Byte *basepc; |
| 26 | static Long maincode; | 28 | static Long maincode; |
| 27 | static Long pc; | 29 | static Long pc; |
| @@ -37,7 +39,6 @@ static int nlocalvar=0; /* number of local variables */ | |||
| 37 | #define MAXFIELDS FIELDS_PER_FLUSH*2 | 39 | #define MAXFIELDS FIELDS_PER_FLUSH*2 |
| 38 | static Word fields[MAXFIELDS]; /* fieldnames to be flushed */ | 40 | static Word fields[MAXFIELDS]; /* fieldnames to be flushed */ |
| 39 | static int nfields=0; | 41 | static int nfields=0; |
| 40 | static int err; /* flag to indicate error */ | ||
| 41 | 42 | ||
| 42 | /* Internal functions */ | 43 | /* Internal functions */ |
| 43 | 44 | ||
| @@ -48,10 +49,7 @@ static void code_byte (Byte c) | |||
| 48 | maxcurr *= 2; | 49 | maxcurr *= 2; |
| 49 | basepc = (Byte *)realloc(basepc, maxcurr*sizeof(Byte)); | 50 | basepc = (Byte *)realloc(basepc, maxcurr*sizeof(Byte)); |
| 50 | if (basepc == NULL) | 51 | if (basepc == NULL) |
| 51 | { | ||
| 52 | lua_error ("not enough memory"); | 52 | lua_error ("not enough memory"); |
| 53 | err = 1; | ||
| 54 | } | ||
| 55 | } | 53 | } |
| 56 | basepc[pc++] = c; | 54 | basepc[pc++] = c; |
| 57 | } | 55 | } |
| @@ -97,10 +95,7 @@ static void push_field (Word name) | |||
| 97 | if (nfields < STACKGAP-1) | 95 | if (nfields < STACKGAP-1) |
| 98 | fields[nfields++] = name; | 96 | fields[nfields++] = name; |
| 99 | else | 97 | else |
| 100 | { | ||
| 101 | lua_error ("too many fields in a constructor"); | 98 | lua_error ("too many fields in a constructor"); |
| 102 | err = 1; | ||
| 103 | } | ||
| 104 | } | 99 | } |
| 105 | 100 | ||
| 106 | static void flush_record (int n) | 101 | static void flush_record (int n) |
| @@ -125,10 +120,7 @@ static void flush_list (int m, int n) | |||
| 125 | code_byte(m); | 120 | code_byte(m); |
| 126 | } | 121 | } |
| 127 | else | 122 | else |
| 128 | { | ||
| 129 | lua_error ("list constructor too long"); | 123 | lua_error ("list constructor too long"); |
| 130 | err = 1; | ||
| 131 | } | ||
| 132 | code_byte(n); | 124 | code_byte(n); |
| 133 | } | 125 | } |
| 134 | 126 | ||
| @@ -137,10 +129,7 @@ static void add_nlocalvar (int n) | |||
| 137 | if (MAX_TEMPS+nlocalvar+MAXVAR+n < STACKGAP) | 129 | if (MAX_TEMPS+nlocalvar+MAXVAR+n < STACKGAP) |
| 138 | nlocalvar += n; | 130 | nlocalvar += n; |
| 139 | else | 131 | else |
| 140 | { | ||
| 141 | lua_error ("too many local variables"); | 132 | lua_error ("too many local variables"); |
| 142 | err = 1; | ||
| 143 | } | ||
| 144 | } | 133 | } |
| 145 | 134 | ||
| 146 | static void incr_nvarbuffer (void) | 135 | static void incr_nvarbuffer (void) |
| @@ -148,10 +137,7 @@ static void incr_nvarbuffer (void) | |||
| 148 | if (nvarbuffer < MAXVAR-1) | 137 | if (nvarbuffer < MAXVAR-1) |
| 149 | nvarbuffer++; | 138 | nvarbuffer++; |
| 150 | else | 139 | else |
| 151 | { | ||
| 152 | lua_error ("variable buffer overflow"); | 140 | lua_error ("variable buffer overflow"); |
| 153 | err = 1; | ||
| 154 | } | ||
| 155 | } | 141 | } |
| 156 | 142 | ||
| 157 | static void code_number (float f) | 143 | static void code_number (float f) |
| @@ -184,10 +170,7 @@ static void init_function (void) | |||
| 184 | { | 170 | { |
| 185 | code = (Byte *) calloc(CODE_BLOCK, sizeof(Byte)); | 171 | code = (Byte *) calloc(CODE_BLOCK, sizeof(Byte)); |
| 186 | if (code == NULL) | 172 | if (code == NULL) |
| 187 | { | ||
| 188 | lua_error("not enough memory"); | 173 | lua_error("not enough memory"); |
| 189 | err = 1; | ||
| 190 | } | ||
| 191 | maxcode = CODE_BLOCK; | 174 | maxcode = CODE_BLOCK; |
| 192 | } | 175 | } |
| 193 | } | 176 | } |
| @@ -242,12 +225,12 @@ static void init_function (void) | |||
| 242 | functionlist : /* empty */ | 225 | functionlist : /* empty */ |
| 243 | | functionlist | 226 | | functionlist |
| 244 | { | 227 | { |
| 245 | pc=maincode; basepc=initcode; maxcurr=maxmain; | 228 | pc=maincode; basepc=*initcode; maxcurr=maxmain; |
| 246 | nlocalvar=0; | 229 | nlocalvar=0; |
| 247 | } | 230 | } |
| 248 | stat sc | 231 | stat sc |
| 249 | { | 232 | { |
| 250 | maincode=pc; initcode=basepc; maxmain=maxcurr; | 233 | maincode=pc; *initcode=basepc; maxmain=maxcurr; |
| 251 | } | 234 | } |
| 252 | | functionlist function | 235 | | functionlist function |
| 253 | | functionlist method | 236 | | functionlist method |
| @@ -266,7 +249,7 @@ function : FUNCTION NAME | |||
| 266 | if (lua_debug) | 249 | if (lua_debug) |
| 267 | { | 250 | { |
| 268 | code_byte(SETFUNCTION); | 251 | code_byte(SETFUNCTION); |
| 269 | code_code((Byte *)lua_file[lua_nfile-1]); | 252 | code_code((Byte *)strdup(lua_file[lua_nfile-1])); |
| 270 | code_word($<vWord>3); | 253 | code_word($<vWord>3); |
| 271 | } | 254 | } |
| 272 | lua_codeadjust (0); | 255 | lua_codeadjust (0); |
| @@ -278,10 +261,7 @@ function : FUNCTION NAME | |||
| 278 | s_tag($<vWord>3) = LUA_T_FUNCTION; | 261 | s_tag($<vWord>3) = LUA_T_FUNCTION; |
| 279 | s_bvalue($<vWord>3) = calloc (pc, sizeof(Byte)); | 262 | s_bvalue($<vWord>3) = calloc (pc, sizeof(Byte)); |
| 280 | if (s_bvalue($<vWord>3) == NULL) | 263 | if (s_bvalue($<vWord>3) == NULL) |
| 281 | { | 264 | lua_error("not enough memory"); |
| 282 | lua_error("not enough memory"); | ||
| 283 | err = 1; | ||
| 284 | } | ||
| 285 | memcpy (s_bvalue($<vWord>3), basepc, pc*sizeof(Byte)); | 265 | memcpy (s_bvalue($<vWord>3), basepc, pc*sizeof(Byte)); |
| 286 | code = basepc; maxcode=maxcurr; | 266 | code = basepc; maxcode=maxcurr; |
| 287 | #if LISTING | 267 | #if LISTING |
| @@ -304,7 +284,7 @@ method : FUNCTION NAME { $<vWord>$ = lua_findsymbol($2); } ':' NAME | |||
| 304 | if (lua_debug) | 284 | if (lua_debug) |
| 305 | { | 285 | { |
| 306 | code_byte(SETFUNCTION); | 286 | code_byte(SETFUNCTION); |
| 307 | code_code((Byte *)lua_file[lua_nfile-1]); | 287 | code_code((Byte *)strdup(lua_file[lua_nfile-1])); |
| 308 | code_word($<vWord>6); | 288 | code_word($<vWord>6); |
| 309 | } | 289 | } |
| 310 | lua_codeadjust (0); | 290 | lua_codeadjust (0); |
| @@ -316,17 +296,14 @@ method : FUNCTION NAME { $<vWord>$ = lua_findsymbol($2); } ':' NAME | |||
| 316 | codereturn(); | 296 | codereturn(); |
| 317 | b = calloc (pc, sizeof(Byte)); | 297 | b = calloc (pc, sizeof(Byte)); |
| 318 | if (b == NULL) | 298 | if (b == NULL) |
| 319 | { | 299 | lua_error("not enough memory"); |
| 320 | lua_error("not enough memory"); | ||
| 321 | err = 1; | ||
| 322 | } | ||
| 323 | memcpy (b, basepc, pc*sizeof(Byte)); | 300 | memcpy (b, basepc, pc*sizeof(Byte)); |
| 324 | code = basepc; maxcode=maxcurr; | 301 | code = basepc; maxcode=maxcurr; |
| 325 | #if LISTING | 302 | #if LISTING |
| 326 | PrintCode(code,code+pc); | 303 | PrintCode(code,code+pc); |
| 327 | #endif | 304 | #endif |
| 328 | /* assign function to table field */ | 305 | /* assign function to table field */ |
| 329 | pc=maincode; basepc=initcode; maxcurr=maxmain; | 306 | pc=maincode; basepc=*initcode; maxcurr=maxmain; |
| 330 | nlocalvar=0; | 307 | nlocalvar=0; |
| 331 | 308 | ||
| 332 | lua_pushvar($<vWord>3+1); | 309 | lua_pushvar($<vWord>3+1); |
| @@ -336,7 +313,7 @@ method : FUNCTION NAME { $<vWord>$ = lua_findsymbol($2); } ':' NAME | |||
| 336 | code_code(b); | 313 | code_code(b); |
| 337 | code_byte(STOREINDEXED0); | 314 | code_byte(STOREINDEXED0); |
| 338 | 315 | ||
| 339 | maincode=pc; initcode=basepc; maxmain=maxcurr; | 316 | maincode=pc; *initcode=basepc; maxmain=maxcurr; |
| 340 | } | 317 | } |
| 341 | ; | 318 | ; |
| 342 | 319 | ||
| @@ -344,18 +321,13 @@ statlist : /* empty */ | |||
| 344 | | statlist stat sc | 321 | | statlist stat sc |
| 345 | ; | 322 | ; |
| 346 | 323 | ||
| 347 | stat : { | 324 | stat : { codedebugline(); } stat1 ; |
| 348 | if (lua_debug) | ||
| 349 | { | ||
| 350 | code_byte(SETLINE); code_word(lua_linenumber); | ||
| 351 | } | ||
| 352 | } | ||
| 353 | stat1 | ||
| 354 | 325 | ||
| 355 | sc : /* empty */ | ';' ; | 326 | sc : /* empty */ | ';' ; |
| 356 | 327 | ||
| 328 | cond : { codedebugline(); } expr1 ; | ||
| 357 | 329 | ||
| 358 | stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END | 330 | stat1 : IF cond THEN PrepJump block PrepJump elsepart END |
| 359 | { | 331 | { |
| 360 | { | 332 | { |
| 361 | Long elseinit = $6+sizeof(Word)+1; | 333 | Long elseinit = $6+sizeof(Word)+1; |
| @@ -374,7 +346,7 @@ stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END | |||
| 374 | } | 346 | } |
| 375 | } | 347 | } |
| 376 | 348 | ||
| 377 | | WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END | 349 | | WHILE {$<vLong>$=pc;} cond DO PrepJump block PrepJump END |
| 378 | 350 | ||
| 379 | { | 351 | { |
| 380 | basepc[$5] = IFFJMP; | 352 | basepc[$5] = IFFJMP; |
| @@ -403,7 +375,7 @@ stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END | |||
| 403 | lua_codeadjust (0); | 375 | lua_codeadjust (0); |
| 404 | } | 376 | } |
| 405 | } | 377 | } |
| 406 | | functioncall { code_byte(0); } | 378 | | functioncall { code_byte(0); } |
| 407 | | LOCAL localdeclist decinit | 379 | | LOCAL localdeclist decinit |
| 408 | { add_nlocalvar($2); | 380 | { add_nlocalvar($2); |
| 409 | adjust_mult_assign($2, $3, 0); | 381 | adjust_mult_assign($2, $3, 0); |
| @@ -443,7 +415,7 @@ block : {$<vInt>$ = nlocalvar;} statlist ret | |||
| 443 | ; | 415 | ; |
| 444 | 416 | ||
| 445 | ret : /* empty */ | 417 | ret : /* empty */ |
| 446 | | { if (lua_debug){code_byte(SETLINE);code_word(lua_linenumber);}} | 418 | | { codedebugline(); } |
| 447 | RETURN exprlist sc | 419 | RETURN exprlist sc |
| 448 | { | 420 | { |
| 449 | if ($3 < 0) code_byte(MULT_RET); | 421 | if ($3 < 0) code_byte(MULT_RET); |
| @@ -486,14 +458,7 @@ expr : '(' expr ')' { $$ = $2; } | |||
| 486 | $$ = 1; | 458 | $$ = 1; |
| 487 | } | 459 | } |
| 488 | | NIL {code_byte(PUSHNIL); $$ = 1; } | 460 | | NIL {code_byte(PUSHNIL); $$ = 1; } |
| 489 | | functioncall | 461 | | functioncall { $$ = 0; } |
| 490 | { | ||
| 491 | $$ = 0; | ||
| 492 | if (lua_debug) | ||
| 493 | { | ||
| 494 | code_byte(SETLINE); code_word(lua_linenumber); | ||
| 495 | } | ||
| 496 | } | ||
| 497 | | NOT expr1 { code_byte(NOTOP); $$ = 1;} | 462 | | NOT expr1 { code_byte(NOTOP); $$ = 1;} |
| 498 | | expr1 AND PrepJump {code_byte(POP); } expr1 | 463 | | expr1 AND PrepJump {code_byte(POP); } expr1 |
| 499 | { | 464 | { |
| @@ -723,6 +688,15 @@ static void codereturn (void) | |||
| 723 | } | 688 | } |
| 724 | } | 689 | } |
| 725 | 690 | ||
| 691 | static void codedebugline (void) | ||
| 692 | { | ||
| 693 | if (lua_debug) | ||
| 694 | { | ||
| 695 | code_byte(SETLINE); | ||
| 696 | code_word(lua_linenumber); | ||
| 697 | } | ||
| 698 | } | ||
| 699 | |||
| 726 | static void adjust_mult_assign (int vars, int exps, int temps) | 700 | static void adjust_mult_assign (int vars, int exps, int temps) |
| 727 | { | 701 | { |
| 728 | if (exps < 0) | 702 | if (exps < 0) |
| @@ -781,7 +755,6 @@ void yyerror (char *s) | |||
| 781 | sprintf (msg,"%s near \"%s\" at line %d in file \"%s\"", | 755 | sprintf (msg,"%s near \"%s\" at line %d in file \"%s\"", |
| 782 | s, lua_lasttext (), lua_linenumber, lua_filename()); | 756 | s, lua_lasttext (), lua_linenumber, lua_filename()); |
| 783 | lua_error (msg); | 757 | lua_error (msg); |
| 784 | err = 1; | ||
| 785 | } | 758 | } |
| 786 | 759 | ||
| 787 | int yywrap (void) | 760 | int yywrap (void) |
| @@ -791,27 +764,21 @@ int yywrap (void) | |||
| 791 | 764 | ||
| 792 | 765 | ||
| 793 | /* | 766 | /* |
| 794 | ** Parse LUA code and returns global statements. | 767 | ** Parse LUA code. |
| 795 | */ | 768 | */ |
| 796 | Byte *lua_parse (void) | 769 | void lua_parse (Byte **code) |
| 797 | { | 770 | { |
| 798 | Byte *init = initcode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte)); | 771 | initcode = code; |
| 772 | *initcode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte)); | ||
| 799 | maincode = 0; | 773 | maincode = 0; |
| 800 | maxmain = CODE_BLOCK; | 774 | maxmain = CODE_BLOCK; |
| 801 | if (init == NULL) | 775 | if (*initcode == NULL) lua_error("not enough memory"); |
| 802 | { | 776 | if (yyparse ()) lua_error("parse error"); |
| 803 | lua_error("not enough memory"); | 777 | (*initcode)[maincode++] = RETCODE0; |
| 804 | return NULL; | ||
| 805 | } | ||
| 806 | err = 0; | ||
| 807 | if (yyparse () || (err==1)) return NULL; | ||
| 808 | initcode[maincode++] = RETCODE0; | ||
| 809 | init = initcode; | ||
| 810 | #if LISTING | 778 | #if LISTING |
| 811 | { static void PrintCode (Byte *code, Byte *end); | 779 | { static void PrintCode (Byte *code, Byte *end); |
| 812 | PrintCode(init,init+maincode); } | 780 | PrintCode(*initcode,*initcode+maincode); } |
| 813 | #endif | 781 | #endif |
| 814 | return init; | ||
| 815 | } | 782 | } |
| 816 | 783 | ||
| 817 | 784 | ||
