-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathnode.ts
More file actions
403 lines (341 loc) · 7.76 KB
/
node.ts
File metadata and controls
403 lines (341 loc) · 7.76 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
/**
* ASTノード
*/
export type Pos = {
line: number;
column: number;
};
export type Loc = {
start: Pos;
end: Pos;
};
export type Node = Namespace | Meta | Statement | Expression | TypeSource | Attribute;
type NodeBase = {
loc: Loc; // コード位置
};
export type Namespace = NodeBase & {
type: 'ns'; // 名前空間
name: string; // 空間名
members: (Definition | Namespace)[]; // メンバー
};
export type Meta = NodeBase & {
type: 'meta'; // メタデータ定義
name: string | null; // 名
value: Expression; // 値
};
// statement
export type Statement =
Definition |
Return |
Each |
For |
Loop |
Break |
Continue |
Assign |
AddAssign |
SubAssign;
const statementTypes = [
'def', 'return', 'each', 'for', 'loop', 'break', 'continue', 'assign', 'addAssign', 'subAssign',
];
export function isStatement(x: Node): x is Statement {
return statementTypes.includes(x.type);
}
export type Definition = NodeBase & {
type: 'def'; // 変数宣言文
dest: Expression; // 宣言式
varType?: TypeSource; // 変数の型
expr: Expression; // 式
mut: boolean; // ミュータブルか否か
attr: Attribute[]; // 付加された属性
};
export type Attribute = NodeBase & {
type: 'attr'; // 属性
name: string; // 属性名
value: Expression; // 値
};
export type Return = NodeBase & {
type: 'return'; // return文
expr: Expression; // 式
};
export type Each = NodeBase & {
type: 'each'; // each文
label?: string; // ラベル
var: Expression; // イテレータ宣言
items: Expression; // 配列
for: Statement | Expression; // 本体処理
};
export type For = NodeBase & {
type: 'for'; // for文
label?: string; // ラベル
var?: string; // イテレータ変数名
from?: Expression; // 開始値
to?: Expression; // 終値
times?: Expression; // 回数
for: Statement | Expression; // 本体処理
};
export type Loop = NodeBase & {
type: 'loop'; // loop文
label?: string; // ラベル
statements: (Statement | Expression)[]; // 処理
};
export type Break = NodeBase & {
type: 'break'; // break文
label?: string; // ラベル
expr?: Expression; // 式
};
export type Continue = NodeBase & {
type: 'continue'; // continue文
label?: string; // ラベル
};
export type AddAssign = NodeBase & {
type: 'addAssign'; // 加算代入文
dest: Expression; // 代入先
expr: Expression; // 式
};
export type SubAssign = NodeBase & {
type: 'subAssign'; // 減算代入文
dest: Expression; // 代入先
expr: Expression; // 式
};
export type Assign = NodeBase & {
type: 'assign'; // 代入文
dest: Expression; // 代入先
expr: Expression; // 式
};
// expressions
export type Expression =
If |
Fn |
Match |
Block |
Exists |
Tmpl |
Str |
Num |
Bool |
Null |
Obj |
Arr |
Plus |
Minus |
Not |
Pow |
Mul |
Div |
Rem |
Add |
Sub |
Lt |
Lteq |
Gt |
Gteq |
Eq |
Neq |
And |
Or |
Identifier |
Call |
Index |
Prop;
const expressionTypes = [
'if', 'fn', 'match', 'block', 'exists', 'tmpl', 'str', 'num', 'bool', 'null', 'obj', 'arr',
'not', 'pow', 'mul', 'div', 'rem', 'add', 'sub', 'lt', 'lteq', 'gt', 'gteq', 'eq', 'neq', 'and', 'or',
'identifier', 'call', 'index', 'prop',
];
export function isExpression(x: Node): x is Expression {
return expressionTypes.includes(x.type);
}
export type Plus = NodeBase & {
type: 'plus'; // 正号
expr: Expression; // 式
};
export type Minus = NodeBase & {
type: 'minus'; // 負号
expr: Expression; // 式
};
export type Not = NodeBase & {
type: 'not'; // 否定
expr: Expression; // 式
};
export type Pow = NodeBase & {
type: 'pow';
left: Expression;
right: Expression;
}
export type Mul = NodeBase & {
type: 'mul';
left: Expression;
right: Expression;
}
export type Div = NodeBase & {
type: 'div';
left: Expression;
right: Expression;
}
export type Rem = NodeBase & {
type: 'rem';
left: Expression;
right: Expression;
}
export type Add = NodeBase & {
type: 'add';
left: Expression;
right: Expression;
}
export type Sub = NodeBase & {
type: 'sub';
left: Expression;
right: Expression;
}
export type Lt = NodeBase & {
type: 'lt';
left: Expression;
right: Expression;
}
export type Lteq = NodeBase & {
type: 'lteq';
left: Expression;
right: Expression;
}
export type Gt = NodeBase & {
type: 'gt';
left: Expression;
right: Expression;
}
export type Gteq = NodeBase & {
type: 'gteq';
left: Expression;
right: Expression;
}
export type Eq = NodeBase & {
type: 'eq';
left: Expression;
right: Expression;
}
export type Neq = NodeBase & {
type: 'neq';
left: Expression;
right: Expression;
}
export type And = NodeBase & {
type: 'and';
left: Expression;
right: Expression;
}
export type Or = NodeBase & {
type: 'or';
left: Expression;
right: Expression;
}
export type If = NodeBase & {
type: 'if'; // if式
label?: string; // ラベル
cond: Expression; // 条件式
then: Statement | Expression; // then節
elseif: {
cond: Expression; // elifの条件式
then: Statement | Expression;// elif節
}[];
else?: Statement | Expression; // else節
};
export type Fn = NodeBase & {
type: 'fn'; // 関数
typeParams: TypeParam[]; // 型パラメータ
params: {
dest: Expression; // 引数名
optional: boolean;
default?: Expression; // 引数の初期値
argType?: TypeSource; // 引数の型
}[];
retType?: TypeSource; // 戻り値の型
children: (Statement | Expression)[]; // 本体処理
};
export type Match = NodeBase & {
type: 'match'; // パターンマッチ
label?: string; // ラベル
about: Expression; // 対象
qs: {
q: Expression; // 条件
a: Statement | Expression; // 結果
}[];
default?: Statement | Expression; // デフォルト値
};
export type Block = NodeBase & {
type: 'block'; // ブロックまたはeval式
label?: string; // ラベル
statements: (Statement | Expression)[]; // 処理
};
export type Exists = NodeBase & {
type: 'exists'; // 変数の存在判定
identifier: Identifier; // 変数名
};
export type Tmpl = NodeBase & {
type: 'tmpl'; // テンプレート
tmpl: Expression[]; // 処理
};
export type Str = NodeBase & {
type: 'str'; // 文字列リテラル
value: string; // 文字列
};
export type Num = NodeBase & {
type: 'num'; // 数値リテラル
value: number; // 数値
};
export type Bool = NodeBase & {
type: 'bool'; // 真理値リテラル
value: boolean; // 真理値
};
export type Null = NodeBase & {
type: 'null'; // nullリテラル
};
export type Obj = NodeBase & {
type: 'obj'; // オブジェクト
value: Map<string, Expression>; // プロパティ
};
export type Arr = NodeBase & {
type: 'arr'; // 配列
value: Expression[]; // アイテム
};
export type Identifier = NodeBase & {
type: 'identifier'; // 変数などの識別子
name: string; // 変数名
};
export type Call = NodeBase & {
type: 'call'; // 関数呼び出し
target: Expression; // 対象
args: Expression[]; // 引数
};
export type Index = NodeBase & {
type: 'index'; // 配列要素アクセス
target: Expression; // 対象
index: Expression; // インデックス
};
export type Prop = NodeBase & {
type: 'prop'; // プロパティアクセス
target: Expression; // 対象
name: string; // プロパティ名
};
// Type source
export type TypeSource = NamedTypeSource | FnTypeSource | UnionTypeSource;
export type NamedTypeSource = NodeBase & {
type: 'namedTypeSource'; // 名前付き型
name: string; // 型名
inner?: TypeSource; // 内側の型
};
export type FnTypeSource = NodeBase & {
type: 'fnTypeSource'; // 関数の型
typeParams: TypeParam[]; // 型パラメータ
params: TypeSource[]; // 引数の型
result: TypeSource; // 戻り値の型
};
export type UnionTypeSource = NodeBase & {
type: 'unionTypeSource'; // ユニオン型
inners: TypeSource[]; // 含まれる型
};
/**
* 型パラメータ
*/
export type TypeParam = {
name: string; // パラメータ名
}