Skip to content

Instantly share code, notes, and snippets.

@appgurueu
Created July 20, 2021 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appgurueu/b323dc18c3e99f05bb98318d3ab335ae to your computer and use it in GitHub Desktop.
Save appgurueu/b323dc18c3e99f05bb98318d3ab335ae to your computer and use it in GitHub Desktop.
deps = {
A = {"B", "E"},
B = {},
C = {"A"},
D = {"C"},
E = {"D"}
}
local status = {}
local load_order = {}
function depends(mod, dependant)
if status[mod] == "loading" then
local cycle = {}
while dependant do
table.insert(cycle, dependant.mod)
dependant = dependant.dependant
end
error("circular dependency: " .. mod .. " <- " .. table.concat(cycle, " <- "))
end
if status[mod] == "loaded" then return end
status[mod] = "loading"
for _, hard_dep in pairs(deps[mod]) do
depends(hard_dep, {mod = mod, dependant = dependant})
end
status[mod] = "loaded"
table.insert(load_order, mod)
end
for mod in pairs(deps) do
depends(mod)
end
print(table.concat(load_order, ", "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment