aboutsummaryrefslogtreecommitdiff
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
parentbe7aa3854be4c8d9203637955a064439f240951f (diff)
downloadlua-41fd23287aae60354c264be8f1807bccd937fbf1.tar.gz
lua-41fd23287aae60354c264be8f1807bccd937fbf1.tar.bz2
lua-41fd23287aae60354c264be8f1807bccd937fbf1.zip
Implementacao da definicao e chamada de METODOS.
-rw-r--r--lua.stx155
-rw-r--r--opcode.c17
-rw-r--r--opcode.h11
3 files changed, 148 insertions, 35 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:
diff --git a/opcode.c b/opcode.c
index b2f5cb41..e5877ad0 100644
--- a/opcode.c
+++ b/opcode.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_opcode="$Id: opcode.c,v 2.2 1994/07/19 21:27:18 celes Exp celes $"; 6char *rcs_opcode="$Id: opcode.c,v 2.3 1994/08/03 14:15:46 celes Exp celes $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <stdlib.h> 9#include <stdlib.h>
@@ -211,6 +211,14 @@ int lua_execute (Byte *pc)
211 tag(top) = T_STRING; svalue(top++) = lua_constant[code.w]; 211 tag(top) = T_STRING; svalue(top++) = lua_constant[code.w];
212 } 212 }
213 break; 213 break;
214
215 case PUSHFUNCTION:
216 {
217 CodeCode code;
218 get_code(code,pc);
219 tag(top) = T_FUNCTION; bvalue(top++) = code.b;
220 }
221 break;
214 222
215 case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: 223 case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
216 case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5: 224 case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
@@ -235,7 +243,7 @@ int lua_execute (Byte *pc)
235 return 1; 243 return 1;
236 } 244 }
237 { 245 {
238 Object *h = lua_hashdefine (avalue(top-1), top); 246 Object *h = lua_hashget (avalue(top-1), top);
239 if (h == NULL) return 1; 247 if (h == NULL) return 1;
240 *(top-1) = *h; 248 *(top-1) = *h;
241 } 249 }
@@ -773,7 +781,7 @@ Object *lua_getfield (Object *object, char *field)
773 Object ref; 781 Object ref;
774 tag(&ref) = T_STRING; 782 tag(&ref) = T_STRING;
775 svalue(&ref) = lua_createstring(field); 783 svalue(&ref) = lua_createstring(field);
776 return (lua_hashdefine(avalue(object), &ref)); 784 return (lua_hashget(avalue(object), &ref));
777 } 785 }
778} 786}
779 787
@@ -791,7 +799,7 @@ Object *lua_getindexed (Object *object, float index)
791 Object ref; 799 Object ref;
792 tag(&ref) = T_NUMBER; 800 tag(&ref) = T_NUMBER;
793 nvalue(&ref) = index; 801 nvalue(&ref) = index;
794 return (lua_hashdefine(avalue(object), &ref)); 802 return (lua_hashget(avalue(object), &ref));
795 } 803 }
796} 804}
797 805
@@ -1025,6 +1033,7 @@ void lua_print (void)
1025 { 1033 {
1026 if (lua_isnumber(obj)) printf("%g\n",lua_getnumber (obj)); 1034 if (lua_isnumber(obj)) printf("%g\n",lua_getnumber (obj));
1027 else if (lua_isstring(obj)) printf("%s\n",lua_getstring (obj)); 1035 else if (lua_isstring(obj)) printf("%s\n",lua_getstring (obj));
1036 else if (lua_isfunction(obj)) printf("function: %p\n",bvalue(obj));
1028 else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction (obj)); 1037 else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction (obj));
1029 else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata (obj)); 1038 else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata (obj));
1030 else if (lua_istable(obj)) printf("table: %p\n",obj); 1039 else if (lua_istable(obj)) printf("table: %p\n",obj);
diff --git a/opcode.h b/opcode.h
index 853efd13..2e875f69 100644
--- a/opcode.h
+++ b/opcode.h
@@ -1,6 +1,6 @@
1/* 1/*
2** TeCGraf - PUC-Rio 2** TeCGraf - PUC-Rio
3** $Id: opcode.h,v 2.1 1994/04/20 22:07:57 celes Exp celes $ 3** $Id: opcode.h,v 2.2 1994/07/19 21:27:18 celes Exp celes $
4*/ 4*/
5 5
6#ifndef opcode_h 6#ifndef opcode_h
@@ -34,6 +34,12 @@ typedef union
34 float f; 34 float f;
35} CodeFloat; 35} CodeFloat;
36 36
37typedef union
38{
39 struct {char c1; char c2; char c3; char c4;} m;
40 Byte *b;
41} CodeCode;
42
37typedef enum 43typedef enum
38{ 44{
39 PUSHNIL, 45 PUSHNIL,
@@ -42,6 +48,7 @@ typedef enum
42 PUSHWORD, 48 PUSHWORD,
43 PUSHFLOAT, 49 PUSHFLOAT,
44 PUSHSTRING, 50 PUSHSTRING,
51 PUSHFUNCTION,
45 PUSHLOCAL0, PUSHLOCAL1, PUSHLOCAL2, PUSHLOCAL3, PUSHLOCAL4, 52 PUSHLOCAL0, PUSHLOCAL1, PUSHLOCAL2, PUSHLOCAL3, PUSHLOCAL4,
46 PUSHLOCAL5, PUSHLOCAL6, PUSHLOCAL7, PUSHLOCAL8, PUSHLOCAL9, 53 PUSHLOCAL5, PUSHLOCAL6, PUSHLOCAL7, PUSHLOCAL8, PUSHLOCAL9,
47 PUSHLOCAL, 54 PUSHLOCAL,
@@ -143,6 +150,8 @@ typedef struct
143#define get_word(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;} 150#define get_word(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;}
144#define get_float(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\ 151#define get_float(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
145 code.m.c3 = *pc++; code.m.c4 = *pc++;} 152 code.m.c3 = *pc++; code.m.c4 = *pc++;}
153#define get_code(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
154 code.m.c3 = *pc++; code.m.c4 = *pc++;}
146 155
147 156
148 157