-
Notifications
You must be signed in to change notification settings - Fork 6
/
tsp-generate-string.tcl
410 lines (342 loc) · 14.2 KB
/
tsp-generate-string.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# string commands
# append, scan, format, string
#########################################################
# generate code for "append" command (assumed to be first parse word)
# varName must be a string or var type; int, boolean, double cause compile error
# return list of: type rhsVarName code
#
proc ::tsp::gen_command_append {compUnitDict tree} {
upvar $compUnitDict compUnit
if {[llength $tree] < 2} {
::tsp::addError compUnit "wrong # args: should be \"append varName ?value value ...?\""
return [list void "" ""]
}
# varname must be text, must exists
set varComponent [lindex [::tsp::parse_word compUnit [lindex $tree 1]] 0]
lassign $varComponent type rawtext varname
if {$type ne "text"} {
::tsp::addError compUnit "append varName not a text word: \"$rawtext\""
return [list void "" ""]
}
set type [::tsp::getVarType compUnit $varname]
if {$type eq "undefined"} {
::tsp::addError compUnit "append varName is not defined"
return [list void "" ""]
}
if {$type eq "array" || $type eq "boolean" || $type eq "int" || $type eq "double"} {
::tsp::addError compUnit "append varName must be type string or var, defined as : $type"
return [list void "" ""]
}
set pre [::tsp::var_prefix $varname]
set code "\n/***** ::tsp::gen_command_append */\n"
if {$type eq "var"} {
# note - this is dead code for now, since we invoke gen_direct_tcl for
# var types now (see a few lines above)
# dup if shared obj, or assign empty var if null
append code [::tsp::lang_dup_var_if_shared $pre$varname]
}
set argVar [::tsp::get_tmpvar compUnit string]
set argVarComponents [list [list text $argVar $argVar]]
foreach node [lrange $tree 2 end] {
# assign arg into a tmp string type
set appendNodeComponents [::tsp::parse_word compUnit $node]
set appendNodeType [lindex [lindex $appendNodeComponents 0] 0]
if {$appendNodeType eq "invalid"} {
::tsp::addError compUnit "append argument parsed as \"$appendNodeType\""
return [list void "" ""]
}
set setTree ""
append code [lindex [::tsp::produce_set compUnit $setTree $argVarComponents $appendNodeComponents] 2]
if {$type eq "var"} {
append code [::tsp::lang_append_var $pre$varname $argVar]
} elseif {$type eq "string"} {
append code [::tsp::lang_append_string $pre$varname $argVar]
} else {
error "::tsp::gen_command_append - unexpected varname type: $type\n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
}
# return the value
return [list $type $pre$varname $code]
}
#########################################################
# generate code for "scan" command (assumed to be first parse word)
# varName must be a string or var type; int, boolean, double cause compile error
# return list of: type rhsVarName code
#
# FIXME: this is probably obsoleted by spill/load checking
proc ::tsp::gen_command_scan {compUnitDict tree} {
upvar $compUnitDict compUnit
if {[llength $tree] < 3} {
::tsp::addError compUnit "wrong # args: should be \"scan string format ?varName varName ...?\""
return [list void "" ""]
}
# FIXME - implements simple cases
# compile scan for simple cases of %d %ld %Ld %f %e %g and one var
set varlist [list]
foreach node [lrange $tree 3 end] {
set nodeComponents [::tsp::parse_word compUnit $node]
set nodeType [lindex $nodeComponents 0 0]
if {[llength $nodeComponents] == 1 && $nodeType eq "text"} {
lassign [lindex $nodeComponents 0] nodeType varname text
if {[::tsp::isValidIdent $varname]} {
set vartype [::tsp::getVarType compUnit $varname]
if {$vartype ne "undefined"} {
# make sure variable will be loaded after command finishes
lappend varlist $varname
} else {
# identifier not defined, bail
}
} else {
# bad identifier, bail
}
} else {
# not simple text, can't be a var we use
}
}
# make sure variable will be loaded after command finishes
::tsp::append_volatile_list compUnit $varlist
return [::tsp::gen_direct_tcl compUnit $tree]
}
#########################################################
# generate code for "binary scan" command (assumed to be first parse word)
# just look at vars to make sure they get loaded after the command
# return list of: type rhsVarName code
#
# FIXME: this is probably obsoleted by spill/load checking
proc ::tsp::gen_command_binary {compUnitDict tree} {
upvar $compUnitDict compUnit
if {[llength $tree] < 4} {
::tsp::addError compUnit "wrong # args: should be \"binary scan value formatString ?varName varName ...?\""
return [list void "" ""]
}
set varlist [list]
set subcommandNode [lindex $tree 1]
set subcommandNodeComponents [::tsp::parse_word compUnit $subcommandNode]
lassign [lindex $subcommandNodeComponents 0] type rawtext text
if {[llength $subcommandNodeComponents] == 1 && $type eq "text"} {
if {$rawtext eq "scan"} {
foreach node [lrange $tree 4 end] {
set nodeComponents [::tsp::parse_word compUnit $node]
set nodeType [lindex $nodeComponents 0 0]
if {[llength $nodeComponents] == 1 && $nodeType eq "text"} {
lassign [lindex $nodeComponents 0] nodeType varname text
if {[::tsp::isValidIdent $varname]} {
set vartype [::tsp::getVarType compUnit $varname]
if {$vartype ne "undefined"} {
# make sure variable will be loaded after command finishes
lappend varlist $varname
} else {
# identifier not defined, bail
}
} else {
# bad identifier, bail
}
} else {
# not simple text, can't be a var we use
}
}
}
}
# make sure variable will be loaded after command finishes
::tsp::append_volatile_list compUnit $varlist
return [::tsp::gen_direct_tcl compUnit $tree]
}
#########################################################
# generate code for "string" command (assumed to be first parse word)
# lang_string determines whether or not the subcommand is compiled
# default is to invoke interp string command
# return list of: type rhsVarName code
#
proc ::tsp::gen_command_string {compUnitDict tree} {
upvar $compUnitDict compUnit
set result ""
set errMsg ""
set rc 0
# quick arg length check, all string commands need at least 3 args
if {[llength $tree] < 3} {
::tsp::addError compUnit "string command doesn't have enough arguments"
return [list void "" ""]
}
# get the string subcommand
set subcommandNode [lindex $tree 1]
set subcommandNodeComponents [::tsp::parse_word compUnit $subcommandNode]
lassign [lindex $subcommandNodeComponents 0] type rawtext text
if {[llength $subcommandNodeComponents] == 1 && $type eq "text"} {
set cmd $rawtext
switch $cmd {
bytelength { }
compare { }
equal { }
first { }
index { set rc [catch {set result [::tsp::gen_string_index compUnit $tree]} errMsg] }
is { }
last { }
length { set rc [catch {set result [::tsp::gen_string_length compUnit $tree]} errMsg] }
map { }
match { }
range { set rc [catch {set result [::tsp::gen_string_range compUnit $tree]} errMsg] }
repeat { }
replace { }
tolower { }
toupper { }
totitle { }
trim { }
trimleft { }
trimright { }
wordend { }
wordstart { }
}
} else {
::tsp::addWarning compUnit "string subcommand is not simple text"
return [::tsp::gen_direct_tcl compUnit $tree]
}
if {$rc != 0} {
::tsp::addError compUnit $errMsg
return [list void "" ""]
}
if {[llength $result] > 0} {
return $result
} else {
return [::tsp::gen_direct_tcl compUnit $tree]
}
}
#########################################################
# generate code for "string index"
# raise error if wrong arguments, etc.
# return list of: type rhsVarName code
#
proc ::tsp::gen_string_index {compUnitDict tree} {
upvar $compUnitDict compUnit
if {[llength $tree] != 4} {
::tsp::addError compUnit "#wrong # args: should be \"string index string charIndex\""
return [list void "" ""]
}
# if string is a scalar var, return null, so that builtin proc will handle it
set node [lindex $tree 2]
lassign [lindex [::tsp::parse_word compUnit $node] 0] type textOrVar text
if {$type eq "scalar" && [::tsp::getVarType compUnit $textOrVar] eq "var"} {
return ""
}
set code "/***** ::tsp::gen_command_string_index */\n"
# get the string
set strResult [::tsp::get_string compUnit [lindex $tree 2]]
lassign $strResult result strVar convertCode
if {$result == 0} {
::tsp::addError compUnit "unable to get string argument: $strVar"
return [list void "" ""]
} else {
set pre [::tsp::var_prefix $strVar]
set strVar $pre$strVar
}
append code $convertCode
# get the index
set idxResult [::tsp::get_index compUnit [lindex $tree 3]]
lassign $idxResult idxValid idxRef idxIsFromEnd convertCode
if {! $idxValid} {
::tsp::addError compUnit "unable to get string index argument: $idxRef"
} else {
if {[::tsp::literalExprTypes $idxRef] eq "stringliteral"} {
# not a int literal, so it must be a scalar
set pre [::tsp::var_prefix $idxRef]
set idxRef $pre$idxRef
}
append code $convertCode
}
set returnVar [::tsp::get_tmpvar compUnit string]
append code [::tsp::lang_string_index $returnVar $idxRef $idxIsFromEnd $strVar]
return [list string $returnVar $code]
}
#########################################################
# generate code for "string length"
# raise error if wrong arguments, etc.
# return list of: type rhsVarName code
#
proc ::tsp::gen_string_length {compUnitDict tree} {
upvar $compUnitDict compUnit
if {[llength $tree] != 3} {
::tsp::addError compUnit "#wrong # args: should be \"string length string\""
return [list void "" ""]
}
# if string is a scalar var, return null, so that builtin proc will handle it
set node [lindex $tree 2]
lassign [lindex [::tsp::parse_word compUnit $node] 0] type textOrVar text
if {$type eq "scalar" && [::tsp::getVarType compUnit $textOrVar] eq "var"} {
return ""
}
set code "/***** ::tsp::gen_command_string_length */\n"
# get the string
set strResult [::tsp::get_string compUnit [lindex $tree 2]]
lassign $strResult result strVar convertCode
if {$result == 0} {
::tsp::addError compUnit "unable to get string argument: $strVar"
return [list void "" ""]
} else {
set pre [::tsp::var_prefix $strVar]
set strVar $pre$strVar
}
append code $convertCode
# get the length
set returnVar [::tsp::get_tmpvar compUnit int]
append code [::tsp::lang_string_length $returnVar $strVar]
return [list int $returnVar $code]
}
#########################################################
# generate code for "string range"
# raise error if wrong arguments, etc.
# return list of: type rhsVarName code
#
proc ::tsp::gen_string_range {compUnitDict tree} {
upvar $compUnitDict compUnit
if {[llength $tree] != 5} {
::tsp::addError compUnit "#wrong # args: should be \"string range string first last\""
return [list void "" ""]
}
# if string is a scalar var, return null, so that builtin proc will handle it
set node [lindex $tree 2]
lassign [lindex [::tsp::parse_word compUnit $node] 0] type textOrVar text
if {$type eq "scalar" && [::tsp::getVarType compUnit $textOrVar] eq "var"} {
return ""
}
set code "/***** ::tsp::gen_command_string_range */\n"
# get the string
set strResult [::tsp::get_string compUnit [lindex $tree 2]]
lassign $strResult result strVar convertCode
if {$result == 0} {
::tsp::addError compUnit "unable to get string argument: $strVar"
return [list void "" ""]
} else {
set pre [::tsp::var_prefix $strVar]
set strVar $pre$strVar
}
append code $convertCode
# get the first index
set idxResult [::tsp::get_index compUnit [lindex $tree 3]]
lassign $idxResult firstIdxValid firstIdxRef firstIdxIsFromEnd convertCode
if {! $firstIdxValid} {
::tsp::addError compUnit "unable to get string index argument: $firstIdxRef"
return [list void "" ""]
} else {
if {[::tsp::literalExprTypes $firstIdxRef] eq "stringliteral"} {
set pre [::tsp::var_prefix $firstIdxRef]
set firstIdxRef $pre$firstIdxRef
}
append code $convertCode
}
# get the last index
set idxResult [::tsp::get_index compUnit [lindex $tree 4]]
lassign $idxResult lastIdxValid lastIdxRef lastIdxIsFromEnd convertCode
if {! $lastIdxValid} {
::tsp::addError compUnit "unable to get string index argument: $lastIdxRef"
return [list void "" ""]
} else {
if {[::tsp::literalExprTypes $lastIdxRef] eq "stringliteral"} {
# not a int literal, so it must be a scalar
set pre [::tsp::var_prefix $lastIdxRef]
set lastIdxRef $pre$lastIdxRef
}
append code $convertCode
}
set returnVar [::tsp::get_tmpvar compUnit string]
append code [::tsp::lang_string_range $returnVar $firstIdxRef $firstIdxIsFromEnd $lastIdxRef $lastIdxIsFromEnd $strVar]
return [list string $returnVar $code]
}