-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathLuaAST.ts
More file actions
863 lines (723 loc) · 25.9 KB
/
LuaAST.ts
File metadata and controls
863 lines (723 loc) · 25.9 KB
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
// Simplified Lua AST based roughly on http://lua-users.org/wiki/MetaLuaAbstractSyntaxTree,
// https://www.lua.org/manual/5.3/manual.html#9 and the TS AST implementation
// We can elide a lot of nodes especially tokens and keywords
// because we don't create the AST from text
import * as ts from "typescript";
import { LuaLibFeature } from "./LuaLib";
import { castArray } from "./utils";
export enum SyntaxKind {
File,
Block,
// Statements
DoStatement,
VariableDeclarationStatement,
AssignmentStatement,
IfStatement,
WhileStatement,
RepeatStatement,
ForStatement,
ForInStatement,
GotoStatement,
LabelStatement,
ReturnStatement,
BreakStatement,
ExpressionStatement,
// Expression
StringLiteral,
NumericLiteral,
NilKeyword,
DotsKeyword,
ArgKeyword,
TrueKeyword,
FalseKeyword,
FunctionExpression,
TableFieldExpression,
TableExpression,
UnaryExpression,
BinaryExpression,
CallExpression,
MethodCallExpression,
Identifier,
TableIndexExpression,
ParenthesizedExpression,
// Operators
// Arithmetic
AdditionOperator, // Maybe use abbreviations for those add, sub, mul ...
SubtractionOperator,
MultiplicationOperator,
DivisionOperator,
FloorDivisionOperator,
ModuloOperator,
PowerOperator,
NegationOperator, // Unary minus
// Concat
ConcatOperator,
// Length
LengthOperator, // Unary
// Relational Ops
EqualityOperator,
InequalityOperator,
LessThanOperator,
LessEqualOperator,
// Syntax Sugar `x > y` <=> `not (y <= x)`
// but we should probably use them to make the output code more readable
GreaterThanOperator,
GreaterEqualOperator, // Syntax Sugar `x >= y` <=> `not (y < x)`
// Logical
AndOperator,
OrOperator,
NotOperator, // Unary
// Bitwise
BitwiseAndOperator,
BitwiseOrOperator,
BitwiseExclusiveOrOperator,
BitwiseRightShiftOperator,
BitwiseLeftShiftOperator,
BitwiseNotOperator, // Unary
}
// TODO maybe name this PrefixUnary? not sure it makes sense to do so, because all unary ops in Lua are prefix
export type UnaryBitwiseOperator = SyntaxKind.BitwiseNotOperator;
export type UnaryOperator =
| SyntaxKind.NegationOperator
| SyntaxKind.LengthOperator
| SyntaxKind.NotOperator
| UnaryBitwiseOperator;
export type BinaryBitwiseOperator =
| SyntaxKind.BitwiseAndOperator
| SyntaxKind.BitwiseOrOperator
| SyntaxKind.BitwiseExclusiveOrOperator
| SyntaxKind.BitwiseRightShiftOperator
| SyntaxKind.BitwiseLeftShiftOperator;
export type BinaryOperator =
| SyntaxKind.AdditionOperator
| SyntaxKind.SubtractionOperator
| SyntaxKind.MultiplicationOperator
| SyntaxKind.DivisionOperator
| SyntaxKind.FloorDivisionOperator
| SyntaxKind.ModuloOperator
| SyntaxKind.PowerOperator
| SyntaxKind.ConcatOperator
| SyntaxKind.EqualityOperator
| SyntaxKind.InequalityOperator
| SyntaxKind.LessThanOperator
| SyntaxKind.LessEqualOperator
| SyntaxKind.GreaterThanOperator
| SyntaxKind.GreaterEqualOperator
| SyntaxKind.AndOperator
| SyntaxKind.OrOperator
| BinaryBitwiseOperator;
export type Operator = UnaryOperator | BinaryOperator;
export type SymbolId = number & { _symbolIdBrand: any };
export enum NodeFlags {
None = 0,
Inline = 1 << 0, // Keep function body on same line
Declaration = 1 << 1, // Prefer declaration syntax `function foo()` over assignment syntax `foo = function()`
TableUnpackCall = 1 << 2, // This is a table.unpack call
}
export interface TextRange {
line?: number;
column?: number;
}
export interface Node extends TextRange {
kind: SyntaxKind;
flags: NodeFlags;
}
export function createNode(kind: SyntaxKind, tsOriginal?: ts.Node): Node {
if (tsOriginal === undefined) {
return { kind, flags: NodeFlags.None };
}
const sourcePosition = getSourcePosition(tsOriginal);
if (sourcePosition) {
return { kind, line: sourcePosition.line, column: sourcePosition.column, flags: NodeFlags.None };
} else {
return { kind, flags: NodeFlags.None };
}
}
export function cloneNode<T extends Node>(node: T): T {
return { ...node };
}
export function setNodePosition<T extends Node>(node: T, position: TextRange): T {
node.line = position.line;
node.column = position.column;
return node;
}
export function setNodeOriginal<T extends Node>(node: T, tsOriginal: ts.Node): T;
export function setNodeOriginal<T extends Node>(node: T | undefined, tsOriginal: ts.Node): T | undefined;
export function setNodeOriginal<T extends Node>(node: T | undefined, tsOriginal: ts.Node): T | undefined {
if (node === undefined) {
return undefined;
}
const sourcePosition = getSourcePosition(tsOriginal);
if (sourcePosition) {
setNodePosition(node, sourcePosition);
}
return node;
}
function getSourcePosition(sourceNode: ts.Node): TextRange | undefined {
const parseTreeNode = ts.getParseTreeNode(sourceNode) ?? sourceNode;
const sourceFile = parseTreeNode.getSourceFile();
if (sourceFile !== undefined && parseTreeNode.pos >= 0) {
const { line, character } = ts.getLineAndCharacterOfPosition(
sourceFile,
parseTreeNode.pos + parseTreeNode.getLeadingTriviaWidth()
);
return { line, column: character };
}
}
export function getOriginalPos(node: Node): TextRange {
return { line: node.line, column: node.column };
}
export function setNodeFlags<T extends Node>(node: T, flags: NodeFlags): T {
node.flags = flags;
return node;
}
export interface File extends Node {
kind: SyntaxKind.File;
statements: Statement[];
luaLibFeatures: Set<LuaLibFeature>;
trivia: string;
}
export function isFile(node: Node): node is File {
return node.kind === SyntaxKind.File;
}
export function createFile(
statements: Statement[],
luaLibFeatures: Set<LuaLibFeature>,
trivia: string,
tsOriginal?: ts.Node
): File {
const file = createNode(SyntaxKind.File, tsOriginal) as File;
file.statements = statements;
file.luaLibFeatures = luaLibFeatures;
file.trivia = trivia;
return file;
}
export interface Block extends Node {
kind: SyntaxKind.Block;
statements: Statement[];
}
export function isBlock(node: Node): node is Block {
return node.kind === SyntaxKind.Block;
}
export function createBlock(statements: Statement[], tsOriginal?: ts.Node): Block {
const block = createNode(SyntaxKind.Block, tsOriginal) as Block;
block.statements = statements;
return block;
}
export interface Statement extends Node {
_statementBrand: any;
leadingComments?: Array<string | string[]>;
trailingComments?: Array<string | string[]>;
}
export interface DoStatement extends Statement {
kind: SyntaxKind.DoStatement;
statements: Statement[];
}
export function isDoStatement(node: Node): node is DoStatement {
return node.kind === SyntaxKind.DoStatement;
}
export function createDoStatement(statements: Statement[], tsOriginal?: ts.Node): DoStatement {
const statement = createNode(SyntaxKind.DoStatement, tsOriginal) as DoStatement;
statement.statements = statements;
return statement;
}
// `local test1, test2 = 12, 42` or `local test1, test2`
export interface VariableDeclarationStatement extends Statement {
kind: SyntaxKind.VariableDeclarationStatement;
left: Identifier[];
right?: Expression[];
}
export function isVariableDeclarationStatement(node: Node): node is VariableDeclarationStatement {
return node.kind === SyntaxKind.VariableDeclarationStatement;
}
export function createVariableDeclarationStatement(
left: Identifier | Identifier[],
right?: Expression | Expression[],
tsOriginal?: ts.Node
): VariableDeclarationStatement {
const statement = createNode(SyntaxKind.VariableDeclarationStatement, tsOriginal) as VariableDeclarationStatement;
statement.left = castArray(left);
if (right) statement.right = castArray(right);
return statement;
}
// `test1, test2 = 12, 42`
export interface AssignmentStatement extends Statement {
kind: SyntaxKind.AssignmentStatement;
left: AssignmentLeftHandSideExpression[];
right: Expression[];
}
export function isAssignmentStatement(node: Node): node is AssignmentStatement {
return node.kind === SyntaxKind.AssignmentStatement;
}
export function createAssignmentStatement(
left: AssignmentLeftHandSideExpression | AssignmentLeftHandSideExpression[],
right?: Expression | Expression[],
tsOriginal?: ts.Node
): AssignmentStatement {
const statement = createNode(SyntaxKind.AssignmentStatement, tsOriginal) as AssignmentStatement;
statement.left = castArray(left);
statement.right = right ? castArray(right) : [];
return statement;
}
export interface IfStatement extends Statement {
kind: SyntaxKind.IfStatement;
condition: Expression;
ifBlock: Block;
elseBlock?: Block | IfStatement;
}
export function isIfStatement(node: Node): node is IfStatement {
return node.kind === SyntaxKind.IfStatement;
}
export function createIfStatement(
condition: Expression,
ifBlock: Block,
elseBlock?: Block | IfStatement,
tsOriginal?: ts.Node
): IfStatement {
const statement = createNode(SyntaxKind.IfStatement, tsOriginal) as IfStatement;
statement.condition = condition;
statement.ifBlock = ifBlock;
statement.elseBlock = elseBlock;
return statement;
}
export interface IterationStatement extends Statement {
body: Block;
}
export function isIterationStatement(node: Node): node is IterationStatement {
return (
node.kind === SyntaxKind.WhileStatement ||
node.kind === SyntaxKind.RepeatStatement ||
node.kind === SyntaxKind.ForStatement ||
node.kind === SyntaxKind.ForInStatement
);
}
export interface WhileStatement extends IterationStatement {
kind: SyntaxKind.WhileStatement;
condition: Expression;
}
export function isWhileStatement(node: Node): node is WhileStatement {
return node.kind === SyntaxKind.WhileStatement;
}
export function createWhileStatement(body: Block, condition: Expression, tsOriginal?: ts.Node): WhileStatement {
const statement = createNode(SyntaxKind.WhileStatement, tsOriginal) as WhileStatement;
statement.body = body;
statement.condition = condition;
return statement;
}
export interface RepeatStatement extends IterationStatement {
kind: SyntaxKind.RepeatStatement;
condition: Expression;
}
export function isRepeatStatement(node: Node): node is RepeatStatement {
return node.kind === SyntaxKind.RepeatStatement;
}
export function createRepeatStatement(body: Block, condition: Expression, tsOriginal?: ts.Node): RepeatStatement {
const statement = createNode(SyntaxKind.RepeatStatement, tsOriginal) as RepeatStatement;
statement.body = body;
statement.condition = condition;
return statement;
}
// TODO maybe rename to ForNumericStatement
export interface ForStatement extends IterationStatement {
kind: SyntaxKind.ForStatement;
controlVariable: Identifier;
controlVariableInitializer: Expression;
limitExpression: Expression;
stepExpression?: Expression;
}
export function isForStatement(node: Node): node is ForStatement {
return node.kind === SyntaxKind.ForStatement;
}
export function createForStatement(
body: Block,
controlVariable: Identifier,
controlVariableInitializer: Expression,
limitExpression: Expression,
stepExpression?: Expression,
tsOriginal?: ts.Node
): ForStatement {
const statement = createNode(SyntaxKind.ForStatement, tsOriginal) as ForStatement;
statement.body = body;
statement.controlVariable = controlVariable;
statement.controlVariableInitializer = controlVariableInitializer;
statement.limitExpression = limitExpression;
statement.stepExpression = stepExpression;
return statement;
}
export interface ForInStatement extends IterationStatement {
kind: SyntaxKind.ForInStatement;
names: Identifier[];
expressions: Expression[];
}
export function isForInStatement(node: Node): node is ForInStatement {
return node.kind === SyntaxKind.ForInStatement;
}
export function createForInStatement(
body: Block,
names: Identifier[],
expressions: Expression[],
tsOriginal?: ts.Node
): ForInStatement {
const statement = createNode(SyntaxKind.ForInStatement, tsOriginal) as ForInStatement;
statement.body = body;
statement.names = names;
statement.expressions = expressions;
return statement;
}
export interface GotoStatement extends Statement {
kind: SyntaxKind.GotoStatement;
label: string; // or identifier ?
}
export function isGotoStatement(node: Node): node is GotoStatement {
return node.kind === SyntaxKind.GotoStatement;
}
export function createGotoStatement(label: string, tsOriginal?: ts.Node): GotoStatement {
const statement = createNode(SyntaxKind.GotoStatement, tsOriginal) as GotoStatement;
statement.label = label;
return statement;
}
export interface LabelStatement extends Statement {
kind: SyntaxKind.LabelStatement;
name: string; // or identifier ?
}
export function isLabelStatement(node: Node): node is LabelStatement {
return node.kind === SyntaxKind.LabelStatement;
}
export function createLabelStatement(name: string, tsOriginal?: ts.Node): LabelStatement {
const statement = createNode(SyntaxKind.LabelStatement, tsOriginal) as LabelStatement;
statement.name = name;
return statement;
}
export interface ReturnStatement extends Statement {
kind: SyntaxKind.ReturnStatement;
expressions: Expression[];
}
export function isReturnStatement(node: Node): node is ReturnStatement {
return node.kind === SyntaxKind.ReturnStatement;
}
export function createReturnStatement(expressions: Expression[], tsOriginal?: ts.Node): ReturnStatement {
const statement = createNode(SyntaxKind.ReturnStatement, tsOriginal) as ReturnStatement;
statement.expressions = expressions;
return statement;
}
export interface BreakStatement extends Statement {
kind: SyntaxKind.BreakStatement;
}
export function isBreakStatement(node: Node): node is BreakStatement {
return node.kind === SyntaxKind.BreakStatement;
}
export function createBreakStatement(tsOriginal?: ts.Node): BreakStatement {
return createNode(SyntaxKind.BreakStatement, tsOriginal) as BreakStatement;
}
export interface ExpressionStatement extends Statement {
kind: SyntaxKind.ExpressionStatement;
expression: Expression;
}
export function isExpressionStatement(node: Node): node is ExpressionStatement {
return node.kind === SyntaxKind.ExpressionStatement;
}
export function createExpressionStatement(expressions: Expression, tsOriginal?: ts.Node): ExpressionStatement {
const statement = createNode(SyntaxKind.ExpressionStatement, tsOriginal) as ExpressionStatement;
statement.expression = expressions;
return statement;
}
export interface Expression extends Node {
_expressionBrand: any;
}
// Expressions
// TODO maybe create subexport interface for Literals/PrimaryExpressions
export interface NilLiteral extends Expression {
kind: SyntaxKind.NilKeyword;
}
export function isNilLiteral(node: Node): node is NilLiteral {
return node.kind === SyntaxKind.NilKeyword;
}
export function createNilLiteral(tsOriginal?: ts.Node): NilLiteral {
return createNode(SyntaxKind.NilKeyword, tsOriginal) as NilLiteral;
}
export interface BooleanLiteral extends Expression {
kind: SyntaxKind.TrueKeyword | SyntaxKind.FalseKeyword;
}
export function isBooleanLiteral(node: Node): node is BooleanLiteral {
return node.kind === SyntaxKind.TrueKeyword || node.kind === SyntaxKind.FalseKeyword;
}
export function createBooleanLiteral(value: boolean, tsOriginal?: ts.Node): BooleanLiteral {
return createNode(value ? SyntaxKind.TrueKeyword : SyntaxKind.FalseKeyword, tsOriginal) as BooleanLiteral;
}
// TODO Call this DotsLiteral or DotsKeyword?
export interface DotsLiteral extends Expression {
kind: SyntaxKind.DotsKeyword;
}
export function isDotsLiteral(node: Node): node is DotsLiteral {
return node.kind === SyntaxKind.DotsKeyword;
}
export function createDotsLiteral(tsOriginal?: ts.Node): DotsLiteral {
return createNode(SyntaxKind.DotsKeyword, tsOriginal) as DotsLiteral;
}
export interface ArgLiteral extends Expression {
kind: SyntaxKind.ArgKeyword;
}
export function isArgLiteral(node: Node): node is ArgLiteral {
return node.kind === SyntaxKind.ArgKeyword;
}
export function createArgLiteral(tsOriginal?: ts.Node): ArgLiteral {
return createNode(SyntaxKind.ArgKeyword, tsOriginal) as ArgLiteral;
}
// StringLiteral / NumberLiteral
// TODO TS uses the export interface "LiteralLikeNode" with a "text: string" member
// but since we don't parse from text I think we can simplify by just having a value member
// TODO NumericLiteral or NumberLiteral?
export interface NumericLiteral extends Expression {
kind: SyntaxKind.NumericLiteral;
value: number;
}
export function isNumericLiteral(node: Node): node is NumericLiteral {
return node.kind === SyntaxKind.NumericLiteral;
}
export function createNumericLiteral(value: number, tsOriginal?: ts.Node): NumericLiteral {
const expression = createNode(SyntaxKind.NumericLiteral, tsOriginal) as NumericLiteral;
expression.value = value;
return expression;
}
export interface StringLiteral extends Expression {
kind: SyntaxKind.StringLiteral;
value: string;
}
export function isStringLiteral(node: Node): node is StringLiteral {
return node.kind === SyntaxKind.StringLiteral;
}
export function createStringLiteral(value: string, tsOriginal?: ts.Node): StringLiteral {
const expression = createNode(SyntaxKind.StringLiteral, tsOriginal) as StringLiteral;
expression.value = value;
return expression;
}
export function isLiteral(
node: Node
): node is NilLiteral | DotsLiteral | ArgLiteral | BooleanLiteral | NumericLiteral | StringLiteral {
return (
isNilLiteral(node) ||
isDotsLiteral(node) ||
isArgLiteral(node) ||
isBooleanLiteral(node) ||
isNumericLiteral(node) ||
isStringLiteral(node)
);
}
export interface FunctionExpression extends Expression {
kind: SyntaxKind.FunctionExpression;
params?: Identifier[];
dots?: DotsLiteral;
body: Block;
}
export function isFunctionExpression(node: Node): node is FunctionExpression {
return node.kind === SyntaxKind.FunctionExpression;
}
export function createFunctionExpression(
body: Block,
params?: Identifier[],
dots?: DotsLiteral,
flags = NodeFlags.None,
tsOriginal?: ts.Node
): FunctionExpression {
const expression = createNode(SyntaxKind.FunctionExpression, tsOriginal) as FunctionExpression;
expression.body = body;
expression.params = params;
expression.dots = dots;
expression.flags = flags;
return expression;
}
export interface TableFieldExpression extends Expression {
kind: SyntaxKind.TableFieldExpression;
value: Expression;
key?: Expression;
}
export function isTableFieldExpression(node: Node): node is TableFieldExpression {
return node.kind === SyntaxKind.TableFieldExpression;
}
export function createTableFieldExpression(
value: Expression,
key?: Expression,
tsOriginal?: ts.Node
): TableFieldExpression {
const expression = createNode(SyntaxKind.TableFieldExpression, tsOriginal) as TableFieldExpression;
expression.value = value;
expression.key = key;
return expression;
}
export interface TableExpression extends Expression {
kind: SyntaxKind.TableExpression;
fields: TableFieldExpression[];
}
export function isTableExpression(node: Node): node is TableExpression {
return node.kind === SyntaxKind.TableExpression;
}
export function createTableExpression(fields: TableFieldExpression[] = [], tsOriginal?: ts.Node): TableExpression {
const expression = createNode(SyntaxKind.TableExpression, tsOriginal) as TableExpression;
expression.fields = fields;
return expression;
}
export interface UnaryExpression extends Expression {
kind: SyntaxKind.UnaryExpression;
operand: Expression;
operator: UnaryOperator;
}
export function isUnaryExpression(node: Node): node is UnaryExpression {
return node.kind === SyntaxKind.UnaryExpression;
}
export function createUnaryExpression(
operand: Expression,
operator: UnaryOperator,
tsOriginal?: ts.Node
): UnaryExpression {
const expression = createNode(SyntaxKind.UnaryExpression, tsOriginal) as UnaryExpression;
expression.operand = operand;
expression.operator = operator;
return expression;
}
export interface BinaryExpression extends Expression {
kind: SyntaxKind.BinaryExpression;
operator: BinaryOperator;
left: Expression;
right: Expression;
}
export function isBinaryExpression(node: Node): node is BinaryExpression {
return node.kind === SyntaxKind.BinaryExpression;
}
export function createBinaryExpression(
left: Expression,
right: Expression,
operator: BinaryOperator,
tsOriginal?: ts.Node
): BinaryExpression {
const expression = createNode(SyntaxKind.BinaryExpression, tsOriginal) as BinaryExpression;
expression.left = left;
expression.right = right;
expression.operator = operator;
return expression;
}
export interface CallExpression extends Expression {
kind: SyntaxKind.CallExpression;
expression: Expression;
params: Expression[];
}
export function isCallExpression(node: Node): node is CallExpression {
return node.kind === SyntaxKind.CallExpression;
}
export function createCallExpression(
expression: Expression,
params: Expression[],
tsOriginal?: ts.Node
): CallExpression {
const callExpression = createNode(SyntaxKind.CallExpression, tsOriginal) as CallExpression;
callExpression.expression = expression;
callExpression.params = params;
return callExpression;
}
export interface MethodCallExpression extends Expression {
kind: SyntaxKind.MethodCallExpression;
prefixExpression: Expression;
name: Identifier;
params: Expression[];
}
export function isMethodCallExpression(node: Node): node is MethodCallExpression {
return node.kind === SyntaxKind.MethodCallExpression;
}
export function createMethodCallExpression(
prefixExpression: Expression,
name: Identifier,
params: Expression[],
tsOriginal?: ts.Node
): MethodCallExpression {
const callExpression = createNode(SyntaxKind.MethodCallExpression, tsOriginal) as MethodCallExpression;
callExpression.prefixExpression = prefixExpression;
callExpression.name = name;
callExpression.params = params;
return callExpression;
}
export interface Identifier extends Expression {
kind: SyntaxKind.Identifier;
exportable: boolean;
text: string;
originalName?: string;
symbolId?: SymbolId;
}
export function isIdentifier(node: Node): node is Identifier {
return node.kind === SyntaxKind.Identifier;
}
export function createIdentifier(
text: string,
tsOriginal?: ts.Node,
symbolId?: SymbolId,
originalName?: string
): Identifier {
const expression = createNode(SyntaxKind.Identifier, tsOriginal) as Identifier;
expression.exportable = true;
expression.text = text;
expression.symbolId = symbolId;
expression.originalName = originalName;
return expression;
}
export function cloneIdentifier(identifier: Identifier, tsOriginal?: ts.Node): Identifier {
return createIdentifier(identifier.text, tsOriginal, identifier.symbolId, identifier.originalName);
}
export function createAnonymousIdentifier(tsOriginal?: ts.Node): Identifier {
const expression = createNode(SyntaxKind.Identifier, tsOriginal) as Identifier;
expression.exportable = false;
expression.text = "____";
return expression;
}
export interface TableIndexExpression extends Expression {
kind: SyntaxKind.TableIndexExpression;
table: Expression;
index: Expression;
}
export function isTableIndexExpression(node: Node): node is TableIndexExpression {
return node.kind === SyntaxKind.TableIndexExpression;
}
export function createTableIndexExpression(
table: Expression,
index: Expression,
tsOriginal?: ts.Node
): TableIndexExpression {
const expression = createNode(SyntaxKind.TableIndexExpression, tsOriginal) as TableIndexExpression;
expression.table = table;
expression.index = index;
return expression;
}
export type AssignmentLeftHandSideExpression = Identifier | TableIndexExpression;
export function isAssignmentLeftHandSideExpression(node: Node): node is AssignmentLeftHandSideExpression {
return isIdentifier(node) || isTableIndexExpression(node);
}
export type FunctionDefinition = (VariableDeclarationStatement | AssignmentStatement) & {
right: [FunctionExpression];
};
export function isFunctionDefinition(
statement: VariableDeclarationStatement | AssignmentStatement
): statement is FunctionDefinition {
return statement.left.length === 1 && statement.right?.length === 1 && isFunctionExpression(statement.right[0]);
}
export type InlineFunctionExpression = FunctionExpression & {
body: { statements: [ReturnStatement & { expressions: Expression[] }] };
};
export function isInlineFunctionExpression(expression: FunctionExpression): expression is InlineFunctionExpression {
return (
expression.body.statements?.length === 1 &&
isReturnStatement(expression.body.statements[0]) &&
expression.body.statements[0].expressions !== undefined &&
(expression.flags & NodeFlags.Inline) !== 0
);
}
export type ParenthesizedExpression = Expression & {
expression: Expression;
};
export function isParenthesizedExpression(node: Node): node is ParenthesizedExpression {
return node.kind === SyntaxKind.ParenthesizedExpression;
}
export function createParenthesizedExpression(expression: Expression, tsOriginal?: ts.Node): ParenthesizedExpression {
const parenthesizedExpression = createNode(
SyntaxKind.ParenthesizedExpression,
tsOriginal
) as ParenthesizedExpression;
parenthesizedExpression.expression = expression;
return parenthesizedExpression;
}