aboutsummaryrefslogtreecommitdiff
path: root/lua.stx
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-02 18:30:53 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-02 18:30:53 -0200
commitfbf887ec2be8b293d6f3ffc88b42c5a9e87bf022 (patch)
tree030c6dd803fff11ae0c368e90b78a9fef2b8b731 /lua.stx
parentae77864844d6b933eb8be68694cbb8498af165dc (diff)
downloadlua-fbf887ec2be8b293d6f3ffc88b42c5a9e87bf022.tar.gz
lua-fbf887ec2be8b293d6f3ffc88b42c5a9e87bf022.tar.bz2
lua-fbf887ec2be8b293d6f3ffc88b42c5a9e87bf022.zip
new way to call functions, plus several small changes. This is
a temporary version!
Diffstat (limited to 'lua.stx')
-rw-r--r--lua.stx190
1 files changed, 105 insertions, 85 deletions
diff --git a/lua.stx b/lua.stx
index ceed9c4b..5f13f6ba 100644
--- a/lua.stx
+++ b/lua.stx
@@ -1,6 +1,6 @@
1%{ 1%{
2 2
3char *rcs_luastx = "$Id: lua.stx,v 2.11 1994/10/21 19:00:12 roberto Exp roberto $"; 3char *rcs_luastx = "$Id: lua.stx,v 2.12 1994/11/01 18:25:20 roberto Exp roberto $";
4 4
5#include <stdio.h> 5#include <stdio.h>
6#include <stdlib.h> 6#include <stdlib.h>
@@ -37,7 +37,6 @@ static int nlocalvar=0; /* number of local variables */
37#define MAXFIELDS FIELDS_PER_FLUSH*2 37#define MAXFIELDS FIELDS_PER_FLUSH*2
38static Word fields[MAXFIELDS]; /* fieldnames to be flushed */ 38static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
39static int nfields=0; 39static int nfields=0;
40static int ntemp; /* number of temporary var into stack */
41static int err; /* flag to indicate error */ 40static int err; /* flag to indicate error */
42 41
43/* Internal functions */ 42/* Internal functions */
@@ -112,7 +111,6 @@ static void flush_record (int n)
112 code_byte(n); 111 code_byte(n);
113 for (i=0; i<n; i++) 112 for (i=0; i<n; i++)
114 code_word(fields[--nfields]); 113 code_word(fields[--nfields]);
115 ntemp -= n;
116} 114}
117 115
118static void flush_list (int m, int n) 116static void flush_list (int m, int n)
@@ -132,27 +130,15 @@ static void flush_list (int m, int n)
132 err = 1; 130 err = 1;
133 } 131 }
134 code_byte(n); 132 code_byte(n);
135 ntemp-=n;
136}
137
138static void incr_ntemp (void)
139{
140 if (ntemp+nlocalvar+MAXVAR+1 < STACKGAP)
141 ntemp++;
142 else
143 {
144 lua_error ("stack overflow");
145 err = 1;
146 }
147} 133}
148 134
149static void add_nlocalvar (int n) 135static void add_nlocalvar (int n)
150{ 136{
151 if (ntemp+nlocalvar+MAXVAR+n < STACKGAP) 137 if (MAX_TEMPS+nlocalvar+MAXVAR+n < STACKGAP)
152 nlocalvar += n; 138 nlocalvar += n;
153 else 139 else
154 { 140 {
155 lua_error ("too many local variables or expression too complicate"); 141 lua_error ("too many local variables");
156 err = 1; 142 err = 1;
157 } 143 }
158} 144}
@@ -190,7 +176,6 @@ static void code_number (float f)
190 code_byte(PUSHFLOAT); 176 code_byte(PUSHFLOAT);
191 code_float(f); 177 code_float(f);
192 } 178 }
193 incr_ntemp();
194} 179}
195 180
196static void init_function (void) 181static void init_function (void)
@@ -235,8 +220,8 @@ static void init_function (void)
235%token <vInt> DEBUG 220%token <vInt> DEBUG
236 221
237%type <vLong> PrepJump 222%type <vLong> PrepJump
238%type <vInt> expr, exprlist, exprlist1, varlist1, funcvalue 223%type <vInt> expr, exprlist, exprlist1, varlist1, funcParams, funcvalue
239%type <vInt> fieldlist, localdeclist 224%type <vInt> fieldlist, localdeclist, decinit
240%type <vInt> ffieldlist1 225%type <vInt> ffieldlist1
241%type <vInt> lfieldlist1 226%type <vInt> lfieldlist1
242%type <vLong> var, singlevar 227%type <vLong> var, singlevar
@@ -290,8 +275,8 @@ function : FUNCTION NAME
290 END 275 END
291 { 276 {
292 if (lua_debug) code_byte(RESET); 277 if (lua_debug) code_byte(RESET);
293 code_byte(RETCODE); code_byte(nlocalvar); 278 codereturn();
294 s_tag($<vWord>3) = T_FUNCTION; 279 s_tag($<vWord>3) = LUA_T_FUNCTION;
295 s_bvalue($<vWord>3) = calloc (pc, sizeof(Byte)); 280 s_bvalue($<vWord>3) = calloc (pc, sizeof(Byte));
296 if (s_bvalue($<vWord>3) == NULL) 281 if (s_bvalue($<vWord>3) == NULL)
297 { 282 {
@@ -330,7 +315,7 @@ method : FUNCTION NAME { $<vWord>$ = lua_findsymbol($2); } ':' NAME
330 { 315 {
331 Byte *b; 316 Byte *b;
332 if (lua_debug) code_byte(RESET); 317 if (lua_debug) code_byte(RESET);
333 code_byte(RETCODE); code_byte(nlocalvar); 318 codereturn();
334 b = calloc (pc, sizeof(Byte)); 319 b = calloc (pc, sizeof(Byte));
335 if (b == NULL) 320 if (b == NULL)
336 { 321 {
@@ -362,7 +347,6 @@ statlist : /* empty */
362 ; 347 ;
363 348
364stat : { 349stat : {
365 ntemp = 0;
366 if (lua_debug) 350 if (lua_debug)
367 { 351 {
368 code_byte(SETLINE); code_word(lua_linenumber); 352 code_byte(SETLINE); code_word(lua_linenumber);
@@ -414,16 +398,18 @@ stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
414 { 398 {
415 { 399 {
416 int i; 400 int i;
417 if ($3 == 0 || nvarbuffer != ntemp - $1 * 2) 401 adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
418 lua_codeadjust ($1 * 2 + nvarbuffer);
419 for (i=nvarbuffer-1; i>=0; i--) 402 for (i=nvarbuffer-1; i>=0; i--)
420 lua_codestore (i); 403 lua_codestore (i);
421 if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0)) 404 if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
422 lua_codeadjust (0); 405 lua_codeadjust (0);
423 } 406 }
424 } 407 }
425 | functioncall { lua_codeadjust (0); } 408 | functioncall { code_byte(0); }
426 | LOCAL localdeclist decinit { add_nlocalvar($2); lua_codeadjust (0); } 409 | LOCAL localdeclist decinit
410 { add_nlocalvar($2);
411 adjust_mult_assign($2, $3, 0);
412 }
427 ; 413 ;
428 414
429elsepart : /* empty */ 415elsepart : /* empty */
@@ -448,7 +434,7 @@ elsepart : /* empty */
448 } 434 }
449 ; 435 ;
450 436
451block : {$<vInt>$ = nlocalvar;} statlist {ntemp = 0;} ret 437block : {$<vInt>$ = nlocalvar;} statlist ret
452 { 438 {
453 if (nlocalvar != $<vInt>1) 439 if (nlocalvar != $<vInt>1)
454 { 440 {
@@ -462,8 +448,9 @@ ret : /* empty */
462 | { if (lua_debug){code_byte(SETLINE);code_word(lua_linenumber);}} 448 | { if (lua_debug){code_byte(SETLINE);code_word(lua_linenumber);}}
463 RETURN exprlist sc 449 RETURN exprlist sc
464 { 450 {
451 if ($3 < 0) code_byte(MULT_RET);
465 if (lua_debug) code_byte(RESET); 452 if (lua_debug) code_byte(RESET);
466 code_byte(RETCODE); code_byte(nlocalvar); 453 codereturn();
467 } 454 }
468 ; 455 ;
469 456
@@ -474,22 +461,22 @@ PrepJump : /* empty */
474 code_word (0); 461 code_word (0);
475 } 462 }
476 463
477expr1 : expr { if ($1 == 0) {lua_codeadjust (ntemp+1); incr_ntemp();}} 464expr1 : expr { if ($1 == 0) code_byte(1); }
478 ; 465 ;
479 466
480expr : '(' expr ')' { $$ = $2; } 467expr : '(' expr ')' { $$ = $2; }
481 | expr1 EQ expr1 { code_byte(EQOP); $$ = 1; ntemp--;} 468 | expr1 EQ expr1 { code_byte(EQOP); $$ = 1; }
482 | expr1 '<' expr1 { code_byte(LTOP); $$ = 1; ntemp--;} 469 | expr1 '<' expr1 { code_byte(LTOP); $$ = 1; }
483 | expr1 '>' expr1 { code_byte(LEOP); code_byte(NOTOP); $$ = 1; ntemp--;} 470 | expr1 '>' expr1 { code_byte(LEOP); code_byte(NOTOP); $$ = 1; }
484 | expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 1; ntemp--;} 471 | expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 1; }
485 | expr1 LE expr1 { code_byte(LEOP); $$ = 1; ntemp--;} 472 | expr1 LE expr1 { code_byte(LEOP); $$ = 1; }
486 | expr1 GE expr1 { code_byte(LTOP); code_byte(NOTOP); $$ = 1; ntemp--;} 473 | expr1 GE expr1 { code_byte(LTOP); code_byte(NOTOP); $$ = 1; }
487 | expr1 '+' expr1 { code_byte(ADDOP); $$ = 1; ntemp--;} 474 | expr1 '+' expr1 { code_byte(ADDOP); $$ = 1; }
488 | expr1 '-' expr1 { code_byte(SUBOP); $$ = 1; ntemp--;} 475 | expr1 '-' expr1 { code_byte(SUBOP); $$ = 1; }
489 | expr1 '*' expr1 { code_byte(MULTOP); $$ = 1; ntemp--;} 476 | expr1 '*' expr1 { code_byte(MULTOP); $$ = 1; }
490 | expr1 '/' expr1 { code_byte(DIVOP); $$ = 1; ntemp--;} 477 | expr1 '/' expr1 { code_byte(DIVOP); $$ = 1; }
491 | expr1 '^' expr1 { code_byte(POWOP); $$ = 1; ntemp--;} 478 | expr1 '^' expr1 { code_byte(POWOP); $$ = 1; }
492 | expr1 CONC expr1 { code_byte(CONCOP); $$ = 1; ntemp--;} 479 | expr1 CONC expr1 { code_byte(CONCOP); $$ = 1; }
493 | '+' expr1 %prec UNARY { $$ = 1; } 480 | '+' expr1 %prec UNARY { $$ = 1; }
494 | '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 1;} 481 | '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 1;}
495 | table { $$ = 1; } 482 | table { $$ = 1; }
@@ -500,9 +487,8 @@ expr : '(' expr ')' { $$ = $2; }
500 code_byte(PUSHSTRING); 487 code_byte(PUSHSTRING);
501 code_word(lua_findconstant($1)); 488 code_word(lua_findconstant($1));
502 $$ = 1; 489 $$ = 1;
503 incr_ntemp();
504 } 490 }
505 | NIL {code_byte(PUSHNIL); $$ = 1; incr_ntemp();} 491 | NIL {code_byte(PUSHNIL); $$ = 1; }
506 | functioncall 492 | functioncall
507 { 493 {
508 $$ = 0; 494 $$ = 0;
@@ -512,13 +498,13 @@ expr : '(' expr ')' { $$ = $2; }
512 } 498 }
513 } 499 }
514 | NOT expr1 { code_byte(NOTOP); $$ = 1;} 500 | NOT expr1 { code_byte(NOTOP); $$ = 1;}
515 | expr1 AND PrepJump {code_byte(POP); ntemp--;} expr1 501 | expr1 AND PrepJump {code_byte(POP); } expr1
516 { 502 {
517 basepc[$3] = ONFJMP; 503 basepc[$3] = ONFJMP;
518 code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1)); 504 code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
519 $$ = 1; 505 $$ = 1;
520 } 506 }
521 | expr1 OR PrepJump {code_byte(POP); ntemp--;} expr1 507 | expr1 OR PrepJump {code_byte(POP); } expr1
522 { 508 {
523 basepc[$3] = ONTJMP; 509 basepc[$3] = ONTJMP;
524 code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1)); 510 code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
@@ -537,33 +523,35 @@ table :
537 } 523 }
538 ; 524 ;
539 525
540functioncall : funcvalue funcParams { code_byte(CALLFUNC); ntemp = $1-1; } 526functioncall : funcvalue funcParams
527 { code_byte(CALLFUNC); code_byte($1+$2); }
541 ; 528 ;
542funcvalue : varexp 529
543 { 530funcvalue : varexp { $$ = 0; }
544 $$ = ntemp; code_byte(PUSHMARK); incr_ntemp();
545 }
546 | varexp ':' NAME 531 | varexp ':' NAME
547 { 532 {
548 code_byte(PUSHSTRING); 533 code_byte(PUSHSTRING);
549 code_word(lua_findconstant($3)); 534 code_word(lua_findconstant($3));
550 incr_ntemp(); 535 code_byte(PUSHSELF);
551 $$ = ntemp-1; 536 $$ = 1;
552 code_byte(PUSHMARKMET);
553 incr_ntemp();
554 } 537 }
555 ; 538 ;
539
556funcParams : '(' exprlist ')' 540funcParams : '(' exprlist ')'
557 | table 541 { if ($2<0) { code_byte(1); $$ = -$2; } else $$ = $2; }
542 | table { $$ = 1; }
558 ; 543 ;
559 544
560exprlist : /* empty */ { $$ = 1; } 545exprlist : /* empty */ { $$ = 0; }
561 | exprlist1 { $$ = $1; } 546 | exprlist1 { $$ = $1; }
562 ; 547 ;
563 548
564exprlist1 : expr { $$ = $1; } 549exprlist1 : expr { if ($1 == 0) $$ = -1; else $$ = 1; }
565 | exprlist1 ',' {if (!$1){lua_codeadjust (ntemp+1); incr_ntemp();}} 550 | exprlist1 ',' { if ($1 < 0) code_byte(1); } expr
566 expr {$$ = $4;} 551 {
552 int r = $1 < 0 ? -$1 : $1;
553 $$ = ($4 == 0) ? -(r+1) : r+1;
554 }
567 ; 555 ;
568 556
569parlist : /* empty */ 557parlist : /* empty */
@@ -641,7 +629,7 @@ var : singlevar { $$ = $1; }
641 | varexp '.' NAME 629 | varexp '.' NAME
642 { 630 {
643 code_byte(PUSHSTRING); 631 code_byte(PUSHSTRING);
644 code_word(lua_findconstant($3)); incr_ntemp(); 632 code_word(lua_findconstant($3));
645 $$ = 0; /* indexed variable */ 633 $$ = 0; /* indexed variable */
646 } 634 }
647 ; 635 ;
@@ -668,8 +656,8 @@ localdeclist : NAME {localvar[nlocalvar]=lua_findsymbol($1); $$ = 1;}
668 } 656 }
669 ; 657 ;
670 658
671decinit : /* empty */ 659decinit : /* empty */ { $$ = 0; }
672 | '=' exprlist1 660 | '=' exprlist1 { $$ = $2; }
673 ; 661 ;
674 662
675setdebug : DEBUG {lua_debug = $1;} 663setdebug : DEBUG {lua_debug = $1;}
@@ -698,7 +686,6 @@ static void lua_pushvar (long number)
698 { 686 {
699 code_byte(PUSHGLOBAL); 687 code_byte(PUSHGLOBAL);
700 code_word(number-1); 688 code_word(number-1);
701 incr_ntemp();
702 } 689 }
703 else if (number < 0) /* local var */ 690 else if (number < 0) /* local var */
704 { 691 {
@@ -709,19 +696,50 @@ static void lua_pushvar (long number)
709 code_byte(PUSHLOCAL); 696 code_byte(PUSHLOCAL);
710 code_byte(number); 697 code_byte(number);
711 } 698 }
712 incr_ntemp();
713 } 699 }
714 else 700 else
715 { 701 {
716 code_byte(PUSHINDEXED); 702 code_byte(PUSHINDEXED);
717 ntemp--;
718 } 703 }
719} 704}
720 705
721static void lua_codeadjust (int n) 706static void lua_codeadjust (int n)
722{ 707{
723 code_byte(ADJUST); 708 if (n+nlocalvar == 0)
724 code_byte(n + nlocalvar); 709 code_byte(ADJUST0);
710 else
711 {
712 code_byte(ADJUST);
713 code_byte(n+nlocalvar);
714 }
715}
716
717static void codereturn (void)
718{
719 if (nlocalvar == 0)
720 code_byte(RETCODE0);
721 else
722 {
723 code_byte(RETCODE);
724 code_byte(nlocalvar);
725 }
726}
727
728static void adjust_mult_assign (int vars, int exps, int temps)
729{
730 if (exps < 0)
731 {
732 int r = vars - (-exps-1);
733 if (r >= 0)
734 code_byte(r);
735 else
736 {
737 code_byte(0);
738 lua_codeadjust(temps);
739 }
740 }
741 else if (vars != exps)
742 lua_codeadjust(temps);
725} 743}
726 744
727static void lua_codestore (int i) 745static void lua_codestore (int i)
@@ -775,10 +793,9 @@ int yywrap (void)
775 793
776 794
777/* 795/*
778** Parse LUA code and execute global statement. 796** Parse LUA code and returns global statements.
779** Return 0 on success or 1 on error.
780*/ 797*/
781int lua_parse (void) 798Byte *lua_parse (void)
782{ 799{
783 Byte *init = initcode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte)); 800 Byte *init = initcode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
784 maincode = 0; 801 maincode = 0;
@@ -786,18 +803,17 @@ int lua_parse (void)
786 if (init == NULL) 803 if (init == NULL)
787 { 804 {
788 lua_error("not enough memory"); 805 lua_error("not enough memory");
789 return 1; 806 return NULL;
790 } 807 }
791 err = 0; 808 err = 0;
792 if (yyparse () || (err==1)) return 1; 809 if (yyparse () || (err==1)) return NULL;
793 initcode[maincode++] = HALT; 810 initcode[maincode++] = RETCODE0;
794 init = initcode; 811 init = initcode;
795#if LISTING 812#if LISTING
796 PrintCode(init,init+maincode); 813{ static void PrintCode (Byte *code, Byte *end);
814 PrintCode(init,init+maincode); }
797#endif 815#endif
798 if (lua_execute (init)) return 1; 816 return init;
799 free(init);
800 return 0;
801} 817}
802 818
803 819
@@ -876,7 +892,6 @@ static void PrintCode (Byte *code, Byte *end)
876 } 892 }
877 break; 893 break;
878 case PUSHINDEXED: printf ("%d PUSHINDEXED\n", (p++)-code); break; 894 case PUSHINDEXED: printf ("%d PUSHINDEXED\n", (p++)-code); break;
879 case PUSHMARK: printf ("%d PUSHMARK\n", (p++)-code); break;
880 case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: case STORELOCAL3: 895 case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: case STORELOCAL3:
881 case STORELOCAL4: case STORELOCAL5: case STORELOCAL6: case STORELOCAL7: 896 case STORELOCAL4: case STORELOCAL5: case STORELOCAL6: case STORELOCAL7:
882 case STORELOCAL8: case STORELOCAL9: 897 case STORELOCAL8: case STORELOCAL9:
@@ -896,6 +911,7 @@ static void PrintCode (Byte *code, Byte *end)
896 printf ("%d STOREGLOBAL %d\n", n, c.w); 911 printf ("%d STOREGLOBAL %d\n", n, c.w);
897 } 912 }
898 break; 913 break;
914 case PUSHSELF: printf ("%d PUSHSELF\n", (p++)-code); break;
899 case STOREINDEXED0: printf ("%d STOREINDEXED0\n", (p++)-code); break; 915 case STOREINDEXED0: printf ("%d STOREINDEXED0\n", (p++)-code); break;
900 case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p)); 916 case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p));
901 p++; 917 p++;
@@ -912,6 +928,7 @@ static void PrintCode (Byte *code, Byte *end)
912 printf("%d STORERECORD %d\n", p-code, *(++p)); 928 printf("%d STORERECORD %d\n", p-code, *(++p));
913 p += *p*sizeof(Word) + 1; 929 p += *p*sizeof(Word) + 1;
914 break; 930 break;
931 case ADJUST0: printf ("%d ADJUST0\n", (p++)-code); break;
915 case ADJUST: 932 case ADJUST:
916 printf ("%d ADJUST %d\n", p-code, *(++p)); 933 printf ("%d ADJUST %d\n", p-code, *(++p));
917 p++; 934 p++;
@@ -922,7 +939,7 @@ static void PrintCode (Byte *code, Byte *end)
922 int n = p-code; 939 int n = p-code;
923 p++; 940 p++;
924 get_word(c,p); 941 get_word(c,p);
925 printf ("%d CREATEARRAY\n", n, c.w); 942 printf ("%d CREATEARRAY %d\n", n, c.w);
926 break; 943 break;
927 } 944 }
928 case EQOP: printf ("%d EQOP\n", (p++)-code); break; 945 case EQOP: printf ("%d EQOP\n", (p++)-code); break;
@@ -990,16 +1007,19 @@ static void PrintCode (Byte *code, Byte *end)
990 } 1007 }
991 break; 1008 break;
992 case POP: printf ("%d POP\n", (p++)-code); break; 1009 case POP: printf ("%d POP\n", (p++)-code); break;
993 case CALLFUNC: printf ("%d CALLFUNC\n", (p++)-code); break; 1010 case CALLFUNC:
1011 printf ("%d CALLFUNC %d %d\n", p-code, *(p+1), *(p+2));
1012 p+=3;
1013 break;
1014 case RETCODE0: printf ("%d RETCODE0\n", (p++)-code); break;
994 case RETCODE: 1015 case RETCODE:
995 printf ("%d RETCODE %d\n", p-code, *(++p)); 1016 printf ("%d RETCODE %d\n", p-code, *(++p));
996 p++; 1017 p++;
997 break; 1018 break;
998 case HALT: printf ("%d HALT\n", (p++)-code); break;
999 case SETFUNCTION: 1019 case SETFUNCTION:
1000 { 1020 {
1001 CodeCode c1; 1021 CodeCode c1;
1002 CodeWord c1; 1022 CodeWord c2;
1003 int n = p-code; 1023 int n = p-code;
1004 p++; 1024 p++;
1005 get_code(c1,p); 1025 get_code(c1,p);