Browse Source

new form for constructors: {[exp] = exp, ...}

Roberto Ierusalimschy 28 years ago
parent
commit
b8af9c56c9
3 changed files with 38 additions and 32 deletions
  1. 23 27
      lua.stx
  2. 12 2
      opcode.c
  3. 3 3
      opcode.h

+ 23 - 27
lua.stx

@@ -1,6 +1,6 @@
 %{
 %{
 
 
-char *rcs_luastx = "$Id: lua.stx,v 3.43 1997/01/31 14:27:11 roberto Exp roberto $";
+char *rcs_luastx = "$Id: lua.stx,v 3.44 1997/02/13 16:18:39 roberto Exp roberto $";
 
 
 #include <stdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
@@ -50,8 +50,6 @@ static TaggedString *localvar[MAXLOCALS];  /* store local variable names */
 static int     nlocalvar=0;	     /* number of local variables */
 static int     nlocalvar=0;	     /* number of local variables */
 
 
 #define MAXFIELDS FIELDS_PER_FLUSH*2
 #define MAXFIELDS FIELDS_PER_FLUSH*2
-static Word    fields[MAXFIELDS];     /* fieldnames to be flushed */
-static int     nfields=0;
 
 
 int lua_debug = 0;
 int lua_debug = 0;
 
 
@@ -103,22 +101,11 @@ static void code_word_at (Byte *p, int n)
   memcpy(p, &w, sizeof(Word));
   memcpy(p, &w, sizeof(Word));
 }
 }
 
 
-static void push_field (Word name)
-{
-  if (nfields < MAXFIELDS)
-    fields[nfields++] = name;
-  else
-   yyerror ("too many fields in nested constructors");
-}
-
 static void flush_record (int n)
 static void flush_record (int n)
 {
 {
-  int i;
   if (n == 0) return;
   if (n == 0) return;
-  code_byte(STORERECORD);
+  code_byte(STOREMAP);
   code_byte(n);
   code_byte(n);
-  for (i=0; i<n; i++)
-    code_word(fields[--nfields]);
 }
 }
 
 
 static void flush_list (int m, int n)
 static void flush_list (int m, int n)
@@ -161,6 +148,17 @@ static void add_varbuffer (Long var)
   yyerror ("variable buffer overflow");
   yyerror ("variable buffer overflow");
 }
 }
 
 
+static void code_string (Word w)
+{
+  code_byte(PUSHSTRING);
+  code_word(w);
+}
+
+static void code_constant (TaggedString *s)
+{
+  code_string(luaI_findconstant(s));
+}
+
 static void code_number (float f)
 static void code_number (float f)
 { 
 { 
   Word i;
   Word i;
@@ -477,8 +475,7 @@ function     : FUNCTION funcname body
 funcname	: var { $$ =$1; init_func(); }
 funcname	: var { $$ =$1; init_func(); }
 		| varexp ':' NAME
 		| varexp ':' NAME
 	{
 	{
-	  code_byte(PUSHSTRING);
-	  code_word(luaI_findconstant($3));
+	  code_constant($3);
 	  $$ = 0;  /* indexed variable */
 	  $$ = 0;  /* indexed variable */
 	  init_func();
 	  init_func();
 	  add_localvar(luaI_createfixedstring("self"));
 	  add_localvar(luaI_createfixedstring("self"));
@@ -605,9 +602,8 @@ expr :  '(' expr ')'  { $$ = $2; }
      |  NUMBER          { code_number($1); $$ = 0; }
      |  NUMBER          { code_number($1); $$ = 0; }
      |  STRING
      |  STRING
      {
      {
-      code_byte(PUSHSTRING);
-      code_word($1);
-      $$ = 0;
+       code_string($1);
+       $$ = 0;
      }
      }
      |	NIL		{code_byte(PUSHNIL); $$ = 0; }
      |	NIL		{code_byte(PUSHNIL); $$ = 0; }
      |  functioncall    { $$ = $1; }
      |  functioncall    { $$ = $1; }
@@ -723,12 +719,13 @@ ffieldlist1 : ffield			{$$=1;}
 		}
 		}
 	   ; 
 	   ; 
 
 
-ffield      : NAME '=' expr1 
-	      { 
-	       push_field(luaI_findconstant($1));
-	      }
+ffield      : ffieldkey '=' expr1
            ;
            ;
 
 
+ffieldkey   : '[' expr1 ']'
+	    | NAME { code_constant($1); }
+	    ;
+
 lfieldlist  : /* empty */ { $$ = 0; }
 lfieldlist  : /* empty */ { $$ = 0; }
 	    | lfieldlist1 lastcomma { $$ = $1; }
 	    | lfieldlist1 lastcomma { $$ = $1; }
 	    ;
 	    ;
@@ -762,9 +759,8 @@ var	  :	singlevar { $$ = $1; }
 	  }
 	  }
 	  |	varexp '.' NAME
 	  |	varexp '.' NAME
 	  {
 	  {
-	   code_byte(PUSHSTRING);
-	   code_word(luaI_findconstant($3));
-	   $$ = 0;		/* indexed variable */
+	    code_constant($3);
+	    $$ = 0;		/* indexed variable */
 	  }
 	  }
 	  ;
 	  ;
 		
 		

