forked from flightaware/tsp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsp-generate-set.tcl
887 lines (765 loc) · 38.9 KB
/
tsp-generate-set.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
#FIXME: be consistent in quoting strings, esp those that are array index
# probaby ok to quote them once when recognized
#########################################################
# check if target variable is undefined, if so then make
# it same as sourcetype
# return ERROR if targetVarName is a proc arg var or is invalid identifier
#
proc ::tsp::gen_check_target_var {compUnitDict targetVarName targetType sourceType} {
upvar $compUnitDict compUnit
if {$targetType eq "undefined" && $sourceType ne "void"} {
if {[::tsp::isProcArg compUnit $targetVarName]} {
::tsp::addError compUnit "proc argument variable \"$targetVarName\" not previously defined"
return ERROR
} elseif {[::tsp::isValidIdent $targetVarName]} {
set targetType $sourceType
::tsp::addWarning compUnit "variable \"$targetVarName\" implicitly defined as type: \"$targetType\" (set)"
::tsp::setVarType compUnit $targetVarName $targetType
} else {
::tsp::addError compUnit "invalid identifier: \"$targetVarName\""
return ERROR
}
}
return $targetType
}
#########################################################
# generate a set command, the basic assignment command
# we only compile:
# set var arg
# where var is:
# text e.g., a scalar var
# arr(idx)
# arr($idx)
# where arg is:
# \backslashed escape character
# text
# interpolated text e.g., "this $is \n$stuff" (string/var target only)
# $var
# $arr(idx)
# $arr($idx)
# [cmd text/$var text/$var ...]
# where idx is:
# text
# $var
#
# returns list of [void "" code] - note return type is always void, and assignment var is empty string
#
# NOTE that anywhere a tcl var is used, it is prefixed with "__" for native compilation, except
# for "array" variables, which are only accessed in the interp. This is done anytime we
# call a lang specific proc (::tsp::lang_*], or generate lang indepedent code.
#
proc ::tsp::gen_command_set {compUnitDict tree} {
upvar $compUnitDict compUnit
set errors 0
set body [dict get $compUnit body]
set len [llength $tree]
if {$len != 3} {
::tsp::addError compUnit "set command must have two args"
return [list void "" ""]
}
set targetStr [parse getstring $body [lindex [lindex $tree 1] 1]]
set sourceStr [parse getstring $body [lindex [lindex $tree 2] 1]]
# check target, should be a single text, text_array_idxtext, or text_array_idxvar
set targetComponents [::tsp::parse_word compUnit [lindex $tree 1]]
set firstType [lindex [lindex $targetComponents 0] 0]
if { !($firstType eq "text" || $firstType eq "text_array_idxtext" || $firstType eq "text_array_idxvar")} {
set errors 1
::tsp::addError compUnit "set target is not a scalar or array variable name: \"$targetStr\""
}
# check source word components
set sourceComponents [::tsp::parse_word compUnit [lindex $tree 2]]
set firstType [lindex [lindex $sourceComponents 0] 0]
if {$firstType eq "invalid"} {
set errors 1
::tsp::addError compUnit "set arg 2 invalid: \"$sourceStr\""
}
if {$errors} {
return [list void "" ""]
}
return [::tsp::produce_set compUnit $tree $targetComponents $sourceComponents]
}
#########################################################
# produce the set command from target and source components (from parse_word)
# note - tree can be empty when sourceComponents is not a [command]
#
proc ::tsp::produce_set {compUnitDict tree targetComponents sourceComponents} {
upvar $compUnitDict compUnit
set errors 0
# determine target variable name a type
set targetComponent [lindex $targetComponents 0]
set targetWordType [lindex $targetComponent 0]
set targetVarName ""
set targetType invalid
set targetArrayIdxtext ""
set targetArrayIdxvar ""
if {$targetWordType eq "text"} {
set targetVarName [lindex $targetComponent 2]
set targetType [::tsp::getVarType compUnit $targetVarName]
# targetType could be undefined, if this is the first reference to this scalar
# resolve later, but make sure it's a valid identifier
if {$targetType eq "undefined"} {
# make sure this can be a valid variable
if {! [::tsp::isValidIdent $targetVarName] } {
set errors 1
::tsp::addError compUnit "set arg 1 previously undefined variable not a valid identifer: \"$targetVarName\""
return [list void "" ""]
}
}
} elseif {$targetWordType eq "text_array_idxtext" || $targetWordType eq "text_array_idxvar"} {
set targetVarName [lindex $targetComponents 1]
set targetType [::tsp::getVarType compUnit $targetVarName]
if {$targetType eq "undefined"} {
# make sure this can be a valid variable and is not a proc arg var
if {[::tsp::isProcArg compUnit $targetVarName]} {
set errors 1
::tsp::addError compUnit "proc argument variable \"$targetVarName\" not previously defined"
return [list void "" ""]
} elseif {! [::tsp::isValidIdent $targetVarName] } {
set errors 1
::tsp::addError compUnit "set arg 1 previously undefined variable not a valid identifer: \"$targetVarName\""
return [list void "" ""]
} else {
set targetType array
::tsp::setVarType compUnit $targetVarName array
::tsp::addWarning compUnit "variable \"${targetVarName}\" implicitly defined as type: \"array\" (set)"
}
} elseif {$targetType ne "array"} {
# variable parsed as an array, but some other type
set errors 1
::tsp::addError compUnit "set arg 1 \"$targetVarName\" previously defined as type: \"$targetType\", now referenced as array"
}
# is index a string or variable?
if {$targetWordType eq "text_array_idxtext"} {
set targetArrayIdxtext [lindex $targetComponents 2]
set targetArrayIdxvarType string
} else {
set targetArrayIdxvar [lindex $targetComponents 2]
set targetArrayIdxvarType [::tsp::getVarType compUnit $targetArrayIdxvar]
if {$targetArrayIdxvarType eq "array"} {
# we don't support index variables as arrays
set errors 1
::tsp::addError compUnit "set arg 1 index variable \"$targetArrayIdxvar\" cannot be an defined as \"array\""
} elseif {$targetArrayIdxvarType eq "undefined"} {
set errors 1
::tsp::addError compUnit "set arg 1 array index undefined identifer: \"$targetArrayIdxvar\""
}
}
} else {
set errors 1
::tsp::addError compUnit "set arg 1 unexpected target type: \"$targetWordType: $targetComponents\""
}
if {$errors} {
return [list void "" ""]
}
# determine source variable/expression and type
set sourceText ""
set sourceType ""
set sourceVarName ""
set sourceArrayIdxtext ""
set sourceArrayIdxvar ""
set sourceCode ""
# is source an interpolated string?
if {[llength $sourceComponents] > 1} {
if {$targetType eq "array"} {
return [list void "" [::tsp::gen_assign_array_interpolated_string compUnit $targetVarName \
$targetArrayIdxtext $targetArrayIdxvar $targetArrayIdxvarType $targetType $sourceComponents ]]
} else {
set sourceType string
set targetType [::tsp::gen_check_target_var compUnit $targetVarName $targetType $sourceType]
if {$targetType eq "ERROR"} {
return [list void "" ""]
}
# append source components into a single string or var assignment
# check that target is either string or var or array
if {$targetType eq "string" || $targetType eq "var"} {
# will fail if embedded command or array is found
return [list void "" [::tsp::gen_assign_var_string_interpolated_string compUnit $targetVarName $targetType $sourceComponents]]
} else {
::tsp::addError compUnit "set command arg 1 variable must be string or var for interpolated string assignment: \"$targetVarName\""
return [list void "" ""]
}
}
} else {
set sourceComponent [lindex $sourceComponents 0]
set sourceWordType [lindex $sourceComponent 0]
if {$sourceWordType eq "backslash"} {
# backslashed string to string or var assignment
# subst the backslashed constant, so that we can quote as a native string
set sourceText [subst [lindex $sourceComponent 1]]
set sourceType string
set targetType [::tsp::gen_check_target_var compUnit $targetVarName $targetType $sourceType]
if {$targetType eq "ERROR"} {
return [list void "" ""]
}
# generate assigment
if {$targetType eq "string"} {
return [list void "" [::tsp::lang_assign_string_const $targetVarName $sourceText]]
} elseif {$targetType eq "var"} {
return [list void "" [::tsp::lang_assign_var_string $targetVarName [::tsp::lang_quote_string $sourceText]]]
} elseif {$targetWordType eq "text_array_idxtext" || $targetWordType eq "text_array_idxvar"} {
return [list void "" [::tsp::gen_assign_array_text compUnit $targetVarName $targetArrayIdxtext \
$targetArrayIdxvar $targetArrayIdxvarType $targetType $sourceText $sourceType]]
} else {
::tsp::addError compUnit "set command arg 1 variable must be string, var, or array for backslash assignment: \"$targetVarName\""
return [list void "" ""]
}
} elseif {$sourceWordType eq "text"} {
# possibly could be a int, double, or boolean literal, otherwise string
set sourceType [::tsp::literalExprTypes [lindex $sourceComponent 2]]
if {$sourceType ne "int" && $sourceType ne "double"} {
set sourceType string
}
set sourceText [lindex $sourceComponent 2]
set targetType [::tsp::gen_check_target_var compUnit $targetVarName $targetType $sourceType]
if {$targetType eq "ERROR"} {
return [list void "" ""]
}
# generate assigment
if {$targetWordType eq "text"} {
return [list void "" [::tsp::gen_assign_scalar_text compUnit $targetVarName $targetType $sourceText $sourceType]]
} elseif {$targetWordType eq "text_array_idxtext" || $targetWordType eq "text_array_idxvar"} {
return [list void "" [::tsp::gen_assign_array_text compUnit $targetVarName $targetArrayIdxtext \
$targetArrayIdxvar $targetArrayIdxvarType $targetType $sourceText $sourceType]]
} else {
if {$errors} {
return [list void "" ""]
}
error "unexpected target word type: $targetWordType \n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
} elseif {$sourceWordType eq "scalar"} {
# assignment from native variable or var, possible type coersion
set sourceVarName [lindex $sourceComponent 1]
set sourceType [::tsp::getVarType compUnit $sourceVarName]
if {$sourceType eq "undefined"} {
::tsp::addError compUnit "set command arg 2 variable not defined: \"$sourceVarName\""
return [list void "" ""]
}
set targetType [::tsp::gen_check_target_var compUnit $targetVarName $targetType $sourceType]
if {$targetType eq "ERROR"} {
return [list void "" ""]
}
# generate assigment
if {$targetWordType eq "text"} {
# don't generate assignment if target and source are the same
if {$targetVarName eq $sourceVarName} {
if {[::tsp:::is_tmpvar $targetVarName]} {
error "self assignment of temp var: $$targetVarName\n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
::tsp::addWarning compUnit "ignoring self assignment: target \"$targetVarName\" source \"$sourceVarName\""
return [list void "" ""]
}
return [list void "" [::tsp::gen_assign_scalar_scalar compUnit $targetVarName $targetType $sourceVarName $sourceType]]
} elseif {$targetWordType eq "text_array_idxtext" || $targetWordType eq "text_array_idxvar"} {
return [list void "" [::tsp::gen_assign_array_scalar compUnit $targetVarName $targetArrayIdxtext \
$targetArrayIdxvar $targetArrayIdxvarType $targetType $sourceVarName $sourceType]]
} else {
error "unexpected target word type: $targetWordType \n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
} elseif {$sourceWordType eq "array_idxtext"} {
set sourceVarName [lindex $sourceComponent 1]
set sourceType [::tsp::getVarType compUnit $sourceVarName]
if {$sourceType ne "array"} {
::tsp::addError compUnit "set command arg 2 variable not defined, referenced as array: \"$sourceVarName\""
return [list void "" ""]
}
set sourceArrayIdxtext [lindex $sourceComponent 2]
set sourceType var
# assignment from var, possible type coersion
set targetType [::tsp::gen_check_target_var compUnit $targetVarName $targetType $sourceType]
if {$targetType eq "ERROR"} {
return [list void "" ""]
}
# generate assigment
if {$targetWordType eq "text"} {
return [list void "" [::tsp::gen_assign_scalar_array compUnit $targetVarName $targetType \
$sourceVarName "" "" $sourceArrayIdxtext]]
} elseif {$targetWordType eq "text_array_idxtext" || $targetWordType eq "text_array_idxvar"} {
# don't generate assignment if target and source are the same
if {$targetVarName eq $sourceVarName && $targetArrayIdxtext eq $sourceArrayIdxtext} {
::tsp::addWarning compUnit "ignoring self assignment: target \"$targetVarName\($targetArrayIdxtext)\" source \"$sourceVarName\($sourceArrayIdxtext)\""
return [list void "" ""]
}
return [list void "" [::tsp::gen_assign_array_array compUnit $targetVarName $targetArrayIdxtext \
$targetArrayIdxvar $targetArrayIdxvarType $targetType $sourceVarName "" "" $sourceArrayIdxtext]]
} else {
error "unexpected target word type: $targetWordType \n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
} elseif {$sourceWordType eq "array_idxvar"} {
set sourceVarName [lindex $sourceComponent 1]
set sourceType [::tsp::getVarType compUnit $sourceVarName]
if {$sourceType ne "array"} {
::tsp::addError compUnit "set command arg 2 variable not defined, referenced as array: \"$sourceVarName\""
return [list void "" ""]
}
set sourceArrayIdxvar [lindex $sourceComponent 2]
set sourceArrayIdxvarType [::tsp::getVarType compUnit $sourceArrayIdxvar]
if {$sourceArrayIdxvarType eq "undefined"} {
::tsp::addError compUnit "set command arg 2 array index variable not defined: \"$sourceArrayIdxvar\""
return [list void "" ""]
}
set sourceType var
# assignment from var, possible type coersion
set targetType [::tsp::gen_check_target_var compUnit $targetVarName $targetType $sourceType]
if {$targetType eq "ERROR"} {
return [list void "" ""]
}
# generate assigment
if {$targetWordType eq "text"} {
return [list void "" [::tsp::gen_assign_scalar_array compUnit $targetVarName $targetType \
$sourceVarName $sourceArrayIdxvar $sourceArrayIdxvarType ""]]
} elseif {$targetWordType eq "text_array_idxtext" || $targetWordType eq "text_array_idxvar"} {
# don't generate assignment if target and source are the same
if {$targetVarName eq $sourceVarName && $targetArrayIdxvar eq $sourceArrayIdxvar} {
::tsp::addWarning compUnit "ignoring self assignment: target \"$targetVarName\($targetArrayIdxvar)\" source \"$sourceVarName\($sourceArrayIdxvar)\""
return [list void "" ""]
}
return [list void "" [::tsp::gen_assign_array_array compUnit $targetVarName $targetArrayIdxtext \
$targetArrayIdxvar $targetArrayIdxvarType $targetType $sourceVarName $sourceArrayIdxvar $sourceArrayIdxvarType ""]]
} else {
error "unexpected target word type: $targetWordType \n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
} elseif {$sourceWordType eq "command"} {
# assignment from command execution
set sourceCmdRange [lindex $sourceComponent 2]
lassign [::tsp::parse_nestedbody compUnit $sourceCmdRange] sourceType sourceRhsVar sourceCode
if {$sourceCode eq ""} {
::tsp::addError compUnit "assignment from nested command: no code generated: target \"$targetVarName\" "
return [list void "" ""]
}
if {$sourceType eq "void"} {
::tsp::addError compUnit "void assignment from nested command: target \"$targetVarName\""
return [list void "" ""]
}
set targetType [::tsp::gen_check_target_var compUnit $targetVarName $targetType $sourceType]
if {$targetType eq "ERROR"} {
return [list void "" ""]
}
# generate assignment
# mostly same as a scalar from scalar assignment
set sourceVarName $sourceRhsVar
append result "\n/***** ::tsp::generate_set assign from command */\n"
append code $sourceCode
set targetType [::tsp::gen_check_target_var compUnit $targetVarName $targetType $sourceType]
if {$targetType eq "ERROR"} {
return [list void "" ""]
}
# generate assigment
if {$targetWordType eq "text"} {
# don't generate assignment if target and source are the same
if {$targetVarName eq $sourceVarName} {
if {[::tsp:::is_tmpvar $targetVarName]} {
error "self assignment of temp var: $$targetVarName\n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
::tsp::addWarning compUnit "ignoring self assignment: target \"$targetVarName\" source \"$sourceVarName\""
return [list void "" ""]
}
append code [::tsp::gen_assign_scalar_scalar compUnit $targetVarName $targetType $sourceVarName $sourceType]
} elseif {$targetWordType eq "text_array_idxtext" || $targetWordType eq "text_array_idxvar"} {
append code [::tsp::gen_assign_array_scalar compUnit $targetVarName $targetArrayIdxtext \
$targetArrayIdxvar $targetArrayIdxvarType $targetType $sourceVarName $sourceType]
} else {
error "unexpected target word type: $targetWordType"
}
append result $code
append result "\n"
return [list void "" $result]
} else {
set errors 1
::tsp::addError compUnit "set arg 2 unexpected source type: \"$sourceWordType\""
}
}
# if any errors, return here
if {$errors} {
return [list void "" ""]
}
}
#########################################################
# assign a scalar variable from text string
#
proc ::tsp::gen_assign_scalar_text {compUnitDict targetVarName targetType sourceText sourceType} {
upvar $compUnitDict compUnit
# set the target as dirty
# puts "gen_assign_scalar_text- ::tsp::setDirty compUnit $targetVarName"
::tsp::setDirty compUnit $targetVarName
set targetPre [::tsp::var_prefix $targetVarName]
append result "\n/***** ::tsp::gen_assign_scalar_text */\n"
switch $targetType {
boolean {
switch $sourceType {
int {
append result "$targetPre$targetVarName = ([::tsp::lang_int_const $sourceText] != 0) ? [::tsp::lang_true_const] : [::tsp::lang_false_const];\n"
return $result
}
double {
append result "$targetPre$targetVarName = ([::tsp::lang_double_const $sourceText] != 0) ? [::tsp::lang_true_const] : [::tsp::lang_false_const];\n"
return $result
}
string {
if {[string is true $sourceText]} {
append result "$targetPre$targetVarName = [::tsp::lang_true_const];\n";
return $result
} elseif {[string is false $sourceText]} {
append result "$targetPre$targetVarName = [::tsp::lang_false_const];\n";
return $result
} else {
::tsp::addError compUnit "set arg 2 string is not a valid boolean value: \"$sourceText\""
return ""
}
}
error "unexpected sourceType: $sourceType \n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
}
int {
switch $sourceType {
int {
append result "$targetPre$targetVarName = [::tsp::lang_int_const $sourceText];\n"
return $result
}
double {
append result "$targetPre$targetVarName = ([::tsp::lang_type_int]) [::tsp::lang_double_const $sourceText];\n"
return $result
}
string {
::tsp::addError compUnit "set arg 2 string not an $targetType value: \"$sourceText\""
return ""
}
error "unexpected sourceType: $sourceType \n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
}
double {
switch $sourceType {
int {
append result "$targetPre$targetVarName = (::tsp::lang_type_double) [::tsp::lang_int_const $sourceText];\n"
return $result
}
double {
append result "$targetPre$targetVarName = [::tsp::lang_double_const $sourceText];\n"
return $result
}
string {
::tsp::addError compUnit "set arg 2 string not an $targetType value: \"$sourceText\""
return ""
}
error "unexpected sourceType: $sourceType \n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
}
string {
append result [::tsp::lang_assign_string_const $targetPre$targetVarName $sourceText]
return $result
}
var {
switch $sourceType {
int {
append result [::tsp::lang_assign_var_int $targetPre$targetVarName $sourceText]
return $result
}
double {
append result [::tsp::lang_assign_var_double $targetPre$targetVarName $sourceText]
return $result
}
string {
append result [::tsp::lang_assign_var_string $targetPre$targetVarName [::tsp::lang_quote_string $sourceText]]
return $result
}
error "unexpected sourceType: $sourceType \n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
}
}
::tsp::addError compUnit "set: error don't know how to assign $targetVarName $targetType from $sourceText $sourceType"
return ""
}
#########################################################
# assign a scalar variable from a scalar
#
proc ::tsp::gen_assign_scalar_scalar {compUnitDict targetVarName targetType sourceVarName sourceType} {
upvar $compUnitDict compUnit
# set the target as dirty
# puts "gen_assign_scalar_scalar- ::tsp::setDirty compUnit $targetVarName"
::tsp::setDirty compUnit $targetVarName
set targetPre [::tsp::var_prefix $targetVarName]
set sourcePre [::tsp::var_prefix $sourceVarName]
append result "\n/***** ::tsp::gen_assign_scalar_scalar */\n"
switch $targetType {
boolean {
switch $sourceType {
boolean {
append result "$targetPre$targetVarName = $sourcePre$sourceVarName;\n"
return $result
}
int -
double {
append result "$targetPre$targetVarName = ($sourcePre$sourceVarName != 0) ? [::tsp::lang_true_const] : [::tsp::lang_false_const];\n"
return $result
}
string {
set errMsg [::tsp::gen_runtime_error compUnit [::tsp::lang_quote_string "unable to convert string to boolean, \"$sourceVarName\", value: "]]
append result [::tsp::lang_convert_boolean_string $targetPre$targetVarName $sourcePre$sourceVarName $errMsg]
return $result
}
var {
set errMsg [::tsp::gen_runtime_error compUnit [::tsp::lang_quote_string "unable to convert var to boolean, \"$sourceVarName\", value: "]]
append result [::tsp::lang_convert_boolean_var $targetPre$targetVarName $sourcePre$sourceVarName $errMsg]
return $result
}
error "unexpected sourceType: $sourceType \n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
}
int -
double {
switch $sourceType {
boolean {
append result "$targetPre$targetVarName = ($sourcePre$sourceVarName) ? 1 : 0;\n"
return $result
}
int -
double {
append result "$targetPre$targetVarName = ([::tsp::lang_type_$targetType]) $sourcePre$sourceVarName;\n"
return $result
}
string {
set errMsg [::tsp::gen_runtime_error compUnit [::tsp::lang_quote_string "unable to convert string to $targetType, \"$sourceVarName\", value: "]]
append result [::tsp::lang_convert_${targetType}_string $targetPre$targetVarName $sourcePre$sourceVarName $errMsg]
return $result
}
var {
set errMsg [::tsp::gen_runtime_error compUnit [::tsp::lang_quote_string "unable to convert var to $targetType, \"$sourceVarName\", value: "]]
append result [::tsp::lang_convert_${targetType}_var $targetPre$targetVarName $sourcePre$sourceVarName $errMsg]
return $result
}
error "unexpected sourceType: $sourceType \n[::tsp::currentLine compUnit]\n[::tsp::error_stacktrace]"
}
}
string {
append result [::tsp::lang_convert_string_$sourceType $targetPre$targetVarName $sourcePre$sourceVarName]
return $result
}
var {
append result [::tsp::lang_assign_var_$sourceType $targetPre$targetVarName $sourcePre$sourceVarName]
return $result
}
}
::tsp::addError compUnit "set: error don't know how to assign $targetVarName $targetType from $sourceVarName $sourceType"
return ""
}
#########################################################
# assign a string or var from an interpolated string
# FIXME: be smarter about combining backslash and strings, just append until a scalar is found or last of components
#
proc ::tsp::gen_assign_var_string_interpolated_string {compUnitDict targetVarName targetType sourceComponents} {
upvar $compUnitDict compUnit
# set the target as dirty
# puts "gen_assign_var_string_interpolated_string- ::tsp::setDirty compUnit $targetVarName"
::tsp::setDirty compUnit $targetVarName
set targetPre [::tsp::var_prefix $targetVarName]
append result "\n/***** ::tsp::gen_assign_var_string_interpolated_string */\n"
set tmp [::tsp::get_tmpvar compUnit string]
set tmp2 ""
set arrVar ""
if {$targetType eq "var"} {
set tmp2 [::tsp::get_tmpvar compUnit string]
append result [::tsp::lang_assign_empty_zero $tmp2 string]
}
foreach component $sourceComponents {
set compType [lindex $component 0]
switch $compType {
text -
backslash {
# subst the backslashed text, so that we can quote it for a native string
set sourceText [subst [lindex $component 1]]
append code [::tsp::lang_assign_string_const $tmp $sourceText]
}
scalar {
# assignment from native variable or var, possible type coersion
set sourceVarName [lindex $component 1]
set sourceType [::tsp::getVarType compUnit $sourceVarName]
if {$sourceType eq "undefined"} {
::tsp::addError compUnit "set command arg 2 interpolated string variable not defined: \"$sourceVarName\""
return [list ""]
}
append code [::tsp::gen_assign_scalar_scalar compUnit $tmp string $sourceVarName $sourceType]
}
command {
set sourceCmdRange [lindex $component 2]
lassign [::tsp::parse_nestedbody compUnit $sourceCmdRange] sourceType sourceRhsVar sourceCode
if {$sourceCode eq ""} {
::tsp::addError compUnit "assignment from nested command: no code generated: target \"$targetVarName\" "
return [list void "" ""]
}
if {$sourceType eq "void"} {
::tsp::addError compUnit "void assignment from nested command: target \"$targetVarName\""
return [list void "" ""]
}
append code $sourceCode
append code [::tsp::gen_assign_scalar_scalar compUnit $tmp string $sourceRhsVar $sourceType ]
}
default {
::tsp::addError compUnit "set arg 2 interpolated string cannot contain $compType, only commands, text, backslash, or scalar variables"
return ""
}
}
if {$targetType eq "string"} {
append code [::tsp::lang_append_string $targetPre$targetVarName $tmp]
} elseif {$targetType eq "var"} {
append code [::tsp::lang_append_string $tmp2 $tmp]
}
}
if {$targetType eq "var"} {
append code [::tsp::gen_assign_scalar_scalar compUnit $targetVarName var $tmp2 string]
}
append result $code "\n"
return $result
}
#########################################################
# assign an array variable from text string
# array index is either a text string, or a variable
#
#
proc ::tsp::gen_assign_array_text {compUnitDict targetVarName targetArrayIdxtext \
targetArrayIdxvar targetArrayIdxvarType targetType sourceText sourceType} {
upvar $compUnitDict compUnit
# make text value into a constant var
set value [::tsp::get_constvar [::tsp::getConstant compUnit $sourceText]]
append result "\n/***** ::tsp::gen_assign_array_text */\n"
if {$targetArrayIdxtext ne ""} {
# constant string index
append code [::tsp::lang_assign_array_var [::tsp::get_constvar [::tsp::getConstant compUnit $targetVarName]] \
[::tsp::get_constvar [::tsp::getConstant compUnit $targetArrayIdxtext]] $value]
append result $code
return $result
} else {
# variable index
set idxPre [::tsp::var_prefix $targetArrayIdxvar]
if {$targetArrayIdxvarType eq "var"} {
set idx $idxPre$targetArrayIdxvar
} else {
# it's a native var, use a shadow var
lassign [::tsp::getCleanShadowVar compUnit $targetArrayIdxvar] idx shadowCode
append code $shadowCode
}
append code [::tsp::lang_assign_array_var [::tsp::get_constvar [::tsp::getConstant compUnit $targetVarName]] $idx $value]
append result $code
return $result
}
}
#########################################################
# assign an array variable from scalar
# array index is either a text string, or a variable
#
#FIXME: this should use shadow vars, and create/update if needed when var is dirty
#
proc ::tsp::gen_assign_array_scalar {compUnitDict targetVarName targetArrayIdxtext \
targetArrayIdxvar targetArrayIdxvarType targetType sourceVarName sourceType} {
upvar $compUnitDict compUnit
append result "\n/***** ::tsp::gen_assign_array_scalar */\n"
# prepare the source variable
if {$sourceType eq "var"} {
set pre [::tsp::var_prefix $sourceVarName]
set value $pre$sourceVarName
} else {
set pre [::tsp::var_prefix $sourceVarName]
if {$pre eq ""} {
# it's a tmp var, so just assign into a tmp var type
set value [::tsp::get_tmpvar compUnit var]
append code [::tsp::lang_assign_var_$sourceType $value $pre$sourceVarName]
} else {
# it's a native var, use a shadow var
lassign [::tsp::getCleanShadowVar compUnit $sourceVarName] value shadowCode
append code $shadowCode
}
}
if {$targetArrayIdxtext ne ""} {
# constant string index
append code [::tsp::lang_assign_array_var [::tsp::get_constvar [::tsp::getConstant compUnit $targetVarName]] \
[::tsp::get_constvar [::tsp::getConstant compUnit $targetArrayIdxtext]] $value]
append result $code
return $result
} else {
# variable index
set idxPre [::tsp::var_prefix $targetArrayIdxvar]
if {$targetArrayIdxvarType eq "var"} {
set idx $idxPre$targetArrayIdxvar
} else {
# it's a native var, use a shadow var
lassign [::tsp::getCleanShadowVar compUnit $targetArrayIdxvar] idx shadowCode
append code $shadowCode
}
append code [::tsp::lang_assign_array_var [::tsp::get_constvar [::tsp::getConstant compUnit $targetVarName]] $idx $value]
append result $code
return $result
}
}
#########################################################
# assign an array var from an interpolated string
#
proc ::tsp::gen_assign_array_interpolated_string {compUnitDict targetVarName targetArrayIdxtext targetArrayIdxvar targetArrayIdxvarType targetType sourceComponents} {
upvar $compUnitDict compUnit
append result "\n/***** ::tsp::gen_assign_array_interpolated_string */\n"
set sourceVar [::tsp::get_tmpvar compUnit var]
append code [::tsp::lang_safe_release $sourceVar]
append code [::tsp::gen_assign_var_string_interpolated_string compUnit $sourceVar var $sourceComponents]
append code [::tsp::lang_preserve $sourceVar]
append code [::tsp::gen_assign_array_scalar compUnit $targetVarName $targetArrayIdxtext \
$targetArrayIdxvar $targetArrayIdxvarType $targetType $sourceVar var]
append result $code
return $result
}
#########################################################
# assign an scalar from an array
# sourceArrayIdx is either a quoted string, or a string
#
proc ::tsp::gen_assign_scalar_array {compUnitDict targetVarName targetType sourceVarName sourceArrayIdxvar sourceArrayIdxvarType sourceArrayIdxtext} {
upvar $compUnitDict compUnit
# target will be marked as dirty in ::tsp::gen_assign_scalar_scalar
append result "\n/***** ::tsp::gen_assign_scalar_array */\n"
set targetVar [::tsp::get_tmpvar compUnit var]
append code [::tsp::lang_safe_release $targetVar]
if {$sourceArrayIdxtext ne ""} {
set errMsg [::tsp::gen_runtime_error compUnit [::tsp::lang_quote_string "unable to get var from array \"$sourceVarName\", index \"$sourceArrayIdxtext\" "]]
append code [::tsp::lang_assign_var_array_idxtext $targetVar [::tsp::get_constvar [::tsp::getConstant compUnit $sourceVarName]] \
[::tsp::get_constvar [::tsp::getConstant compUnit $sourceArrayIdxtext]] $errMsg]
} else {
set errMsg [::tsp::gen_runtime_error compUnit [::tsp::lang_quote_string "unable to get var from array \"$sourceVarName\", index var \"$sourceArrayIdxvar\" "]]
set idxPre [::tsp::var_prefix $sourceArrayIdxvar]
if {$sourceArrayIdxvarType eq "var"} {
set idx $idxPre$sourceArrayIdxvar
} else {
# it's a native var, use a shadow var
lassign [::tsp::getCleanShadowVar compUnit $sourceArrayIdxvar] idx shadowCode
append code $shadowCode
}
append code [::tsp::lang_assign_var_array_idxvar $targetVar [::tsp::get_constvar [::tsp::getConstant compUnit $sourceVarName]] $idx $errMsg]
}
append code [::tsp::lang_preserve $targetVar]
append code [::tsp::gen_assign_scalar_scalar compUnit $targetVarName $targetType $targetVar var]
append result $code
return $result
}
#########################################################
# assign an array from an array
#
proc ::tsp::gen_assign_array_array {compUnitDict targetVarName targetArrayIdxtext targetArrayIdxvar targetArrayIdxvarType targetType sourceVarName sourceArrayIdxvar sourceArrayIdxvarType sourceArrayIdxtext } {
upvar $compUnitDict compUnit
append result "\n/***** ::tsp::gen_assign_array_array */\n"
set assignVar [::tsp::get_tmpvar compUnit var]
append code [::tsp::lang_safe_release $assignVar]
if {$sourceArrayIdxtext ne ""} {
set errMsg [::tsp::gen_runtime_error compUnit [::tsp::lang_quote_string "unable to get var from array \"$sourceVarName\", index \"$sourceArrayIdxtext\" "]]
append code [::tsp::lang_assign_var_array_idxtext $assignVar [::tsp::get_constvar [::tsp::getConstant compUnit $sourceVarName]] \
[::tsp::get_constvar [::tsp::getConstant compUnit $sourceArrayIdxtext]] $errMsg]
} else {
set errMsg [::tsp::gen_runtime_error compUnit [::tsp::lang_quote_string "unable to get var from array \"$sourceVarName\", index var \"$sourceArrayIdxvar\" "]]
set idxPre [::tsp::var_prefix $sourceArrayIdxvar]
if {$sourceArrayIdxvarType eq "var"} {
set idx $idxPre$sourceArrayIdxvar
} else {
set idx [::tsp::get_tmpvar compUnit var]
append code [::tsp::lang_assign_var_$sourceArrayIdxvarType $idx $idxPre$sourceArrayIdxvar]
}
append code [::tsp::lang_assign_var_array_idxvar $assignVar [::tsp::get_constvar [::tsp::getConstant compUnit $sourceVarName]] $idx $errMsg]
}
append code [::tsp::lang_preserve $assignVar]
append code [::tsp::gen_assign_array_scalar compUnit $targetVarName $targetArrayIdxtext \
$targetArrayIdxvar $targetArrayIdxvarType $targetType $assignVar var ]
append result $code
return $result
}