diff options
| author | Waldemar Celes <celes@tecgraf.puc-rio.br> | 1994-07-19 18:27:18 -0300 |
|---|---|---|
| committer | Waldemar Celes <celes@tecgraf.puc-rio.br> | 1994-07-19 18:27:18 -0300 |
| commit | 493d718b7fe0f1075072a44d7946e38ca7d773d3 (patch) | |
| tree | 3239639a562d742002342166cce005f7d70cc503 /lua.stx | |
| parent | 1c749a3059051c52c3bc24540e27b0ccbcfff273 (diff) | |
| download | lua-493d718b7fe0f1075072a44d7946e38ca7d773d3.tar.gz lua-493d718b7fe0f1075072a44d7946e38ca7d773d3.tar.bz2 lua-493d718b7fe0f1075072a44d7946e38ca7d773d3.zip | |
Uso de arvores binarias para armazenar nomes e realocacao dinamica
de tabelas (pilhas, hashtable, globais, codigo, etc.)
Diffstat (limited to 'lua.stx')
| -rw-r--r-- | lua.stx | 49 |
1 files changed, 25 insertions, 24 deletions
| @@ -1,6 +1,6 @@ | |||
| 1 | %{ | 1 | %{ |
| 2 | 2 | ||
| 3 | char *rcs_luastx = "$Id: lua.stx,v 2.3 1994/04/19 19:06:15 celes Exp celes $"; | 3 | char *rcs_luastx = "$Id: lua.stx,v 2.4 1994/04/20 16:22:21 celes Exp celes $"; |
| 4 | 4 | ||
| 5 | #include <stdio.h> | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| @@ -16,17 +16,17 @@ char *rcs_luastx = "$Id: lua.stx,v 2.3 1994/04/19 19:06:15 celes Exp celes $"; | |||
| 16 | 16 | ||
| 17 | #define LISTING 0 | 17 | #define LISTING 0 |
| 18 | 18 | ||
| 19 | #ifndef GAPCODE | 19 | #ifndef CODE_BLOCK |
| 20 | #define GAPCODE 50 | 20 | #define CODE_BLOCK 256 |
| 21 | #endif | 21 | #endif |
| 22 | static Word maxcode; | 22 | static Long maxcode; |
| 23 | static Word maxmain; | 23 | static Long maxmain; |
| 24 | static Word maxcurr ; | 24 | static Long maxcurr ; |
| 25 | static Byte *code = NULL; | 25 | static Byte *code = NULL; |
| 26 | static Byte *initcode; | 26 | static Byte *initcode; |
| 27 | static Byte *basepc; | 27 | static Byte *basepc; |
| 28 | static Word maincode; | 28 | static Long maincode; |
| 29 | static Word pc; | 29 | static Long pc; |
| 30 | 30 | ||
| 31 | #define MAXVAR 32 | 31 | #define MAXVAR 32 |
| 32 | static long varbuffer[MAXVAR]; /* variables in an assignment list; | 32 | static long varbuffer[MAXVAR]; /* variables in an assignment list; |
| @@ -48,7 +48,7 @@ static void code_byte (Byte c) | |||
| 48 | { | 48 | { |
| 49 | if (pc>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 | maxcurr += GAPCODE; | 51 | maxcurr *= 2; |
| 52 | basepc = (Byte *)realloc(basepc, maxcurr*sizeof(Byte)); | 52 | basepc = (Byte *)realloc(basepc, maxcurr*sizeof(Byte)); |
| 53 | if (basepc == NULL) | 53 | if (basepc == NULL) |
| 54 | { | 54 | { |
| @@ -155,7 +155,8 @@ static void incr_nvarbuffer (void) | |||
| 155 | } | 155 | } |
| 156 | 156 | ||
| 157 | static void code_number (float f) | 157 | static void code_number (float f) |
| 158 | { Word i = (Word)f; | 158 | { |
| 159 | Word i = (Word)f; | ||
| 159 | if (f == (float)i) /* f has an (short) integer value */ | 160 | if (f == (float)i) /* f has an (short) integer value */ |
| 160 | { | 161 | { |
| 161 | if (i <= 2) code_byte(PUSH0 + i); | 162 | if (i <= 2) code_byte(PUSH0 + i); |
| @@ -184,10 +185,10 @@ static void code_number (float f) | |||
| 184 | %union | 185 | %union |
| 185 | { | 186 | { |
| 186 | int vInt; | 187 | int vInt; |
| 187 | long vLong; | ||
| 188 | float vFloat; | 188 | float vFloat; |
| 189 | char *pChar; | 189 | char *pChar; |
| 190 | Word vWord; | 190 | Word vWord; |
| 191 | Long vLong; | ||
| 191 | Byte *pByte; | 192 | Byte *pByte; |
| 192 | } | 193 | } |
| 193 | 194 | ||
| @@ -203,7 +204,7 @@ static void code_number (float f) | |||
| 203 | %token <pChar> NAME | 204 | %token <pChar> NAME |
| 204 | %token <vInt> DEBUG | 205 | %token <vInt> DEBUG |
| 205 | 206 | ||
| 206 | %type <vWord> PrepJump | 207 | %type <vLong> PrepJump |
| 207 | %type <vInt> expr, exprlist, exprlist1, varlist1, typeconstructor | 208 | %type <vInt> expr, exprlist, exprlist1, varlist1, typeconstructor |
| 208 | %type <vInt> fieldlist, localdeclist | 209 | %type <vInt> fieldlist, localdeclist |
| 209 | %type <vInt> ffieldlist, ffieldlist1 | 210 | %type <vInt> ffieldlist, ffieldlist1 |
| @@ -240,13 +241,13 @@ function : FUNCTION NAME | |||
| 240 | { | 241 | { |
| 241 | if (code == NULL) /* first function */ | 242 | if (code == NULL) /* first function */ |
| 242 | { | 243 | { |
| 243 | code = (Byte *) calloc(GAPCODE, sizeof(Byte)); | 244 | code = (Byte *) calloc(CODE_BLOCK, sizeof(Byte)); |
| 244 | if (code == NULL) | 245 | if (code == NULL) |
| 245 | { | 246 | { |
| 246 | lua_error("not enough memory"); | 247 | lua_error("not enough memory"); |
| 247 | err = 1; | 248 | err = 1; |
| 248 | } | 249 | } |
| 249 | maxcode = GAPCODE; | 250 | maxcode = CODE_BLOCK; |
| 250 | } | 251 | } |
| 251 | pc=0; basepc=code; maxcurr=maxcode; | 252 | pc=0; basepc=code; maxcurr=maxcode; |
| 252 | nlocalvar=0; | 253 | nlocalvar=0; |
| @@ -301,7 +302,7 @@ sc : /* empty */ | ';' ; | |||
| 301 | stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END | 302 | stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END |
| 302 | { | 303 | { |
| 303 | { | 304 | { |
| 304 | Word elseinit = $6+sizeof(Word)+1; | 305 | Long elseinit = $6+sizeof(Word)+1; |
| 305 | if (pc - elseinit == 0) /* no else */ | 306 | if (pc - elseinit == 0) /* no else */ |
| 306 | { | 307 | { |
| 307 | pc -= sizeof(Word)+1; | 308 | pc -= sizeof(Word)+1; |
| @@ -317,21 +318,21 @@ stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END | |||
| 317 | } | 318 | } |
| 318 | } | 319 | } |
| 319 | 320 | ||
| 320 | | WHILE {$<vWord>$=pc;} expr1 DO PrepJump block PrepJump END | 321 | | WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END |
| 321 | 322 | ||
| 322 | { | 323 | { |
| 323 | basepc[$5] = IFFJMP; | 324 | basepc[$5] = IFFJMP; |
| 324 | code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1)); | 325 | code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1)); |
| 325 | 326 | ||
| 326 | basepc[$7] = UPJMP; | 327 | basepc[$7] = UPJMP; |
| 327 | code_word_at(basepc+$7+1, pc - ($<vWord>2)); | 328 | code_word_at(basepc+$7+1, pc - ($<vLong>2)); |
| 328 | } | 329 | } |
| 329 | 330 | ||
| 330 | | REPEAT {$<vWord>$=pc;} block UNTIL expr1 PrepJump | 331 | | REPEAT {$<vLong>$=pc;} block UNTIL expr1 PrepJump |
| 331 | 332 | ||
| 332 | { | 333 | { |
| 333 | basepc[$6] = IFFUPJMP; | 334 | basepc[$6] = IFFUPJMP; |
| 334 | code_word_at(basepc+$6+1, pc - ($<vWord>2)); | 335 | code_word_at(basepc+$6+1, pc - ($<vLong>2)); |
| 335 | } | 336 | } |
| 336 | 337 | ||
| 337 | 338 | ||
| @@ -357,7 +358,7 @@ elsepart : /* empty */ | |||
| 357 | | ELSEIF expr1 THEN PrepJump block PrepJump elsepart | 358 | | ELSEIF expr1 THEN PrepJump block PrepJump elsepart |
| 358 | { | 359 | { |
| 359 | { | 360 | { |
| 360 | Word elseinit = $6+sizeof(Word)+1; | 361 | Long elseinit = $6+sizeof(Word)+1; |
| 361 | if (pc - elseinit == 0) /* no else */ | 362 | if (pc - elseinit == 0) /* no else */ |
| 362 | { | 363 | { |
| 363 | pc -= sizeof(Word)+1; | 364 | pc -= sizeof(Word)+1; |
| @@ -459,13 +460,13 @@ expr : '(' expr ')' { $$ = $2; } | |||
| 459 | typeconstructor: '@' | 460 | typeconstructor: '@' |
| 460 | { | 461 | { |
| 461 | code_byte(PUSHBYTE); | 462 | code_byte(PUSHBYTE); |
| 462 | $<vWord>$ = pc; code_byte(0); | 463 | $<vLong>$ = pc; code_byte(0); |
| 463 | incr_ntemp(); | 464 | incr_ntemp(); |
| 464 | code_byte(CREATEARRAY); | 465 | code_byte(CREATEARRAY); |
| 465 | } | 466 | } |
| 466 | objectname fieldlist | 467 | objectname fieldlist |
| 467 | { | 468 | { |
| 468 | basepc[$<vWord>2] = $4; | 469 | basepc[$<vLong>2] = $4; |
| 469 | if ($3 < 0) /* there is no function to be called */ | 470 | if ($3 < 0) /* there is no function to be called */ |
| 470 | { | 471 | { |
| 471 | $$ = 1; | 472 | $$ = 1; |
| @@ -725,9 +726,9 @@ int yywrap (void) | |||
| 725 | */ | 726 | */ |
| 726 | int lua_parse (void) | 727 | int lua_parse (void) |
| 727 | { | 728 | { |
| 728 | Byte *init = initcode = (Byte *) calloc(GAPCODE, sizeof(Byte)); | 729 | Byte *init = initcode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte)); |
| 729 | maincode = 0; | 730 | maincode = 0; |
| 730 | maxmain = GAPCODE; | 731 | maxmain = CODE_BLOCK; |
| 731 | if (init == NULL) | 732 | if (init == NULL) |
| 732 | { | 733 | { |
| 733 | lua_error("not enough memory"); | 734 | lua_error("not enough memory"); |
