diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1995-10-26 12:21:56 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1995-10-26 12:21:56 -0200 |
commit | 15d48576ea737d51e579f101a870e37f62b81f22 (patch) | |
tree | ed2390188cfb5304d366696262ccda593f3e8589 /lua.stx | |
parent | 39b071f7b13e6ed6eff4a0f0471d2450a9c48084 (diff) | |
download | lua-15d48576ea737d51e579f101a870e37f62b81f22.tar.gz lua-15d48576ea737d51e579f101a870e37f62b81f22.tar.bz2 lua-15d48576ea737d51e579f101a870e37f62b81f22.zip |
functions now may be declared with any "var" as a name;
therefore they do not have a "baptism" name.
Changes in debug API to acomodate that.
Diffstat (limited to 'lua.stx')
-rw-r--r-- | lua.stx | 124 |
1 files changed, 60 insertions, 64 deletions
@@ -1,6 +1,6 @@ | |||
1 | %{ | 1 | %{ |
2 | 2 | ||
3 | char *rcs_luastx = "$Id: lua.stx,v 3.22 1995/10/25 13:05:51 roberto Exp roberto $"; | 3 | char *rcs_luastx = "$Id: lua.stx,v 3.23 1995/10/25 14:33:25 roberto Exp roberto $"; |
4 | 4 | ||
5 | #include <stdio.h> | 5 | #include <stdio.h> |
6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
@@ -54,6 +54,14 @@ static int nfields=0; | |||
54 | 54 | ||
55 | /* Internal functions */ | 55 | /* Internal functions */ |
56 | 56 | ||
57 | static void yyerror (char *s) | ||
58 | { | ||
59 | static char msg[256]; | ||
60 | sprintf (msg,"%s near \"%s\" at line %d in file `%s'", | ||
61 | s, lua_lasttext (), lua_linenumber, lua_parsedfile); | ||
62 | lua_error (msg); | ||
63 | } | ||
64 | |||
57 | static void code_byte (Byte c) | 65 | static void code_byte (Byte c) |
58 | { | 66 | { |
59 | if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */ | 67 | if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */ |
@@ -148,6 +156,8 @@ static void add_localvar (Word name) | |||
148 | 156 | ||
149 | static void store_localvar (Word name, int n) | 157 | static void store_localvar (Word name, int n) |
150 | { | 158 | { |
159 | if (*initcode == basepc) | ||
160 | yyerror("local variable outside function body"); | ||
151 | if (nlocalvar+n < MAXLOCALS) | 161 | if (nlocalvar+n < MAXLOCALS) |
152 | localvar[nlocalvar+n] = name; | 162 | localvar[nlocalvar+n] = name; |
153 | else | 163 | else |
@@ -249,6 +259,20 @@ static void savemain (void) | |||
249 | maincode=pc; *initcode=basepc; maxmain=maxcurr; | 259 | maincode=pc; *initcode=basepc; maxmain=maxcurr; |
250 | } | 260 | } |
251 | 261 | ||
262 | static void init_func (void) | ||
263 | { | ||
264 | if (funcCode == NULL) /* first function */ | ||
265 | { | ||
266 | funcCode = newvector(CODE_BLOCK, Byte); | ||
267 | maxcode = CODE_BLOCK; | ||
268 | } | ||
269 | savemain(); /* save main values */ | ||
270 | /* set func values */ | ||
271 | pc=0; basepc=funcCode; maxcurr=maxcode; | ||
272 | nlocalvar = 0; | ||
273 | luaI_codedebugline(lua_linenumber); | ||
274 | } | ||
275 | |||
252 | static void codereturn (void) | 276 | static void codereturn (void) |
253 | { | 277 | { |
254 | if (nlocalvar == 0) | 278 | if (nlocalvar == 0) |
@@ -300,23 +324,31 @@ static void adjust_mult_assign (int vars, Long exps, int temps) | |||
300 | lua_codeadjust(temps); | 324 | lua_codeadjust(temps); |
301 | } | 325 | } |
302 | 326 | ||
303 | static void lua_codestore (int i) | 327 | static void storesinglevar (Long v) |
304 | { | 328 | { |
305 | if (varbuffer[i] > 0) /* global var */ | 329 | if (v > 0) /* global var */ |
306 | { | 330 | { |
307 | code_byte(STOREGLOBAL); | 331 | code_byte(STOREGLOBAL); |
308 | code_word(varbuffer[i]-1); | 332 | code_word(v-1); |
309 | } | 333 | } |
310 | else if (varbuffer[i] < 0) /* local var */ | 334 | else if (v < 0) /* local var */ |
311 | { | 335 | { |
312 | int number = (-varbuffer[i]) - 1; | 336 | int number = (-v) - 1; |
313 | if (number < 10) code_byte(STORELOCAL0 + number); | 337 | if (number < 10) code_byte(STORELOCAL0 + number); |
314 | else | 338 | else |
315 | { | 339 | { |
316 | code_byte(STORELOCAL); | 340 | code_byte(STORELOCAL); |
317 | code_byte(number); | 341 | code_byte(number); |
318 | } | 342 | } |
319 | } | 343 | } |
344 | else | ||
345 | code_byte(STOREINDEXED0); | ||
346 | } | ||
347 | |||
348 | static void lua_codestore (int i) | ||
349 | { | ||
350 | if (varbuffer[i] != 0) /* global or local var */ | ||
351 | storesinglevar(varbuffer[i]); | ||
320 | else /* indexed var */ | 352 | else /* indexed var */ |
321 | { | 353 | { |
322 | int j; | 354 | int j; |
@@ -352,14 +384,6 @@ static void codeIf (Long thenAdd, Long elseAdd) | |||
352 | code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1)); | 384 | code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1)); |
353 | } | 385 | } |
354 | 386 | ||
355 | static void yyerror (char *s) | ||
356 | { | ||
357 | static char msg[256]; | ||
358 | sprintf (msg,"%s near \"%s\" at line %d in file `%s'", | ||
359 | s, lua_lasttext (), lua_linenumber, lua_parsedfile); | ||
360 | lua_error (msg); | ||
361 | } | ||
362 | |||
363 | 387 | ||
364 | /* | 388 | /* |
365 | ** Parse LUA code. | 389 | ** Parse LUA code. |
@@ -419,8 +443,8 @@ void lua_parse (TFunc *tf) | |||
419 | %type <vInt> fieldlist, localdeclist, decinit | 443 | %type <vInt> fieldlist, localdeclist, decinit |
420 | %type <vInt> ffieldlist, ffieldlist1, semicolonpart | 444 | %type <vInt> ffieldlist, ffieldlist1, semicolonpart |
421 | %type <vInt> lfieldlist, lfieldlist1 | 445 | %type <vInt> lfieldlist, lfieldlist1 |
422 | %type <vInt> functiontoken | 446 | %type <vInt> parlist |
423 | %type <vLong> var, singlevar | 447 | %type <vLong> var, singlevar, funcname |
424 | %type <pFunc> body | 448 | %type <pFunc> body |
425 | 449 | ||
426 | %left AND OR | 450 | %left AND OR |
@@ -438,59 +462,29 @@ void lua_parse (TFunc *tf) | |||
438 | functionlist : /* empty */ | 462 | functionlist : /* empty */ |
439 | | functionlist globalstat | 463 | | functionlist globalstat |
440 | | functionlist function | 464 | | functionlist function |
441 | | functionlist method | ||
442 | ; | 465 | ; |
443 | 466 | ||
444 | globalstat : stat sc | 467 | globalstat : stat sc |
445 | | setdebug | 468 | | setdebug |
446 | ; | 469 | ; |
447 | 470 | ||
448 | function : functiontoken NAME body | 471 | function : FUNCTION funcname body |
449 | { | 472 | { |
450 | code_byte(PUSHFUNCTION); | 473 | code_byte(PUSHFUNCTION); |
451 | code_code($3); | 474 | code_code($3); |
452 | code_byte(STOREGLOBAL); | 475 | storesinglevar($2); |
453 | code_word(luaI_findsymbol($2)); | ||
454 | $3->lineDefined = $1; | ||
455 | $3->name1 = $2->ts.str; | ||
456 | $3->name2 = NULL; | ||
457 | $3->fileName = lua_parsedfile; | ||
458 | } | 476 | } |
459 | ; | 477 | ; |
460 | 478 | ||
461 | method : functiontoken NAME methkind NAME body | 479 | funcname : var { $$ =$1; init_func(); } |
462 | { | 480 | | varexp ':' NAME |
463 | /* assign function to table field */ | ||
464 | lua_pushvar(luaI_findsymbol($2)+1); | ||
465 | code_byte(PUSHSTRING); | ||
466 | code_word(luaI_findconstant($4)); | ||
467 | code_byte(PUSHFUNCTION); | ||
468 | code_code($5); | ||
469 | code_byte(STOREINDEXED0); | ||
470 | $5->lineDefined = $1; | ||
471 | $5->name1 = $4->ts.str; | ||
472 | $5->name2 = $2->ts.str; | ||
473 | $5->fileName = lua_parsedfile; | ||
474 | } | ||
475 | ; | ||
476 | |||
477 | functiontoken : FUNCTION | ||
478 | { | 481 | { |
479 | if (funcCode == NULL) /* first function */ | 482 | code_byte(PUSHSTRING); |
480 | { | 483 | code_word(luaI_findconstant($3)); |
481 | funcCode = newvector(CODE_BLOCK, Byte); | 484 | $$ = 0; /* indexed variable */ |
482 | maxcode = CODE_BLOCK; | 485 | init_func(); |
483 | } | 486 | add_localvar(luaI_findsymbolbyname("self")); |
484 | savemain(); /* save main values */ | ||
485 | /* set func values */ | ||
486 | pc=0; basepc=funcCode; maxcurr=maxcode; | ||
487 | nlocalvar=0; | ||
488 | $$ = lua_linenumber; | ||
489 | } | 487 | } |
490 | ; | ||
491 | |||
492 | methkind : ':' { add_localvar(luaI_findsymbolbyname("self")); } | ||
493 | | '.' /* no self */ | ||
494 | ; | 488 | ; |
495 | 489 | ||
496 | body : '(' parlist ')' block END | 490 | body : '(' parlist ')' block END |
@@ -499,6 +493,8 @@ body : '(' parlist ')' block END | |||
499 | $$ = new(TFunc); | 493 | $$ = new(TFunc); |
500 | $$->size = pc; | 494 | $$->size = pc; |
501 | $$->code = newvector(pc, Byte); | 495 | $$->code = newvector(pc, Byte); |
496 | $$->fileName = lua_parsedfile; | ||
497 | $$->lineDefined = $2; | ||
502 | memcpy($$->code, basepc, pc*sizeof(Byte)); | 498 | memcpy($$->code, basepc, pc*sizeof(Byte)); |
503 | /* save func values */ | 499 | /* save func values */ |
504 | funcCode = basepc; maxcode=maxcurr; | 500 | funcCode = basepc; maxcode=maxcurr; |
@@ -674,8 +670,8 @@ exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; } | |||
674 | } | 670 | } |
675 | ; | 671 | ; |
676 | 672 | ||
677 | parlist : /* empty */ { lua_codeadjust(0); } | 673 | parlist : /* empty */ { lua_codeadjust(0); $$ = lua_linenumber; } |
678 | | parlist1 { lua_codeadjust(0); } | 674 | | parlist1 { lua_codeadjust(0); $$ = lua_linenumber; } |
679 | ; | 675 | ; |
680 | 676 | ||
681 | parlist1 : NAME | 677 | parlist1 : NAME |