aboutsummaryrefslogtreecommitdiff
path: root/testsuite/runnable/recursive_record.elna
blob: 6f6b4fa68809edd4be21ca64493cc82bc3fd25cf (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
type
  Node = record
    value: Int;
    next: ^Node
  end

var
  first: ^Node
  second: ^Node
  third: ^Node

proc malloc(size: Word): Pointer
extern

proc free(ptr: Pointer)
extern

proc sum(node: ^Node): Int
var
  result: Int
begin
  result := 0;
  if node = nil then
    result := 0
  else
    result := node^.value + sum(node^.next)
  end;
  return result

begin
  first := malloc(#size(Node));
  second := malloc(#size(Node));
  third := malloc(#size(Node));

  first^.value := 1;
  first^.next := second;
  second^.value := 2;
  second^.next := third;
  third^.value := 3;
  third^.next := nil;

  assert(sum(first) = 6);
  assert(first^.next^.next^.value = 3);

  free(first);
  free(second);
  free(third)
end.