uxn/etc/asma.lua

104 lines
2.7 KiB
Lua
Raw Permalink Normal View History

2022-11-10 18:47:14 -05:00
local output = assert(io.open('.asma.tal', 'w'))
local process_subtree
process_subtree = function(items)
local middle = math.floor(#items / 2 + 1.25)
local node = items[middle]
if not node then
return
end
node.left = process_subtree((function()
2021-05-03 14:15:06 -04:00
local _accum_0 = { }
local _len_0 = 1
2022-11-10 18:47:14 -05:00
for i, item in ipairs(items) do
if i < middle then
_accum_0[_len_0] = item
_len_0 = _len_0 + 1
2021-05-03 14:15:06 -04:00
end
end
2022-11-10 18:47:14 -05:00
return _accum_0
end)())
node.right = process_subtree((function()
local _accum_0 = { }
local _len_0 = 1
for i, item in ipairs(items) do
if i > middle then
_accum_0[_len_0] = item
_len_0 = _len_0 + 1
end
2021-05-03 14:15:06 -04:00
end
2022-11-10 18:47:14 -05:00
return _accum_0
end)())
return node
2021-05-03 14:15:06 -04:00
end
2022-11-10 18:47:14 -05:00
local process_tree
process_tree = function(items)
local sorted_items
2021-05-03 14:15:06 -04:00
do
local _accum_0 = { }
local _len_0 = 1
2022-11-10 18:47:14 -05:00
for _index_0 = 1, #items do
local item = items[_index_0]
_accum_0[_len_0] = item
2021-05-03 14:15:06 -04:00
_len_0 = _len_0 + 1
end
2022-11-10 18:47:14 -05:00
sorted_items = _accum_0
2021-05-03 14:15:06 -04:00
end
2022-11-10 18:47:14 -05:00
table.sort(sorted_items, function(a, b)
return a.order < b.order
end);
(process_subtree(sorted_items)).label = '&_entry'
for _index_0 = 1, #items do
local item = items[_index_0]
output:write(('\t%-11s %-10s %-12s %s%s\n'):format(item.label, item.left and item.left.ref or ' $2', (item.right and item.right.ref or ' $2') .. item.extra, item.key, item.rest))
2021-05-03 14:15:06 -04:00
end
end
2022-11-10 18:47:14 -05:00
local parse_tree
parse_tree = function(it)
local items = { }
for l in it do
if l == '' then
process_tree(items)
output:write('\n')
return
2021-05-03 14:15:06 -04:00
end
2022-11-10 18:47:14 -05:00
local item = {
extra = ''
}
item.key, item.rest = l:match('^%s*%S+%s+%S+%s+%S+%s+(%S+)(.*)')
if item.key:match('^%&') then
item.extra = (' %s'):format(item.key)
item.key, item.rest = item.rest:match('^%s+(%S+)(.*)')
2021-05-03 14:15:06 -04:00
end
2022-11-10 18:47:14 -05:00
if item.key:match('^%"') then
item.order = item.key:sub(2)
elseif item.key:match('^%x%x') then
item.order = string.char(tonumber(item.key, 16))
2021-05-03 14:15:06 -04:00
else
2022-11-10 18:47:14 -05:00
error(('unknown key: %q'):format(item.key))
2021-05-03 14:15:06 -04:00
end
2022-11-10 18:47:14 -05:00
if item.order:match('^%a') then
item.label = ('&%s'):format(item.order)
elseif item.order:match('^.$') then
item.label = ('&%x'):format(item.order:byte())
else
error(('unknown label: %q'):format(item.order))
2021-05-03 14:15:06 -04:00
end
2022-11-10 18:47:14 -05:00
item.ref = (':%s'):format(item.label)
table.insert(items, item)
end
end
local it = assert(io.lines('projects/library/asma.tal'))
local waiting_for_cut = true
for l in it do
output:write(l)
output:write('\n')
if l:find('--- cut here ---', 1, true) then
waiting_for_cut = false
end
if not waiting_for_cut and '@' == l:sub(1, 1) then
parse_tree(it)
2021-05-03 14:15:06 -04:00
end
end
2022-11-10 18:47:14 -05:00
output:close()
return os.execute('mv .asma.tal projects/library/asma.tal')