-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtsp-generate-var.tcl
233 lines (190 loc) · 8.41 KB
/
tsp-generate-var.tcl
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# var scope commands
# upvar, global, variable, unset (unset causes error)
#########################################################
# generate code for "unset" command (assumed to be first parse word)
# currently, this causes a compile error
# note that if programmer really wants unset, use "::unset varName ?... varName?"
#
proc ::tsp::gen_command_unset {compUnitDict tree} {
upvar $compUnitDict compUnit
::tsp::addError compUnit "unset command currently not supported"
return [list void "" ""]
}
#########################################################
# generate code for "upvar" command (assumed to be first parse word)
# pick out variables that will need to be loaded on proc entry, and
# spilled on proc exit. actual upvar command is passed to ::tsp::gen_direct_tcl
# return list of: type rhsVarName code
#
proc ::tsp::gen_command_upvar {compUnitDict tree} {
upvar $compUnitDict compUnit
set len [llength $tree]
if {$len < 3} {
::tsp::addError compUnit "wrong # args: should be \"upvar ?level? othervar localVar ?otherVar localVar?\""
return [list void "" ""]
}
if {$len % 2 == 0} {
# contains level option
set levelComponent [lindex [::tsp::parse_word compUnit [lindex $tree 1]] 0]
lassign $levelComponent type rawtext text
if {$type ne "text" || ![regexp {(#)?\d+} $text]} {
::tsp::addError compUnit "can't parse \"upvar ?level?\" as valid level specifier"
return [list void "" ""]
}
set idx 2
} else {
set idx 1
}
append code "\n/***** ::tsp::gen_command_upvar */\n"
# generate the code to call the command
set directResult [::tsp::gen_direct_tcl compUnit $tree]
append code [lindex $directResult 2]
set upvared [list]
# check that local variables are defined, if not define them as var
# generate code to get vars from interp after the real upvar command code
foreach {otherVarComponent localVarComponent} [lrange $tree $idx end] {
lassign [lindex [::tsp::parse_word compUnit $otherVarComponent] 0] typeOther rawOther otherVar
lassign [lindex [::tsp::parse_word compUnit $localVarComponent] 0] typeLocal rawLocal localVar
if {$typeLocal ne "text"} {
::tsp::addError compUnit "upvar local \"$rawLocal\" not a text var name"
return [list void "" ""]
}
set type [::tsp::getVarType compUnit $localVar]
if {$type eq "undefined"} {
if {[::tsp::isProcArg compUnit $localVar]} {
::tsp::addError compUnit "proc argument variable \"$localVar\" not previously defined"
return [list void "" ""]
} elseif {[::tsp::isValidIdent $localVar]} {
::tsp::addWarning compUnit "variable \"${localVar}\" implicitly defined as type: \"var\" (upvar)"
::tsp::setVarType compUnit $localVar var
set type var
} else {
::tsp::addError compUnit "invalid identifier: \"$localVar\""
return [list void "" ""]
}
}
lappend upvared $localVar
}
if {[llength $upvared] > 0} {
append code "\n/**** load upvared: $upvared */\n"
append code [::tsp::lang_load_vars compUnit $upvared 1]
}
# add upvared variables to the finalSpill
set existing [dict get $compUnit finalSpill]
foreach var $upvared {
if {[lsearch $existing $var] == -1} {
dict lappend compUnit finalSpill $var
}
}
return [list void "" $code]
}
#########################################################
# generate code for "global" command (assumed to be first parse word)
# pick out variables that will need to be loaded on proc entry, and
# spilled on proc exit. actual global command is passed to ::tsp::gen_direct_tcl
# return list of: type rhsVarName code
#
proc ::tsp::gen_command_global {compUnitDict tree} {
upvar $compUnitDict compUnit
set len [llength $tree]
if {$len < 2} {
::tsp::addError compUnit "wrong # args: should be \"global varName ?varName?\""
return [list void "" ""]
}
append code "\n/***** ::tsp::gen_command_global */\n"
# generate the code to call the command
set directResult [::tsp::gen_direct_tcl compUnit $tree]
append code [lindex $directResult 2]
set upvared [list]
# check that local variables are defined, if not define them as var
# generate code to get vars from interp after the real global command code
foreach {localVarComponent} [lrange $tree 1 end] {
lassign [lindex [::tsp::parse_word compUnit $localVarComponent] 0] typeLocal rawLocal localVar
if {$typeLocal ne "text"} {
::tsp::addError compUnit "global \"$rawLocal\" not a text var name"
return [list void "" ""]
}
set type [::tsp::getVarType compUnit $localVar]
if {$type eq "undefined"} {
if {[::tsp::isProcArg compUnit $localVar]} {
::tsp::addError compUnit "proc argument variable \"$localVar\" not previously defined"
return [list void "" ""]
} elseif {[::tsp::isValidIdent $localVar]} {
::tsp::addWarning compUnit "variable \"${localVar}\" implicitly defined as type: \"var\" (global)"
::tsp::setVarType compUnit $localVar var
set type var
} else {
::tsp::addError compUnit "invalid identifier: \"$localVar\""
return [list void "" ""]
}
}
lappend upvared $localVar
}
if {[llength $upvared] > 0} {
append code "\n/* load global: $upvared */\n"
append code [::tsp::lang_load_vars compUnit $upvared 1]
}
# add upvared variables to the finalSpill
set existing [dict get $compUnit finalSpill]
foreach var $upvared {
if {[lsearch $existing $var] == -1} {
dict lappend compUnit finalSpill $var
}
}
return [list void "" $code]
}
#########################################################
# generate code for "variable" command (assumed to be first parse word)
# pick out variables that will need to be loaded on proc entry, and
# spilled on proc exit. actual variable command is passed to ::tsp::gen_direct_tcl
# return list of: type rhsVarName code
#
proc ::tsp::gen_command_variable {compUnitDict tree} {
upvar $compUnitDict compUnit
set len [llength $tree]
if {$len < 2} {
::tsp::addError compUnit "wrong # args: should be \"variable ?name value? name ?value?\""
return [list void "" ""]
}
append code "\n/***** ::tsp::gen_command_variable */\n"
# generate the code to call the command
set directResult [::tsp::gen_direct_tcl compUnit $tree]
append code [lindex $directResult 2]
set upvared [list]
# check that local variables are defined, if not define them as var
# generate code to get vars from interp after the real variable command code
foreach {localVarComponent valueComponent} [lrange $tree 1 end] {
lassign [lindex [::tsp::parse_word compUnit $localVarComponent] 0] typeLocal rawLocal localVar
if {$typeLocal ne "text"} {
::tsp::addError compUnit "variable local \"$rawLocal\" not a text var name"
return [list void "" ""]
}
set type [::tsp::getVarType compUnit $localVar]
if {$type eq "undefined"} {
if {[::tsp::isProcArg compUnit $localVar]} {
::tsp::addError compUnit "proc argument variable \"$localVar\" not previously defined"
return [list void "" ""]
} elseif {[::tsp::isValidIdent $localVar]} {
::tsp::addWarning compUnit "variable \"${localVar}\" implicitly defined as type: \"var\" (variable)"
::tsp::setVarType compUnit $localVar var
set type var
} else {
::tsp::addError compUnit "invalid identifier: \"$localVar\""
return [list void "" ""]
}
}
lappend upvared $localVar
}
if {[llength $upvared] > 0} {
append code "\n/**** load variable: $upvared */\n"
append code [::tsp::lang_load_vars compUnit $upvared 1]
}
# add upvared variables to the finalSpill
set existing [dict get $compUnit finalSpill]
foreach var $upvared {
if {[lsearch $existing $var] == -1} {
dict lappend compUnit finalSpill $var
}
}
return [list void "" $code]
}