Skip to content
Tcl Source Code Artifact [125a79455e]
Login

Artifact [125a79455e]

Artifact 125a79455e06db0d460dacc1884a84961b1d7170a11969f4dfcbd2bd7d7ca227:


# v18k : les notes à travers les types (chemins, produits, collectes, parse)
proc t {label script} {
    if {[catch {uplevel 1 $script} r]} {set r "ERREUR $r"}
    puts [format "%-36s %s" $label $r]
}
type def mm int
type def pt {{mm}x; {mm}y}
type def point {{double}x; {double}y}
set n [note set src=capteur [type cast mm 5]]
set o [note set ok [type cast mm 1500]]

puts "== chemins"
t "set L(0) \$n"                    {set {list.mm}L 1 2; set L(0) $n; list $L [note get $L(0)] [type get $L(0)]}
t "lset L 1 \$n"                    {set {list.mm}L 1 2; lset L 1 $n; note get [lindex $L 1]}
t "set LP(1.x) \$o"                 {set {list.pt}LP {1 2} {3 4}; set LP(1.x) $o; list [note get $LP(1.x)] [note get $LP(0.x)]}
t "écraser par une valeur nue"      {set {list.mm}L $n 2; set L(0) 9; note get $L(0)}

puts "== produits"
t "constructeur positionnel"        {set {pt}P $o $n; list [note get $P(x)] [note get $P(y)]}
t "constructeur nom/valeur"         {set {pt}P {/y}$n {/x}1; list [note get $P(x)] [note get $P(y)]}
t "écriture d'un champ"             {set {pt}P 1 2; set P(y) $n; list $P [note get $P(y)] [note get $P(x)]}
t "lecture multiple P(x,y)"         {set {pt}P $o $n; lmap v $P(x,y) {note get $v}}
t "lecture P(*)"                    {set {pt}P $o 2; lmap v $P(*) {note get $v}}
t "étiquette qualifiée, pas une note" {set {point}q {point/x}1 {point/y}2; list $q [note get $q(x)]}
t "dans un proc"                    {apply {{} {set {pt}Q $::o 2; set Q(y) $::n; list [note get $Q(x)] [note get $Q(y)]}}}

puts "== écriture canonique des éléments notés"
t "list.double"                     {set {list.double}L [note set a 1] 2; list $L [note get [lindex $L 0]]}
t "list.int"                        {set {list.int}L [note set a 0x10] 2; list $L [note get [lindex $L 0]]}
t "point"                           {set {point}q [note set a 1] 2; set q(y) [note set b 3]; list $q [note get $q(x)] [note get $q(y)]}

puts "== type parse : les notes sont des données"
t "{mm@src=capteur}"                {set v [type parse {{mm@src=capteur}1500}]; list $v [type get $v] [note get $v]}
t "{mm@ok,src%3Dcapteur}"           {set v [type parse {{mm@ok,src%3Dcapteur}1500}]; list [type get $v] [note get $v]}
t "{@src%3Dcapteur} (sans type)"    {set v [type parse {{@src%3Dcapteur}x}]; list [type get $v] [note get $v]}
t "arbre {[email protected],c}"                {note get [type parse {{mm@a.(b,c)}1}]}
t "aller-retour canonic"            {set c [type canonic $o]; list $c [note get [type parse $c]] [type canonic [type parse $c]]}
t "aller-retour, valeur avec virgule" {set c [type canonic [type parse {{@%22a%2Cb%22}x}]]; list $c [note get [type parse $c]]}

