-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathExpression.ts
1449 lines (1292 loc) · 46.8 KB
/
Expression.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
/* eslint-disable no-bitwise */
import type { Token, Identifier } from '../lexer';
import { TokenKind } from '../lexer';
import type { Block, CommentStatement, FunctionStatement } from './Statement';
import type { Range } from 'vscode-languageserver';
import util from '../util';
import type { BrsTranspileState } from './BrsTranspileState';
import { ParseMode } from './Parser';
import * as fileUrl from 'file-url';
import type { WalkOptions, WalkVisitor } from '../astUtils/visitors';
import { walk, InternalWalkMode } from '../astUtils/visitors';
import { isAALiteralExpression, isArrayLiteralExpression, isCallExpression, isCallfuncExpression, isCommentStatement, isDottedGetExpression, isEscapedCharCodeLiteralExpression, isIntegerType, isLiteralBoolean, isLiteralExpression, isLiteralNumber, isLiteralString, isLongIntegerType, isStringType, isUnaryExpression, isVariableExpression } from '../astUtils/reflection';
import type { TranspileResult, TypedefProvider } from '../interfaces';
import { VoidType } from '../types/VoidType';
import { DynamicType } from '../types/DynamicType';
import type { BscType } from '../types/BscType';
export type ExpressionVisitor = (expression: Expression, parent: Expression) => void;
/** A BrightScript expression */
export abstract class Expression {
/**
* The starting and ending location of the expression.
*/
public abstract range: Range;
public abstract transpile(state: BrsTranspileState): TranspileResult;
/**
* When being considered by the walk visitor, this describes what type of element the current class is.
*/
public visitMode = InternalWalkMode.visitExpressions;
public abstract walk(visitor: WalkVisitor, options: WalkOptions);
}
export class BinaryExpression extends Expression {
constructor(
public left: Expression,
public operator: Token,
public right: Expression
) {
super();
this.range = util.createRangeFromPositions(this.left.range.start, this.right.range.end);
}
public readonly range: Range;
transpile(state: BrsTranspileState) {
return [
state.sourceNode(this.left, this.left.transpile(state)),
' ',
state.transpileToken(this.operator),
' ',
state.sourceNode(this.right, this.right.transpile(state))
];
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
walk(this, 'left', visitor, options);
walk(this, 'right', visitor, options);
}
}
}
export class CallExpression extends Expression {
static MaximumArguments = 32;
constructor(
readonly callee: Expression,
readonly openingParen: Token,
readonly closingParen: Token,
readonly args: Expression[],
/**
* The namespace that currently wraps this call expression. This is NOT the namespace of the callee...that will be represented in the callee expression itself.
*/
readonly namespaceName: NamespacedVariableNameExpression
) {
super();
this.range = util.createRangeFromPositions(this.callee.range.start, this.closingParen.range.end);
}
public readonly range: Range;
transpile(state: BrsTranspileState, nameOverride?: string) {
let result = [];
//transpile the name
if (nameOverride) {
result.push(state.sourceNode(this.callee, nameOverride));
} else {
result.push(...this.callee.transpile(state));
}
result.push(
state.transpileToken(this.openingParen)
);
for (let i = 0; i < this.args.length; i++) {
//add comma between args
if (i > 0) {
result.push(', ');
}
let arg = this.args[i];
result.push(...arg.transpile(state));
}
result.push(
state.transpileToken(this.closingParen)
);
return result;
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
walk(this, 'callee', visitor, options);
for (let i = 0; i < this.args.length; i++) {
walk(this.args, i, visitor, options, this);
}
}
}
}
export class FunctionExpression extends Expression implements TypedefProvider {
constructor(
readonly parameters: FunctionParameterExpression[],
public body: Block,
readonly functionType: Token | null,
public end: Token,
readonly leftParen: Token,
readonly rightParen: Token,
readonly asToken?: Token,
readonly returnTypeToken?: Token,
/**
* If this function is enclosed within another function, this will reference that parent function
*/
readonly parentFunction?: FunctionExpression,
readonly namespaceName?: NamespacedVariableNameExpression
) {
super();
if (this.returnTypeToken) {
this.returnType = util.tokenToBscType(this.returnTypeToken);
} else if (this.functionType.text.toLowerCase() === 'sub') {
this.returnType = new VoidType();
} else {
this.returnType = new DynamicType();
}
}
/**
* The type this function returns
*/
public returnType: BscType;
/**
* The list of function calls that are declared within this function scope. This excludes CallExpressions
* declared in child functions
*/
public callExpressions = [] as CallExpression[];
/**
* If this function is part of a FunctionStatement, this will be set. Otherwise this will be undefined
*/
public functionStatement?: FunctionStatement;
/**
* A list of all child functions declared directly within this function
*/
public childFunctionExpressions = [] as FunctionExpression[];
/**
* The range of the function, starting at the 'f' in function or 's' in sub (or the open paren if the keyword is missing),
* and ending with the last n' in 'end function' or 'b' in 'end sub'
*/
public get range() {
return util.createRangeFromPositions(
(this.functionType ?? this.leftParen).range.start,
(this.end ?? this.body ?? this.returnTypeToken ?? this.asToken ?? this.rightParen).range.end
);
}
transpile(state: BrsTranspileState, name?: Identifier, includeBody = true) {
let results = [];
//'function'|'sub'
results.push(
state.transpileToken(this.functionType)
);
//functionName?
if (name) {
results.push(
' ',
state.transpileToken(name)
);
}
//leftParen
results.push(
state.transpileToken(this.leftParen)
);
//parameters
for (let i = 0; i < this.parameters.length; i++) {
let param = this.parameters[i];
//add commas
if (i > 0) {
results.push(', ');
}
//add parameter
results.push(param.transpile(state));
}
//right paren
results.push(
state.transpileToken(this.rightParen)
);
//as [Type]
if (this.asToken) {
results.push(
' ',
//as
state.transpileToken(this.asToken),
' ',
//return type
state.sourceNode(this.returnTypeToken, this.returnType.toTypeString())
);
}
if (includeBody) {
state.lineage.unshift(this);
let body = this.body.transpile(state);
state.lineage.shift();
results.push(...body);
}
results.push('\n');
//'end sub'|'end function'
results.push(
state.indent(),
state.transpileToken(this.end)
);
return results;
}
getTypedef(state: BrsTranspileState, name?: Identifier) {
return this.transpile(state, name, false);
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
for (let i = 0; i < this.parameters.length; i++) {
walk(this.parameters, i, visitor, options, this);
}
//This is the core of full-program walking...it allows us to step into sub functions
if (options.walkMode & InternalWalkMode.recurseChildFunctions) {
walk(this, 'body', visitor, options);
}
}
}
}
export class FunctionParameterExpression extends Expression {
constructor(
public name: Identifier,
public typeToken?: Token,
public defaultValue?: Expression,
public asToken?: Token,
readonly namespaceName?: NamespacedVariableNameExpression
) {
super();
if (typeToken) {
this.type = util.tokenToBscType(typeToken);
} else {
this.type = new DynamicType();
}
}
public type: BscType;
public get range(): Range {
return {
start: this.name.range.start,
end: this.typeToken ? this.typeToken.range.end : this.name.range.end
};
}
public transpile(state: BrsTranspileState) {
let result = [
//name
state.transpileToken(this.name)
] as any[];
//default value
if (this.defaultValue) {
result.push(' = ');
result.push(this.defaultValue.transpile(state));
}
//type declaration
if (this.asToken) {
result.push(' ');
result.push(state.transpileToken(this.asToken));
result.push(' ');
result.push(state.sourceNode(this.typeToken, this.type.toTypeString()));
}
return result;
}
walk(visitor: WalkVisitor, options: WalkOptions) {
// eslint-disable-next-line no-bitwise
if (this.defaultValue && options.walkMode & InternalWalkMode.walkExpressions) {
walk(this, 'defaultValue', visitor, options);
}
}
}
export class NamespacedVariableNameExpression extends Expression {
constructor(
//if this is a `DottedGetExpression`, it must be comprised only of `VariableExpression`s
readonly expression: DottedGetExpression | VariableExpression
) {
super();
this.range = expression.range;
}
range: Range;
transpile(state: BrsTranspileState) {
return [
state.sourceNode(this, this.getName(ParseMode.BrightScript))
];
}
public getNameParts() {
let parts = [] as string[];
if (isVariableExpression(this.expression)) {
parts.push(this.expression.name.text);
} else {
let expr = this.expression;
parts.push(expr.name.text);
while (isVariableExpression(expr) === false) {
expr = expr.obj as DottedGetExpression;
parts.unshift(expr.name.text);
}
}
return parts;
}
getName(parseMode: ParseMode) {
if (parseMode === ParseMode.BrighterScript) {
return this.getNameParts().join('.');
} else {
return this.getNameParts().join('_');
}
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
walk(this, 'expression', visitor, options);
}
}
}
export class DottedGetExpression extends Expression {
constructor(
readonly obj: Expression,
readonly name: Identifier,
readonly dot: Token
) {
super();
this.range = util.createRangeFromPositions(this.obj.range.start, this.name.range.end);
}
public readonly range: Range;
transpile(state: BrsTranspileState) {
//if the callee starts with a namespace name, transpile the name
if (state.file.calleeStartsWithNamespace(this)) {
return new NamespacedVariableNameExpression(this as DottedGetExpression | VariableExpression).transpile(state);
} else {
return [
...this.obj.transpile(state),
'.',
state.transpileToken(this.name)
];
}
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
walk(this, 'obj', visitor, options);
}
}
}
export class XmlAttributeGetExpression extends Expression {
constructor(
readonly obj: Expression,
readonly name: Identifier,
readonly at: Token
) {
super();
this.range = util.createRangeFromPositions(this.obj.range.start, this.name.range.end);
}
public readonly range: Range;
transpile(state: BrsTranspileState) {
return [
...this.obj.transpile(state),
'@',
state.transpileToken(this.name)
];
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
walk(this, 'obj', visitor, options);
}
}
}
export class IndexedGetExpression extends Expression {
constructor(
readonly obj: Expression,
readonly index: Expression,
readonly openingSquare: Token,
readonly closingSquare: Token
) {
super();
this.range = util.createRangeFromPositions(this.obj.range.start, this.closingSquare.range.end);
}
public readonly range: Range;
transpile(state: BrsTranspileState) {
return [
...this.obj.transpile(state),
state.transpileToken(this.openingSquare),
...this.index.transpile(state),
state.transpileToken(this.closingSquare)
];
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
walk(this, 'obj', visitor, options);
walk(this, 'index', visitor, options);
}
}
}
export class GroupingExpression extends Expression {
constructor(
readonly tokens: {
left: Token;
right: Token;
},
public expression: Expression
) {
super();
this.range = util.createRangeFromPositions(this.tokens.left.range.start, this.tokens.right.range.end);
}
public readonly range: Range;
transpile(state: BrsTranspileState) {
return [
state.transpileToken(this.tokens.left),
...this.expression.transpile(state),
state.transpileToken(this.tokens.right)
];
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
walk(this, 'expression', visitor, options);
}
}
}
export class LiteralExpression extends Expression {
constructor(
public token: Token
) {
super();
this.type = util.tokenToBscType(token);
}
public get range() {
return this.token.range;
}
/**
* The (data) type of this expression
*/
public type: BscType;
transpile(state: BrsTranspileState) {
let text: string;
if (this.token.kind === TokenKind.TemplateStringQuasi) {
//wrap quasis with quotes (and escape inner quotemarks)
text = `"${this.token.text.replace(/"/g, '""')}"`;
} else if (isStringType(this.type)) {
text = this.token.text;
//add trailing quotemark if it's missing. We will have already generated a diagnostic for this.
if (text.endsWith('"') === false) {
text += '"';
}
} else {
text = this.token.text;
}
return [
state.sourceNode(this, text)
];
}
walk(visitor: WalkVisitor, options: WalkOptions) {
//nothing to walk
}
}
/**
* This is a special expression only used within template strings. It exists so we can prevent producing lots of empty strings
* during template string transpile by identifying these expressions explicitly and skipping the bslib_toString around them
*/
export class EscapedCharCodeLiteralExpression extends Expression {
constructor(
readonly token: Token & { charCode: number }
) {
super();
this.range = token.range;
}
readonly range: Range;
transpile(state: BrsTranspileState) {
return [
state.sourceNode(this, `chr(${this.token.charCode})`)
];
}
walk(visitor: WalkVisitor, options: WalkOptions) {
//nothing to walk
}
}
export class ArrayLiteralExpression extends Expression {
constructor(
readonly elements: Array<Expression | CommentStatement>,
readonly open: Token,
readonly close: Token,
readonly hasSpread = false
) {
super();
this.range = util.createRangeFromPositions(this.open.range.start, this.close.range.end);
}
public readonly range: Range;
transpile(state: BrsTranspileState) {
let result = [];
result.push(
state.transpileToken(this.open)
);
let hasChildren = this.elements.length > 0;
state.blockDepth++;
for (let i = 0; i < this.elements.length; i++) {
let previousElement = this.elements[i - 1];
let element = this.elements[i];
if (isCommentStatement(element)) {
//if the comment is on the same line as opening square or previous statement, don't add newline
if (util.linesTouch(this.open, element) || util.linesTouch(previousElement, element)) {
result.push(' ');
} else {
result.push(
'\n',
state.indent()
);
}
state.lineage.unshift(this);
result.push(element.transpile(state));
state.lineage.shift();
} else {
result.push('\n');
result.push(
state.indent(),
...element.transpile(state)
);
//add a comma if we know there will be another non-comment statement after this
for (let j = i + 1; j < this.elements.length; j++) {
let el = this.elements[j];
//add a comma if there will be another element after this
if (isCommentStatement(el) === false) {
result.push(',');
break;
}
}
}
}
state.blockDepth--;
//add a newline between open and close if there are elements
if (hasChildren) {
result.push('\n');
result.push(state.indent());
}
result.push(
state.transpileToken(this.close)
);
return result;
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
for (let i = 0; i < this.elements.length; i++) {
walk(this.elements, i, visitor, options, this);
}
}
}
}
export class AAMemberExpression extends Expression {
constructor(
public keyToken: Token,
public colonToken: Token,
/** The expression evaluated to determine the member's initial value. */
public value: Expression
) {
super();
this.range = util.createRangeFromPositions(keyToken.range.start, this.value.range.end);
}
public range: Range;
transpile(state: BrsTranspileState) {
//TODO move the logic from AALiteralExpression loop into this function
return [];
}
walk(visitor: WalkVisitor, options: WalkOptions) {
walk(this, 'value', visitor, options);
}
}
export class AALiteralExpression extends Expression {
constructor(
readonly elements: Array<AAMemberExpression | CommentStatement>,
readonly open: Token,
readonly close: Token
) {
super();
this.range = util.createRangeFromPositions(this.open.range.start, this.close.range.end);
}
public readonly range: Range;
transpile(state: BrsTranspileState) {
let result = [];
//open curly
result.push(
state.transpileToken(this.open)
);
let hasChildren = this.elements.length > 0;
//add newline if the object has children and the first child isn't a comment starting on the same line as opening curly
if (hasChildren && (isCommentStatement(this.elements[0]) === false || !util.linesTouch(this.elements[0], this.open))) {
result.push('\n');
}
state.blockDepth++;
for (let i = 0; i < this.elements.length; i++) {
let element = this.elements[i];
let previousElement = this.elements[i - 1];
let nextElement = this.elements[i + 1];
//don't indent if comment is same-line
if (isCommentStatement(element as any) &&
(util.linesTouch(this.open, element) || util.linesTouch(previousElement, element))
) {
result.push(' ');
//indent line
} else {
result.push(state.indent());
}
//render comments
if (isCommentStatement(element)) {
result.push(...element.transpile(state));
} else {
//key
result.push(
state.transpileToken(element.keyToken)
);
//colon
result.push(
state.transpileToken(element.colonToken),
' '
);
//determine if comments are the only members left in the array
let onlyCommentsRemaining = true;
for (let j = i + 1; j < this.elements.length; j++) {
if (isCommentStatement(this.elements[j]) === false) {
onlyCommentsRemaining = false;
break;
}
}
//value
result.push(...element.value.transpile(state));
//add trailing comma if not final element (excluding comments)
if (i !== this.elements.length - 1 && onlyCommentsRemaining === false) {
result.push(',');
}
}
//if next element is a same-line comment, skip the newline
if (nextElement && isCommentStatement(nextElement) && nextElement.range.start.line === element.range.start.line) {
//add a newline between statements
} else {
result.push('\n');
}
}
state.blockDepth--;
//only indent the closing curly if we have children
if (hasChildren) {
result.push(state.indent());
}
//close curly
result.push(
state.transpileToken(this.close)
);
return result;
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
for (let i = 0; i < this.elements.length; i++) {
if (isCommentStatement(this.elements[i])) {
walk(this.elements, i, visitor, options, this);
} else {
walk(this.elements, i, visitor, options, this);
}
}
}
}
}
export class UnaryExpression extends Expression {
constructor(
public operator: Token,
public right: Expression
) {
super();
this.range = util.createRangeFromPositions(this.operator.range.start, this.right.range.end);
}
public readonly range: Range;
transpile(state: BrsTranspileState) {
return [
state.transpileToken(this.operator),
' ',
...this.right.transpile(state)
];
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
walk(this, 'right', visitor, options);
}
}
}
export class VariableExpression extends Expression {
constructor(
readonly name: Identifier,
readonly namespaceName: NamespacedVariableNameExpression
) {
super();
this.range = this.name.range;
}
public readonly range: Range;
public isCalled: boolean;
public getName(parseMode: ParseMode) {
return parseMode === ParseMode.BrightScript ? this.name.text : this.name.text;
}
transpile(state: BrsTranspileState) {
let result = [];
//if the callee is the name of a known namespace function
if (state.file.calleeIsKnownNamespaceFunction(this, this.namespaceName?.getName(ParseMode.BrighterScript))) {
result.push(
state.sourceNode(this, [
this.namespaceName.getName(ParseMode.BrightScript),
'_',
this.getName(ParseMode.BrightScript)
])
);
//transpile normally
} else {
result.push(
state.transpileToken(this.name)
);
}
return result;
}
walk(visitor: WalkVisitor, options: WalkOptions) {
//nothing to walk
}
}
export class SourceLiteralExpression extends Expression {
constructor(
readonly token: Token
) {
super();
this.range = token.range;
}
public readonly range: Range;
private getFunctionName(state: BrsTranspileState, parseMode: ParseMode) {
let func = state.file.getFunctionScopeAtPosition(this.token.range.start).func;
let nameParts = [];
while (func.parentFunction) {
let index = func.parentFunction.childFunctionExpressions.indexOf(func);
nameParts.unshift(`anon${index}`);
func = func.parentFunction;
}
//get the index of this function in its parent
nameParts.unshift(
func.functionStatement.getName(parseMode)
);
return nameParts.join('$');
}
transpile(state: BrsTranspileState) {
let text: string;
switch (this.token.kind) {
case TokenKind.SourceFilePathLiteral:
text = `"${fileUrl(state.srcPath)}"`;
break;
case TokenKind.SourceLineNumLiteral:
text = `${this.token.range.start.line + 1}`;
break;
case TokenKind.FunctionNameLiteral:
text = `"${this.getFunctionName(state, ParseMode.BrightScript)}"`;
break;
case TokenKind.SourceFunctionNameLiteral:
text = `"${this.getFunctionName(state, ParseMode.BrighterScript)}"`;
break;
case TokenKind.SourceLocationLiteral:
text = `"${fileUrl(state.srcPath)}:${this.token.range.start.line + 1}"`;
break;
case TokenKind.PkgPathLiteral:
let pkgPath1 = `pkg:/${state.file.pkgPath}`
.replace(/\\/g, '/')
.replace(/\.bs$/i, '.brs');
text = `"${pkgPath1}"`;
break;
case TokenKind.PkgLocationLiteral:
let pkgPath2 = `pkg:/${state.file.pkgPath}`
.replace(/\\/g, '/')
.replace(/\.bs$/i, '.brs');
text = `"${pkgPath2}:" + str(LINE_NUM)`;
break;
case TokenKind.LineNumLiteral:
default:
//use the original text (because it looks like a variable)
text = this.token.text;
break;
}
return [
state.sourceNode(this, text)
];
}
walk(visitor: WalkVisitor, options: WalkOptions) {
//nothing to walk
}
}
/**
* This expression transpiles and acts exactly like a CallExpression,
* except we need to uniquely identify these statements so we can
* do more type checking.
*/
export class NewExpression extends Expression {
constructor(
readonly newKeyword: Token,
readonly call: CallExpression
) {
super();
this.range = util.createRangeFromPositions(this.newKeyword.range.start, this.call.range.end);
}
/**
* The name of the class to initialize (with optional namespace prefixed)
*/
public get className() {
//the parser guarantees the callee of a new statement's call object will be
//a NamespacedVariableNameExpression
return this.call.callee as NamespacedVariableNameExpression;
}
public get namespaceName() {
return this.call.namespaceName;
}
public readonly range: Range;
public transpile(state: BrsTranspileState) {
const cls = state.file.getClassFileLink(
this.className.getName(ParseMode.BrighterScript),
this.namespaceName?.getName(ParseMode.BrighterScript)
)?.item;
//new statements within a namespace block can omit the leading namespace if the class resides in that same namespace.
//So we need to figure out if this is a namespace-omitted class, or if this class exists without a namespace.
return this.call.transpile(state, cls?.getName(ParseMode.BrightScript));
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
walk(this, 'call', visitor, options);
}
}
}
export class CallfuncExpression extends Expression {
constructor(
readonly callee: Expression,
readonly operator: Token,
readonly methodName: Identifier,
readonly openingParen: Token,
readonly args: Expression[],
readonly closingParen: Token
) {
super();
this.range = util.createRangeFromPositions(
callee.range.start,
(closingParen ?? args[args.length - 1] ?? openingParen ?? methodName ?? operator).range.end
);
}
public readonly range: Range;
public transpile(state: BrsTranspileState) {
let result = [];
result.push(
...this.callee.transpile(state),
state.sourceNode(this.operator, '.callfunc'),
state.transpileToken(this.openingParen),
//the name of the function
state.sourceNode(this.methodName, ['"', this.methodName.text, '"']),
', '
);
//transpile args
//callfunc with zero args never gets called, so pass invalid as the first parameter if there are no args
if (this.args.length === 0) {
result.push('invalid');
} else {
for (let i = 0; i < this.args.length; i++) {
//add comma between args
if (i > 0) {
result.push(', ');
}
let arg = this.args[i];
result.push(...arg.transpile(state));
}
}
result.push(
state.transpileToken(this.closingParen)
);
return result;
}
walk(visitor: WalkVisitor, options: WalkOptions) {
if (options.walkMode & InternalWalkMode.walkExpressions) {
walk(this, 'callee', visitor, options);
for (let i = 0; i < this.args.length; i++) {
walk(this.args, i, visitor, options, this);
}
}
}
}
/**
* Since template strings can contain newlines, we need to concatenate multiple strings together with chr() calls.
* This is a single expression that represents the string contatenation of all parts of a single quasi.
*/
export class TemplateStringQuasiExpression extends Expression {