-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-list-alloc-print.chm
62 lines (51 loc) · 992 Bytes
/
test-list-alloc-print.chm
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
// CHM test source code
#include "stl.h"
/*
This test showcases that, in the function f,
the compiler is able to determine the correct type
of the variable x from the context.
It knows the type of x and because of that it even
allows the user to access its fields.
After successfull compilation without any errors,
the program should print integers 99 to 0 on separate lines.
*/
<a : b ~ struct List : <a> >
struct List
{
b *next;
a value;
};
<a : b ~ struct List : <a> >
b *push(b *head, a value)
{
b *item = new();
// new() is an stl function behaving as malloc(sizeof(b))
item->value = value;
item->next = head;
return item;
}
<a : b ~ struct List : <a> >
b *pull(b *head)
{
b* next = head->next;
delete(head);
return next;
}
<a>
void f()
{
a x = nullptr();
char nl = '\n';
for (int i = 0; i < 100; i++) {
x = push(x, i);
}
for (int i = 0; i < 100; i++) {
print(&x->value);
print(&nl);
x = pull(x);
}
}
int main()
{
f();
}