diff -u a/generic/tclCompExpr.c b/generic/tclCompExpr.c
--- a/generic/tclCompExpr.c 2026-10-15 13:49:15.182923740 +0200
+++ b/generic/tclCompExpr.c 2026-10-15 13:49:15.184483261 +0200
@@ -2962,7 +2962,8 @@
convert = 0;
break;
case ASSIGN:
- if (nodes[nodePtr->left].lexeme == IS_TYPE) {
+ if (nodePtr->left >= 0
+ && nodes[nodePtr->left].lexeme == IS_TYPE) {
/*
* "var is type = value": the stack holds [name type value].
* Invoke ::tcl::unsupported::declare name type value.
@@ -2974,7 +2975,8 @@
TclEmitInstInt4(INST_REVERSE, 4, envPtr);
TclEmitInstInt4(INST_REVERSE, 3, envPtr);
TclEmitInvoke(envPtr, INST_INVOKE_STK1, 4);
- } else if (nodes[nodePtr->left].lexeme == LIST) {
+ } else if (nodePtr->left >= 0
+ && nodes[nodePtr->left].lexeme == LIST) {
/*
* Destructuring assignment "(a,(b,c),d) = value": the stack holds
* the (nested) list of names and the value. Store each leaf
diff -u a/generic/tclTypeGeneric.c b/generic/tclTypeGeneric.c
--- a/generic/tclTypeGeneric.c 2026-10-15 13:49:15.178518136 +0200
+++ b/generic/tclTypeGeneric.c 2026-10-15 13:49:15.180419677 +0200
@@ -210,6 +210,16 @@
}
void
+TclTypesMarkBits(TypeData *d, const char *name)
+{
+ Tcl_HashEntry *hPtr = Tcl_FindHashEntry(&d->generics, name);
+
+ if (hPtr != NULL) {
+ ((TypeGeneric *) Tcl_GetHashValue(hPtr))->bits = 1;
+ }
+}
+
+void
TclTypesFreeGenerics(TypeData *d)
{
Tcl_HashSearch search;
@@ -864,6 +874,28 @@
if (res != inst) {
TclTypesCopyInto(inst, res);
}
+ if (g->bits) {
+ /* bitarray(n, ordre) : largeur et sens des indices */
+
+ Tcl_WideInt w = 0;
+ const char *ord = Tcl_GetString(bound.p[1].value);
+
+ if (Tcl_GetWideIntFromObj(NULL, bound.p[0].value, &w) != TCL_OK
+ || w < 1 || w > 1000000) {
+ ERR(interp, "PARAM", "bitarray needs a width between 1 and "
+ "1000000, not \"%s\"", Tcl_GetString(bound.p[0].value));
+ goto fail;
+ }
+ if (strcmp(ord, "lsb0") != 0 && strcmp(ord, "msb0") != 0) {
+ ERR(interp, "PARAM", "bitarray order must be lsb0 (index 0 is the "
+ "rightmost bit) or msb0 (index 0 is the leftmost bit), "
+ "not \"%s\"", ord);
+ goto fail;
+ }
+ inst->kind = TCLT_INT;
+ inst->bitWidth = (Tcl_Size) w;
+ inst->bitMsb0 = (strcmp(ord, "msb0") == 0);
+ }
if (!TclTypesInhabited(inst, stack, 0)) {
ERR(interp, "RECURSIVE", "type \"%s\" has no finite value: every "
"recursive reference must go through a sum variant or a "
diff -u a/generic/tclTypePath.c b/generic/tclTypePath.c
--- a/generic/tclTypePath.c 2026-10-15 13:49:15.174715416 +0200
+++ b/generic/tclTypePath.c 2026-10-15 13:49:15.176280643 +0200
@@ -1145,6 +1145,61 @@
return rc;
}
+/*
+ * bitarray(n, ordre) : un indice i désigne un bit ; lsb0 : i = 0 est le bit
+ * de droite, msb0 : i = 0 est le bit de gauche. BitsPos rend la position
+ * correspondante depuis le poids faible ; BitsSlice transforme i:j en
+ * (lo, len) depuis le poids faible.
+ */
+static int
+BitsPos(Tcl_Interp *interp, const TclType *tp, const char *name,
+ Tcl_Size *loPtr)
+{
+ Tcl_Size i;
+
+ if (ListIndex(interp, name, tp->bitWidth, &i) != TCL_OK) {
+ return TCL_ERROR;
+ }
+ if (i < 0 || i >= tp->bitWidth) {
+ PathErr(interp, "bit index \"%s\" out of range for %s", name,
+ tp->name);
+ return TCL_ERROR;
+ }
+ *loPtr = tp->bitMsb0 ? tp->bitWidth - 1 - i : i;
+ return TCL_OK;
+}
+
+static int
+BitsSlice(Tcl_Interp *interp, const TclType *tp, const char *from,
+ const char *to, Tcl_Size *loPtr, Tcl_Size *lenPtr)
+{
+ Tcl_Size i, j;
+
+ if (ListIndex(interp, from, tp->bitWidth, &i) != TCL_OK
+ || ListIndex(interp, to, tp->bitWidth, &j) != TCL_OK) {
+ return TCL_ERROR;
+ }
+ if (i < 0 || j >= tp->bitWidth || i > j) {
+ PathErr(interp, "bit range \"%s:%s\" is empty or out of range",
+ from, to);
+ return TCL_ERROR;
+ }
+ *loPtr = tp->bitMsb0 ? tp->bitWidth - 1 - j : i;
+ *lenPtr = j - i + 1;
+ return TCL_OK;
+}
+
+/* Le type d'une tranche de len bits du même sens que tp. */
+static TclType *
+BitsSliceType(Tcl_Interp *interp, const TclType *tp, Tcl_Size len)
+{
+ char text[64];
+
+ snprintf(text, sizeof(text), "bitarray(%lld,%s)", (long long) len,
+ tp->bitMsb0 ? "msb0" : "lsb0");
+ return TclTypeParse(interp, text, (Tcl_Size) strlen(text));
+}
+
/* --------------------------------------------------------------------
* Liens var : une case de type var.T contient le NOM d'une variable. La
* lecture traverse le lien (la variable est lue à chaque fois : la valeur
@@ -1309,6 +1364,28 @@
}
return res;
}
+ case TCLT_INT:
+ if (tp->bitWidth > 0) {
+ /* un bit : un entier 0 ou 1 */
+
+ Tcl_Obj *bit;
+ Tcl_Size lo;
+
+ if (quoted) {
+ PathErr(interp, "a bitarray can't be indexed with the key "
+ "'%s'%s", name, "");
+ return NULL;
+ }
+ if (BitsPos(interp, tp, name, &lo) != TCL_OK
+ || TclBitsGet(interp, v, lo, 1, &bit) != TCL_OK) {
+ return NULL;
+ }
+ elem = TclNewAnnotated(interp, TclTypeParse(interp, "int", 3), -1,
+ bit);
+ TclDiscard(bit);
+ return elem;
+ }
+ /* FALLTHRU */
default:
PathErr(interp, "can't index a value of type %s with \"%s\"",
tp->name, name);
@@ -1320,6 +1397,7 @@
typedef struct Items {
TclObjVec vec;
int multi;
+ int bitSlice; /* a:b sur un bitarray : UN entier, pas une liste */
} Items;
static void
@@ -1339,6 +1417,26 @@
int variant;
Tcl_Size n, i, lo, hi;
+ if (TclAnnotInfo(a, &v, &tp, &variant, NULL) == 1
+ && tp->kind == TCLT_INT && tp->bitWidth > 0) {
+ /* tranche de bits b(i:j) : un bitarray de j-i+1 bits */
+
+ Tcl_Obj *bits, *w;
+ Tcl_Size blo, blen;
+
+ if (BitsSlice(interp, tp, from, to, &blo, &blen) != TCL_OK
+ || TclBitsGet(interp, v, blo, blen, &bits) != TCL_OK) {
+ return TCL_ERROR;
+ }
+ w = TclNewAnnotated(interp, BitsSliceType(interp, tp, blen), -1, bits);
+ TclDiscard(bits);
+ if (w == NULL) {
+ return TCL_ERROR;
+ }
+ TclVecAdd(&out->vec, w);
+ out->bitSlice = 1;
+ return TCL_OK;
+ }
if (TclAnnotInfo(a, &v, &tp, &variant, NULL) != 1
|| tp->kind != TCLT_LIST) {
PathErr(interp, "a range needs a list value, not \"%s\"%s",
@@ -1412,6 +1510,9 @@
return TCL_ERROR;
}
rc = TCL_OK;
+ if (part.bitSlice) {
+ out->bitSlice = 1;
+ }
for (k = 0; k < part.vec.n && rc == TCL_OK; k++) {
rc = ReadSteps(interp, part.vec.v[k], p, i + 1, out);
}
@@ -1472,7 +1573,7 @@
out.vec.v[k] = target;
}
}
- if (!out.multi && out.vec.n == 1) {
+ if ((!out.multi || out.bitSlice) && out.vec.n == 1) {
*resultPtr = out.vec.v[0];
Tcl_IncrRefCount(*resultPtr);
} else {
@@ -1551,6 +1652,40 @@
return TclNewAnnotated(interp, tp, variant, v);
}
+ /* bitarray : écriture d'un bit b(i) ou d'une tranche b(i:j) */
+ if (tp->kind == TCLT_INT && tp->bitWidth > 0) {
+ Tcl_Obj *nv;
+ Tcl_Size lo, len;
+
+ if (!last || step->kind == IS_KEY || step->kind == IS_TAG) {
+ PathErr(interp, "can't assign into a bit of %s with \"%s\"",
+ tp->name, name);
+ return NULL;
+ }
+ if (step->kind == IS_RANGE) {
+ if (BitsSlice(interp, tp, name, Tcl_GetString(step->b), &lo, &len)
+ != TCL_OK) {
+ return NULL;
+ }
+ } else {
+ len = 1;
+ if (BitsPos(interp, tp, name, &lo) != TCL_OK) {
+ return NULL;
+ }
+ }
+ if (TclBitsPut(interp, v, lo, len, val, &nv) != TCL_OK) {
+ return NULL;
+ }
+ res = TclNewAnnotated(interp, tp, -1, nv);
+ TclDiscard(nv);
+ return res;
+ }
+ if (step->kind == IS_RANGE) {
+ PathErr(interp, "only simple names can be assigned, not ranges or "
+ "tags%s%s", "", "");
+ return NULL;
+ }
+
/* 1. Repérer la case. */
switch (tp->kind) {
case TCLT_PRODUCT:
@@ -1750,6 +1885,12 @@
}
for (i = 0; i < set.n; i++) {
for (j = 0; j < set.path[i].n; j++) {
+ /* une tranche a:b est admise en dernier pas : elle vise un
+ * bitarray (SetIn refuse les autres conteneurs) */
+ if (set.path[i].step[j].kind == IS_RANGE
+ && j == set.path[i].n - 1) {
+ continue;
+ }
if (set.path[i].step[j].kind > IS_KEY) {
PathErr(interp, "only simple names can be assigned, not "
"ranges or tags%s%s", "", "");
diff -u a/generic/tclTypes.c b/generic/tclTypes.c
--- a/generic/tclTypes.c 2026-10-15 13:49:15.158641984 +0200
+++ b/generic/tclTypes.c 2026-10-15 13:49:15.164639419 +0200
@@ -621,6 +621,171 @@
return type == TCL_NUMBER_INT || type == TCL_NUMBER_BIG;
}
+/* --------------------------------------------------------------------
+ * bitarray(n, ordre) : lecture et écriture de bits dans un entier
+ * (positions comptées depuis le poids faible, 0 = bit de droite).
+ * -------------------------------------------------------------------- */
+
+/* Les opérations mp_* échouent seulement faute de mémoire. */
+#define MPOK(call) \
+ do { \
+ if ((call) != MP_OKAY) { \
+ Tcl_Panic("out of memory in bitarray operation"); \
+ } \
+ } while (0)
+
+/* (value >> lo) mod 2^len, entier non négatif. */
+int
+TclBitsGet(
+ Tcl_Interp *interp,
+ Tcl_Obj *value,
+ Tcl_Size lo,
+ Tcl_Size len,
+ Tcl_Obj **resultPtr)
+{
+ mp_int big, q, r;
+
+ if (Tcl_GetBignumFromObj(interp, TCL_ANNOT_CONTENT(value), &big)
+ != TCL_OK) {
+ return TCL_ERROR;
+ }
+ MPOK(mp_init(&q));
+ MPOK(mp_init(&r));
+ MPOK(mp_div_2d(&big, (int) lo, &q, NULL));
+ MPOK(mp_mod_2d(&q, (int) len, &r));
+ mp_clear(&big);
+ mp_clear(&q);
+ *resultPtr = Tcl_NewBignumObj(&r); /* r est repris */
+ return TCL_OK;
+}
+
+/* value avec les len bits à partir de lo remplacés par bits (0 <= bits <
+ * 2^len, sinon erreur). */
+int
+TclBitsPut(
+ Tcl_Interp *interp,
+ Tcl_Obj *value,
+ Tcl_Size lo,
+ Tcl_Size len,
+ Tcl_Obj *bits,
+ Tcl_Obj **resultPtr)
+{
+ mp_int big, nb, cur, tmp, res;
+ int fits;
+
+ if (Tcl_GetBignumFromObj(interp, TCL_ANNOT_CONTENT(value), &big)
+ != TCL_OK) {
+ return TCL_ERROR;
+ }
+ if (Tcl_GetBignumFromObj(interp, TCL_ANNOT_CONTENT(bits), &nb)
+ != TCL_OK) {
+ mp_clear(&big);
+ return TCL_ERROR;
+ }
+ fits = !mp_isneg(&nb) && (Tcl_Size) mp_count_bits(&nb) <= len;
+ if (!fits) {
+ ERR(interp, "BITS", "value \"%s\" does not fit in %" TCL_SIZE_MODIFIER
+ "d bit%s", Tcl_GetString(bits), len, (len == 1) ? "" : "s");
+ mp_clear(&big);
+ mp_clear(&nb);
+ return TCL_ERROR;
+ }
+ MPOK(mp_init(&cur));
+ MPOK(mp_init(&tmp));
+ MPOK(mp_init(&res));
+ MPOK(mp_div_2d(&big, (int) lo, &tmp, NULL));
+ MPOK(mp_mod_2d(&tmp, (int) len, &cur)); /* anciens bits */
+ MPOK(mp_mul_2d(&cur, (int) lo, &tmp)); /* ... à leur place */
+ MPOK(mp_sub(&big, &tmp, &res)); /* effacés */
+ MPOK(mp_mul_2d(&nb, (int) lo, &tmp)); /* nouveaux bits à leur place */
+ MPOK(mp_add(&res, &tmp, &cur));
+ mp_clear(&big);
+ mp_clear(&nb);
+ mp_clear(&tmp);
+ mp_clear(&res);
+ *resultPtr = Tcl_NewBignumObj(&cur); /* cur est repris */
+ return TCL_OK;
+}
+
+/* popcount(x) : nombre de bits à 1 d'un entier >= 0. */
+static int
+PopcountCmd(
+ TCL_UNUSED(void *),
+ Tcl_Interp *interp,
+ Tcl_Size objc,
+ Tcl_Obj *const objv[])
+{
+ mp_int big, q;
+ Tcl_WideInt count = 0;
+
+ if (objc != 2) {
+ Tcl_WrongNumArgs(interp, 1, objv, "value");
+ return TCL_ERROR;
+ }
+ if (Tcl_GetBignumFromObj(interp, TCL_ANNOT_CONTENT(objv[1]), &big)
+ != TCL_OK) {
+ return TCL_ERROR;
+ }
+ if (mp_isneg(&big)) {
+ mp_clear(&big);
+ Tcl_SetObjResult(interp, Tcl_NewStringObj(
+ "popcount needs a non-negative integer", TCL_AUTO_LENGTH));
+ Tcl_SetErrorCode(interp, "TCL", "TYPE", "BITS", (char *) NULL);
+ return TCL_ERROR;
+ }
+ MPOK(mp_init(&q));
+ while (!mp_iszero(&big)) {
+ mp_int r;
+ Tcl_WideUInt w;
+
+ MPOK(mp_init(&r));
+ MPOK(mp_mod_2d(&big, 64, &r));
+ w = mp_get_mag_u64(&r);
+ mp_clear(&r);
+ while (w != 0) {
+ w &= w - 1;
+ count++;
+ }
+ MPOK(mp_div_2d(&big, 64, &q, NULL));
+ mp_exch(&big, &q);
+ }
+ mp_clear(&big);
+ mp_clear(&q);
+ Tcl_SetObjResult(interp, Tcl_NewWideIntObj(count));
+ return TCL_OK;
+}
+
+/* bitlen(x) : largeur d'un bitarray, sinon nombre de bits de |x|. */
+static int
+BitlenCmd(
+ TCL_UNUSED(void *),
+ Tcl_Interp *interp,
+ Tcl_Size objc,
+ Tcl_Obj *const objv[])
+{
+ mp_int big;
+ TclType *tp = NULL;
+ Tcl_WideInt n;
+
+ if (objc != 2) {
+ Tcl_WrongNumArgs(interp, 1, objv, "value");
+ return TCL_ERROR;
+ }
+ if (TclAnnotInfo(objv[1], NULL, &tp, NULL, NULL) == 1
+ && tp->bitWidth > 0) {
+ Tcl_SetObjResult(interp, Tcl_NewWideIntObj(tp->bitWidth));
+ return TCL_OK;
+ }
+ if (Tcl_GetBignumFromObj(interp, TCL_ANNOT_CONTENT(objv[1]), &big)
+ != TCL_OK) {
+ return TCL_ERROR;
+ }
+ n = mp_count_bits(&big);
+ mp_clear(&big);
+ Tcl_SetObjResult(interp, Tcl_NewWideIntObj(n));
+ return TCL_OK;
+}
+
/*
* Garde contre la régression infinie : avec un type récursif comme
* {int}leaf, {list.tree}node, la valeur "x" est une liste d'UN élément, qui
@@ -712,6 +877,23 @@
Tcl_GetString(value));
return TCL_ERROR;
}
+ if (tp->bitWidth > 0) {
+ mp_int big;
+ int fits;
+
+ if (Tcl_GetBignumFromObj(NULL, value, &big) != TCL_OK) {
+ return TCL_ERROR;
+ }
+ fits = !mp_isneg(&big)
+ && (Tcl_Size) mp_count_bits(&big) <= tp->bitWidth;
+ mp_clear(&big);
+ if (!fits) {
+ ERR(interp, "BITS", "value \"%s\" does not fit in %s (%"
+ TCL_SIZE_MODIFIER "d bits)", Tcl_GetString(value),
+ tp->name, tp->bitWidth);
+ return TCL_ERROR;
+ }
+ }
return TCL_OK;
case TCLT_DOUBLE:
if (Tcl_GetDoubleFromObj(NULL, value, &dbl) != TCL_OK) {
@@ -1556,6 +1738,8 @@
into->arity = res->arity;
into->keyed = res->keyed;
into->varTyped = res->varTyped;
+ into->bitWidth = res->bitWidth;
+ into->bitMsb0 = res->bitMsb0;
if (res->nkeys > 0) {
into->keys = (TclDictKey *) Tcl_Alloc(res->nkeys * sizeof(TclDictKey));
into->nkeys = res->nkeys;
@@ -2027,5 +2211,17 @@
Tcl_CreateObjCommand2(interp, "::tcl::mathop::is", IsOpObjCmd, NULL, NULL);
Tcl_CreateObjCommand2(interp, "::tcl::unsupported::declare", DeclareObjCmd,
NULL, NULL);
+ Tcl_CreateObjCommand2(interp, "::tcl::mathfunc::popcount", PopcountCmd,
+ NULL, NULL);
+ Tcl_CreateObjCommand2(interp, "::tcl::mathfunc::bitlen", BitlenCmd,
+ NULL, NULL);
+ /* bitarray(n, ordre) : type paramétré prédéfini. Le corps n'est que
+ * "int" (le commentaire nomme les paramètres) ; la largeur et l'ordre
+ * sont posés par l'instanciation (tclTypeGeneric.c). */
+ if (TclTypesDefineGeneric(NULL, d, "bitarray({int}n, {string}ord = lsb0)",
+ (Tcl_Size) strlen("bitarray({int}n, {string}ord = lsb0)"),
+ "int # n ord", 11) == TCL_OK) {
+ TclTypesMarkBits(d, "bitarray");
+ }
TclAnnotInit(interp);
}
diff -u a/generic/tclTypes.h b/generic/tclTypes.h
--- a/generic/tclTypes.h 2026-10-15 13:49:15.166993362 +0200
+++ b/generic/tclTypes.h 2026-10-15 13:49:15.168560537 +0200
@@ -73,6 +73,10 @@
TclTypeField *fields;
int varTyped; /* VAR : la variable visée doit être typée,
* var(typed).T ; elem est T (string si omis) */
+ Tcl_Size bitWidth; /* INT : bitarray(n, ordre) a n > 0 bits, sinon
+ * 0 */
+ int bitMsb0; /* bitarray : 1 si l'indice 0 est le bit de
+ * gauche (msb0), 0 si de droite (lsb0) */
};
/* Les descripteurs vivent autant que l'interpréteur (pas de comptage). */
@@ -198,6 +202,13 @@
Tcl_Obj *script, Tcl_Obj **resultPtr);
int TclTypeIsPlainString(const TclType *tp);
int TclTypeIsAny(const TclType *tp);
+/* bitarray(n, ordre) : entier de n bits dont on lit/écrit les bits.
+ * Les positions (lo, len) sont toujours celles du poids faible (0 = bit de
+ * droite) ; la conversion depuis l'indice msb0 est faite par l'appelant. */
+int TclBitsGet(Tcl_Interp *interp, Tcl_Obj *value, Tcl_Size lo,
+ Tcl_Size len, Tcl_Obj **resultPtr);
+int TclBitsPut(Tcl_Interp *interp, Tcl_Obj *value, Tcl_Size lo,
+ Tcl_Size len, Tcl_Obj *bits, Tcl_Obj **resultPtr);
int TclTypeDictKeyAllowed(const TclType *tp, const char *key);
/* Valide content puis ajoute les valeurs par défaut des clés manquantes
* (dict(x?=0)). content est un objet PRIVÉ, modifié en place. */
diff -u a/generic/tclTypesInt.h b/generic/tclTypesInt.h
--- a/generic/tclTypesInt.h 2026-10-15 13:49:15.170384660 +0200
+++ b/generic/tclTypesInt.h 2026-10-15 13:49:15.172392826 +0200
@@ -42,6 +42,8 @@
TypeParam *params;
char *body; /* texte du type, avec les paramètres libres */
Tcl_Size bodyLen;
+ int bits; /* 1 : bitarray, l'instance reçoit bitWidth et
+ * bitMsb0 */
} TypeGeneric;
typedef struct TypeData {
@@ -107,6 +109,8 @@
const char *spec, Tcl_Size specLen, const char *body,
Tcl_Size bodyLen);
void TclTypesFreeGenerics(TypeData *d);
+/* Marque le type paramétré NAME comme bitarray. */
+void TclTypesMarkBits(TypeData *d, const char *name);
/* tclTypes.c : services utilisés par le compilateur de types */
TclType *TclTypesNewType(TypeData *d, TclTypeKind kind, const char *name);