This repository has been archived by the owner on Dec 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tHnimAst.nim
356 lines (282 loc) · 8.37 KB
/
tHnimAst.nim
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
import std/[strutils, sequtils, strformat, macros, options]
import
hmisc/core/all,
hmisc/preludes/unittest
import
hmisc/algo/clformat,
hmisc/types/colorstring
import
hnimast,
hnimast/[obj_field_macros, pprint, hast_common]
import compiler/ast
macro showTyped*(body: typed) =
echo body.treeRepr1(positionIndexed = false)
suite "HNimAst":
test "{enumPref} :macro:":
type
En = enum
en1 = 2
let val = en1
check:
enumPref(En) == "en"
enumPref(val) == "en"
enumPref(en1) == "en"
proc gen[T](a: T, pr: string): void =
doAssert enumPref(a) == pr
gen(en1, "en")
type Alias = En
var alias: Alias
gen(alias, "en")
test "{enumNames} :macro:":
type
En = enum
en1 = "hello"
en2 = "world"
let val = en1
check:
enumNames(En) == @["en1", "en2"]
enumNames(en1) == @["en1", "en2"]
enumNames(val) == @["en1", "en2"]
test "{parseObject} parse nim pragma":
macro parse(body: untyped): untyped =
for stmt in body:
for obj in stmt:
if obj.kind == nnkTypeDef:
let obj = parseObject(obj)
if obj.pragma.isSome():
for call in obj.pragma.get().elements:
discard
for field in obj.flds:
if field.pragma.isSome():
for call in field.pragma.get().elements:
discard
parse:
type
Type {.zzz(Check).} = object
f1 {.check(it < 10).}: float = 12.0
Type*[A] {.ss.} = object
f1: int
Type* {.ss.} = object
f23: int
Type[A] {.ss.} = object
f33: int
Type[B] {.ss.} = object
case a: bool
of true:
b: int
of false:
c: float
Hhhhh = object
f1: float
f3: int
case f5: bool
of false:
f2: char
else:
f4: float
test "{parseObject} filter pragma pragmas":
macro parse(body: untyped): untyped =
var obj = body[0][0].parseObject()
for call in obj.pragma.get().elements:
discard
obj.pragma = none(NPragma)
obj.eachFieldMut do(fld: var ObjectField[NimNode]):
fld.pragma = none(NPragma)
result = nnkTypeSection.newTree obj.toNimNode()
parse:
type
Type {.zz(C), ee: "333", ee.} = object
f1 {.check(it < 2).}: float = 32.0
test "{eachCase}":
macro mcr(body: untyped): untyped =
let obj = body[0][0].parseObject()
let objid = ident "hjhh"
let impl = objid.eachCase(obj) do(fld: NObjectField) -> NimNode:
newCall("echo", newDot(ident "hjhh", ident(fld.name)))
# quote do:
# echo `objid`.`fld`
result = newStmtList(body)
result.add quote do:
let hjhh {.inject.} = Hello()
result.add impl
mcr:
type
Hello = object
ewre: char
case a: uint8:
of 0:
zee: float
of 2:
eee: string
of 4:
eee3: int
eee24: int
eee2343: int
eee321344: int
else:
eee23: string
# Fails compilation with `Error: cannot evaluate at compile time: lhs1`
# if I put `lhs1` inside of the callback - I have no idea why, and I
# don't want to spend another five hours trying to figure this out, only
# to later realize this is simply a compiler bug. btw, this code worked
# perfectly well earlier.
# test "{eachParallelCase}":
# ## Automatically generate comparison proc for case objects.
# macro mcr(body: untyped): untyped =
# let
# obj = body[0][0].parseObject()
# rhs1: NimNode = macros.ident"rhs"
# lhs1: NimNode = macros.ident"lhs"
# let val = newNQuoteTree(nnkDotExpr, @[toNimNodeAux lhs1])
# let impl = eachParallelCase((lhs1, rhs1), obj, proc(fld: NObjectField): NImNode = lhs1)
# # result = lhs1
# # let fld = ident fld.name
# # result = nquote do:
# # if `lhs1`.`fld` != `rhs1`.`fld`:
# # return false
# # let eqcmp = [ident "=="].newProcDeclNode(
# # newNType("bool"),
# # { "lhs" : obj.name, "rhs" : obj.name },
# # pragma = newNPragma(newIdent("noSideEffect")),
# # impl = (
# # nquote do:
# # `impl`
# # return true
# # ),
# # exported = false
# # )
# # result = nnkStmtList.newTree(body, eqcmp)
# mcr:
# type
# A = object
# fdl: seq[float]
# case b: char
# of '0':
# qw: char
# wer: float
# of '-':
# ee: float
# else:
# eeerw: char
# # nil # TODO
# echo A() == A()
test "{eachPath}":
macro mcr(body: untyped): untyped =
let obj = body[0][0].parseObject()
let self = ident "self"
let impl = self.eachPath(obj) do(
path: NObjectPath,
flds: seq[NObjectField]
) -> NimNode:
discard
mcr:
type
A = object
c: int
f: int
case a: char
of 'e':
e: int
of 'q':
q: char
else:
hello: seq[seq[int]]
suite "PQuote do":
test "1":
let hello = "hello"
let code = pquote do:
@@@!(newPIdent(hello & "World"))
echo code
test "2":
let args = @[nnkExprColonExpr.newPTree(
newPIdent("arg"),
newPIdent("int")
)]
let code2 = pquote do:
proc zzz(arg1: float, arg2: @@@^args) =
discard
echov code2
test "3":
let vals = @[newPLit("3"), newPLit("4")]
let code3 = pquote do:
let vals = @["1", "2", @@@vals]
echov code3
test "4":
let code3 = pquote do:
let vals = @[@@@!(newPLit("123"))]
echov code3
test "5":
let subfields = @[
nnkIdentDefs.newPTree(newPIdent "f2", newPIdent "int", newEmptyPNode()),
nnkIdentDefs.newPTree(newPIdent "f3", newPIdent "int", newEmptyPNode()),
nnkIdentDefs.newPTree(newPIdent "f4", newPIdent "int", newEmptyPNode()),
]
let code = pquote do:
type
E = object
f1: int
_: @@@^(subfields)
echov code
test "6":
let code = pquote do:
a.@@@!(newPIdent("hello"))
echov code
suite "working with PNode":
test "Core":
echov newPIdent("hello")
echov newReturn(newPIdent("qqqq"))
echov newPrefix("!", newPIdent("eee"))
block:
var decl = newPProcDecl("nice", {"arg1" : newPType("HHH")}).toNNode()
var procdef: ProcDecl[PNode]
procdef.name = "Hello"
procdef.signature = newProcNType[PNode](@[])
procdef.docComment = "werqwre"
let pd = procdef.toNNode()
echov pd
let node = parsePNodeStr($pd)
echov node.treeRepr()
decl.comment = "hello world"
echov decl
block:
var en = PEnumDecl(name: "Eee").withIt do:
it.values.add makeEnumField(
name = "Hello",
value = some(newPIdent("EEE")),
comment = "documentation comment"
)
en.docComment = """
Aliquam erat volutpat. Nunc eleifend leo vitae magna. In id erat non
orci commodo lobortis. Proin neque massa, cursus ut, gravida ut,
lobortis eget, lacus. Sed diam. Praesent fermentum tempor tellus.
Nullam tempus. Mauris ac felis vel velit tristique imperdiet."""
echov en.toNNode()
test "Parsing objects":
let node = """
type Type = object
hello: float
""".parsePNodeStr()
var obj = parseObject(node)
obj.exported = true
echov obj.toNNode()
test "Runtime ordinal parsing":
echo parsePNodeStr("1").dropStmtList().parseRTimeOrdinal()
echo parsePNodeStr("'1'").dropStmtList().parseRTimeOrdinal()
echo "type E = enum\n f1 = 12".
parsePNodeStr().
dropStmtList().
parseEnumImpl().
toNNode()
test "NimDecl API":
let pproc = newPProcDecl(name = "hello")
let pdecl = pproc.toNimDecl()
let pnode = pdecl.toNNode()
write(stdout, pdecl)
var decls: seq[NimTypeDecl[PNode]]
decls.add toNimTypeDecl(newEnumDecl[PNode](
"Test", iinfo = currLInfo()))
write(stdout, decls.toNimDecl())
suite "Distinct API":
let node = newPProcDecl(name = "hello").toNNode()
let pr: ProcDeclNode[PNode] = node.asProcDecl()
echov pr.name().treeRepr()