blob: 1667bf46f0adafc19922b1cd036b7927e4996234 (
plain)
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
|
program()
type
Node = record
value: Int;
next: ^Node
end
var
first: ^Node
second: ^Node
third: ^Node
proc malloc#[T](size: Word) -> ^T
extern
proc free#[T](ptr: ^T)
extern
proc sum(node: ^Node) -> Int
var
result: Int
begin
result := 0;
if node = cast(0u: ^Node) then
result := 0
else
result := node^.value + sum(node^.next)
end;
return result
begin
first := malloc#[Node](#size(Node));
second := malloc#[Node](#size(Node));
third := malloc#[Node](#size(Node));
first^.value := 1;
first^.next := second;
second^.value := 2;
second^.next := third;
third^.value := 3;
third^.next := cast(0u: ^Node);
assert(sum(first) = 6);
assert(first^.next^.next^.value = 3);
free(first);
free(second);
free(third)
end.
|