Skip to content

Commit

Permalink
Eval lazy functions for different comptime values
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Jan 28, 2020
1 parent d2dff75 commit 1b44e5e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
5 changes: 4 additions & 1 deletion nelua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1050,11 +1050,14 @@ end

local function lazy_args_matches(largs, rargs)
for _,larg,rarg in iters.izip(largs, rargs) do
--TODO: if traits.is_attr(larg) and traits.is_attr(rargs)
local ltype = traits.is_attr(larg) and larg.type or larg
local rtype = traits.is_attr(rarg) and rarg.type or rarg
if ltype ~= rtype then
return false
elseif traits.is_attr(larg) and larg.comptime then
if not traits.is_attr(rarg) or larg.value ~= rarg.value then
return false
end
end
end
return true
Expand Down
12 changes: 12 additions & 0 deletions spec/05-cgenerator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,18 @@ it("lazy functions with comptime arguments", function()
local a = cast(@number, 1)
assert(type(a) == 'number')
print(a)
local function iszero(x: auto)
if x == 0 then
return true
else
return false
end
end
assert(iszero(0) == true)
assert(iszero(1) == false)
assert(iszero(2) == false)
]])
end)

Expand Down
16 changes: 6 additions & 10 deletions spec/06-preprocessor_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -420,24 +420,20 @@ it("lazy function", function()
assert.analyze_ast([=[
## local printtypes = {}
local function printtype(x: auto)
##[[
if x.type:is_float() then
table.insert(printtypes, 'float')
elseif x.type:is_integral() then
table.insert(printtypes, 'integral')
elseif x.type:is_boolean() then
table.insert(printtypes, 'boolean')
end
]]
## table.insert(printtypes, x.type.name)
return x
end
assert(printtype(1) == 1)
assert(printtype(3.14) == 3.14)
assert(printtype(true) == true)
assert(printtype(false) == false)
local a: uint64 = 1
assert(printtype(a) == 1)
local b: uint64 = 1
assert(printtype(b) == 1)
##[[ afterinfer(function()
local types = table.concat(printtypes, ' ')
staticassert(types == 'integral float boolean', types)
staticassert(types == 'int64 float64 boolean boolean uint64', types)
end) ]]
]=])
end)
Expand Down

0 comments on commit 1b44e5e

Please sign in to comment.