simplification
Download
101 lines 2.4 KiBLuaModified 2026-11-09 raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
local Files = require("files")
local search_path
search_path = function(directories, optimization)
  local suffixes
  if optimization > 0 then
    suffixes = {
      "?.lua",
      "?/init.lua",
      "?.nom",
      "?/init.nom"
    }
  else
    suffixes = {
      "?.nom",
      "?/init.nom"
    }
  end
  return table.concat((function()
    local _accum_0 = { }
    local _len_0 = 1
    for _index_0 = 1, #directories do
      local dir = directories[_index_0]
      for _index_1 = 1, #suffixes do
        local s = suffixes[_index_1]
        _accum_0[_len_0] = dir .. "/" .. s
        _len_0 = _len_0 + 1
      end
    end
    return _accum_0
  end)(), ";")
end
local file_loader
file_loader = function(directories, optimization)
  if optimization == nil then
    optimization = 1
  end
  local nomsupath = search_path(directories, optimization)
  return function(name)
    local path
    if name:match("%.nom$") or name:match("%.lua$") then
      path = name
    else
      local err
      path, err = package.searchpath(name, nomsupath, "/")
      if not (path) then
        return nil, err
      end
    end
    path = path:gsub("^%./", "")
    local code = Files.read(path)
    if not (code) then
      return nil, "Couldn't read the file '" .. tostring(path) .. "'"
    end
    if path:match("%.lua$") then
      local nom_path = path:gsub("%.lua$", ".nom")
      do
        local nom_code = Files.read(nom_path)
        if nom_code then
          if not (code:match("^[^\n]*\n%-%- Source hash: (%x+)") == Files.hash(nom_code)) then
            return nom_code, nom_path
          end
        end
      end
    end
    return code, path
  end
end
local table_loader
table_loader = function(modules)
  return function(name)
    local code = modules[name]
    if not (code) then
      return nil, "There's no module named '" .. tostring(name) .. "'"
    end
    return code, (name:match("%.nom$") and name or name .. ".nom")
  end
end
local chain_loaders
chain_loaders = function(...)
  local loaders = {
    ...
  }
  return function(name)
    local errors = { }
    for _index_0 = 1, #loaders do
      local loader = loaders[_index_0]
      local code, filename = loader(name)
      if code then
        return code, filename
      end
      table.insert(errors, filename)
    end
    return nil, table.concat(errors, "\n")
  end
end
return {
  search_path = search_path,
  file_loader = file_loader,
  table_loader = table_loader,
  chain_loaders = chain_loaders
}