-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy patht40_ast.nim
513 lines (435 loc) Β· 9.5 KB
/
t40_ast.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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
include preamble
import foreign
suite "tasteful tests":
var r = 0
block:
## whelp operates multi-var proc parameters correctly
r = 0
proc foo(a, b, c: int = 3) {.cps: Cont.} =
inc r
## a=1, b=2, c=3
var a = 5
## a=5, b=2, c=3
noop()
inc r
## a=5, b=2, c=3
var b = b + a
## a=5, b=7, c=3
noop()
inc r
## a=5, b=7, c=3
check "proc parameters":
a == 5
b == 7
c == 3
let c = whelp foo(1, 2)
trampoline c
check r == 3
block:
## reassignment of mutable var proc params
skip"pending issue #47":
r = 0
proc foo(a, b, c: var int) {.cps: Cont.} =
inc r
a = 5
noop()
inc r
b = b + a
noop()
inc r
check "var param assignment":
a == 5
b == 7
c == 3
var (x, y, z) = (1, 2, 3)
foo(x, y, z)
check "var param assignment":
x == 5
y == 7
z == 3
check r == 3
block:
## a block statement with a break under an if
r = 0
proc foo() {.cps: Cont.} =
inc r
block:
inc r
if true:
inc r
break
fail"block break failed to break block"
inc r
foo()
check r == 4
block:
## a block with a continuation followed by a break
r = 0
proc foo() {.cps: Cont.} =
inc r
block:
inc r
if true:
inc r
noop()
inc r
break
fail"block break failed to break block"
inc r
foo()
check r == 5
block:
## named breaks are supported, with nested breaks
r = 0
proc foo() {.cps: Cont.} =
inc r
block:
block found:
inc r
block:
inc r
break
inc r
block:
inc r
break found
fail"A: should be unreachable"
inc r
break
fail"B: should be unreachable"
inc r
foo()
check r == 6
block:
## case statements can support control-flow
r = 0
proc foo() {.cps: Cont.} =
inc r
var i = 0
while i < 3:
inc r
inc i
case i
of 1:
inc r
discard
of 2:
check r == 5
inc r
continue
of 3:
check r == 7
inc r
break
else:
fail"unexpected default case"
let q = r - 2 # work-around
check i == q, "bad control flow"
inc r
inc r
foo()
check r == 9
block:
## calling a function pointer inside an object works
type Fn = object
fn: proc(i: int): int
let fn = Fn(fn: proc(i: int): int = i * 2)
proc foo() {.cps: Cont.} =
noop()
check fn.fn(10) == 20
foo()
block:
## calling a macro that calls a foreign symbol works
r = 0
proc foo() {.cps: Cont.} =
inc r
let i = 42
noop()
inc r
check jsonifyImplicit(i) == $i
foo()
check r == 2
block:
## calling a template with explicit symbol binding
r = 0
proc foo() {.cps: Cont.} =
inc r
let i = 42
noop()
inc r
check jsonifyBind(i) == $i
foo()
check r == 2
block:
## template calls inside nested call nodes are fine
r = 0
# It is crucial that none of these templates call
# any other templates/macros.
template negate(b: untyped): untyped =
not b
template isEmpty(s: untyped): untyped =
s == ""
template makeStr(s: untyped): untyped =
let r = $s
r
proc foo() {.cps: Cont.} =
inc r
# It has to have this exact amount of templates nesting or it won't
# reproduce the bug.
let chk =
negate:
negate:
negate:
isEmpty:
makeStr "foo"
check chk
foo()
check r == 1
block:
## calling a function pointer produced by an expression
proc bar(i: int): proc(): int =
result = proc(): int =
i * 2
proc foo() {.cps: Cont.} =
var i = 2
noop()
i = bar(i)()
noop()
check i == 4
foo()
block:
## a simple if-else split across continuations works
r = 0
proc foo() {.cps: Cont.} =
inc r
if true:
noop()
inc r
else:
fail "this branch should not run"
inc r
foo()
check r == 3
block:
## a simple case statement split across continuations works
r = 0
proc foo() {.cps: Cont.} =
inc r
case true:
of true:
noop()
inc r
of false:
fail "this branch should not run"
inc r
foo()
check r == 3
block:
## block control-flow across continuations works
r = 0
proc foo() {.cps: Cont.} =
inc r
block:
inc r
noop()
inc r
inc r
foo()
check r == 4
block:
## if control-flow across continuations works
r = 0
proc foo() {.cps: Cont.} =
inc r
if true:
inc r
noop()
inc r
inc r
foo()
check r == 4
block:
## implicit generics in continuation creation
when true:
skip "not working, ref #51"
else:
proc foo(v: auto) {.cps: Cont.} =
discard $v
inc r
r = 0
foo(42)
check r == 1
r = 0
foo("string")
check r == 1
block:
## explicit generics in continuation creation
when true:
skip "not working, ref #51"
else:
proc foo[T](v: T) {.cps: Cont.} =
discard $v
inc r
r = 0
foo(42)
check r == 1
r = 0
foo("string")
check r == 1
block:
## nested blocks with an interior break works
r = 0
proc foo() {.cps: Cont.} =
inc r
block a:
inc r
block:
inc r
break a
fail "inner block continued"
noop()
fail "block was not broken out"
inc r
foo()
check r == 4
block:
## various varargs usages are supported just fine
# various forms of concat
func concatConv(parts: varargs[string, `$`]): string =
for i in parts:
result.add i
func concat(parts: varargs[string]): string =
for i in parts:
result.add i
func concatDuo(a, b: varargs[string]): string =
for i in a:
result.add i
for i in b:
result.add i
func `&!`(parts: varargs[string]): string =
for i in parts:
result.add i
func `&`(s: string, parts: varargs[string]): string =
result = s
for i in parts:
result.add i
func `%&`(parts: varargs[string], s: string): string =
for i in parts:
result.add i
result.add s
func `%&%`(a, b: varargs[string]): string =
for i in a:
result.add i
for i in b:
result.add i
r = 0
proc foo() {.cps: Cont.} =
inc r
check concatConv("foo ", 42, 0) == "foo 420"
check concat("foo ", $42, $0) == "foo 420"
check concatDuo(["f", "o", "o "], $42, $0) == "foo 420"
check &!["foo ", $42, $0] == "foo 420"
check "foo " & [$42, $0] == "foo 420"
check ["f", "o", "o "] %& "420" == "foo 420"
check ["foo "] %&% [$42, $0] == "foo 420"
check concatConv(@["foo ", $42, $0]) == "foo 420"
check concat(@["foo ", $42, $0]) == "foo 420"
check concatDuo(@["f", "o", "o "], @[$42, $0]) == "foo 420"
check &!(@["foo ", $42, $0]) == "foo 420"
check "foo " & @[$42, $0] == "foo 420"
check @["f", "o", "o "] %& "420" == "foo 420"
check @["foo "] %&% @[$42, $0] == "foo 420"
foo()
check r == 1
block:
## implicit up-casting
type O = ref object of RootObj
var r = 0
proc foo() {.cps: Continuation.} =
inc r
let o: RootRef = new O
#{.push warning[CondTrue]: off.}
check r == 1 and o is RootRef
#{.pop.}
check o of RootObj
foo()
check r == 1
block:
## implicit conversion between non-object types
var r = 0
proc foo() {.cps: Continuation.} =
inc r
let n: Natural = 1
# implicit conversion in decl
let i: int = n
# implicit conversion in assignment
var j: int
j = n
check i == n
foo()
check r == 1
block:
## splitting within pragma blocks
r = 0
proc foo() {.cps: Cont.} =
{.cast(gcsafe).}:
noop()
inc r
noop()
inc r
foo()
check r == 2
block:
## accessing a field of a variant object
type
O = object
case switch: bool
of true:
x: int
else:
discard
r = 0
proc foo() {.cps: Cont.} =
noop()
inc r
let o = O(switch: true, x: 42)
check o.x == 42
foo()
check r == 1
block:
## calling a non-local function pointer
r = 0
proc bar() =
inc r
let fn = bar
proc foo() {.cps: Cont.} =
noop()
inc r
fn()
foo()
check r == 2
block:
## calling a local proc variable
r = 0
proc bar() =
inc r
proc foo() {.cps: Cont.} =
let fn = bar
noop()
inc r
fn()
foo()
check r == 2
block:
## initializing a variable with calls containing object variant access
type
O = object
case switch: bool
of true: x: int
else: nil
func bar(x: int): int = x
proc foo() {.cps: Continuation.} =
let o = O(switch: true, x: 1)
let x = bar(o.x)
check x == 1
foo()