Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isolating changes for the for-in variable synthesis #377

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/terralib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3158,7 +3158,7 @@ function typecheck(topexp,luaenv,simultaneousdefinitions)
return newobject(s,T.fornum,variable,initial,limit,step,body)
elseif s:is "forlist" then
local iterator = checkexp(s.iterator)

local typ = iterator.type
if typ:ispointertostruct() then
typ,iterator = typ.type, insertdereference(iterator)
Expand All @@ -3167,8 +3167,16 @@ function typecheck(topexp,luaenv,simultaneousdefinitions)
diag:reporterror(iterator,"expected a struct with a __for metamethod but found ",typ)
return s
end
local itersym = terra.newsymbol(typ, "__for_iter")
local itervar = newobject(s, T.allocvar, "__for_iter", itersym)
local iterref = newobject(s, T.var, "__for_iter", itersym)
iterref.type = typ
local iterAssign = asterraexpression(
s,
createassignment(s, List {itervar}, List {asterraexpression(s, iterator)}),
"statement")
local generator = typ.metamethods.__for

local function bodycallback(...)
local exps = List()
for i = 1,select("#",...) do
Expand All @@ -3183,9 +3191,12 @@ function typecheck(topexp,luaenv,simultaneousdefinitions)
local stats = createstatementlist(s, List { assign, body })
return terra.newquote(stats)
end

local value = invokeuserfunction(s, "invoking __for", false ,generator,terra.newquote(iterator), bodycallback)
return asterraexpression(s,value,"statement")

local value = asterraexpression(
s,
invokeuserfunction(s, "invoking __for", false, generator, iterref, bodycallback),
"statement")
return asterraexpression(s,createstatementlist(s, List { iterAssign, value }),"statement")
elseif s:is "ifstat" then
local br = s.branches:map(checkcondbranch)
local els = (s.orelse and checkblock(s.orelse))
Expand Down
35 changes: 35 additions & 0 deletions tests/forlist3.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

local callcount = 0

struct iter {n: int}

iter.metamethods.__for = function(self, body)
return quote
[ body(`self.n) ]
[ body(`self.n) ]
end
end

local callinfo = {n=0}
terra this_should_be_called_once(n: int)
[terralib.cast({} -> {}, function() callinfo.n = callinfo.n + 1 end)]()
return iter{n}
end

local checkcalls = {n = 0, expect = {5, 5}}
local spy = terralib.cast({int} -> {}, function(val)
checkcalls.n = checkcalls.n + 1
assert(checkcalls.expect[checkcalls.n] == val, "spy called with incorrect value")
end
)

terra test()
for x in this_should_be_called_once(5) do
spy(x)
end
end

test()

assert(checkcalls.n == #checkcalls.expect, "spy called the wrong number of times")
assert(callinfo.n == 1, "body expansion called the wrong number of times.")