+ 12 - 2
opcode.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 ** TecCGraf - PUC-Rio
 */
 */
 
 
-char *rcs_opcode="$Id: opcode.c,v 3.81 1997/02/20 15:51:14 roberto Exp roberto $";
+char *rcs_opcode="$Id: opcode.c,v 3.82 1997/02/26 17:38:41 roberto Unstable roberto $";
 
 
 #include <setjmp.h>
 #include <setjmp.h>
 #include <stdio.h>
 #include <stdio.h>
@@ -1184,7 +1184,7 @@ static StkId lua_execute (Byte *pc, StkId base)
    }
    }
    break;
    break;
 
 
-   case STORERECORD:
+   case STORERECORD:  /* opcode obsolete: supersed by STOREMAP */
    {
    {
     int n = *(pc++);
     int n = *(pc++);
     Object *arr = top-n-1;
     Object *arr = top-n-1;
@@ -1200,6 +1200,16 @@ static StkId lua_execute (Byte *pc, StkId base)
    }
    }
    break;
    break;
 
 
+   case STOREMAP: {
+     int n = *(pc++);
+     Object *arr = top-(2*n)-1;
+     while (n--) {
+       *(lua_hashdefine (avalue(arr), top-2)) = *(top-1);
+       top-=2;
+     }
+   }
+   break;
+
    case ADJUST0:
    case ADJUST0:
      adjust_top(base);
      adjust_top(base);
      break;
      break;

+ 3 - 3
opcode.h

@@ -1,6 +1,6 @@
 /*
 /*
 ** TeCGraf - PUC-Rio
 ** TeCGraf - PUC-Rio
-** $Id: opcode.h,v 3.25 1997/02/11 11:35:05 roberto Exp roberto $
+** $Id: opcode.h,v 3.26 1997/02/20 15:51:14 roberto Exp roberto $
 */
 */
 
 
 #ifndef opcode_h
 #ifndef opcode_h
@@ -101,8 +101,8 @@ CALLFUNC,/*	n m	v_n...v_1 f	r_m...r_1	f(v1,...,v_n)  */
 RETCODE0,
 RETCODE0,
 RETCODE,/*	b	-		-  */
 RETCODE,/*	b	-		-  */
 SETLINE,/*	w	-		-		LINE=w  */
 SETLINE,/*	w	-		-		LINE=w  */
-VARARGS/*	b	v_n...v_1	{v_1...v_n;n=n}  */
-
+VARARGS,/*	b	v_n...v_1	{v_1...v_n;n=n}  */
+STOREMAP/*	n	v_n k_n ...v_1 k_1 t	-	t[k_i]=v_i  */
 } OpCode;
 } OpCode;