aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-03-06 14:30:55 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-03-06 14:30:55 -0300
commitb8af9c56c95722e7a3289f02568b9e2a7495c484 (patch)
tree463f99205e403ff0790dc7dc7a4fd7ab0676f935
parentc3c0b52a1f1318c3b1131d260100ad9d999db5d5 (diff)
downloadlua-b8af9c56c95722e7a3289f02568b9e2a7495c484.tar.gz
lua-b8af9c56c95722e7a3289f02568b9e2a7495c484.tar.bz2
lua-b8af9c56c95722e7a3289f02568b9e2a7495c484.zip
new form for constructors: {[exp] = exp, ...}
-rw-r--r--lua.stx50
-rw-r--r--opcode.c14
-rw-r--r--opcode.h6
3 files changed, 38 insertions, 32 deletions
diff --git a/lua.stx b/lua.stx
index d803d4ba..cb0a29d3 100644
--- a/lua.stx
+++ b/lua.stx
@@ -1,6 +1,6 @@
1%{ 1%{
2 2
3char *rcs_luastx = "$Id: lua.stx,v 3.43 1997/01/31 14:27:11 roberto Exp roberto $"; 3char *rcs_luastx = "$Id: lua.stx,v 3.44 1997/02/13 16:18:39 roberto Exp roberto $";
4 4
5#include <stdio.h> 5#include <stdio.h>
6#include <stdlib.h> 6#include <stdlib.h>
@@ -50,8 +50,6 @@ static TaggedString *localvar[MAXLOCALS]; /* store local variable names */
50static int nlocalvar=0; /* number of local variables */ 50static int nlocalvar=0; /* number of local variables */
51 51
52#define MAXFIELDS FIELDS_PER_FLUSH*2 52#define MAXFIELDS FIELDS_PER_FLUSH*2
53static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
54static int nfields=0;
55 53
56int lua_debug = 0; 54int lua_debug = 0;
57 55
@@ -103,22 +101,11 @@ static void code_word_at (Byte *p, int n)
103 memcpy(p, &w, sizeof(Word)); 101 memcpy(p, &w, sizeof(Word));
104} 102}
105 103
106static void push_field (Word name)
107{
108 if (nfields < MAXFIELDS)
109 fields[nfields++] = name;
110 else
111 yyerror ("too many fields in nested constructors");
112}
113
114static void flush_record (int n) 104static void flush_record (int n)
115{ 105{
116 int i;
117 if (n == 0) return; 106 if (n == 0) return;
118 code_byte(STORERECORD); 107 code_byte(STOREMAP);
119 code_byte(n); 108 code_byte(n);
120 for (i=0; i<n; i++)
121 code_word(fields[--nfields]);
122} 109}
123 110
124static void flush_list (int m, int n) 111static void flush_list (int m, int n)
@@ -161,6 +148,17 @@ static void add_varbuffer (Long var)
161 yyerror ("variable buffer overflow"); 148 yyerror ("variable buffer overflow");
162} 149}
163 150
151static void code_string (Word w)
152{
153 code_byte(PUSHSTRING);
154 code_word(w);
155}
156
157static void code_constant (TaggedString *s)
158{
159 code_string(luaI_findconstant(s));
160}
161
164static void code_number (float f) 162static void code_number (float f)
165{ 163{
166 Word i; 164 Word i;
@@ -477,8 +475,7 @@ function : FUNCTION funcname body
477funcname : var { $$ =$1; init_func(); } 475funcname : var { $$ =$1; init_func(); }
478 | varexp ':' NAME 476 | varexp ':' NAME
479 { 477 {
480 code_byte(PUSHSTRING); 478 code_constant($3);
481 code_word(luaI_findconstant($3));
482 $$ = 0; /* indexed variable */ 479 $$ = 0; /* indexed variable */
483 init_func(); 480 init_func();
484 add_localvar(luaI_createfixedstring("self")); 481 add_localvar(luaI_createfixedstring("self"));
@@ -605,9 +602,8 @@ expr : '(' expr ')' { $$ = $2; }
605 | NUMBER { code_number($1); $$ = 0; } 602 | NUMBER { code_number($1); $$ = 0; }
606 | STRING 603 | STRING
607 { 604 {
608 code_byte(PUSHSTRING); 605 code_string($1);
609 code_word($1); 606 $$ = 0;
610 $$ = 0;
611 } 607 }
612 | NIL {code_byte(PUSHNIL); $$ = 0; } 608 | NIL {code_byte(PUSHNIL); $$ = 0; }
613 | functioncall { $$ = $1; } 609 | functioncall { $$ = $1; }
@@ -723,12 +719,13 @@ ffieldlist1 : ffield {$$=1;}
723 } 719 }
724 ; 720 ;
725 721
726ffield : NAME '=' expr1 722ffield : ffieldkey '=' expr1
727 {
728 push_field(luaI_findconstant($1));
729 }
730 ; 723 ;
731 724
725ffieldkey : '[' expr1 ']'
726 | NAME { code_constant($1); }
727 ;
728
732lfieldlist : /* empty */ { $$ = 0; } 729lfieldlist : /* empty */ { $$ = 0; }
733 | lfieldlist1 lastcomma { $$ = $1; } 730 | lfieldlist1 lastcomma { $$ = $1; }
734 ; 731 ;
@@ -762,9 +759,8 @@ var : singlevar { $$ = $1; }
762 } 759 }
763 | varexp '.' NAME 760 | varexp '.' NAME
764 { 761 {
765 code_byte(PUSHSTRING); 762 code_constant($3);
766 code_word(luaI_findconstant($3)); 763 $$ = 0; /* indexed variable */
767 $$ = 0; /* indexed variable */
768 } 764 }
769 ; 765 ;
770 766
diff --git a/opcode.c b/opcode.c
index 919b2059..ed8a534e 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 3.81 1997/02/20 15:51:14 roberto Exp roberto $"; 6char *rcs_opcode="$Id: opcode.c,v 3.82 1997/02/26 17:38:41 roberto Unstable roberto $";
7 7
8#include <setjmp.h> 8#include <setjmp.h>
9#include <stdio.h> 9#include <stdio.h>
@@ -1184,7 +1184,7 @@ static StkId lua_execute (Byte *pc, StkId base)
1184 } 1184 }
1185 break; 1185 break;
1186 1186
1187 case STORERECORD: 1187 case STORERECORD: /* opcode obsolete: supersed by STOREMAP */
1188 { 1188 {
1189 int n = *(pc++); 1189 int n = *(pc++);
1190 Object *arr = top-n-1; 1190 Object *arr = top-n-1;
@@ -1200,6 +1200,16 @@ static StkId lua_execute (Byte *pc, StkId base)
1200 } 1200 }
1201 break; 1201 break;
1202 1202
1203 case STOREMAP: {
1204 int n = *(pc++);
1205 Object *arr = top-(2*n)-1;
1206 while (n--) {
1207 *(lua_hashdefine (avalue(arr), top-2)) = *(top-1);
1208 top-=2;
1209 }
1210 }
1211 break;
1212
1203 case ADJUST0: 1213 case ADJUST0:
1204 adjust_top(base); 1214 adjust_top(base);
1205 break; 1215 break;
diff --git a/opcode.h b/opcode.h
index c3942159..c15b9533 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 3.25 1997/02/11 11:35:05 roberto Exp roberto $ 3** $Id: opcode.h,v 3.26 1997/02/20 15:51:14 roberto Exp roberto $
4*/ 4*/
5 5
6#ifndef opcode_h 6#ifndef opcode_h
@@ -101,8 +101,8 @@ CALLFUNC,/* n m v_n...v_1 f r_m...r_1 f(v1,...,v_n) */
101RETCODE0, 101RETCODE0,
102RETCODE,/* b - - */ 102RETCODE,/* b - - */
103SETLINE,/* w - - LINE=w */ 103SETLINE,/* w - - LINE=w */
104VARARGS/* b v_n...v_1 {v_1...v_n;n=n} */ 104VARARGS,/* b v_n...v_1 {v_1...v_n;n=n} */
105 105STOREMAP/* n v_n k_n ...v_1 k_1 t - t[k_i]=v_i */
106} OpCode; 106} OpCode;
107 107
108 108