Skip to content
Tcl Source Code Artifact [a4fece9b6f]
Login

Artifact [a4fece9b6f]

Artifact a4fece9b6f2bf9b36bc875d85099bfbc99010bb97e7de33cc25422287c342d57:


--- a/generic/tclCompile.h
+++ b/generic/tclCompile.h
@@ -857,6 +857,10 @@
      * field x of the typed product a, else as loadArray */
     INST_LOAD_FIELD_NUM,
 
+    /* "(a, (b, c)) = v" in expr: deep content of v, then each name at a
+     * path of the names list receives the element at the same path */
+    INST_TUPLE_ASSIGN,
+
     /* The last opcode */
     LAST_INST_OPCODE
 };
--- a/generic/tclCompile.c
+++ b/generic/tclCompile.c
@@ -688,6 +688,11 @@
 	 * the number in field x of the typed product a (no type wrapper);
 	 * otherwise exactly loadArray4.
 	 * Stack: ... field => ... value */
+    {"tupleAssign",	  1,   -2,	   0,	{OPERAND_NONE}},
+	/* "(a, (b, c), d) = v" in expr: content of v (deep), then for each
+	 * path of the list paths, the name at that path in names receives
+	 * the element at the same path of the content ("" if absent).
+	 * Stack: ... names value paths => ... content */
 
     {NULL, 0, 0, 0, {OPERAND_NONE}}
 };
--- a/generic/tclCompExpr.c
+++ b/generic/tclCompExpr.c
@@ -523,9 +523,11 @@
 static const char *	FindTupleTarget(const char *start, Tcl_Size numBytes);
 static int		IsTypeAhead(const char *start, Tcl_Size numBytes);
 static Tcl_Size		ElementTargetLen(const char *start, Tcl_Size numBytes);
-static void		EmitTupleStores(CompileEnv *envPtr, OpNode *nodes,
-			    int index, int *path, int depth, int *counter,
-			    int root);
+static void		CollectTuplePaths(OpNode *nodes, int index, int *path,
+			    int depth, int *counter, int root, Tcl_Obj *paths,
+			    CompileEnv *envPtr, Tcl_Obj *const *names,
+			    int *nameIdx);
+static int		TupleNamesOnly(OpNode *nodes, int index, int root);
 static void		ConvertTreeToTokens(const char *start, Tcl_Size numBytes,
 			    OpNode *nodes, Tcl_Token *tokenPtr,
 			    Tcl_Parse *parsePtr);
@@ -552,30 +554,85 @@
  *----------------------------------------------------------------------
  */
 
+/*
+ * The paths of the names of a tuple assignment, in the order (and with the
+ * rules) of EmitTupleStores: each leaf name gives a list of indices.
+ */
+/*
+ * Is the left side of a tuple assignment made only of names, commas and
+ * parentheses? Then its literals, consumed in order while it was compiled,
+ * are exactly its leaf names (CollectTuplePaths can give their compiled
+ * locals).
+ */
+static int
+TupleNamesOnly(
+    OpNode *nodes,
+    int index,
+    int root)
+{
+    if (index == OT_LITERAL) {
+	return 1;
+    }
+    if (index < 0) {
+	return 0;
+    }
+    switch (nodes[index].lexeme) {
+    case COMMA:
+	return TupleNamesOnly(nodes, nodes[index].left, 0)
+		&& TupleNamesOnly(nodes, nodes[index].right, 0);
+    case OPEN_PAREN:
+    case LIST:
+    case NULL_FUNC:
+	(void) root;
+	return TupleNamesOnly(nodes, nodes[index].right, 0);
+    default:
+	return 0;
+    }
+}
+
 static void
