-
-
Save appgurueu/b323dc18c3e99f05bb98318d3ab335ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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