forked from terralang/terra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class6.t
62 lines (46 loc) · 927 Bytes
/
class6.t
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
IO = terralib.includec("stdio.h")
local Class = require("lib/javalikesimple")
struct A(Class()) {
a : int
}
terra A:times2() : int
return self.a*2
end
struct B(Class(A)) {
b : int
}
terra B:combine(a : int) : int
return self.b + self.a + a
end
struct C(Class(B)) {
c : double
}
terra C:combine(a : int) : int
return self.c + self.a + self.b + a
end
terra C:times2() : double
return self.a * 4
end
terra doubleAnA(a : &A)
return a:times2()
end
terra combineAB(b : &B)
return b:combine(3)
end
terra foobar1()
var c : C
c:init()
c.a,c.b,c.c = 1,2,3.5
return c:times2()
end
assert(foobar1() == 4)
terra foobar()
var a : A, b : B, c : C
a:init(); b:init(); c:init()
a.a = 1
b.a,b.b = 1,2
c.a,c.b,c.c = 1,2,3.5
var r = b:times2() + doubleAnA(&a) + doubleAnA(&b) + doubleAnA(&c) + combineAB(&b) + combineAB(&c)
return r
end
assert(25 == foobar())