-EmitTupleStores(
-    CompileEnv *envPtr,
+CollectTuplePaths(
     OpNode *nodes,
-    int index,			/* Node, or OT_LITERAL for a leaf name. */
+    int index,
     int *path,
     int depth,
-    int *counter,		/* Next element number at this depth. */
-    int root)			/* Index is the outermost LIST node. */
+    int *counter,
+    int root,
+    Tcl_Obj *paths,
+    CompileEnv *envPtr,		/* With names: compiled locals. */
+    Tcl_Obj *const *names,	/* Leaf names in order, or NULL. */
+    int *nameIdx)
 {
     int k;
 
     if (index == OT_LITERAL) {
-	path[depth] = (*counter)++;
-	TclEmitInstInt4(INST_OVER, 1, envPtr);
-	for (k = 0; k <= depth; k++) {
-	    TclEmitInstInt4(INST_LIST_INDEX_IMM, path[k], envPtr);
+	Tcl_Obj *p = Tcl_NewListObj(0, NULL);
+	Tcl_Size lv = -1;
+
+	if (names != NULL && envPtr->procPtr != NULL) {
+	    Tcl_Size nlen, c;
+	    const char *nm = TclGetStringFromObj(names[*nameIdx], &nlen);
+
+	    for (c = 0; c < nlen; c++) {
+		if (nm[c] == ':' || nm[c] == '(' || nm[c] == ')') {
+		    break;		/* qualifié ou tableau : par nom */
+		}
+	    }
+	    if (nlen > 0 && c == nlen) {
+		lv = TclFindCompiledLocal(nm, nlen, 1, envPtr);
+	    }
 	}
-	TclEmitInstInt4(INST_OVER, 1, envPtr);
+	if (names != NULL) {
+	    (*nameIdx)++;
+	}
+	/* {LOCAL index...} : LOCAL = variable locale compilée, ou -1 (par
+	 * le nom pris dans la liste des noms) */
+	Tcl_ListObjAppendElement(NULL, p, Tcl_NewWideIntObj(lv));
+	path[depth] = (*counter)++;
 	for (k = 0; k <= depth; k++) {
-	    TclEmitInstInt4(INST_LIST_INDEX_IMM, path[k], envPtr);
+	    Tcl_ListObjAppendElement(NULL, p, Tcl_NewWideIntObj(path[k]));
 	}
-	TclEmitOpcode(INST_STORE_STK, envPtr);
-	TclEmitOpcode(INST_POP, envPtr);
+	Tcl_ListObjAppendElement(NULL, paths, p);
 	return;
     }
     if (index < 0) {
@@ -583,26 +640,26 @@
     }
     switch (nodes[index].lexeme) {
     case COMMA:
-	EmitTupleStores(envPtr, nodes, nodes[index].left, path, depth,
-		counter, 0);
-	EmitTupleStores(envPtr, nodes, nodes[index].right, path, depth,
-		counter, 0);
+	CollectTuplePaths(nodes, nodes[index].left, path, depth, counter, 0,
+		paths, envPtr, names, nameIdx);
+	CollectTuplePaths(nodes, nodes[index].right, path, depth, counter, 0,
+		paths, envPtr, names, nameIdx);
 	break;
     case OPEN_PAREN:
-	EmitTupleStores(envPtr, nodes, nodes[index].right, path, depth,
-		counter, 0);
+	CollectTuplePaths(nodes, nodes[index].right, path, depth, counter, 0,
+		paths, envPtr, names, nameIdx);
 	break;
     case LIST:
     case NULL_FUNC:
 	if (root) {
-	    EmitTupleStores(envPtr, nodes, nodes[index].right, path, depth,
-		    counter, 0);
+	    CollectTuplePaths(nodes, nodes[index].right, path, depth, counter,
+		    0, paths, envPtr, names, nameIdx);
 	} else if (depth + 1 < TUPLE_MAX_DEPTH) {
 	    int inner = 0;
 
 	    path[depth] = (*counter)++;
-	    EmitTupleStores(envPtr, nodes, nodes[index].right, path,
-		    depth + 1, &inner, 0);
+	    CollectTuplePaths(nodes, nodes[index].right, path, depth + 1,
+		    &inner, 0, paths, envPtr, names, nameIdx);
 	}
 	break;
     default:
@@ -610,6 +667,7 @@
     }
 }
 
+
 /*
  *----------------------------------------------------------------------
  *
@@ -2893,6 +2951,10 @@
      * local (storeScalar, the name is not pushed) */
     Tcl_Size assignNode[DECL_MAX], assignLocal[DECL_MAX];
     int nAssign = 0;
+    /* "(a, b) = v": ASSIGN node -> its first literal (the leaf names) */
+    Tcl_Size tupleNode[DECL_MAX];
+    Tcl_Obj *const *tupleLit[DECL_MAX];
+    int nTupleLit = 0;
 
     while (1) {
 	int next;
@@ -2904,6 +2966,12 @@
 	    if (nodePtr->lexeme == QUESTION) {
 		convert = 1;
 	    }
+	    if (nodePtr->lexeme == ASSIGN && next >= 0
+		    && nodes[next].lexeme == LIST && nTupleLit < DECL_MAX) {
+		/* "(a, b) = v": its names are the literals from here */
+		tupleNode[nTupleLit] = nodePtr - nodes;
+		tupleLit[nTupleLit++] = *litObjvPtr;
+	    }
 	} else if (nodePtr->mark == MARK_RIGHT) {
 	    next = nodePtr->right;
 
@@ -3047,21 +3115,36 @@
 		     */
 
 		    int path[TUPLE_MAX_DEPTH], counter = 0;
+		    Tcl_Obj *paths = Tcl_NewListObj(0, NULL);
+		    Tcl_Size plen;
+		    const char *ptext;
 
 		    /*
 		     * Règle B : un produit est un élément ; l'affectation par
 		     * tuple lit son CONTENU (ses champs, et ceux des produits
-		     * imbriqués), pas sa vue liste.
+		     * imbriqués), pas sa vue liste. Une instruction
+		     * (tupleAssign) fait le contenu et toutes les affectations,
+		     * selon les chemins des noms établis ici.
 		     */
-		    TclEmitPush(TclRegisterLiteral(envPtr,
-			    "::tcl::unsupported::content", 27, LITERAL_CMD_NAME),
+		    Tcl_Obj *const *names = NULL;
+		    int t, nameIdx = 0;
+
+		    for (t = 0; t < nTupleLit; t++) {
+			if (tupleNode[t] == nodePtr - nodes) {
+			    break;
+			}
+		    }
+		    if (t < nTupleLit && TupleNamesOnly(nodes, nodePtr->left, 1)) {
+			names = tupleLit[t];
+		    }
+		    Tcl_IncrRefCount(paths);
+		    CollectTuplePaths(nodes, nodePtr->left, path, 0, &counter,
+			    1, paths, envPtr, names, &nameIdx);
+		    ptext = TclGetStringFromObj(paths, &plen);
+		    TclEmitPush(TclRegisterLiteral(envPtr, ptext, plen, 0),
 			    envPtr);
-		    TclEmitInstInt4(INST_REVERSE, 2, envPtr);
-		    TclEmitInvoke(envPtr, INST_INVOKE_STK1, 2);
-		    EmitTupleStores(envPtr, nodes, nodePtr->left, path, 0,
-			    &counter, 1);
-		    TclEmitInstInt4(INST_REVERSE, 2, envPtr);
-		    TclEmitOpcode(INST_POP, envPtr);
+		    Tcl_DecrRefCount(paths);
+		    TclEmitOpcode(INST_TUPLE_ASSIGN, envPtr);
 		} else {
 		    int d;
 
--- a/generic/tclExecute.c
+++ b/generic/tclExecute.c
@@ -2995,6 +2995,24 @@
 	part1Ptr = part2Ptr = NULL;
 	goto doCallPtrGetVar;
 
+    case INST_TUPLE_ASSIGN: {
+	Tcl_Obj *tupPtr;
+
+	TRACE(("\"%.30s\" \"%.30s\" => ", O2S(OBJ_AT_DEPTH(2)),
+		O2S(OBJ_UNDER_TOS)));
+	DECACHE_STACK_INFO();
+	tupPtr = TclTupleAssign(interp, OBJ_AT_DEPTH(2), OBJ_UNDER_TOS,
+		OBJ_AT_TOS);
+	CACHE_STACK_INFO();
+	if (tupPtr == NULL) {
+	    TRACE_ERROR(interp);
+	    goto gotError;
+	}
+	objResultPtr = tupPtr;
+	TRACE_APPEND(("\"%.30s\"\n", O2S(objResultPtr)));
+	NEXT_INST_V(1, 3, -1);
+    }
+
     case INST_LOAD_FIELD_NUM:
 	opnd = TclGetUInt4AtPtr(pc + 1);
 	objResultPtr = TclTypedFieldNumber(LOCAL(opnd), OBJ_AT_TOS);