-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathastdefs.lua
380 lines (318 loc) · 8.69 KB
/
astdefs.lua
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
--[[
This module defines all AST shapes.
It also servers as a documentation for AST nodes schemas.
These shapes are used for shape checking in tests
and when manually creating AST nodes.
]]
local aster = require 'nelua.aster'
local shaper = aster.shaper
-- Block containing a list of statements.
aster.register('Block', shaper.array_of(shaper.Node))
-- Number literal.
aster.register('Number', {
shaper.string + shaper.number, -- value
shaper.string + shaper.falsy, -- literal type
})
-- String literal.
aster.register('String', {
shaper.string, -- value
shaper.string + shaper.falsy, -- literal type
})
-- Boolean literal, (e.g `true` and `false`).
aster.register('Boolean', {
shaper.boolean -- true or false
})
-- Nilptr literal, (e.g `nilptr`).
aster.register('Nilptr', {})
-- Nil literal, (e.g `nil`).
aster.register('Nil', {})
-- Variable arguments literal, (e.g `...`).
aster.register('Varargs', {})
-- List of statements that evaluates to an expression.
aster.register('DoExpr', {
shaper.Block -- statements block
})
-- Preprocess code to be executed and removed.
aster.register('Preprocess', {
shaper.string -- code
}, {
is_preprocess = true
})
-- Preprocess expression to be replaced by an ASTNode containing an expression.
aster.register('PreprocessExpr', {
shaper.string -- code
}, {
is_preprocess = true
})
-- Preprocess expression to be replaced by a string containing a name.
aster.register('PreprocessName', {
shaper.string -- code
}, {
is_preprocess = true
})
local name = shaper.string + shaper.PreprocessName
-- Initializer list pair field.
aster.register('Pair', {
shaper.Node + name, -- field name or expr
shaper.Node, -- value expr
})
-- Initializer list (e.g `{}`), used for initialing tables, records and arrays.
aster.register('InitList',
shaper.array_of(shaper.Pair + shaper.Node), -- pair or exprs
{
is_unpackable = true,
})
-- Indexing with `.`.
aster.register('DotIndex', {
name, -- name
shaper.Node, -- expr
}, {
is_index = true
})
-- Indexing with `:`.
aster.register('ColonIndex', {
name, -- name
shaper.Node, -- expr
}, {
is_index = true
})
-- Indexing with brackets (e.g `[key]`).
aster.register('KeyIndex', {
shaper.Node, -- key expr
shaper.Node, -- expr
}, {
is_index = true
})
-- Annotation used in a variable, type or function declaration.
aster.register('Annotation', {
name, -- name
shaper.array_of(shaper.Node) + shaper.falsy, -- annotation arguments
})
-- Identifier.
aster.register('Id', {
name -- name
})
-- Identifier declaration.
aster.register('IdDecl', {
name + shaper.DotIndex, -- name
shaper.Node + shaper.falsy, -- type expr
shaper.array_of(shaper.Annotation) + shaper.falsy, -- annotations
})
-- Expression surround by parenthesis (e.g `(expr)`)
aster.register('Paren', {
shaper.Node -- expr
})
-- Type expression (e.g `@typeexpr`).
aster.register('Type', {
shaper.Node -- type expr
})
-- Variable arguments type, used in function declaration arguments only.
aster.register('VarargsType', {
shaper.one_of{"varautos", "varanys", "cvarargs"} + shaper.falsy
})
-- Function type.
aster.register('FuncType', {
shaper.array_of(shaper.Node), -- arguments types
shaper.array_of(shaper.Node) + shaper.falsy, -- returns types
})
-- Record field.
aster.register('RecordField', {
name, -- name
shaper.Node, -- type expr
})
-- Record type.
aster.register('RecordType', shaper.array_of(shaper.RecordField)) -- fields
-- Union field.
aster.register('UnionField', {
name + shaper.falsy, -- name
shaper.Node, -- type expr
})
-- Union type.
aster.register('UnionType', shaper.array_of(shaper.UnionField)) -- fields
-- Variant type.
aster.register('VariantType', shaper.array_of(shaper.Node)) -- types exprs
-- Enum field.
aster.register('EnumField', {
name, -- name
shaper.Node + shaper.falsy, -- value expr
})
-- Enum type.
aster.register('EnumType', {
shaper.Node + shaper.falsy, -- primitive type expr
shaper.array_of(shaper.EnumField), -- field types
})
-- Array type.
aster.register('ArrayType', {
shaper.Node, -- subtype type expr
shaper.Node + shaper.falsy, -- size expr
})
-- Pointer type.
aster.register('PointerType', {
shaper.Node + shaper.falsy, -- subtype type expr
})
-- Optional type.
aster.register('OptionalType', {
shaper.Node -- subtype type expr
})
-- Generic type.
aster.register('GenericType', {
shaper.Id + shaper.DotIndex, -- name
shaper.array_of(shaper.Node), -- arguments (type expr or expr)
})
-- Anonymous function (function without a name).
aster.register('Function', {
shaper.array_of(shaper.IdDecl + shaper.VarargsType), -- typed arguments
shaper.array_of(shaper.Node) + shaper.falsy, -- typed returns
shaper.array_of(shaper.Annotation) + shaper.falsy,
shaper.Node, -- block
}, {
is_function = true
})
-- Call.
aster.register('Call', {
shaper.array_of(shaper.Node), -- arguments exprs
shaper.Node, -- caller expr
}, {
is_call = true,
is_unpackable = true,
})
-- Call a method.
aster.register('CallMethod', {
name, -- method name
shaper.array_of(shaper.Node), -- arguments exprs
shaper.Node, -- caller expr
}, {
is_call = true,
is_unpackable = true,
})
-- Unary operator.
aster.register('UnaryOp', {
shaper.one_of{"not", "unm", "len", "bnot", "ref", "deref"}, -- op name
shaper.Node, -- right expr
}, {
is_operator = true
})
-- Binary operator.
aster.register('BinaryOp', {
shaper.Node, --- left expr
shaper.one_of{"or", "and",
"eq", "ne", "le", "lt", "ge", "gt",
"bor", "bxor", "band", "shl", "shr", "asr",
"concat",
"add", "sub",
"mul", "div", "idiv", "tdiv", "mod", "tmod",
"pow"}, -- op name
shaper.Node, -- right expr
}, {
is_operator = true
})
-- Return statement.
aster.register('Return',
shaper.array_of(shaper.Node), -- returned exprs
{
is_unpackable = true,
is_breakflow = true,
})
-- In statement.
aster.register('In', {
shaper.Node, -- final expr
}, {
is_breakflow = true,
})
-- If statement.
aster.register('If', {
shaper.array_of(shaper.Node + shaper.Block), -- ifs (expr followed by block)
shaper.Block + shaper.falsy, -- else block
})
-- Switch statement.
aster.register('Switch', {
shaper.Node, -- switch expr
shaper.array_of(shaper.array_of(shaper.Node) + shaper.Block), -- cases (exprs followed by block}
shaper.Block + shaper.falsy, -- else block
})
-- Do statement.
aster.register('Do', {
shaper.Block, -- statements block
})
-- Defer statement.
aster.register('Defer', {
shaper.Block -- statements block
})
-- While statement.
aster.register('While', {
shaper.Node, -- expr
shaper.Block, -- statements block
})
-- Repeat statement.
aster.register('Repeat', {
shaper.Block, -- statements block
shaper.Node, -- expr
})
-- Numeric for statement.
aster.register('ForNum', {
shaper.IdDecl, -- iterated var
shaper.Node, -- begin expr
shaper.one_of{"eq", "ne", "le", "lt", "ge", "gt"} + shaper.falsy, -- compare operator
shaper.Node, -- end expr
shaper.Node + shaper.falsy, -- increment expr
shaper.Block, -- block
})
-- For in statement.
aster.register('ForIn', {
shaper.array_of(shaper.IdDecl), -- iteration vars
shaper.array_of(shaper.Node), -- in exprs
shaper.Block, -- statements block
})
-- Break statement.
aster.register('Break', {}, {
is_breakflow = true,
})
-- Continue statement.
aster.register('Continue', {}, {
is_breakflow = true,
})
-- Fallthrough statement.
aster.register('Fallthrough', {}, {
is_breakflow = true,
})
-- Label statement.
aster.register('Label', {
name, -- label name
})
-- Goto statement.
aster.register('Goto', {
name, -- label name
})
-- Variable declaration statement.
aster.register('VarDecl', {
shaper.one_of{"local","global"}, -- scope
shaper.array_of(shaper.IdDecl), -- var names with types
shaper.array_of(shaper.Node) + shaper.falsy, -- exprs of initial values
}, {
is_unpackable = true,
})
-- Variable assignment statement.
aster.register('Assign', {
shaper.array_of(shaper.Node), -- var exprs
shaper.array_of(shaper.Node), -- values exprs
}, {
is_unpackable = true,
})
-- Function definition statement.
aster.register('FuncDef', {
shaper.one_of{"local","global"} + shaper.falsy, -- scope
shaper.IdDecl + shaper.Id + shaper.DotIndex + shaper.ColonIndex, -- name
shaper.array_of(shaper.IdDecl + shaper.VarargsType), -- typed arguments
shaper.array_of(shaper.Node) + shaper.falsy, -- typed returns
shaper.array_of(shaper.Annotation) + shaper.falsy,
shaper.Block, -- statements block
}, {
is_function = true,
})
-- Directive statement, used only internally for some preprocessor directives.
aster.register('Directive', {
shaper.string, -- name
shaper.table, -- arguments exprs
})
-- No operation statement, used only internally when discarding AST nodes.
aster.register('NoOp', {})