forked from terralang/terra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.t
226 lines (174 loc) · 3.23 KB
/
class.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
IO = terralib.includec("stdio.h")
local Class = require("lib/javalike")
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 returnA(a : A)
return a
end
terra foobar1()
var c = C.alloc()
c.a,c.b,c.c = 1,2,3.5
return c:times2()
end
assert(foobar1() == 4)
terra foobar()
var a = A.alloc()
a.a = 1
var b = B.alloc()
b.a,b.b = 1,2
var c = C.alloc()
c.a,c.b,c.c = 1,2,3.5
var r = doubleAnA(a) + doubleAnA(b) + doubleAnA(c) + combineAB(b) + combineAB(c)
a:free()
b:free()
c:free()
return r
end
assert(23 == foobar())
Doubles = Class.Interface("Doubles", { times2 = {} -> int })
Adds = Class.Interface("Adds", { add = int -> int })
struct D(Class(nil,Doubles,Adds)) {
data : int
}
terra D:times2() : int
return self.data * 2
end
terra D:add(a : int) : int
return self.data + a
end
Doubles:Define()
Adds:Define()
terra aDoubles(a : &Doubles.type)
return a:times2()
end
terra aAdds(a : &Adds.type)
return a:add(3)
end
terra foobar2()
var a : D
a:init()
a.data = 3
return aDoubles(&a) + aAdds(&a)
end
assert(12 == foobar2())
local IO = terralib.includec("stdio.h")
struct Animal(Class()) {
data : int
}
terra Animal:speak() : {}
IO.printf("... %d\n",self.data)
end
struct Dog(Class(Animal)) {
}
terra Dog:speak() : {}
IO.printf("woof! %d\n",self.data)
end
struct Cat(Class(Animal)) {
}
terra Cat:speak() : {}
IO.printf("meow! %d\n",self.data)
end
terra dospeak(a : &Animal)
a:speak()
end
terra barnyard()
var c : Cat
var d : Dog
c:init()
d:init()
c.data,d.data = 0,1
dospeak(&c)
dospeak(&d)
end
barnyard()
local Add = Class.Interface("Add", { add = int -> int })
local Sub = Class.Interface("Sub", { sub = int -> int })
local struct P(Class(nil,Add)) {
data : int
}
local struct C(Class(P,Sub)) {
data2 : int
}
terra P:add(b : int) : int
self.data = self.data + b
return self.data
end
terra C:sub(b : int) : int
return self.data2 - b
end
Add:Define()
Sub:Define()
terra doadd(a : &Add.type)
return a:add(1)
end
terra dopstuff(p : &P)
return p:add(2) + doadd(p)
end
terra dosubstuff(s : &Sub.type)
return s:sub(1)
end
terra dotests()
var p : P
p:init()
var c : C
c:init()
p.data = 1
c.data = 1
c.data2 = 2
return dopstuff(&p) + dopstuff(&c) + dosubstuff(&c)
end
assert(dotests() == 15)
terra timeadd(a :&P, N : int)
IO.printf("%p\n",a)
for i = 0, N,10 do
a:add(1)
a:add(1)
a:add(1)
a:add(1)
a:add(1)
a:add(1)
a:add(1)
a:add(1)
a:add(1)
a:add(1)
end
return a
end
local a = global(C)
terra doinit() : &P
a:init()
a.data = 0
return &a
end
local v = doinit()
timeadd:compile()
local b = terralib.currenttimeinseconds()
--timeadd(v,100000000)
local e = terralib.currenttimeinseconds()
print(e - b)
print(v.data)