Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
CapsAdmin committed Oct 29, 2024
1 parent 9c03777 commit dc81c59
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions nattlua/analyzer/mutation_solver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ local function get_value(mut)
end

local function mutation_solver(mutations, scope, obj)
-- common case early exit, if the last mutation was done in the same scope
if mutations[#mutations] and mutations[#mutations].scope == scope then
local first = get_value(mutations[#mutations])
local union

if first.Type == "union" then
union = first:Copy()
else
union = Union({first})
end

if obj.Type == "upvalue" then union:SetUpvalue(obj) end

return union:Simplify()
end

local mutations = remove_redundant_mutations(mutations, scope, obj)

if not mutations then return end
Expand All @@ -138,18 +154,16 @@ local function mutation_solver(mutations, scope, obj)

if i > 1 then
if obj.Type == "upvalue" then -- upvalue
local data = mut.scope:FindTrackedUpvalue(obj)
if mut.scope:GetStatementType() == "if" then
local data = mut.scope:FindTrackedUpvalue(obj)

if data then
local stack = data.stack

if stack then
if data and data.stack then
local val

if mut.scope:IsElseConditionalScope() then
val = stack[#stack].falsy
val = data.stack[#data.stack].falsy
else
val = stack[#stack].truthy
val = data.stack[#data.stack].truthy
end

if val and (val.Type ~= "union" or not val:IsEmpty()) then
Expand All @@ -171,16 +185,9 @@ local function mutation_solver(mutations, scope, obj)
union:AddType(value)
end

local value = union

if obj.Type == "upvalue" then value:SetUpvalue(obj) end

if #value:GetData() == 1 then
value = value:GetData()[1]
return value
end
if obj.Type == "upvalue" then union:SetUpvalue(obj) end

return value
return union:Simplify()
end

return mutation_solver

0 comments on commit dc81c59

Please sign in to comment.