puts "== conversions du noyau : le type et les notes restent (annotlost)"
::tcl::unsupported::annotlost on
set cmds {
 {binary format w $x} {binary format i $x} {binary format a* $s} {expr {wide($x)}}
 {format %u $x} {format %c $x} {expr {$x ** 2}} {expr {int($d)}} {expr {round($d)}}
 {binary format q $d} {string index abc end-$x} {lrange {a b c d} end-$x end}
 {lseq $x} {string repeat a $x} {clock format $x -format %s} {dict incr ii k $x}
 {lsort -integer [list $x 3]} {string trim $s} {split $s ""} {encoding convertto utf-8 $s}
 {string compare -length $x $s ab} {tcl::mathop::+ $x $x} {for {set j 0} {$j < $x} {incr j} {}}
}
# v18k : une chaîne typée qui sert de motif, de script, de nom de commande,
# d'option, de sous-commande, de canal, d'encodage, d'espace de noms
set cmds2 {
 {regexp $s abc} {regsub $s abc x} {switch -regexp abc $s {} default {}} {lsearch -regexp {abc} $s}
 {eval $sc} {if 1 $sc} {uplevel #0 $sc} {expr $se} {$sl a}
 {lsort $so {b a}} {string $ss abc} {flush $sch} {encoding convertto $sen a}
 {namespace exists $sns} {clock format 0 -format $sf -gmt 1}
}
proc balaye {cmds} {
    set bad {}
    foreach c $cmds {
	set {mm}x [note set ok [type cast mm 1]]
	set {double}d [note set ok [type cast double 1.5]]
	set {string}s [note set ok [type cast string a]]
	foreach {v t} {sc {set z 1} se {1+2} sl list so -increasing ss length
		sch stdout sen utf-8 sns ::tcl sf %Y} {
	    set {string}$v [note set ok [type cast string $t]]
	}
	catch {eval $c}
	set l [::tcl::unsupported::annotlost get]
	foreach v {x d s sc se sl so ss sch sen sns sf} {
	    if {[type get [set $v]] eq "" || [note get [set $v]] eq ""} {lappend l $v}
	}
	if {$l ne ""} {lappend bad $c}
    }
    return [expr {$bad eq "" ? "[llength $cmds] commandes, rien de perdu" : "perdu : $bad"}]
}
t "global"                          {balaye $cmds}
t "dans un proc"                    {apply {{c} {balaye $c}} $cmds}
t "motifs, scripts, noms"           {balaye $cmds2}
t "noms, dans un proc"              {apply {{c} {balaye $c}} $cmds2}
t "limite : nom de variable (proc)" {apply {{} {set {string}vn [note set ok [type cast string zz]]; set zz 1; set $vn; list [type get $vn] [llength [::tcl::unsupported::annotlost get]]}}}
t "repli : copie nue"               {apply {{} {set {string}vn [note set ok [type cast string zz]]; set zz 1; set [type canonic -value $vn]; list [type get $vn] [note get $vn] [llength [::tcl::unsupported::annotlost get]]}}}
t "string cat ne copie pas"         {apply {{} {set {string}vn [note set ok [type cast string zz]]; set zz 1; set [string cat $vn]; list [type get $vn] [llength [::tcl::unsupported::annotlost get]]}}}
::tcl::unsupported::annotlost off

puts "== ce qui sort d'une collection typée garde son type (v18k)"
set {list.mm}M 5 [note set z [type cast mm 6]] 7
set {dict.mm}D a 3 b [note set ok [type cast mm 4]]
foreach {l c} {
    lrange {lrange $M 0 1}  lsort {lsort $M}  lsort-unique {lsort -unique $M}
    lsort-command {lsort -command {apply {{a b} {expr {$a - $b}}}} $M}
    lreverse {lreverse $M}  lsearch-all-inline {lsearch -all -inline $M *}
    lsearch-inline {lsearch -inline $M 6}  lsort-indices {lsort -indices $M}
    {dict get} {dict get $D b}  {dict values} {dict values $D}
    {dict filter key} {dict filter $D key a}  {dict filter value} {dict filter $D value 4}
    {dict filter script} {dict filter $D script {k v} {expr {$v > 3}}}
} {
    t $l [list apply {{c} {set r [uplevel 1 $c]; list [type get $r] [note get $r]}} $c]
}
t "élément noté après lsort"        {set e [lindex [lsort $M] 1]; list [type get $e] [note get $e]}
t "dict for"                        {set r {}; dict for {k v} $D {lappend r $k [type get $v] [note get $v]}; set r}
t "dict get compilé"                {apply {{D} {set v [dict get $D b]; list [type get $v] [note get $v]}} $D}
t "le comparateur reçoit des mm"    {lsort -command {apply {{a b} {if {[type get $a] ne "mm"} {error "pas mm"}; expr {$a - $b}}}} $M}
t "comparateur {kg} : refusé"       {type def kg int; proc {int}cmpkg {{kg}a; {kg}b} {expr {$a - $b}}; lsort -command cmpkg $M}
t "nominal (poly) : tranche gardée" {type def poly list.double; set {poly}p 1 2 3; set p [lrange $p 1 end]; list [type get $p] $p}
t "longueur fixe : list.T"          {set {list(3).double}f 1 2 3; type get [lrange $f 0 1]}
t "lrange puis lappend : calcul"    {set x [lrange $M 0 1]; lappend x 9; list [type get $x] $x}

puts "== x is T dans expr suit type is (v18k : un mm n'est pas un kg)"
type def poly2 list.double
set {mm}va 5; set {kg}vc 3; set {int}vi 2; set vn 7; set {poly2}vp 1 2
foreach {v T} {vc mm va kg va mm va int vi mm vn mm vn kg vi double vp poly2 vp list.double va any} {
    t "$v is $T" [list apply {{v T} {upvar 1 $v x; list [expr "x is $T"] [type is $T $x]}} $v $T]
}

puts "== accumulateur typé : la boucle cumulée reste compilée (v18k)"
type def pos147 int(($>=0))
set L147 [lrepeat 100 3]
proc acc {T init L body} {
    proc acc0 {L} "set {$T}s $init
	if {\[catch {foreach x \$L {$body}} e\]} {return \"ERREUR \$e\"}
	list \$s \[type get \$s\] \[note get \$s\]"
    acc0 $L
}
foreach {lab T init body} {
    double        double {0.0}                 {set s [expr {$s + $x}]}
    {mm nominal}  mm     0                     {set s [expr {$s + $x}]}
    {int, doubles} int   0                     {set s [expr {$s + $x * 0.5}]}
    {double noté} double {[note set ok 0.0]}   {set s [expr {$s + $x}]}
    {prédicat}    pos147 10                    {set s [expr {$s - $x}]}
} {
    ::tcl::unsupported::mapreport clear
    ::tcl::unsupported::mapreport on
    set r [acc $T $init $L147 $body]
    set rep [::tcl::unsupported::mapreport get]
    ::tcl::unsupported::mapreport off
    set pass [expr {[string match "*pass fold*" $rep] ? "compilée" : "ordinaire"}]
    set env(TCL_TYPE_NOVECLOOP) 1
    set r0 [acc $T $init $L147 $body]
    unset env(TCL_TYPE_NOVECLOOP)
    t $lab [list list $pass $r [expr {$r eq $r0 ? "= boucle ordinaire" : "DIFFÉRENT : $r0"}]]
}