asma.moon (2599B)
1 -- 2 -- Asma tree helper script 3 -- 4 -- This script balances the trees at the end of projects/library/asma.tal. 5 -- 6 -- To run, you need Lua or LuaJIT, and just run etc/asma.lua from the top 7 -- directory of Uxn's git repository: 8 -- 9 -- lua etc/asma.lua 10 -- 11 -- This file is written in MoonScript, which is a language that compiles to 12 -- Lua, the same way as e.g. CoffeeScript compiles to JavaScript. Since 13 -- installing MoonScript has more dependencies than Lua, the compiled 14 -- etc/asma.lua is kept in Uxn's repository and will be kept updated as this 15 -- file changes. 16 -- 17 18 output = assert io.open '.asma.tal', 'w' 19 20 process_subtree = (items) -> 21 middle = math.floor #items / 2 + 1.25 22 node = items[middle] 23 if not node 24 return 25 node.left = process_subtree [ item for i, item in ipairs items when i < middle ] 26 node.right = process_subtree [ item for i, item in ipairs items when i > middle ] 27 node 28 29 process_tree = (items) -> 30 sorted_items = [ item for item in *items ] 31 table.sort sorted_items, (a, b) -> a.order < b.order 32 (process_subtree sorted_items).label = '&_entry' 33 for item in *items 34 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 35 36 parse_tree = (it) -> 37 items = {} 38 for l in it 39 if l == '' 40 process_tree items 41 output\write '\n' 42 return 43 item = { extra: '' } 44 item.key, item.rest = l\match '^%s*%S+%s+%S+%s+%S+%s+(%S+)(.*)' 45 if item.key\match '^%&' 46 item.extra = ' %s'\format item.key 47 item.key, item.rest = item.rest\match '^%s+(%S+)(.*)' 48 if item.key\match '^%"' 49 item.order = item.key\sub 2 50 elseif item.key\match '^%x%x' 51 item.order = string.char tonumber item.key, 16 52 else 53 error 'unknown key: %q'\format item.key 54 if item.order\match '^%a' 55 item.label = '&%s'\format item.order 56 elseif item.order\match '^.$' 57 item.label = '&%x'\format item.order\byte! 58 else 59 error 'unknown label: %q'\format item.order 60 item.ref = ':%s'\format item.label 61 table.insert items, item 62 63 it = assert io.lines 'projects/library/asma.tal' 64 waiting_for_cut = true 65 for l in it 66 output\write l 67 output\write '\n' 68 if l\find '--- cut here ---', 1, true 69 waiting_for_cut = false 70 if not waiting_for_cut and '@' == l\sub 1, 1 71 parse_tree it 72 output\close! 73 os.execute 'mv .asma.tal projects/library/asma.tal' 74