local function str_to_source(str) return "\"" .. str .. "\"" end local function table_to_str(tbl, indent) indent = indent or 1 -- to prevent people doing stuff that this function doesn't expect assert(not getmetatable(tbl), "cannot export a table with a metatable") local src = "{" for k, v in pairs(tbl) do print(k, v) if type(k) == "function" or type(v) == "function" then error("Cannot export function " .. tostring(k) .. " = " .. tostring(v)) end if type(k) ~= "number" then src = src .. "[" if type(k) == "string" then src = src .. str_to_source(k) elseif type(k) == "table" then error("Tables as keys aren't supported for exporting") else src = src .. tostring(k) end src = src .. "]=" end if type(v) == "table" then src = src .. table_to_str(v, indent + 1) elseif type(v) == "string" then src = src .. str_to_source(v) else src = src .. tostring(v) end src = src .. "," end src = src .. "}" return src end function export_to_source(tbl, file_name) local src = table_to_str(tbl) src = "return " .. src local file = io.open(file_name, "w") if file == nil then error("could not open file '" .. file_name .. "'") end file:write(src) file:close() end