-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathloader.ts
1363 lines (1261 loc) · 54.5 KB
/
loader.ts
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as debug from './debugger';
import * as draw from './drawable'
import * as path from 'path';
import * as fs from 'fs';
import * as vscode from 'vscode';
import * as util from './util'
function parseTParams(type: string, beg: string = '<', end: string = '>', sep: string = ',') : string[] {
let result: string[] = [];
let param_list_index: number = 0;
let index: number = 0;
let param_first: number = -1;
let param_last: number = -1;
for (let c of type)
{
if (c === beg)
{
++param_list_index;
}
else if (c === end)
{
if (param_last === -1 && param_list_index === 1)
param_last = index;
--param_list_index;
}
else if (c === sep)
{
if (param_last === -1 && param_list_index === 1)
param_last = index;
}
else
{
if (param_first === -1 && param_list_index === 1)
param_first = index;
}
if (param_first !== -1 && param_last !== -1)
{
while (param_first < param_last && type[param_first] === ' ')
++param_first;
while (param_first < param_last && type[param_last - 1] === ' ')
--param_last;
result.push(type.substring(param_first, param_last));
param_first = -1;
param_last = -1;
}
++index;
}
return result;
}
function addSpacesInTemplates(str: string): string {
let result = '';
let p = '';
for (let c of str) {
if (c === '>') {
if (p === '>')
result += ' >';
else
result += '>';
}
else
result += c;
p = c;
}
return result;
}
// function parseSubscripts(type: string, beg: string = '[', end: string = ']') : string[] {
// let result: string[] = [];
// // TODO:
// return result;
// }
export class Variable {
constructor(
public name: string,
public type: string
) {}
tparam(i: number): string {
if (this._tparams === undefined)
this._tparams = parseTParams(this.type);
return i < this._tparams.length ? this._tparams[i] : '';
}
// TODO: support 2d arrays [1][2]
// parseSubscripts
nparam(i: number): string {
if (this._nparams === undefined)
this._nparams = parseTParams(this.type, '[', ']');
return i < this._nparams.length ? this._nparams[i] : '';
}
private _tparams: string[] | undefined;
private _nparams: string[] | undefined;
}
interface IExpressionPart {
toString(variable: Variable, parameter: number | undefined): string;
}
class ExpressionString implements IExpressionPart {
constructor(private _str: string) {}
toString(variable: Variable, parameter: number | undefined): string { return this._str; }
}
class ExpressionThis implements IExpressionPart {
toString(variable: Variable, parameter: number | undefined): string { return '(' + variable.name + ')'; }
}
class ExpressionNParam implements IExpressionPart {
constructor(protected _i: number) {}
toString(variable: Variable, parameter: number | undefined): string { return variable.nparam(this._i); }
}
class ExpressionTParam extends ExpressionNParam {
toString(variable: Variable, parameter: number | undefined): string { return variable.tparam(this._i); }
}
class ExpressionType implements IExpressionPart {
toString(variable: Variable, parameter: number | undefined): string { return variable.type; }
}
class ExpressionIndex implements IExpressionPart {
toString(variable: Variable, parameter: number | undefined): string { return parameter !== undefined ? parameter.toString() : "0"; }
}
export class Expression
{
constructor(expression: string) {
const regexp = new RegExp('\\$[A-Za-z_]\\w*', 'g');
let matches = expression.matchAll(regexp);
let index = 0;
for (const match of matches) {
if (match.index !== undefined) {
if (match.index > index) {
this._parts.push(new ExpressionString(expression.substr(index, match.index - index)));
index = match.index;
}
if (match[0] === '$this') {
this._parts.push(new ExpressionThis());
index = match.index + 5;
}
else if (match[0] === '$T') {
this._parts.push(new ExpressionType());
index = match.index + 2;
}
else if (match[0].startsWith('$T')) {
this._parts.push(new ExpressionTParam(parseInt(match[0].substr(2))));
index = match.index + match[0].length;
}
else if (match[0].startsWith('$N')) {
this._parts.push(new ExpressionNParam(parseInt(match[0].substr(2))));
index = match.index + match[0].length;
}
else if (match[0] === '$i') {
this._parts.push(new ExpressionIndex());
index = match.index + 2;
}
}
}
if (index < expression.length) {
this._parts.push(new ExpressionString(expression.substr(index)));
}
}
toString(variable: Variable, parameter: number | undefined = undefined): string {
let result: string = '';
for (let part of this._parts) {
result += part.toString(variable, parameter);
}
// TODO: do this only for C++ ?
// It is possible that this should depend on the debugger
return addSpacesInTemplates(result);
}
private _parts : IExpressionPart[] = [];
}
class EvaluatedExpression {
constructor(
public expression: Expression,
public name: string,
public type: string
) {}
get variable(): Variable { return new Variable(this.name, this.type); }
}
async function evaluateExpression(dbg: debug.Debugger, variable: Variable, expressionName: string, expressionType: string | undefined = undefined): Promise<EvaluatedExpression | undefined> {
const expr = new Expression(expressionName);
const str = expr.toString(variable);
// Technically if expressionType is passed we don't have to evaluate the expression because
// the type is known. But right now this function is used to check if a type contains members
// defined in json file. So get the value anyway and after that alter the type.
let vt = await dbg.getValueAndRawType(str);
if (vt === undefined)
return undefined;
if (expressionType !== undefined) {
const typeExpr = new Expression(expressionType);
const type = typeExpr.toString(variable);
if (type !== undefined && type !== '')
vt[1] = type;
}
return new EvaluatedExpression(expr, str, vt[1]);
}
async function evaluateIndexedExpression(dbg: debug.Debugger, variable: Variable, expressionName: string, parameter: number): Promise<EvaluatedExpression | undefined> {
const expr = new Expression(expressionName);
const str = expr.toString(variable, parameter);
let vt = await dbg.getValueAndRawType(str);
if (vt === undefined)
return undefined;
return new EvaluatedExpression(expr, str, vt[1]);
}
function getValueFromExpressionStr(dbg: debug.Debugger, str: string): number | boolean | undefined {
const language = dbg.language();
if (language != undefined) {
if (language === debug.Language.Cpp || language === debug.Language.JavaScript || language === debug.Language.Java) {
if (str === 'true')
return true;
else if (str === 'false')
return false;
}
else if (language === debug.Language.Python) {
if (str === 'True')
return true;
else if (str === 'False')
return false;
}
}
const n = parseFloat(str);
return isNaN(n) ? undefined : n;
}
async function getValueOrEvaluateExpression(dbg: debug.Debugger, variable: Variable, expression: string): Promise<EvaluatedExpression | number | boolean | undefined> {
const expr = new Expression(expression);
const str = expr.toString(variable);
const val = getValueFromExpressionStr(dbg, str);
if (val !== undefined)
return val;
const vt = await dbg.getValueAndRawType(str);
return vt !== undefined ? new EvaluatedExpression(expr, str, vt[1]) : undefined;
}
// Various Containers
export class Container {
element(variable: Variable): string | undefined {
return undefined;
}
async size(dbg: debug.Debugger, variable: Variable): Promise<number> {
return 0;
}
async *elements(dbg: debug.Debugger, variable: Variable): AsyncGenerator<string, void, unknown> {}
}
export class RandomAccessContainer extends Container {}
export class ContiguousContainer extends RandomAccessContainer {}
// Static array
export class Array extends ContiguousContainer
{
constructor(private _start: Expression, private _size: Expression) {
super();
}
element(variable: Variable): string | undefined {
return '(' + this._start.toString(variable) + ')[0]';
}
async size(dbg: debug.Debugger, variable: Variable): Promise<number> {
const sizeStr = this._size.toString(variable);
// TODO: Check if it's possible to parse size at this point
const sizeVal = await dbg.getValue(sizeStr);
return sizeVal !== undefined ? parseInt(sizeVal) : 0;
}
async *elements(dbg: debug.Debugger, variable: Variable): AsyncGenerator<string, void, unknown> {
const size = await this.size(dbg, variable);
if (! (size > 0)) // also handle NaN
return;
// NOTE: This loop could be done asynchroniously with await Promise.all()
// but an array can potentially store great number of objects so there
// would be great number of promises. And it will not be possible to
// end fast in case of error because the program would wait for all.
for (let i = 0; i < size; ++i) {
const elStr = '(' + this._start.toString(variable) + ')[' + i.toString() + ']';
yield elStr;
}
}
}
// Dynamic array
export class DArray extends ContiguousContainer {
constructor(private _start: Expression, private _finish: Expression) {
super();
}
element(variable: Variable): string | undefined {
return '(' + this._start.toString(variable) + ')[0]';
}
async size(dbg: debug.Debugger, variable: Variable): Promise<number> {
const sizeStr = '(' + this._finish.toString(variable) + ')-(' + this._start.toString(variable) + ')';
const sizeVal = await dbg.getValue(sizeStr);
return sizeVal !== undefined ? parseInt(sizeVal) : 0;
}
async *elements(dbg: debug.Debugger, variable: Variable): AsyncGenerator<string, void, unknown> {
const size = await this.size(dbg, variable);
if (! (size > 0)) // also handle NaN
return;
for (let i = 0; i < size; ++i) {
const elStr = '(' + this._start.toString(variable) + ')[' + i.toString() + ']';
yield elStr;
}
}
}
// Indexable/subscriptable array
export class IArray extends RandomAccessContainer
{
constructor(private _element: Expression, private _size: Expression) {
super();
}
element(variable: Variable): string | undefined {
return this._element.toString(variable, 0);
}
async size(dbg: debug.Debugger, variable: Variable): Promise<number> {
const sizeStr = this._size.toString(variable);
// TODO: Check if it's possible to parse size at this point
const sizeVal = await dbg.getValue(sizeStr);
return sizeVal !== undefined ? parseInt(sizeVal) : 0;
}
async *elements(dbg: debug.Debugger, variable: Variable): AsyncGenerator<string, void, unknown> {
const size = await this.size(dbg, variable);
if (! (size > 0)) // also handle NaN
return;
// NOTE: This loop could be done asynchroniously with await Promise.all()
// but an array can potentially store great number of objects so there
// would be great number of promises. And it will not be possible to
// end fast in case of error because the program would wait for all.
for (let i = 0; i < size; ++i) {
const elStr = this._element.toString(variable, i);
yield elStr;
}
}
}
// TODO: Later when direct memory access is implemented allow passing pointers
export class LinkedList extends Container
{
constructor(
private _size: Expression,
private _head: Expression,
private _next: Expression,
private _value: Expression) {
super();
}
element(variable: Variable): string | undefined {
const headName = '(' + this._head.toString(variable) + ')';
// TEMP: The original type is used by expression to get Tparams, not the head's type
// Variable should be renamed or replaced with something in expression.toString()
const headVar = new Variable(headName, variable.type);
return this._value.toString(headVar);
}
async size(dbg: debug.Debugger, variable: Variable): Promise<number> {
const sizeStr = this._size.toString(variable);
// TODO: Check if it's possible to parse size at this point
const sizeVal = await dbg.getValue(sizeStr);
return sizeVal !== undefined ? parseInt(sizeVal) : 0;
}
async *elements(dbg: debug.Debugger, variable: Variable): AsyncGenerator<string, void, unknown> {
const size = await this.size(dbg, variable);
if (! (size > 0)) // also handle NaN
return;
const headName = '(' + this._head.toString(variable) + ')';
// TEMP: The original type is used by expression to get Tparams, not the node's type
let nodeVar = new Variable(headName, variable.type);
for (let i = 0; i < size; ++i) {
const elStr = '(' + this._value.toString(nodeVar) + ')';
yield elStr;
nodeVar.name = '(' + this._next.toString(nodeVar) + ')';
}
}
}
// Unit
export enum Unit { None, Degree, Radian };
// Value
export class Value {
constructor(private _name: Expression) {
}
async load(dbg: debug.Debugger, variable: Variable): Promise<number | undefined> {
const valStr = this._name.toString(variable);
const valVal = await dbg.getValue(valStr);
return valVal !== undefined ? parseFloat(valVal) : undefined;
}
}
// Base Loader
export class Loader {
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
return undefined;
}
}
// Containers of drawables
export class ContainerLoader extends Loader {}
export class Numbers extends ContainerLoader {
constructor(private _container: Container, private _numberType: string) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
let ys: number[] = [];
for await (let elStr of this._container.elements(dbg, variable)) {
const elVal = await dbg.getValue(elStr);
if (elVal === undefined)
return undefined;
const el = parseFloat(elVal);
ys.push(el);
}
return new draw.Plot(util.indexesArray(ys), ys, draw.System.None);
}
}
export class Values extends ContainerLoader {
constructor(private _container: Container, private _value: Value, private _valueType: string) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
let ys: number[] = [];
for await (let elStr of this._container.elements(dbg, variable)) {
const v = new Variable(elStr, this._valueType);
const n = await this._value.load(dbg, v);
if (n === undefined)
return undefined
ys.push(n);
}
return new draw.Plot(util.indexesArray(ys), ys, draw.System.None);
}
}
export class Points extends ContainerLoader {
constructor(private _container: Container, private _point: Point, private _pointType: string) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
let xs: number[] = [];
let ys: number[] = [];
let system = draw.System.None;
for await (let elStr of this._container.elements(dbg, variable)) {
let v = new Variable(elStr, this._pointType);
const point = await this._point.load(dbg, v);
if (point === undefined)
return undefined;
const p = point as draw.Point;
xs.push(p.x)
ys.push(p.y);
system = p.system;
}
return new draw.Plot(xs, ys, system);
}
}
export class Geometries extends ContainerLoader {
constructor(private _container: Container, private _geometry: Geometry, private _geometryType: string) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
let drawables: draw.Drawable[] = [];
for await (let elStr of this._container.elements(dbg, variable)) {
const v = new Variable(elStr, this._geometryType);
const d = await this._geometry.load(dbg, v);
if (d === undefined)
return undefined;
drawables.push(d);
}
return new draw.Drawables(drawables);
}
}
export class DynamicGeometries extends ContainerLoader {
constructor(private _container: Container) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
let drawables: draw.Drawable[] = [];
for await (let elStr of this._container.elements(dbg, variable)) {
const elType = await dbg.getRawType(elStr);
if (elType === undefined)
return undefined;
const v = new Variable(elStr, elType);
let elLoad = this._loaders.get(elType);
if (elLoad === undefined) {
elLoad = await getLoader(dbg, v);
if (elLoad === undefined)
return undefined;
this._loaders.set(elType, elLoad);
}
const d = await elLoad.load(dbg, v);
if (d === undefined)
return undefined;
drawables.push(d);
}
return new draw.Drawables(drawables);
}
private _loaders: Map<string, Geometry> = new Map<string, Geometry>();
}
// Geometric primitives
export class Geometry extends Loader {
}
export class Point extends Geometry {
constructor(
private _xEval: EvaluatedExpression,
private _yEval: EvaluatedExpression,
private _system: draw.System,
private _unit: Unit) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
const xStr = this._xEval.expression.toString(variable);
const xVal = await dbg.getValue(xStr);
if (xVal === undefined)
return undefined;
const yStr = this._yEval.expression.toString(variable);
const yVal = await dbg.getValue(yStr);
if (yVal === undefined)
return undefined;
let x = parseFloat(xVal);
let y = parseFloat(yVal);
// Convert radians to degrees if needed
if (this._unit === Unit.Radian) {
const r2d = 180 / Math.PI;
x *= r2d;
y *= r2d;
}
return new draw.Point(x, y, this._system);
}
}
export class PointsRange extends Geometry {
constructor(private _containerExpr: EvaluatedExpression,
private _pointsLoad: Points) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
const contStr = this._containerExpr.expression.toString(variable);
const contVar = new Variable(contStr, this._containerExpr.type);
return this._pointsLoad.load(dbg, contVar);
}
}
export class Segment extends Geometry {
constructor(private _p0Expr: EvaluatedExpression,
private _p1Expr: EvaluatedExpression) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
const p0Str = this._p0Expr.expression.toString(variable);
const p0Var = new Variable(p0Str, this._p0Expr.type);
const p1Str = this._p1Expr.expression.toString(variable);
const p1Var = new Variable(p1Str, this._p1Expr.type);
this._p0Load = await getPointLoaderIfUndefined(this._p0Load, dbg, p0Var);
this._p1Load = await getPointLoaderIfUndefined(this._p1Load, dbg, p1Var);
const p0 = await this._p0Load?.load(dbg, p0Var) as draw.Point;
const p1 = await this._p1Load?.load(dbg, p1Var) as draw.Point;
if (p0 == undefined || p1 === undefined)
return undefined;
return new draw.Plot([p0.x, p1.x], [p0.y, p1.y], p0.system);
}
private _p0Load: Point | undefined = undefined;
private _p1Load: Point | undefined = undefined;
}
async function getPointLoaderIfUndefined(loader: Point | undefined, dbg: debug.Debugger, variable: Variable): Promise<Point | undefined> {
if (loader === undefined) {
// TODO: only search for Points
const load = await getLoader(dbg, variable);
if (load instanceof Point)
loader = load as Point;
}
return loader;
}
export class Box extends Geometry {
constructor(private _minExpr: EvaluatedExpression,
private _maxExpr: EvaluatedExpression) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
const minStr = this._minExpr.expression.toString(variable);
const minVar = new Variable(minStr, this._minExpr.type);
const maxStr = this._maxExpr.expression.toString(variable);
const maxVar = new Variable(maxStr, this._maxExpr.type);
this._minLoad = await getPointLoaderIfUndefined(this._minLoad, dbg, minVar);
this._maxLoad = await getPointLoaderIfUndefined(this._maxLoad, dbg, maxVar);
const min = await this._minLoad?.load(dbg, minVar) as draw.Point;
const max = await this._maxLoad?.load(dbg, maxVar) as draw.Point;
if (min == undefined || max === undefined)
return undefined;
return loadBox(min.x, min.y, max.x, max.y, min.system);
}
private _minLoad: Point | undefined = undefined;
private _maxLoad: Point | undefined = undefined;
}
export class Box2 extends Geometry {
constructor(
private _minxEval: EvaluatedExpression,
private _minyEval: EvaluatedExpression,
private _maxxEval: EvaluatedExpression,
private _maxyEval: EvaluatedExpression,
private _system: draw.System,
private _unit: Unit) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
const minxStr = this._minxEval.expression.toString(variable);
const minxVal = await dbg.getValue(minxStr);
if (minxVal === undefined)
return undefined;
const minyStr = this._minyEval.expression.toString(variable);
const minyVal = await dbg.getValue(minyStr);
if (minyVal === undefined)
return undefined;
const maxxStr = this._maxxEval.expression.toString(variable);
const maxxVal = await dbg.getValue(maxxStr);
if (maxxVal === undefined)
return undefined;
const maxyStr = this._maxyEval.expression.toString(variable);
const maxyVal = await dbg.getValue(maxyStr);
if (maxyVal === undefined)
return undefined;
let minx = parseFloat(minxVal);
let miny = parseFloat(minyVal);
let maxx = parseFloat(maxxVal);
let maxy = parseFloat(maxyVal);
// Convert radians to degrees if needed
if (this._unit === Unit.Radian) {
const r2d = 180 / Math.PI;
minx *= r2d;
miny *= r2d;
maxx *= r2d;
maxy *= r2d;
}
return loadBox(minx, miny, maxx, maxy, this._system);
}
}
// TODO: This way the longitude interval has to be calcualted from Ring points
// TODO: This logic should rather be implemented in draw
function loadBox(minx: number, miny: number, maxx: number, maxy: number, system: draw.System): draw.Ring {
if (system !== draw.System.Geographic) {
return new draw.Ring([minx, minx, maxx, maxx], [miny, maxy, maxy, miny], false, system, true);
}
else {
miny = util.bounded(miny, -90, 90);
maxy = util.bounded(maxy, -90, 90);
if (maxx < minx)
maxx = minx + util.uLon(maxx - minx);
if (maxx - minx > 360)
maxx = minx + 360;
let xs: number[] = [minx];
let ys: number[] = [miny];
const fstep = maxy == -90 || maxy == 90 ? 361 : 2.5;
for (let x = minx; x < maxx; x += fstep) {
xs.push(x); ys.push(maxy);
}
xs.push(maxx); ys.push(maxy);
const bstep = miny == -90 || miny == 90 ? 361 : 2.5;
for (let x = maxx; x > minx; x -= bstep) {
xs.push(x); ys.push(miny);
}
xs.push(minx); ys.push(miny);
return new draw.Ring(xs, ys, false, system, true);
}
}
export class Linestring extends PointsRange {
constructor(containerExpr: EvaluatedExpression,
pointsLoad: Points) {
super(containerExpr, pointsLoad);
}
}
export class Ring extends PointsRange {
constructor(containerExpr: EvaluatedExpression,
pointsLoad: Points,
private _orientation: EvaluatedExpression | number | boolean | undefined = undefined,
private _cw: boolean = true) {
super(containerExpr, pointsLoad);
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
const plot = await super.load(dbg, variable);
if (plot instanceof draw.Plot && plot.xs) {
// TODO: This doesn't work well with classes like Shapely Polygon
// where is_ccw member indicates the actual order of internal rings.
// Such rings should be reversed based on the exterior ring.
// Here rings are reversed locally.
const isCw = await this._isCw(dbg, variable);
return new draw.Ring(plot.xs, plot.ys, !isCw, plot.system);
}
else
return undefined;
}
async _isCw(dbg: debug.Debugger, variable: Variable): Promise<boolean> {
if (this._orientation !== undefined) {
let flag: number | boolean | undefined = undefined;
if (this._orientation instanceof EvaluatedExpression) {
const str = (this._orientation as EvaluatedExpression).expression.toString(variable);
const val = await dbg.getValue(str);
if (val !== undefined)
flag = getValueFromExpressionStr(dbg, val);
}
else
flag = this._orientation;
if (flag !== undefined) {
const is_false = flag === 0 || flag === false;
if (this._cw && is_false || !this._cw && !is_false)
return false;
}
}
return true;
}
}
export class MultiPoint extends PointsRange {
constructor(containerExpr: EvaluatedExpression,
pointsLoad: Points) {
super(containerExpr, pointsLoad);
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
const plot = await super.load(dbg, variable);
if (plot instanceof draw.Plot)
plot.plotStyle = draw.PlotStyle.Markers;
return plot;
}
}
export class Polygon extends Geometry {
constructor(
private _extEval: EvaluatedExpression,
private _intEval: EvaluatedExpression | undefined) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
const extStr = this._extEval.expression.toString(variable);
const extVar = new Variable(extStr, this._extEval.type);
if (this._extLoad === undefined) {
// TODO: only search for Ring
const loader = await getLoader(dbg, extVar);
if (loader instanceof Ring)
this._extLoad = loader;
}
if (this._extLoad === undefined)
return undefined;
const extRing = await this._extLoad.load(dbg, extVar);
if (!(extRing instanceof draw.Ring))
return undefined;
if (this._intEval === undefined)
return new draw.Polygon(extRing);
const isCw = extRing;
// TODO: Get Container loader and check the size here to avoid
// accessing element of empty container.
const intStr = this._intEval.expression.toString(variable);
const intVar = new Variable(intStr, this._intEval.type);
if (this._intLoad === undefined) {
// TODO: only search for Geometries or containers of Ring
const loader = await getLoader(dbg, intVar);
if (loader instanceof Geometries)
this._intLoad = loader;
}
if (this._intLoad === undefined) {
// TEMP: Possible reason for this is that Container is returned
// instead of Geometries above if size of Container is 0
// and it is not possible to access the first element to get
// the loader for elements.
// So for now instead of returning undefined here assume this is
// the case and return only the exterior ring.
return new draw.Polygon(extRing);
//return undefined;
}
const intRings = await this._intLoad.load(dbg, intVar);
if (!(intRings instanceof draw.Drawables))
return undefined;
// Would it be possible to cast drawables to Ring[] instead of creating new array?
let rings: draw.Ring[] = [];
for (let d of intRings.drawables) {
if (!(d instanceof draw.Ring))
return undefined;
rings.push(d as draw.Ring);
}
return new draw.Polygon(extRing, rings);
}
private _extLoad: Ring | undefined = undefined;
private _intLoad: Geometries | undefined = undefined;
}
export class MultiGeometry extends Geometry {
constructor(private _containerExpr: EvaluatedExpression,
private _geometriesLoad: Geometries) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
const contStr = this._containerExpr.expression.toString(variable);
const contVar = new Variable(contStr, this._containerExpr.type);
return this._geometriesLoad.load(dbg, contVar);
}
}
export class GeometryCollection extends Geometry {
constructor(private _containerExpr: EvaluatedExpression,
private _dynamicGeometriesLoad: DynamicGeometries) {
super();
}
async load(dbg: debug.Debugger, variable: Variable): Promise<draw.Drawable | undefined> {
const contStr = this._containerExpr.expression.toString(variable);
const contVar = new Variable(contStr, this._containerExpr.type);
return this._dynamicGeometriesLoad.load(dbg, contVar);
}
}
class LanguageTypes {
public kinds: Map<string, any[]> = new Map<string, any[]>();
public aliases: Map<string, string[]> = new Map<string, string[]>();
}
export class Types {
*matchWithAliases(type: string, lang: debug.Language, kinds: string[] | undefined = undefined) {
for (const entry of this.match(type, lang, kinds))
yield [entry, type];
for (const alias of this.aliases(type, lang))
for (const entry of this.match(alias, lang, kinds))
yield [entry, alias];
}
*match(type: string, lang: debug.Language, kinds: string[] | undefined = undefined) {
for (const entry of this.get(lang, kinds))
if (type.match('^' + entry.type + '$'))
yield entry;
}
// Technically this function returns original types as defined in the json file, not aliases
private *aliases(type: string, lang: debug.Language) {
for (const alias of this._aliases(this._languages, type, lang))
yield alias;
for (const alias of this._aliases(this._languagesUD, type, lang))
yield alias;
}
private *get(lang: debug.Language, kinds: string[] | undefined = undefined) {
for (const type of this._get(this._languages, lang, kinds))
yield type;
for (const type of this._get(this._languagesUD, lang, kinds))
yield type;
}
private *_aliases(map: Map<debug.Language, LanguageTypes>, type: string, lang: debug.Language) {
const lt = map.get(lang);
if (lt === undefined)
return;
const aliases = lt.aliases.get(type);
if (aliases === undefined)
return;
for (const alias of aliases)
yield alias;
}
private *_get(map: Map<debug.Language, LanguageTypes>, language: debug.Language, kinds: string[] | undefined = undefined) {
const lt = map.get(language);
if (lt === undefined)
return;
if (kinds === undefined) {
for (const k of lt.kinds) {
for (const t of k[1]) {
yield t;
}
}
} else {
for (const kind of kinds) {
const k = lt.kinds.get(kind);
if (k !== undefined) {
for (const t of k) {
yield t;
}
}
}
}
}
// TODO: update separate files only if needed
// TODO: load language files only if they match currently debugged language
update(dbg: debug.Debugger) {
{
const p = path.join(__filename, '..', '..', 'resources');
const files = this._jsonFiles(p, this._files);
if (files[1]) { // modified
this._languages.clear();
this._parseLanguages(files[0], this._languages);
}
}
{
let dir = vscode.workspace.getConfiguration().get<string>('graphicalDebugging.additionalTypesDirectory');
let p: string | undefined = undefined;
if (dir?.startsWith('.')) {
const workspaceDir = dbg.workspaceFolder();
if (workspaceDir)
p = path.join(workspaceDir, dir);
}
else {
p = dir;
}
if (p !== undefined) {
const files = this._jsonFiles(p, this._filesUD);
if (files[1]) { // modified
this._languagesUD.clear();
this._parseLanguages(files[0], this._languagesUD);
}
}
}
}
private _parseLanguages(filePaths: string[],
languages: Map<debug.Language, LanguageTypes>) {
const files = this._parseFiles(filePaths);
for (const file of files) {
const lang = this._stringToLanguage(file.language);
if (lang === undefined)
continue;
if (! languages.has(lang))
languages.set(lang, new LanguageTypes());
let language = languages.get(lang);
if (language === undefined) // silence TS
continue;
if (file.types !== undefined) {
for (const type of file.types) {
if (type.kind === undefined)
continue;
if (! language.kinds.has(type.kind))
language.kinds.set(type.kind, []);
let types = language.kinds.get(type.kind);
if (types === undefined) // silence TS
continue;
types.push(type);
}
}
if (file.aliases !== undefined) {
for (const alias of file.aliases) {
if (alias.name === undefined)
continue;
if (! language.aliases.has(alias.name))
language.aliases.set(alias.name, []);
let aliases = language.aliases.get(alias.name);
if (aliases === undefined) // silence TS
continue;
const typeWithSpaces = addSpacesInTemplates(alias.type);
aliases.push(typeWithSpaces);
}
}
}
}
// Parse list of files
private _parseFiles(filePaths: string[]): any[] {
let result: any[] = [];
for (const filePath of filePaths) {
try {
// TODO: In order to quickly check json files ideally
// only the top level should be read and parsed.
const f = fs.readFileSync(filePath, 'utf-8');
const o = JSON.parse(f);
if (o.name === 'graphicaldebugging') {
result.push(o);
}
} catch(err) {}
}
return result;