-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathzevv.nim
307 lines (252 loc) Β· 5.23 KB
/
zevv.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
const
strictTrampoline {.booldefine.} = false
import balls
import posix
import std/macros
#import std/unittest
import cps
type
C = ref object of Continuation
# Trampoline with count safeguard
var jumps = 0
when strictTrampoline:
proc run(c: C) =
jumps = 0
var c = c
while c != nil and c.fn != nil:
c = c.fn(c)
inc jumps
doAssert jumps < 1000, "Too many iterations on trampoline, looping?"
else:
template run(c) = c
# Helper templates.
# This checks if the trampoline jumped the right number of times
template expJumps(expect: int, body: untyped) =
body
doAssert jumps == expect, "Trampoline jumped " & $jumps & " times, expected " & $expect
# is a primitive that keeps track of how often it is called, verify with
# `expPrims` macro
var prims = 0
proc prim(c: C): C {.cpsMagic.} =
inc prims
return c
template expPrims(expect: int, body: untyped) =
prims = 0
body
doAssert prims == expect, "prim was called " & $prims & " times, expected " & $expect
# Wrapper for defining a function and sending it to the trampoline
template runCps(body: untyped) =
proc t() {.cps:C.} = body
run t()
# We have a lot of these for the purpose of control-flow validation
{.warning[UnreachableCode]: off.}
var r: int
suite "suite, suite zevv":
test "nocall":
expPrims 0: runCps:
discard
test "onecall":
expPrims 1: runCps:
prim()
test "twocall":
expPrims 2: runCps:
prim()
prim()
test "if true":
expPrims 3: runCps:
var a: int
prim()
if true:
prim()
prim()
test "if false":
expPrims 2: runCps:
var a: int
prim()
if false:
prim()
prim()
test "if true if false":
expPrims 3: runCps:
prim()
if true:
prim()
if false:
prim()
prim()
test "nested if 1":
expPrims 4: runCps:
var a: int
prim()
if true:
prim()
if true:
prim()
prim()
test "nested if 2":
expPrims 3: runCps:
var a: int
prim()
if true:
prim()
if false:
prim()
prim()
test "nested if 3":
expPrims 2: runCps:
prim()
if false:
prim()
if true:
prim()
prim()
test "block1":
expPrims 3: runCps:
prim()
block:
prim()
prim()
test "while1":
expPrims 5: runCps:
prim()
var a: int = 0
while a < 3:
prim()
inc a
prim()
test "break1":
expPrims 3: runCps:
prim()
while true:
prim()
break
prim()
prim()
test "break2":
expPrims 3: runCps:
prim()
block:
prim()
break
prim()
prim()
prim()
test "for1":
runCps:
var a: int = 0
for i in 0..3:
inc a, 1
check a == 4
test "for2":
expPrims 1: runCps:
var a: int = 0
prim()
for i in 0..3:
inc a, 1
check a == 4
test "multiple variables in one var":
runCps:
var a, b: int16
check $type(a) == "int16"
check $type(b) == "int16"
test "wrongreturn":
runCps:
var n = 0
while n == 0:
echo "one ", n
let s = len("")
inc n
test "continue":
expPrims 8: runCps:
prim()
var i: int = 0
while i < 10:
inc i
if i < 5:
continue
prim()
prim()
test "for3":
expPrims 1: runCps:
var a: int = 0
for i in 0..3:
inc a, 1
check a == 4
prim()
test "defer":
expPrims 3: runCps:
prim()
defer:
prim()
prim()
test "nested while":
expPrims 100: runCps:
var i: int
var j: int
while i < 10:
inc i
j = 0
while j < 10:
inc j
prim()
test "paper example 1":
expPrims 2: runCps:
var t: bool = false
while not t:
prim()
break
prim()
prim()
proc foo(c: C, fd: int16): C {.cpsMagic.} =
discard
test "int":
proc test1() {.cps:C} =
foo(1)
test1()
test "'i16":
proc test1() {.cps:C} =
foo(1'i16)
test1()
test "int16()":
proc test1() {.cps:C} =
foo(int16(1))
test1()
test ".int16":
proc test1() {.cps:C} =
foo(1.int16)
test1()
test "type problem":
type Thing = distinct int
proc foo(): Thing = 1.Thing
runCps:
var a = foo()
test "Running -> Lampable -> Done":
proc running(c: C): bool = c != nil and c.fn != nil
proc dismissed(c: C): bool = c == nil
proc done(c: C): bool = c != nil and c.fn == nil
var save: C
proc jield(c: C): C {.cpsMagic.} =
save = c
proc count() {.cps:C.} =
var i = 0
while i < 2:
jield()
echo i
inc i
var c: Continuation = whelp count()
c = c.fn(c) # boot
check c.state == Running
c = c.fn(c) # first jield
check c.state == Dismissed
c = save
check c.state == Running
c = c.fn(c) # echo
check c.state == Running
c = c.fn(c) # second jield
check c.state == Dismissed
c = save
check c.state == Running
c = c.fn(c) # echo
check c.state == Running
c = c.fn(c) # done
check c.state == Finished