-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvarx-1.0.1.tm
179 lines (158 loc) · 4.97 KB
/
varx-1.0.1.tm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package require ensembled
namespace eval ::varx {ensembled}
proc ::varx::variables args {
::uplevel 1 [::list ::foreach @@_var_@@ $args {
::variable ${@@_var_@@}
}]
}
proc ::varx::define args {
::set defined [list]
::foreach var $args {
::upvar 1 $var v
::if {![::info exists v]} {
::set v {}
::lappend defined $var
}
}
::return $defined
}
proc ::varx::sets args {
::foreach {var val} $args {
::upvar 1 $var ref
::set ref $val
}
::return $args
}
proc ::varx::switch args {
::set nocase 0; ::set next 0; ::set response {}
::while {$args ne {}} {
set args [lassign $args arg]
::if { ! [::string equal [::string index $arg 0] -] } {
::if { ! [::info exists var] } {
::set var $arg
} else {
::set args [::concat $arg $args]
::break
}
} else {
::switch -- $arg {
-- { ::break }
-nocase { ::set nocase 1 }
-upvar { ::set upvar 1 }
}
}
}
::if {[::info exists upvar]} {
::upvar 1 $var val
} else { ::set val $var }
::foreach { pattern response } $args {
::if { $next } {
::if { $response eq "-" } { ::continue } else {
::set val $response
::return $response
}
}
::if { $nocase ? [::string match -nocase $pattern $val] : [::string match $pattern $val] } {
::if { $response eq "-" } { ::set next 1 ; ::continue }
::set val $response
::return $response
}
}
::return
}
# proc myproc { new prev name } {
# puts "$name | $prev --> $new" ; # v | 1 --> 2
# }
# set v 1
# ::varx trace v myproc
# set v 2
proc ::varx::vtrace { var callback {value {}} } {
::upvar 1 $var current
::if {![::info exists current]} {::set current $value}
::uplevel 1 [::list ::trace add variable $var write [ ::namespace code [::list traceback $callback $current] ]]
}
proc ::varx::traceback { callback prev var args } {
::upvar 1 $var value
::uplevel 1 [::list ::trace remove variable $var write [ ::namespace code [::list traceback $callback $prev ] ]]
::uplevel 1 [::list ::trace add variable $var write [ ::namespace code [::list traceback $callback $value ] ]]
::set a [::info args [::lindex $callback 0]]
::if { "args" in $a } {
::set args [::list $value $prev $var]
} else {
::switch -- [::llength $a] {
0 { ::set args {} }
1 { ::set args [::list $value] }
2 { ::set args [::list $value $prev] }
3 { ::set args [::list $value $prev $var] }
default { ::throw error {wrong # args: should be "", "value", "value prevValue", or "value prevValue varName"} }
}
}
::uplevel #0 [::list try [list $callback {*}$args]]
}
proc ::varx::untrace { var } {
::upvar 1 $var $var
::set info [::trace info variable $var]
::if { $info eq {} } { ::return }
::uplevel 1 [::list ::trace remove variable $var {*}[::lindex $info 0]]
}
proc ::varx::pipe args {
::set cmds [::split $args |]
::foreach cmd $cmds {
::set cmd [::string trim $cmd]
::set s [::string last -> $cmd]
::if { $s != -1 } {
::upvar 1 ___setter setter
::set setter [ ::string trim [ ::string range $cmd $s+2 end ] ]
::set cmd [ ::string trim [ ::string range $cmd 0 $s-1 ] ]
::if { [::llength $cmd] == 1 } { ::set cmd [::lindex $cmd 0] }
::set cmd [ ::list ::try [ ::list ::set $setter [::namespace current]::$cmd ] ]
::uplevel 1 $cmd
::uplevel 1 {
::set ${___setter} [::try [::set ${___setter}] ]
::unset ___setter
}
} else {
::if { [::llength $cmd] == 1 } { ::set cmd [::lindex $cmd 0] }
::uplevel 1 [ ::list ::try [::namespace current]::$cmd ]
}
}
}
proc ::varx::alias { var alias } { ::uplevel 1 [::list ::upvar 0 $var $alias] }
proc ::varx::empty { varName args } {
::upvar 1 $varName var
::tailcall ::if [::expr {![::info exists var] || $var eq {}}] {*}$args
}
proc ::varx::null {varName args} {
::upvar 1 $varName var
::tailcall ::if [::expr {![::info exists var]}] {*}$args
}
# ::varx true 0 {
# puts "is false"
# } else {
# puts "not false"
# }
proc ::varx::false {varName args} {
::upvar 1 $varName var
::tailcall ::if [::expr {[::info exists var] && [::string is false -strict $var]}] {*}$args
}
# ::varx true 1 {
# puts "is true"
# } else {
# puts "not true"
# }
proc ::varx::true {varName args} {
::upvar 1 $varName var
::tailcall ::if [::expr {[::info exists var] && [::string is true -strict $var]}] {*}$args
}
proc ::varx::is? {varName check} {
::upvar 1 $varName $varName
::switch -- $check {
true { return [::expr { [::info exists $varName] && [::string is true -strict [::set $varName]]}] }
false { return [::expr { [::info exists $varName] && [::string is false -strict [::set $varName]]}] }
null { return [::info exists $varName] }
empty { return [::expr { ! [::info exists $varName] || [::set $varName] eq {} }] }
}
}
proc ::varx::is {varName checks args} {
::upvar 1 $varName $varName
}