Rune - Fix array passing, optimize constant array indices
[rune.git] / tests / global.d
1 #!/usr/local/bin/rune -x
2 #
3 # Demonstrate global elements in classes, procedures, and compound
4 # types.
5 #
6
7 import "sys";
8 import <stdio>;
9
10 alias stdio.File *stdout = stdio.stdout;
11
12 class Test {
13         int a = 2;
14         global int b = 6;
15         global const int c1 = 1;
16         global const int c2 = 2;
17         global const int c3 = 3;
18         int c;
19         myint_t z;
20
21
22 typedef int myint_t = 99;
23
24 class Fx1 {                     # 4 + (4 pad) + 40 + 8
25         int f1data;
26         Fx2 *f2ptr;
27         Fx2 f2obj;
28 }
29
30 class Fx2 {                     # 8 bytes
31         int f2data;
32         Fx3 f3obj;
33 }
34
35 class Fx3 {
36         int f3data;
37 }
38
39 # note that ( 7 ) is not compound, but Rune will always cast a non-compound
40 # expression to compound in the resolver if the resolver needs a compound
41 # expression.  (7) initializes the 'y', not the 'x', because 'x' is a
42 # global field within the object and thus out-of-band (even though the
43 # object itself is being declared as a global, multiple instances of the
44 # object will share 'x').
45 #
46 (global int x = 2, int y = 6) A = ( 7 );
47 myint_t B = 8;
48 myint_t C;
49 Fx2 fx2;
50 Fx1 fx1 = ( 1, &fx2, ( 2, ( 3 ) ) );
51 Fx1 fxx = ( puref1(1), purefx2(), ( puref1(2), ( puref1(3) ) ) );
52 int X;
53 int *Y = &X;
54
55 const int xsize = 8192;
56 const int xmask = (xsize - 1);
57
58 public 
59 int
60 main(int ac, char **av)
61 {
62         Fs fs;
63         int i;
64         Test t1 = ( 3, 4 );
65         Test t2;
66         Test t3;
67         Test t4 = ( 8, 9, 10 );
68         void *v;
69         myint_t x;
70
71         fs.setfd(1);
72         stdout->show("fx1 should be (1, 2, 3): ",
73                      fx1.f1data, fx1.f2obj.f2data, fx1.f2obj.f3obj.f3data);
74         stdout->show("sizeof(A) should be 4:", sizeof(A));
75         stdout->show("A.x should be 2:", A.x);
76         stdout->show("A.y should be 7:", A.y);
77         stdout->show("B should be 8:", B);
78         stdout->show("C should be 99:", C);
79         stdout->show("t1.a should be 3:", t1.a);
80         stdout->show("t1.c should be 4:", t1.c);
81         stdout->show("t2.a should be 2:", t2.a);
82         stdout->show("t3.z should be 99:", t3.z);
83         stdout->show("t4.z should be 10:", t4.z);
84         stdout->show("xsize should be 8192:", xsize);
85         stdout->show("xmask should be 8191:", xmask);
86         stdout->show("t4.z should be 10:", t4.z);
87         fs.write("\n", 1);
88         return(0);
89 }
90
91 pure int
92 puref1(int n)
93 {
94         return n;
95 }
96
97 pure Fx2 *
98 purefx2()
99 {
100         return &fx2;
101 }