Rune - Fix array passing, optimize constant array indices
[rune.git] / tests / cmp1.d
1 #!/usr/local/bin/rune -x
2 #
3 #       Demonstrate the use of lvalue scope.  lvalue scope may be applied
4 #       to individual procedural arguments or to the return value.  It
5 #       causes the caller to pass the object by reference instead of by
6 #       value, allowing the procedure to modify the contents of the
7 #       object.
8 #
9 #       This feature is most often used when implementing operators that
10 #       require lvalues, such as "++" and "&=", but it can be used to
11 #       great effect with normal procedures as well.
12
13 import "sys";
14 import <stdio>;
15
16 class Fubar {
17         int x;
18         int y;
19         heap lvalue int z;
20         Fubar *link;
21
22         method void
23         test1()
24         {
25                 this.x = 1;
26         }
27
28         method Fubar *
29         test2()
30         {
31                 return(&this);
32         }
33
34         method int
35         test3()
36         {
37                 return this.test4() + 1;
38         }
39
40         method int
41         test4()
42         {
43                 return this.y + 2;
44         }
45 }
46
47 Fubar *g;
48 Fubar gg;
49
50 int
51 main(int ac, char **av)
52 {
53         unlocked Fubar *a;
54         Fubar **aa;
55         Fubar *b;
56         Fubar c;
57         int x;
58         (Fubar *xx, Fubar *yy) cc;
59         unlocked int *xp;
60         int **xpp;
61
62         cc.xx = a;
63         cc.yy = a;
64
65         a = b;
66         g = &gg;
67
68         a.new();
69         aa = &a;
70         (*aa).new();
71         a->x = 1;
72         a->x = 2;
73         a->x = 3;
74         a->test1();
75         b = a->test2();
76         b = g->test2();
77         xp = &x;
78         xpp = &xp;
79         *xpp = xp;      # clearing *xpp (i.e. contents of xp) before assignment XXX 
80         *xpp = xp;
81         **xpp = 45;
82         **xpp = 46;
83         **xpp = 47;
84
85         b.new();
86         b->x = 4;
87         b->x = 5;
88         b->x = 6;
89
90         a->test3();
91
92         b->link.new();
93
94 #       if (a->y < b->y)
95 #               x = 7;
96 #       else
97 #               x = 8;
98
99 #       if (c.z < c.z)
100 #               x = 9;
101 #       else
102 #               x = 10;
103
104 }
105
106 int *
107 xfubar(int *z)
108 {
109         return z;
110 }