Skip to content

Commit a677680

Browse files
committed
optimizations
1 parent 277f022 commit a677680

File tree

4 files changed

+83
-78
lines changed

4 files changed

+83
-78
lines changed

nattlua/analyzer/expressions/postfix_call.lua

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,78 @@
11
local table = _G.table
22
local math_huge = _G.math.huge
33
local ipairs = _G.ipairs
4-
local NormalizeTuples = require("nattlua.types.tuple").NormalizeTuples
54
local Union = require("nattlua.types.union").Union
65
local Tuple = require("nattlua.types.tuple").Tuple
76
local Nil = require("nattlua.types.symbol").Nil
87
local AnalyzeImport = require("nattlua.analyzer.expressions.import").AnalyzeImport
98

10-
local function postfix_call(self, self_arg, node, callable)
11-
local types = {self_arg}
12-
self:AnalyzeExpressions(node.expressions, types)
13-
local arguments
9+
local function normalize_tuples(self, callable, types)
10+
local is_typesystem = self:IsTypesystem()
1411

15-
if self:IsTypesystem() then
16-
if
17-
#types == 1 and
18-
types[1].Type == "tuple" and
12+
if
13+
#types == 1 and
14+
types[1].Type == "tuple" and
15+
(
16+
not is_typesystem or
1917
callable:GetInputSignature():GetTupleLength() == math_huge
20-
then
21-
arguments = types[1]
18+
)
19+
then
20+
return types[1]
21+
end
22+
23+
if is_typesystem then return Tuple(types) end
24+
25+
local temp = {}
26+
local temp_i = 1
27+
28+
for i, v in ipairs(types) do
29+
if v.Type == "tuple" then
30+
if i == #types then
31+
temp[temp_i] = v
32+
temp_i = temp_i + 1
33+
else
34+
local obj = v:GetWithNumber(1)
35+
36+
if obj then
37+
temp[temp_i] = obj
38+
temp_i = temp_i + 1
39+
end
40+
end
2241
else
23-
arguments = Tuple(types)
42+
temp[temp_i] = v
43+
temp_i = temp_i + 1
2444
end
25-
else
26-
arguments = NormalizeTuples(types)
2745
end
2846

47+
if #temp == 1 and temp[1].Type ~= "tuple" and temp[1].Type ~= "union" then
48+
return Tuple({temp[1]})
49+
end
50+
51+
local arguments = Tuple(temp)
52+
temp = {}
53+
54+
for i = 1, 128 do
55+
local v, is_inf = arguments:GetAtTupleIndex(i)
56+
57+
if v and v.Type == "tuple" or is_inf then
58+
-- inf tuple
59+
temp[i] = v or Any()
60+
61+
break
62+
end
63+
64+
if not v then break end
65+
66+
temp[i] = v
67+
end
68+
69+
return Tuple(temp)
70+
end
71+
72+
local function postfix_call(self, self_arg, node, callable)
73+
local types = {self_arg}
74+
self:AnalyzeExpressions(node.expressions, types)
75+
local arguments = normalize_tuples(self, callable, types)
2976
self:PushCurrentExpression(node)
3077
local ret, err = self:Call(callable, arguments, node)
3178
self:PopCurrentExpression()

nattlua/analyzer/operators/function_call_body.lua

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -485,23 +485,32 @@ return function(self, obj, input)
485485
local output
486486

487487
do
488-
local union = Union()
488+
local union = {}
489489

490-
for _, ret in ipairs(returns) do
490+
for i, ret in ipairs(returns) do
491491
if #ret.types == 1 then
492-
union:AddType(ret.types[1])
492+
union[i] = ret.types[1]
493493
elseif #ret.types == 0 then
494-
local tup = Tuple({Nil()})
495-
union:AddType(tup)
494+
union[i] = Nil()
496495
else
497-
local tup = Tuple(ret.types)
498-
union:AddType(tup)
496+
union[i] = Tuple(ret.types)
499497
end
500498
end
501-
502-
output = union:Simplify()
503-
504-
if output.Type ~= "tuple" then output = Tuple({output}) end
499+
500+
if #union == 1 then
501+
if union[1].Type == "tuple" then
502+
output = union[1]
503+
elseif union[1].Type == "union" then
504+
output = union[1]
505+
output = output:Simplify()
506+
if output.Type ~= "tuple" then output = Tuple({output}) end
507+
else
508+
output = Tuple({union[1]})
509+
end
510+
else
511+
output = Union(union):Simplify()
512+
if output.Type ~= "tuple" then output = Tuple({output}) end
513+
end
505514
end
506515

507516
if not obj:IsExplicitOutputSignature() then

nattlua/analyzer/statements/generic_for.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local table = _G.table
22
local ipairs = ipairs
33
local Tuple = require("nattlua.types.tuple").Tuple
4-
local NormalizeTuples = require("nattlua.types.tuple").NormalizeTuples
54
local Union = require("nattlua.types.union").Union
65
local Nil = require("nattlua.types.symbol").Nil
76
local type_errors = require("nattlua.types.error_messages")

nattlua/types/tuple.lua

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,7 @@ function META.IsNotSubsetOfTuple(a--[[#: TTuple]], b--[[#: TTuple]])
303303
for i = 1, math.max(a:GetMinimumLength2(), b:GetMinimumLength2()) do
304304
local ok, reason, a_val, b_val, offset = a.IsSubsetOfTupleAtIndex(a, b, i)
305305

306-
if not ok then
307-
table.insert(errors, {reason, a_val, b_val, offset})
308-
end
306+
if not ok then table.insert(errors, {reason, a_val, b_val, offset}) end
309307
end
310308

311309
return errors[1] and errors or nil
@@ -746,9 +744,7 @@ function META.New(data--[[#: nil | List<|TBaseType|>]])
746744
}
747745
)
748746

749-
if data and data[1] then
750-
self:SetTable(data)
751-
end
747+
if data and data[1] then self:SetTable(data) end
752748

753749
return self
754750
end
@@ -760,50 +756,4 @@ return {
760756
self:SetRepeat(math.huge)
761757
return self
762758
end,
763-
NormalizeTuples = function(types--[[#: List<|TBaseType|>]])
764-
local arguments
765-
766-
if #types == 1 and types[1] and types[1].Type == "tuple" then
767-
arguments = types[1]
768-
else
769-
local temp = {}
770-
771-
for i, v in ipairs(types) do
772-
if v.Type == "tuple" then
773-
if i == #types then
774-
table.insert(temp, v)
775-
else
776-
local obj = v:GetWithNumber(1)
777-
778-
if obj then table.insert(temp, obj) end
779-
end
780-
else
781-
table.insert(temp, v)
782-
end
783-
end
784-
785-
local old_temp = temp
786-
arguments = META.New(temp)
787-
local temp = {}
788-
789-
for i = 1, 128 do
790-
local v, is_inf = arguments:GetAtTupleIndex(i)
791-
792-
if v and v.Type == "tuple" or is_inf then
793-
-- inf tuple
794-
temp[i] = v or Any()
795-
796-
break
797-
end
798-
799-
if not v then break end
800-
801-
temp[i] = v
802-
end
803-
804-
arguments = META.New(temp)
805-
end
806-
807-
return arguments
808-
end,
809759
}

0 commit comments

Comments
 (0)