Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://angg.twu.net/LUA/PrecParser1.lua.html -- http://angg.twu.net/LUA/PrecParser1.lua -- (find-angg "LUA/PrecParser1.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- (defun p () (interactive) (find-angg "LUA/PrecParser1.lua")) -- (defun o () (interactive) (find-angg "LUA/Parenthesize2.lua")) -- (defun oo () (interactive) (find-angg "LUA/Parenthesize1.lua")) -- (defun s () (interactive) (find-angg "LUA/Stack1.lua")) require "Parenthesize2" -- (find-angg "LUA/Parenthesize2.lua") require "Stack1" -- (find-angg "LUA/Stack1.lua") numberify = function (str) return str:match("^[0-9]+$") and str+0 or str end PrecParse = Class { type = "PrecParse", new = function () return PrecParse {} end, __tostring = function (pp) return tostring(trees(unpack(pp))) end, -- See: (find-es "lua5" "methodsover") __index = methodsover(Stack.__index) { -- See: (find-angg "LUA/Stack1.lua") halfpre = function (pp) local op = pp:pop(); return pp:push(pre(op,"?")) end, halfbin = function (pp) local a,op = pp:pop2(); return pp:push(bin(a,op,"?")) end, closepre = function (pp) local a,b = pp:pop2(); return pp:push(pre(a[0],b)) end, closebin = function (pp) local a,b = pp:pop2(); return pp:push(bin(a[1], a[0], b)) end, closepost = function (pp) local a,op = pp:pop2(); return pp:push(post(a,op)) end, -- orbp1 = function (pp) return opi:orbp(pp:pick(1)) end, -- use the global opi reducep = function (pp, lbp) return #pp>=2 and pp:orbp1()>lbp end, reduce1 = function (pp) if opi:oprefix(pp:pick(1)) then return pp:closepre() end if opi:oinfix (pp:pick(1)) then return pp:closebin() end error("bad reduce") end, reduceforlbp = function (pp, lbp) while pp:reducep(lbp) do pp:reduce1() end return pp end, reduceforop = function (pp, op) return pp:reduceforlbp(opi:oplbp(op)) end, reduceall = function (pp, op) return pp:reduceforlbp(0) end, -- half = function (pp) if opi:opinfix (pp:pick(0)) then return pp:halfbin() end if opi:opprefix(pp:pick(0)) then return pp:halfpre() end error("bad half") end, -- pushx = function (pp, x) if opi:opinfix (x) then return pp:reduceforop(x):push(x):half() elseif opi:opprefix (x) then return pp :push(x):half() elseif opi:oppostfix(x) then return pp:reduceforop(x):push(x):closepost() else return pp:push(x) end end, pushxs = function (pp, str) for i,x in ipairs(split(str)) do pp:pushx(numberify(x)) end return pp end, }, } --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "PrecParser1.lua" s = PrecParse.new() = s:pushs (2, "*") = s:halfbin () = s:pushs ("u-") = s:halfpre () = s:pushs (3) = s:closepre () = s:closebin () = s:pushs ("!") = s:closepost() * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "PrecParser1.lua" s = PrecParse.new() = opi:opinfo("+") = opi:oplbp("+") = opi:oprbp("+") = opi:oplbp("!") = opi:oprbp("!") = opi:oplbp("u-") = opi:oprbp("u-") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "PrecParser1.lua" s = PrecParse.new() = s:pushs(1) = s:pushs("+"):halfbin() = s:pushs(2) = s:pushs("+"):halfbin() = s:pushs(3) = s:pushs("^"):halfbin() = s:pushs(4) = s:pushs("^"):halfbin() = s:pushs(5) = s:pushs("^"):halfbin() = s:pushs(6) = s:reduceforop("*") = s:reduceall() * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "PrecParser1.lua" s = PrecParse.new() = s:pushx(2) = s:pushx("*") = s:pushx(3) = s:pushx("+") = s:pushx("u-") = s:pushx(4) = s:pushx("!") = s:pushx("^") = s:pushx(5) = s:pushx("+") s = PrecParse.new() = s:pushxs("2 * 3 + u- 4 ! ^ 5 +") = s:pushxs("6 + 7") = s:reduceall() = s[1] = opi:text(s[1]) --]] -- Local Variables: -- coding: utf-8-unix -- End: