blob: bff757a44de06709afd3d73c479d3f350d92f55d (
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
// simply declare external functions
func puts(str : char*) : void;
// global context can contain functions,
// structs, global variables and namespaces
var global1 : int;
var global2 : double = 123.45;
func globalFunc() : void {
puts("Hello\n");
}
// structs and functions can be declared generic
// by providing a list of placeholder typenames
struct S1<T1, T2> {
t1: T1 *;
t2: T1;
m1() : T2 {
return this->t1 + this->t2;
}
}
func generic1<A>(a1 : A, a2 : A) : A {
return a1 + a2;
}
// namespaces can contain everything that
// the global context can
namespace N1 {
var v1 : int;
func f1() : void {
puts("Hello\n");
}
struct S1 {
test : char *;
}
// nested namespaces
namespace N2 {
var v1 : int;
struct S1 {
i1 : int;
i2 : int;
i3 : int;
m1(i: int) : int {
// implicit 'this' parameter, pointer to containing struct
this->i3 = this->i1 * this->i2;
// lookup is done hierarchically
f1(v1); // this is basically N1::N2::f1(N1::N2::v1);
N1::f1(N1::v1); // the rest becomes exactly what they say
N2::f1(N2::v1);
N1::N2::f1();
return this->i1 + this->i2;
}
}
struct S2 {
s: char *;
abc(): S1 {
var result : S1;
return result;
}
}
func f1() : void {
// these have the same type
var s1 : N1::N2::S1;
var s2 : S1;
s1.m1(123);
}
}
}
struct List<T> {
array: T *;
get(index: int): T {
return this->array[index];
}
}
func main(argc : int, argv : char**) : int {
var s1 : N1::N2::S1;
var s2 : N1::N2::S1;
var s3 : N1::S1;
s1.i1 = 123;
s1.i2 = 456;
s1.m1(s2.m1(s3.m1(89)));
N1::N2::f1();
// one 'copy' is compiled for every unique instantiation
var s4 : S1<int, long>;
s4.t1 = 123;
s4.t2 = 456;
s4.m1();
generic1<int>(1, 2);
generic1<double>(3.4, 5.6);
var s: N1::N2::S2;
s.abc();
var l1: List<int>;
l1.get(1);
var i1: int = generic1<int>(1, 2);
return 0;
}
|