Поправил еще одну ошибку и сделал чтобы убирались лишние отступы
function print_tree(tree, indent)
indent = indent or 0
for _, branch in ipairs(tree.children) do
print(string.rep('\t', indent) .. branch.value)
if branch.children then
print_tree(branch, indent + 1)
end
end
end
function comp(a, b)
return a.value < b.value
end
function read(file)
local line = file:read()
if not line then
print('error 002')
return nil
end
local _, indent = line:find('^\t+')
indent = indent or 0
local last = indent
local first = indent
local tree = {children = {}}
local branch
while line do
_, indent = line:find('^\t+')
if indent < first then
print('error 001')
return nil
end
if indent > last then
tree = branch
tree.children = {}
last = last + 1
else
while indent < last do
table.sort(tree.children, comp)
tree = tree.parent
last = last - 1
end
end
branch = {parent = tree, value = line:sub(indent + 1)}
table.insert(tree.children, branch)
line = file:read()
end
while tree.parent do
table.sort(tree.children, comp)
tree = tree.parent
end
table.sort(tree.children, comp)
return tree
end
--if arg[1] then
--local file = io.open(arg[1])
local file = io.input()
if file and file:seek('set', 3) then
local tree = read(file)
if tree then
print_tree(tree)
else
print('epic fail...')
end
end
--end