forked from terralang/terra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrayt2.t
82 lines (75 loc) · 2.04 KB
/
arrayt2.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
local f = assert(io.popen("uname", 'r'))
local s = assert(f:read('*a'))
f:close()
if s~="Darwin\n" then
print("Warning, not running test b/c this isn't a mac")
return
end
C = terralib.includecstring [[
#include <stdio.h>
#include <stdlib.h>
]]
local arraytypes = {}
function Array(T)
if arraytypes[T] then return arraytypes[T] end
local struct ArrayImpl {
data : &T;
N : int;
}
arraytypes[T] = ArrayImpl
terra ArrayImpl:init(N : int)
self.data = [&T](C.malloc(N*sizeof(T)))
self.N = N
end
terra ArrayImpl:free()
C.free(self.data)
end
ArrayImpl.metamethods.__apply = macro(function(self,idx)
return `self.data[idx]
end)
ArrayImpl.metamethods.__methodmissing = macro(function(methodname,selfexp,...)
local args = terralib.newlist {...}
local params = args:map(function(a) return symbol(a:gettype()) end)
local terra elemfn(a : &T, [params])
return a:[methodname](params)
end
local RT = elemfn:gettype().returntype
return quote
var self = selfexp
var r : Array(RT)
r:init(self.N)
for i = 0,r.N do
r.data[i] = elemfn(&self.data[i],args)
end
in
r
end
end)
return ArrayImpl
end
local OC = require("lib/objc")
local IO = terralib.includec("stdio.h")
struct Rect {
a : double,
b : double,
c : double,
d : double
}
terra str(data : &uint8)
return OC.NSString:stringWithUTF8String(data)
end
terra main()
OC.NSAutoreleasePool:new()
var app = OC.NSApplication:sharedApplication()
var rec = Rect {0,0,200,200}
var windows : Array(OC.ID)
windows:init(2)
windows(0) = OC.NSWindow
windows(1) = OC.NSWindow
windows = windows:alloc():initWithContentRect_styleMask_backing_defer(rec,1,2,false)
windows:makeKeyAndOrderFront(nil)
IO.printf("entering run loop\n")
app:run()
end
terralib.linklibrary("/System/Library/Frameworks/Cocoa.framework/Cocoa")
main:compile()