Artifact d04cbf589d4e3a5ea9d8406e0c91139a135e5857e21759f794240578352254a4:
- File typeparse/tests_tclsh/t110_canonic_parse.tcl — part of check-in [dd10cbf51b] at 2026-11-29 17:26:47 on branch tip-764_tcl90 — Notes and types together Notes now follow a value at any depth: path writes (set P(y) $n), product fields (construction, $P(x), $P(x,y), $P(*)), and nested paths. Only data notes travel; a pending field label such as {point/x} stays with the constructor. type parse reads notes as data: {mm@src=capteur}1500 no longer fails. Typed collections keep their type on the way out The element type is carried once by the collection and applied when a value leaves it. Elements are not copied, and compact storage is kept. lindex, lsearch -inline, dict get and dict for give a typed element. lrange, lreverse, lsort (including -unique and -command), lsearch -all -inline and dict filter keep the collection type; dict values gives list.T. The comparison command of lsort -command receives typed values, so a {kg} parameter rejects an mm. Canonical text with modes type canonic and type parse share the options -type, -notes, -value, -deep and -list. With no option, they cover everything, at full depth. Element notes and element types under any are written as prefixes, for example {list.mm}{{{@ok}5} 7}. The round trip is exact, checked on randomly generated values. -list returns {type notes value}, ready for lassign or a tuple assignment. type parse -list splits a canonical text by syntax alone, so the type may be unknown to the reader. Reading a canonical text never executes anything. Proposed public C API: Tcl_GetCanonicObj, Tcl_ParseCanonicObj and the TCL_CANON_* flags. Behaviour change: type canonic -notes now returns the notes alone; the old meaning is -type -notes. Core conversions no longer drop annotations A typed value used as a number, a regexp, a script, a command name, an option, a subcommand, a channel, an encoding, a namespace or a clock format keeps its type and notes. Core patch: annot-conversions.patch. New loss detector: ::tcl::unsupported::annotlost or TCL_TYPE_TRACE=lost. Found along the way, a Tcl 9 bug: lsort checked getElementsProc on objv\[1\] instead of the list. Fixes In expr, x is T now agrees with type is: an mm is not a kg, even though both are built on int. An integer typed as double now prints as 7.0. Known limits A typed value used as a variable name loses its annotation. So does a value whose representation is taken over by an extension type. type parse -value on an unknown type leaves the element prefixes in place. Tests 128 script tests pass in 5 modes; valgrind finds no leaks or errors. The fuzzers give identical output across modes. In the full core suite, fileName, http11, socket and tcltest show failures that do not appear when these files run alone. The comparison run with the old version used different settings, so this is not confirmed as environment-only. Next type eval: implicit conversion inside its block, and unit algebra for * and /. (user: Florentis, size: 3466) [annotate] [blame] [check-ins using]
# t110 (v17c) : type canonic ?-notes? VALEUR, type parse TEXTE. # {T}contenu, {T@n1,n2}contenu, {@notes}contenu ; deux lois : # type parse [type canonic $v] ≡ $v ; type canonic [type parse $t] = $t # quand $t est canonique. proc t {nom script} { if {[catch {uplevel 1 $script} r]} {set r "ERREUR $r"} puts [format "%-30s %s" $nom $r] } # la forme canonique, et l'aller-retour vérifié proc ar {v} { set c [type canonic $v] set y [type parse $c] list $c [expr {[type canonic $y] eq $c && [type get $y] eq [type get $v] && [note get $y] eq [note get $v] && $y eq $v ? "≡" : "DIFF"}] } type def num110 {{int}i | {double}d} type def S110 {{int}i, {double}d} type def pt110 {{num110}x; {num110}y} type def opt110 {"none", {int}some} type def Jour110 string namespace eval ::g110 {type def P {{double}x; {double}y}} puts "== scalaires" t "int 0x10" {ar [type cast int 0x10]} t "double 2e1" {ar [type cast double 2e1]} t "grand entier" {ar [type cast int 123456789012345678901234567890]} t "string avec espaces" {ar [type cast string "a b"]} t "string vide" {ar [type cast string ""]} t "nominal" {ar [type cast Jour110 lundi]} t "alias : le type visé" {type alias coord110 double; type canonic [type cast coord110 3]} puts "== conteneurs : le texte reconstruit" t "list.int espacée" {type canonic [type cast list.int "1 2"]} t "list de listes" {ar [type cast list.list.int {{1 2} 3}]} t "dict.int" {ar [type cast dict.int {a 1 b 0x2}]} t "produit nommé" {ar [type cast pt110 {1 2.5}]} t "produit anonyme" {ar [type cast {{int}a; {double}b} {1 2}]} t "type d'un espace" {ar [type cast ::g110::P {1 2}]} t "list d'un type d'espace" {ar [type cast list.::g110::P {{1 2}}]} t "cases (-compact)" {type canonic [type cast {-compact}list.double {1 2}]} puts "== sommes et unions" t "somme : la variante" {ar [type cast S110/d 2]} t "littéral" {ar [type cast opt110 none]} t "union : lue du contenu" {ar [type cast num110 0x1F]} t "list.num110" {ar [type cast list.num110 {1 2.0 0x10}]} puts "== notes" t "une note" {ar {vitesse}[type cast double 5]} t "un ensemble trié" {type canonic [note add zeta [note set alpha [type cast double 5]]]} t "seulement notée" {ar {vitesse}5} t "-type -notes" {type canonic -type -notes {vitesse}[type cast double 5]} t "-type -notes sans note" {type canonic -type -notes [type cast pt110 {1 2}]} t "-notes (v18k)" {type canonic -notes {vitesse}[type cast double 5]} t "-notes sans note (v18k)" {type canonic -notes [type cast pt110 {1 2}]} t "note encodée" {set c [type canonic [type parse {{@a%2Cb}x}]]; list $c [note get [type parse $c]]} t "élément noté normalisé" {type canonic [type cast list.double [list {vitesse}5 6]]} puts "== refus" t "valeur nue" {type canonic 5} t "pas d'accolade" {type parse 5} t "sans contenu" {type parse {{int}}} t "deux mots" {type parse {{int}x y}} t "type inconnu" {type parse {{intt}5}} t "contenu refusé" {type parse {{int}abc}} t "note vide" {type parse {{int@a,,b}5}} t "encodage incomplet" {type parse {{int@%4}5}} t "arguments" {type canonic}