aboutsummaryrefslogtreecommitdiff
path: root/lua.stx
diff options
context:
space:
mode:
authorWaldemar Celes <celes@tecgraf.puc-rio.br>1994-08-05 16:31:09 -0300
committerWaldemar Celes <celes@tecgraf.puc-rio.br>1994-08-05 16:31:09 -0300
commit41fd23287aae60354c264be8f1807bccd937fbf1 (patch)
tree58670393eeb575ee622ca52dbde7b65f7ce3098f /lua.stx
parentbe7aa3854be4c8d9203637955a064439f240951f (diff)
downloadlua-41fd23287aae60354c264be8f1807bccd937fbf1.tar.gz
lua-41fd23287aae60354c264be8f1807bccd937fbf1.tar.bz2
lua-41fd23287aae60354c264be8f1807bccd937fbf1.zip
Implementacao da definicao e chamada de METODOS.
Diffstat (limited to 'lua.stx')
-rw-r--r--lua.stx155
1 files changed, 125 insertions, 30 deletions
diff --git a/lua.stx b/lua.stx
index f3d6776b..199c464a 100644
--- a/lua.stx
+++ b/lua.stx
@@ -1,6 +1,6 @@
1%{ 1%{
2 2
3char *rcs_luastx = "$Id: lua.stx,v 2.5 1994/07/19 21:27:18 celes Exp $"; 3char *rcs_luastx = "$Id: lua.stx,v 2.6 1994/08/03 14:15:46 celes Exp celes $";
4 4
5#include <stdio.h> 5#include <stdio.h>
6#include <stdlib.h> 6#include <stdlib.h>
@@ -77,6 +77,16 @@ static void code_float (float n)
77 code_byte(code.m.c4); 77 code_byte(code.m.c4);
78} 78}
79 79
80static void code_code (Byte *b)
81{
82 CodeCode code;
83 code.b = b;
84 code_byte(code.m.c1);
85 code_byte(code.m.c2);
86 code_byte(code.m.c3);
87 code_byte(code.m.c4);
88}
89
80static void code_word_at (Byte *p, Word n) 90static void code_word_at (Byte *p, Word n)
81{ 91{
82 CodeWord code; 92 CodeWord code;
@@ -185,6 +195,21 @@ static void code_number (float f)
185 incr_ntemp(); 195 incr_ntemp();
186} 196}
187 197
198static void init_function (void)
199{
200 if (code == NULL) /* first function */
201 {
202 code = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
203 if (code == NULL)
204 {
205 lua_error("not enough memory");
206 err = 1;
207 }
208 maxcode = CODE_BLOCK;
209 }
210}
211
212
188%} 213%}
189 214
190 215
@@ -205,9 +230,10 @@ static void code_number (float f)
205%token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END 230%token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
206%token RETURN 231%token RETURN
207%token LOCAL 232%token LOCAL
233%token FUNCTION
208%token <vFloat> NUMBER 234%token <vFloat> NUMBER
209%token <vWord> FUNCTION STRING 235%token <vWord> STRING
210%token <pChar> NAME 236%token <pChar> NAME
211%token <vInt> DEBUG 237%token <vInt> DEBUG
212 238
213%type <vLong> PrepJump 239%type <vLong> PrepJump
@@ -215,7 +241,8 @@ static void code_number (float f)
215%type <vInt> fieldlist, localdeclist 241%type <vInt> fieldlist, localdeclist
216%type <vInt> ffieldlist, ffieldlist1 242%type <vInt> ffieldlist, ffieldlist1
217%type <vInt> lfieldlist, lfieldlist1 243%type <vInt> lfieldlist, lfieldlist1
218%type <vLong> var, objectname 244%type <vInt> functionvalue
245%type <vLong> var, singlevar, objectname
219 246
220 247
221%left AND OR 248%left AND OR
@@ -240,21 +267,13 @@ functionlist : /* empty */
240 maincode=pc; initcode=basepc; maxmain=maxcurr; 267 maincode=pc; initcode=basepc; maxmain=maxcurr;
241 } 268 }
242 | functionlist function 269 | functionlist function
270 | functionlist method
243 | functionlist setdebug 271 | functionlist setdebug
244 ; 272 ;
245 273
246function : FUNCTION NAME 274function : FUNCTION NAME
247 { 275 {
248 if (code == NULL) /* first function */ 276 init_function();
249 {
250 code = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
251 if (code == NULL)
252 {
253 lua_error("not enough memory");
254 err = 1;
255 }
256 maxcode = CODE_BLOCK;
257 }
258 pc=0; basepc=code; maxcurr=maxcode; 277 pc=0; basepc=code; maxcurr=maxcode;
259 nlocalvar=0; 278 nlocalvar=0;
260 $<vWord>$ = lua_findsymbol($2); 279 $<vWord>$ = lua_findsymbol($2);
@@ -284,11 +303,62 @@ function : FUNCTION NAME
284 memcpy (s_bvalue($<vWord>3), basepc, pc*sizeof(Byte)); 303 memcpy (s_bvalue($<vWord>3), basepc, pc*sizeof(Byte));
285 code = basepc; maxcode=maxcurr; 304 code = basepc; maxcode=maxcurr;
286#if LISTING 305#if LISTING
287PrintCode(code,code+pc); 306 PrintCode(code,code+pc);
288#endif 307#endif
289 } 308 }
290 ; 309 ;
291 310
311method : FUNCTION NAME { $<vWord>$ = lua_findsymbol($2); } ':' NAME
312 {
313 init_function();
314 pc=0; basepc=code; maxcurr=maxcode;
315 nlocalvar=0;
316 localvar[nlocalvar]=lua_findsymbol("self"); /* self param. */
317 add_nlocalvar(1);
318 $<vWord>$ = lua_findconstant($5);
319 }
320 '(' parlist ')'
321 {
322 if (lua_debug)
323 {
324 code_byte(SETFUNCTION);
325 code_word(lua_nfile-1);
326 code_word($<vWord>6);
327 }
328 lua_codeadjust (0);
329 }
330 block
331 END
332 {
333 Byte *b;
334 if (lua_debug) code_byte(RESET);
335 code_byte(RETCODE); code_byte(nlocalvar);
336 b = calloc (pc, sizeof(Byte));
337 if (b == NULL)
338 {
339 lua_error("not enough memory");
340 err = 1;
341 }
342 memcpy (b, basepc, pc*sizeof(Byte));
343 code = basepc; maxcode=maxcurr;
344#if LISTING
345 PrintCode(code,code+pc);
346#endif
347 /* assign function to table field */
348 pc=maincode; basepc=initcode; maxcurr=maxmain;
349 nlocalvar=0;
350
351 lua_pushvar($<vWord>3+1);
352 code_byte(PUSHSTRING);
353 code_word($<vWord>6);
354 code_byte(PUSHFUNCTION);
355 code_code(b);
356 code_byte(STOREINDEXED0);
357
358 maincode=pc; initcode=basepc; maxmain=maxcurr;
359 }
360 ;
361
292statlist : /* empty */ 362statlist : /* empty */
293 | statlist stat sc 363 | statlist stat sc
294 ; 364 ;
@@ -499,10 +569,23 @@ dimension : /* empty */ { code_byte(PUSHNIL); incr_ntemp();}
499 | expr1 569 | expr1
500 ; 570 ;
501 571
502functioncall : functionvalue {code_byte(PUSHMARK); $<vInt>$ = ntemp; incr_ntemp();} 572functioncall : functionvalue
503 '(' exprlist ')' { code_byte(CALLFUNC); ntemp = $<vInt>2-1;} 573 {
574 code_byte(PUSHMARK); $<vInt>$ = ntemp; incr_ntemp();
575 if ($1 != 0) lua_pushvar($1);
576 }
577 '(' exprlist ')' { code_byte(CALLFUNC); ntemp = $<vInt>2-1;}
504 578
505functionvalue : var {lua_pushvar ($1); } 579functionvalue : var {lua_pushvar ($1); $$ = 0; }
580 | singlevar ':' NAME
581 {
582 $$ = $1;
583 lua_pushvar($1);
584 code_byte(PUSHSTRING);
585 code_word(lua_findconstant($3));
586 incr_ntemp();
587 lua_pushvar(0);
588 }
506 ; 589 ;
507 590
508exprlist : /* empty */ { $$ = 1; } 591exprlist : /* empty */ { $$ = 1; }
@@ -590,16 +673,7 @@ varlist1 : var
590 } 673 }
591 ; 674 ;
592 675
593var : NAME 676var : singlevar { $$ = $1; }
594 {
595 Word s = lua_findsymbol($1);
596 int local = lua_localname (s);
597 if (local == -1) /* global var */
598 $$ = s + 1; /* return positive value */
599 else
600 $$ = -(local+1); /* return negative value */
601 }
602
603 | var {lua_pushvar ($1);} '[' expr1 ']' 677 | var {lua_pushvar ($1);} '[' expr1 ']'
604 { 678 {
605 $$ = 0; /* indexed variable */ 679 $$ = 0; /* indexed variable */
@@ -612,6 +686,17 @@ var : NAME
612 } 686 }
613 ; 687 ;
614 688
689singlevar : NAME
690 {
691 Word s = lua_findsymbol($1);
692 int local = lua_localname (s);
693 if (local == -1) /* global var */
694 $$ = s + 1; /* return positive value */
695 else
696 $$ = -(local+1); /* return negative value */
697 }
698 ;
699
615localdeclist : NAME {localvar[nlocalvar]=lua_findsymbol($1); $$ = 1;} 700localdeclist : NAME {localvar[nlocalvar]=lua_findsymbol($1); $$ = 1;}
616 | localdeclist ',' NAME 701 | localdeclist ',' NAME
617 { 702 {
@@ -799,6 +884,16 @@ static void PrintCode (Byte *code, Byte *end)
799 printf ("%d PUSHSTRING %d\n", n, c.w); 884 printf ("%d PUSHSTRING %d\n", n, c.w);
800 } 885 }
801 break; 886 break;
887 case PUSHFUNCTION:
888 {
889 CodeCode c;
890 int n = p-code;
891 p++;
892 get_code(c,p);
893 printf ("%d PUSHFUNCTION %p\n", n, c.b);
894 }
895 break;
896
802 case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3: 897 case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
803 case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7: 898 case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
804 case PUSHLOCAL8: case PUSHLOCAL9: 899 case PUSHLOCAL8: case PUSHLOCAL9: