forked from microsoft/verona
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_harness.verona
102 lines (89 loc) · 2.31 KB
/
queue_harness.verona
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
// Copyright Microsoft and Project Verona Contributors.
// SPDX-License-Identifier: MIT
// This just directly includes the queue file. Stop gap until module system
// and packaging implenented
// Module system will work on a directory level, rather than individual file
use "library/queue.verona"
/**
* Simple harness to illustrate the memory management of the queue.
*
* The trace produced highlights, how regions can be collected
* independently.
*/
/**
* Simple wrapper of a U64, with a finaliser,
* so finalisation can be traced.
**/
class U64ObjF
{
v: U64 & imm;
create(x: U64 & imm) : U64ObjF & iso
{
var o = new U64ObjF;
o.v = x;
o
}
final(self: mut)
{
Builtin.print1("Final U64ObjF {}\n", self.v)
}
}
class Main
{
refine(a: Queue[U64ObjF]) {}
// Add and remove some elements to a queue
run(q: Queue[U64ObjF] & mut)
{
q.add(U64ObjF.create(1));
q.add(U64ObjF.create(2));
var a1 = q.remove();
Main.print(q.id, a1);
var a2 = q.remove();
Main.print(q.id, a2);
var a3 = q.remove();
Main.print(q.id, a3);
}
// Creates to queues, and interleaves adding and removing elements from the queues.
// The nodes of the queues get deleted periodically, and per queue, but the Values
// get delete as soon as they are removed from the queues.
main()
{
var q = mut-view (Queue.create(1));
var q2 = mut-view (Queue.create(2));
// Type inference is choosing Queue[U64ObjF & iso], which we can't codegen yet.
Main.refine(q);
Main.run(q);
// CHECK-L: Queue 1 elem: 1
// CHECK-L: Queue 1 elem: 2
Main.run(q2);
Main.run(q);
Main.run(q2);
Main.run(q2);
Main.run(q);
Main.run(q2);
Main.run(q2);
Main.run(q);
Main.run(q);
Main.run(q2);
Main.run(q);
Main.run(q2);
Main.run(q2);
Main.run(q);
Main.run(q);
Main.run(q2);
Main.run(q);
Main.run(q);
Main.run(q2);
Main.run(q);
Main.run(q);
Builtin.print("Finished\n");
}
print(id: U64 & imm, a: (U64ObjF & iso) | (None & imm))
{
match (a)
{
var vv: U64ObjF => Builtin.print2("Queue {} elem: {}\n", id, (mut-view vv).v),
var n: None => Builtin.print1("Queue {} empty\n", id),
}
}
}