Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://angg.twu.net/LUA/Arith5.lua.html -- http://angg.twu.net/LUA/Arith5.lua -- (find-angg "LUA/Arith5.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- (defun e4 () (interactive) (find-angg "LUA/Arith4.lua")) -- (defun e () (interactive) (find-angg "LUA/Arith5.lua")) -- (find-es "lpeg" "lpeg-quickref") -- (find-es "lpeg" "globals") require "lpeg" B,C,P,R,S,V = lpeg.B,lpeg.C,lpeg.P,lpeg.R,lpeg.S,lpeg.V Cb,Cc,Cf,Cg = lpeg.Cb,lpeg.Cc,lpeg.Cf,lpeg.Cg Cp,Cs,Ct = lpeg.Cp,lpeg.Cs,lpeg.Ct Carg,Cmt = lpeg.Carg,lpeg.Cmt lpeg_methods = getmetatable(P("")).__index s = S(" \t\n")^0 AssocOp = Class { type = "AssocOp", from = function (opsstr, name) local a = AssocOp {opsstr=opsstr, name=name} return a:prep() end, __index = { prep = function (a) local opslist = split(a.opsstr) local op = P(opslist[1]) for i=2,#opslist do op = op + P(opslist[i]) end local ops = C(op)*s a.ops = ops return a end, eoes = function (a, e) local f = function (...) local A = {...} if #A == 1 then return A[1] end A.name = a.name return HTable(A) end return (e * (a.ops * e)^0) / f end, }, } lpeg_methods.inassoc = function (exprpat, oppat) return oppat:eoes(exprpat) end num = R("09")^1 nums = C(num)*s assocop_mul = AssocOp.from("* /", "mul") assocop_add = AssocOp.from("+ -", "add") expr_mul = assocop_mul:eoes(nums) expr_add = assocop_add:eoes(expr_mul) --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Arith5.lua" = expr_add:match("1 * 2 / 3 + 4 * 5 * 6 + 7") = expr_add:match("1 * 2 / 3 + 4 * 5 * 6 + 7") expr_pat = nums: inassoc(assocop_mul): inassoc(assocop_add) = expr_pat:match("1 * 2 / 3 + 4 * 5 * 6 + 7") --]] -- Local Variables: -- coding: utf-8-unix -- End: