Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://anggtwu.net/LUA/Pratt1.lua.html -- http://anggtwu.net/LUA/Pratt1.lua -- (find-angg "LUA/Pratt1.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- Some of the classes here will be useful for writing Pratt parsers -- someday, but right now this one processes, and produces, underbrace -- diagrams... -- -- Used by: -- (find-angg "LUA/Und2D1.lua") -- -- (defun e () (interactive) (find-angg "LUA/Pratt1.lua")) -- «.Range» (to "Range") -- «.Range-tests» (to "Range-tests") -- «.Ranges» (to "Ranges") -- «.Ranges-tests» (to "Ranges-tests") -- ____ -- | _ \ __ _ _ __ __ _ ___ -- | |_) / _` | '_ \ / _` |/ _ \ -- | _ < (_| | | | | (_| | __/ -- |_| \_\__,_|_| |_|\__, |\___| -- |___/ -- -- «Range» (to ".Range") Range = Class { type = "Range", __tostring = function (r) local c = r.rs and "_" or "" if r.a == r.b then return format("%d", r.a)..c end return format("%d-%d", r.a, r.b)..c end, __index = { }, } -- «Range-tests» (to ".Range-tests") --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Pratt1.lua" = Range {a=2,b=3} = Range {a=20,b=20} --]] -- ____ -- | _ \ __ _ _ __ __ _ ___ ___ -- | |_) / _` | '_ \ / _` |/ _ \/ __| -- | _ < (_| | | | | (_| | __/\__ \ -- |_| \_\__,_|_| |_|\__, |\___||___/ -- |___/ -- -- «Ranges» (to ".Ranges") Ranges = Class { type = "Ranges", __tostring = function (rs) return rs:tostring() end, __index = { tostring = function (rs, verbose) local f = function (i) return tostring(rs[i]) end local fv = function (i) return i..":"..tostring(rs[i]) end if verbose then f = fv end return mapconcat(f, seq(1,#rs), " ") end, abtoij = function (rs, a, b) local i = 1 while i<#rs and rs[i].b<a do i = i+1 end local j = i while j<#rs and rs[j].b<b do j = j+1 end return i,j end, packij = function (rs, i, j, verbose) local newrs = Ranges {} local newr = Range {a=rs[i].a, b=rs[j].b, rs=newrs} for k=i,j do table.insert(newrs, rs[k]) end -- if verbose then print(newr) end -- if verbose then print(newrs) end for k=i,j do table.remove(rs, i) end if verbose then print(rs) end table.insert(rs, i, newr) if verbose then print(rs) end return rs end, }, } -- «Ranges-tests» (to ".Ranges-tests") --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Pratt1.lua" rs = Ranges { Range {a=1, b=9}, Range {a=10, b=19}, Range {a=20, b=29}, Range {a=30, b=39}, Range {a=40, b=49}, Range {a=50, b=59}, } = rs = rs:tostring("v") = rs:abtoij(25,45) = rs:abtoij(20,40) = rs:abtoij(29,49) = rs:abtoij(30,49) = rs:abtoij(30,50) = rs:packij(2, 4) = rs[2] = rs[2].rs = PP(rs[3]) --]] -- Local Variables: -- coding: utf-8-unix -- End: