-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNinaExprTree.cs
416 lines (411 loc) · 16.7 KB
/
NinaExprTree.cs
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
namespace Nina;
class NinaExprTree {
public NinaExprTree? boss = null;
public NinaExprTreeType type = NinaExprTreeType.None;
public NinaCodeBlock block;
public NinaASTBlockExpression? compiledBlock = null;
public NinaExprTree? l = null;
public NinaExprTree? r = null;
public NinaExprTree(bool _isPlaceholder) {
block = new NinaCodeBlock();
type = ! _isPlaceholder
? NinaExprTreeType.Void
: NinaExprTreeType.Placeholder;
}
public NinaExprTree(NinaCodeBlock _block) {
block = _block;
type = _block.type != NinaCodeBlockType.Operator
? NinaExprTreeType.Data
: NinaExprTreeType.Operator;
}
public NinaExprTree(NinaASTBlockExpression _compiledBlock) {
compiledBlock = _compiledBlock;
type = NinaExprTreeType.CompiledBlock;
}
public NinaExprTree? get(int _n) {
if (_n == 0 && l != null) return l;
else if (_n == 1 && r != null) return r;
else {
NinaError.error("莫名其妙的错误.", 101253);
return null;
}
}
public NinaExprTree? get() {
if (type == NinaExprTreeType.Data) return this;
else if (r != null) return r;
else if (l != null) return l;
else {
NinaError.error("莫名其妙的错误.", 613545);
return null;
}
}
public void remove(int _n) {
if (_n == 0 && l != null) {
l.boss = null;
l = null;
}
else if (_n == 1 && r != null) {
r.boss = null;
r = null;
}
else {
NinaError.error("莫名其妙的错误.", 164914);
}
}
public void remove(NinaExprTree _v) {
if (l == _v)
remove(0);
else if (r == _v)
remove(1);
else
NinaError.error("莫名其妙的错误.", 200329);
}
public void append(NinaExprTree _tree) {
if (l == null)
l = _tree;
else if (r == null)
r = _tree;
else
NinaError.error("莫名其妙的错误.", 944907);
if (_tree.boss != null)
_tree.boss.remove(_tree);
_tree.boss = this;
}
public void replace(int _n, NinaExprTree _v) {
if (_v.boss != null)
_v.boss.remove(_v);
if (_n == 0 && l != null) {
_v.boss = this;
l.boss = null;
l = _v;
}
else if (_n == 1 && r != null) {
_v.boss = this;
r.boss = null;
r = _v;
}
else {
NinaError.error("莫名其妙的错误.", 496672);
}
}
public void replace(NinaExprTree _ov, NinaExprTree _nv) {
if (l == _ov)
replace(0, _nv);
else if (r == _ov)
replace(1, _nv);
else
NinaError.error("莫名其妙的错误.", 157782);
}
public void abdicate(NinaExprTree _tree) {
if (boss != null)
boss.replace(this, _tree);
_tree.append(this);
}
public ANinaASTExpression compile(
HashSet<NinaWithStatementTypes> _withs, bool _isRoot = true) {
NinaErrorPosition pos
= new NinaErrorPosition(
block.file, block.line, block.col
);
if (type == NinaExprTreeType.CompiledBlock) {
return compiledBlock !;
}
else if (type == NinaExprTreeType.Void) {
return new NinaASTListExpression(pos);
}
else if (type == NinaExprTreeType.Data) {
if (block.type == NinaCodeBlockType.Identifier) {
return
new NinaASTIdentifierExpression(
NinaCompilerUtil.format_identifier(block.code),
pos
);
}
else if (block.type == NinaCodeBlockType.Number) {
return
new NinaASTLiteralExpression(
(double) block.val_num !,
pos
);
}
else if (block.type == NinaCodeBlockType.String) {
return
new NinaASTLiteralExpression(
block.val_str !,
pos
);
}
else {
NinaError.error(
"无效的表达式.", 119832,
new NinaErrorPosition(block.file, block.line, block.col)
);
}
}
else if (type == NinaExprTreeType.Operator) {
if (l == null || r == null) {
NinaError.error("莫名其妙的错误.", 584489);
}
else if (block.val_op_unary == false) {
ANinaASTExpression node_l = l!.compile(_withs: _withs, _isRoot: false);
ANinaASTExpression node_r = r!.compile(_withs: _withs, _isRoot: false);
ANinaASTExpression? expr_l = node_l as ANinaASTCommonExpression;
ANinaASTExpression? expr_r = node_r as ANinaASTCommonExpression;
NinaASTListExpression? list_l = node_l as NinaASTListExpression;
NinaASTListExpression? list_r = node_r as NinaASTListExpression;
NinaASTBlockExpression? compiledBlock_l = node_l as NinaASTBlockExpression;
NinaASTBlockExpression? compiledBlock_r = node_r as NinaASTBlockExpression;
if (block.val_op == NinaOperatorType.Arr) {
if ((list_l == null && expr_l == null)
|| compiledBlock_r == null) {
NinaError.error(
"无效的匿名函数创建表达式.", 186009,
new NinaErrorPosition(block.file, block.line, block.col)
);
}
else {
NinaASTSuperListExpression plist
= list_l != null
? NinaCompilerUtil.transfer_list2params(list_l, block)
: NinaCompilerUtil.transfer_list2params(expr_l !, block);
return new NinaASTBinaryExpression(
_type: NinaOperatorType.Arr,
_expr_l: plist,
_expr_r: compiledBlock_r,
pos
);
}
}
else if (compiledBlock_l != null || compiledBlock_r != null) {
NinaError.error(
"表达式的两侧发现无效的语句块表达式.",
201919,
new NinaErrorPosition(block.file, block.line, block.col)
);
}
else if (block.val_op == NinaOperatorType.Com) {
if (_isRoot) {
NinaError.error(
"无效的列表表达式.", 924405,
new NinaErrorPosition(block.file, block.line, block.col)
);
}
else if (list_l != null && list_r != null) {
list_l.list.AddRange(list_r.list);
return list_l;
}
else if (list_l != null && expr_r != null) {
list_l.list.Add(expr_r);
return list_l;
}
else if (expr_l != null && list_r != null) {
List<ANinaASTExpression> list
= new List<ANinaASTExpression>() {
expr_l
};
list.AddRange(list_r.list);
list_l = new NinaASTListExpression(
list,
pos
);
return list_l;
}
else if (expr_l != null && expr_r != null) {
return
new NinaASTListExpression(
new List<ANinaASTExpression>() {
expr_l, expr_r
},
pos
);
}
else {
NinaError.error(
"无效的列表表达式.", 301923,
new NinaErrorPosition(block.file, block.line, block.col)
);
}
}
else if (block.val_op == NinaOperatorType.BraL) {
if (expr_l == null) {
NinaError.error(
"函数调用表达式的左侧表达式无效.",
593929,
new NinaErrorPosition(block.file, block.line, block.col)
);
}
else if (list_r != null) {
return
new NinaASTBinaryExpression(
_type: NinaOperatorType.BraL,
_expr_l: expr_l,
_expr_r: list_r,
pos
);
}
else if (expr_r != null) {
return
new NinaASTBinaryExpression(
_type: NinaOperatorType.BraL,
_expr_l: expr_l,
_expr_r: new NinaASTListExpression(
new List<ANinaASTExpression>() {
expr_r
},
pos
),
pos
);
}
else {
NinaError.error(
"函数调用表达式的右侧表达式无效.",
194021,
new NinaErrorPosition(block.file, block.line, block.col)
);
}
}
else if (expr_l == null || expr_r == null) {
NinaError.error(
"表达式两侧的表达式无效.",
249439,
new NinaErrorPosition(block.file, block.line, block.col)
);
}
else if (block.val_op == NinaOperatorType.MBraL) {
NinaASTBinaryExpression ret
= new NinaASTBinaryExpression(
_type: NinaOperatorType.MBraL,
_expr_l: expr_l,
_expr_r: expr_r,
pos
);
if (_withs.Contains(NinaWithStatementTypes.Strongly))
ret.annos.Add(NinaConstsProviderUtil.NINA_ANNO_STRONGLY);
return ret;
}
else if (block.val_op == NinaOperatorType.Dot) {
NinaASTIdentifierExpression? id
= expr_r as NinaASTIdentifierExpression;
if (id == null) {
NinaError.error(
"成员访问语法糖表达式的右侧表达式无效.",
595886,
new NinaErrorPosition(block.file, block.line, block.col)
);
}
else {
NinaASTBinaryExpression ret
= new NinaASTBinaryExpression(
_type: NinaOperatorType.MBraL,
_expr_l: expr_l,
_expr_r: new NinaASTLiteralExpression(
NinaCompilerUtil.unformat_identifier(id.name),
pos
),
pos
);
if (_withs.Contains(NinaWithStatementTypes.Strongly))
ret.annos.Add(NinaConstsProviderUtil.NINA_ANNO_STRONGLY);
return ret;
}
}
else if (block.val_op == NinaOperatorType.Equ) {
return
new NinaASTBinaryExpression(
_type: NinaOperatorType.Equ,
_expr_l: expr_l,
_expr_r: expr_r,
pos
);
}
else {
NinaASTBinaryExpression ret
= new NinaASTBinaryExpression(
_type: (NinaOperatorType) block.val_op !,
_expr_l: expr_l,
_expr_r: expr_r,
pos
);
if (_withs.Contains(NinaWithStatementTypes.Strongly))
ret.annos.Add(NinaConstsProviderUtil.NINA_ANNO_STRONGLY);
return ret;
}
}
else {
ANinaASTExpression node = r!.compile(_withs: _withs, _isRoot: false);
ANinaASTExpression? expr = node as ANinaASTCommonExpression;
NinaASTListExpression? list = node as NinaASTListExpression;
NinaASTBlockExpression? compiledBlock = node as NinaASTBlockExpression;
if (block.val_op == NinaOperatorType.Object) {
if (compiledBlock == null) {
NinaError.error(
"对象创建表达式的右侧表达式无效.",
301529,
new NinaErrorPosition(block.file, block.line, block.col)
);
}
return
new NinaASTObjectExpression(
NinaCompilerUtil.transfer_block2init(compiledBlock !, block),
pos
);
}
else if (block.val_op == NinaOperatorType.Array) {
if (list != null) {
return
new NinaASTObjectExpression(
list !,
pos
);
}
else if (expr != null) {
return
new NinaASTObjectExpression(
new NinaASTListExpression(
new List<ANinaASTExpression>() {
expr
},
pos
),
pos
);
}
else {
NinaError.error(
"数组创建表达式的右侧表达式无效.",
103928,
new NinaErrorPosition(block.file, block.line, block.col)
);
}
}
else if (expr == null) {
NinaError.error(
"单目运算表达式的右侧表达式无效.",
113938,
new NinaErrorPosition(block.file, block.line, block.col)
);
}
else if (block.val_op == NinaOperatorType.At) {
node.annos.Add(NinaConstsProviderUtil.NINA_ANNO_SPECIALARG);
return node;
}
else {
NinaASTUnaryExpression ret
= new NinaASTUnaryExpression(
_type: (NinaOperatorType) block.val_op !,
_expr: expr,
pos
);
if (_withs.Contains(NinaWithStatementTypes.Strongly))
ret.annos.Add(NinaConstsProviderUtil.NINA_ANNO_STRONGLY);
return ret;
}
}
}
else {
NinaError.error("莫名其妙的错误.", 214128);
}
return new NinaASTLiteralExpression(pos);
}
}