-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.ts
209 lines (193 loc) · 3.48 KB
/
ast.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
/**
* A node type
* @example
* let x = 5;
* // => Program
* @example
* 5
* // => NumericLiteral
* @example
* x
* // => Identifier
*/
export type NodeType =
// Statements
| "Program"
| "VariableDeclaration"
| "FunctionDeclaration"
// Expressions
| "AssignmentExpr"
| "BinaryExpr"
| "UnaryExpr"
| "MemberExpr"
| "CallExpr"
// Literals
| "NumericLiteral"
| "NullLiteral"
| "Identifier"
| "Property"
| "ObjectLiteral";
/**
* A statement
* @example
* let x = 5;
*/
export interface Stmt {
type: NodeType;
}
/**
* A program
* @example
* let x = 5;
*
* // => Program { body: [LetStmt] }
* @example
*/
export interface Program extends Stmt {
type: "Program";
body: Stmt[];
}
/**
* Variable declaration expression
*/
export interface VarDeclaration extends Stmt {
type: "VariableDeclaration";
constant: boolean;
identifier: string;
value: Expr | undefined;
}
/**
* Function declaration expression
*/
export interface FnDeclaration extends Stmt {
type: "FunctionDeclaration";
name: string;
params: string[];
body: Stmt[];
}
/**
* A numeric literal
* @example
* 5
* // => NumericLiteral { value: 5 }
* @example
* 5 + 5
* // => BinaryExpr { operator: "+", left: NumericLiteral, right: NumericLiteral }
*/
export interface NumericLiteral extends Expr {
type: "NumericLiteral";
value: number;
}
/**
* An identifier
* @example
* let x = 5;
* // => Identifier { value: "x" }
* @example
* let add = (a, b) => a + b;
* // => Identifier { value: "add" }
*/
export interface Identifier extends Expr {
type: "Identifier";
value: string;
}
/**
* A binary expression
* @example
* 1 + 2
* // => BinaryExpr { operator: "+", left: NumericLiteral, right: NumericLiteral }
* @example
* 1 + 2 * 3
*/
export interface Expr extends Stmt {}
export interface BinaryExpr extends Expr {
type: "BinaryExpr";
operator: string;
left: Expr;
right: Expr;
}
/**
* An assignment expression
* @example
* x = 5
* @example
* x = add(1, 2)
*/
export interface AssignmentExpr extends Expr {
type: "AssignmentExpr";
assignee: Identifier;
value: Expr;
}
/**
* A call expression
* @example
* add(1, 2)
* // => CallExpr { value: "add", params: [NumericLiteral, NumericLiteral] }
* @example
* add(1, add(2, 3))
* // => CallExpr { value: "add", params: [NumericLiteral, CallExpr] }
*/
export interface CallExpr extends Expr {
type: "CallExpr";
args: Expr[];
caller: Expr;
}
/**
* A member expression
* @example
* x.y
* // => MemberExpr { object: Identifier, property: Identifier }
*/
export interface MemberExpr extends Expr {
type: "MemberExpr";
object: Expr;
property: Expr;
computed: boolean;
}
/**
* A unary expression
* @example
* -1
* // => UnaryExpr { operator: "-", argument: NumericLiteral }
* @example
* -add(1, 2)
* // => UnaryExpr { operator: "-", argument: CallExpr }
*/
export interface UnaryExpr extends Expr {
type: "UnaryExpr";
operator: string;
argument: Stmt;
}
/**
* A null literal
* @example
* null
* // => NullLiteral
* @example
* let x = null;
* // => NullLiteral
*/
export interface NullLiteral extends Expr {
type: "NullLiteral";
value: "null";
}
/**
* A property
* @example
* { x: 5 }
* // => Property { key: "x", value: NumericLiteral }
*/
export interface Property extends Expr {
type: "Property";
key: string;
value: Expr | undefined;
}
/**
* An object
* @example
* { x: 5 }
*/
export interface ObjectLiteral extends Expr {
type: "ObjectLiteral";
properties: Property[];
}