-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathiterator.t
51 lines (42 loc) · 939 Bytes
/
iterator.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
local CT = require 'std.constraint'
local M = {}
M.Iterator = CT.TerraOperator("__for")
M.Iteratable = CT.Method("iter", {}, CT.Value(M.Iterator))
M.FromSlice = terralib.memoize(function(T)
local struct s {
f : &T
l : &T
}
s.metamethods.__for = function(iter,body)
return quote
var p = iter.f
while p ~= iter.l do
[body(`@p)]
p = p + 1
end
end
end
return s
end)
M.Tuple = function(self,body)
return quote
escape
for i=0,#self:gettype().entries - 1 do
emit(body(`self.["_"..i]))
end
end
end
end
do
local function iter(invar, state)
if state < invar[2] then
return state + 1, invar[1][state+1]
else
return nil, nil
end
end
function M.FromArgs(...)
return iter, {{...}, select("#", ...)}, 0
end
end
return M