Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://anggtwu.net/LUA/CME3.lua.html
--   http://anggtwu.net/LUA/CME3.lua
--          (find-angg "LUA/CME3.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/CME3.lua"))
-- (defun o () (interactive) (find-angg "LUA/CME2.lua"))

-- «.AST-clean»		(to "AST-clean")
-- «.gr.last»		(to "gr.last")
-- «.foldplic»		(to "foldplic")
-- «.foldplic-tests»	(to "foldplic-tests")
-- «.grammar»		(to "grammar")
-- «.grammar-tests»	(to "grammar-tests")
-- «.matrices»		(to "matrices")
-- «.matrices-tests»	(to "matrices-tests")
-- «.fmts»		(to "fmts")
-- «.fmts-tests»	(to "fmts-tests")
-- «.verbatim»		(to "verbatim")
-- «.verbatim-tests»	(to "verbatim-tests")

require "ELpeg1"       -- (find-angg "LUA/ELpeg1.lua")
require "ToTeX1"       -- (find-angg "LUA/ToTeX1.lua")
require "MapAST2"      -- (find-angg "LUA/MapAST2.lua")
require "Verbatim4"    -- (find-angg "LUA/Verbatim3.lua")
require "Pict3"        -- (find-angg "LUA/Pict3.lua")

-- «AST-clean»  (to ".AST-clean")
MapAST.__index.trivs = Set.from(split("var num fun () .() {}"))
AST.__index.clean      = function (o) return MapAST_delaps(MapAST_deltrivs(o)) end
AST.__index.toverbatim = function (o)
    return Verbatim.from(splitlines(tostring(o)))
  end

-- «gr.last»  (to ".gr.last")
-- (find-angg "LUA/ELpeg1.lua" "Gram-Vlast")
Gram.__index.set = function (gr, name, pat)
    gr.last = pat
    gr.entries[name] = pat
  end

-- «foldplic»  (to ".foldplic")
foldpost = function (A)
    local o = A[1]
    for i=2,#A do o = mkast(A[i], o) end
    return o
  end
foldpre = function (A)
    local o = A[#A]
    for i=#A-1,1,-1 do o = mkast(A[i], o) end
    return o
  end
assocplic = function (pe, po) return Ct(pe*(_*po)^0) / foldpost end
assocpost = function (pe, po) return Ct(pe*(_*po)^0) / foldpost end
assocpre  = function (po, pe) return Ct((po*_)^0*pe) / foldpre end

-- «foldplic-tests»  (to ".foldplic-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "CME3.lua"
o = AST {mkast("+", "a", "b"), "'", "'", "'"}
= o
= foldpost(o)

o = AST {"?", "*", mkast("+", "a", "b")}
= o
= foldpre(o)

--]==]


-- «grammar»  (to ".grammar")
gr,V,VA,VE,PE = Gram.new()
_ = S(" ")^0

VA.num   = Cs(P"-"^-1 * R"09"^1)
VA.var   = anyof "x y u w a b e"
VA.fun   = anyof "f'' f' f g' g h h' k k' ln sin cos sqrt ddx"
VA.call  = P"!" * (R("AZ", "az")^1):Cs()
VA.ap    = V.fun *_* V.exprbasic

V.exprbasic = Cparen( "(", ")", V.expr)
            + Cparen(".(", ")", V.expr)
            + Cparen( "{", "}", V.expr)
            + Cparen( "[", "]", V.expr)
            + Cparen(".[", "]", V.expr)
            + V.call + V.ap + V.num + V.fun + V.var
V.exprplic  = assocplic(gr.last, Cs"'"  )
V.exprsubst = assocl(gr.last,    Cs"s"  )
V.exprpow   = assocr(gr.last,    Cs"^"  )
V.exprmul   = assocl(gr.last,    anyof("mul m"))
V.exprMul   = assocl(gr.last,    Cs"*"  )
V.exprdiv   = assocl(gr.last,    Cs"/"  )
V.exprplus  = assocl(gr.last,    anyof("+ -"))
V.exprDiv   = assocl(gr.last,    Cs"//" )
V.expreq    = assocl(gr.last,    Cs"="  )
V.exprceq   = assocl(gr.last,    Cs":=" )
V.exprnl    = assocl(gr.last,    Cs";;" )
V.expr      = _* V.exprnl

CME_parser  = gr:compile("expr")
CME_parse   = function (str) return CME_parser:match(str) end
CME_parsec  = function (str) return CME_parser:match(str):clean() end
CME         = function (str) return totex(CME_parse(str)) end


-- «grammar-tests»  (to ".grammar-tests")
--[==[
* (show2-use "/tmp/")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "CME3.lua"

= CME_parsec(".(x^2 * f(g(x))) s .[ f(x):=e^{3mx} ;; f'(x):=3me^{3mx} ].")

= CME_parsec("ddx(f(sin(x^4) + ln x))")
= CME_parsec("f'(sin(x^4) + ln x) m (ddx(sin(x^4) + ln x))")
= CME_parsec("f'(sin(x^4) + ln x) m (ddx(sin(x^4)) + ddx(ln x))")
= CME_parsec("f'(sin(x^4) + ln x) m ((cos(x^4) * ddx(x^4)) + ddx(ln x))")
= CME_parsec("f'(sin(x^4) + ln x) m ((cos(x^4) * ddx(x^4)) + 1/x)")
= CME_parsec("f'(sin(x^4) + ln x) m ((cos(x^4) * 4 m x^3) + 1/x)")

= CME_parse("x s x")
= CME_parse(".(x^2 * f(g(x))) s x")
= CME_parse(".(x^2 * f(g(x))) s .[ x:=y ]")
= CME_parse(".(x^2 * f(g(x))) s .[ x:=y ;; x:=y ]")
= CME_parse(".(x^2 * f(g(x))) s .[ x:=y ;; x:=y ;; x:=y ]")

= CME_parse (".(x^2 * f(g(x))) s .[ f(x):=e^{3mx} ;; f'(x):=3me^{3mx}]")
= CME_parse (".(x^2 * f(g(x))) s .[ f(x):=e^{3 m x} ;; f'(x):=3 m e^{3 m x} ].")
= CME_parse (".(x^2 * f(g(x))) s .[ f(x):=e^{3mx} ;; f'(x):=3me^{3mx} ].")
= CME_parsec(".(x^2 * f(g(x))) s .[ f(x):=e^{3mx} ;; f'(x):=3me^{3mx} ].")
= CME       (".(x^2 * f(g(x))) s .[ f(x):=e^{3mx} ;; f'(x):=3me^{3mx} ].")
= CME       (".(x^2 * f(g(x))) s .[ f(x):=e^{3mx} ;; f'(x):=3me^{3mx} ].") :show {em=1}
* (etv)

= CME_parse " (f(y)=y^2+y^3) s [y:=200]  = (f(200)=200^2 + 200^3) "
= CME_parse " (f(y)=y^2+y^3) s [y:=a+b]  = (f(a+b)=(a+b)^2 + (a+b)^3) "
= CME_parse " (f(y)=y^2+y^3) s [y:=g(x)] = (f(g(x))=g(x)^2 + g(x)^3) "


o = CME_parse " (f(y)=y^2+y^3) s [y:=200]  = (f(200)=200^2 + 200^3) "
o = CME_parse " (f(y)=y^2+y^3) s [y:=a+b]  = (f(a+b)=(a+b)^2 + (a+b)^3) "
o = CME_parse " (f(y)=y^2+y^3) s [y:=g(x)] = (f(g(x))=g(x)^2 + g(x)^3) "
= o
= ma:f(o)

--]==]



-- «matrices»  (to ".matrices")
usepackages.amsmath = [=[ \usepackage{amsmath} ]=]
defs.matrices = [=[
\def\sm  #1{\begin{smallmatrix}#1\end{smallmatrix}}
\def\mat #1{\begin{matrix}#1\end{matrix}}
\def\psm #1{\left (\sm {#1}\right )}
\def\bsm #1{\left [\sm {#1}\right ]}
\def\pmat#1{\left (\mat{#1}\right )}
\def\bmat#1{\left [\mat{#1}\right ]}
]=]

-- «matrices-tests»  (to ".matrices-tests")
--[==[
* (show2-use "/tmp/")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "CME3.lua"
= ([=[ foo=\bsm{a & b \\ c & d} ]=]):show0 {em=1}
= ([=[ foo=\bsm{a & b \\ c & d} ]=]):show  {em=1}
* (etv)

--]==]



--   __           _       
--  / _|_ __ ___ | |_ ___ 
-- | |_| '_ ` _ \| __/ __|
-- |  _| | | | | | |_\__ \
-- |_| |_| |_| |_|\__|___/
--                        
-- «fmts»  (to ".fmts")
-- (find-angg "LUA/CME2.lua" "fmts")
-- Based on: (find-angg "LUA/ELpeg-cme1.lua" "fmts-and-funs")
fmts[ "()"] = "(<1>)"
fmts[".()"] = "\\left(<1>\\right)"
fmts[ "[]"] = "\\bmat{<1>}"
fmts[".[]"] = "\\bsm{<1>}"
fmts[ "{}"] = "{<1>}"
fmts["'"]   = "{<1>}'"
fmts["s"]   = "<1> <2>"
fmts["+"]   = "<1> + <2>"
fmts["-"]   = "<1> - <2>"
fmts["*"]   = "<1> \\cdot <2>"
fmts["/"]   = "<1> / <2>"
fmts["//"]  = "\\frac{<1>}{<2>}"
fmts[ "="]  = "<1> = <2>"
fmts[":="]  = "<1> \\,:=\\, <2>"
fmts[";;"]  = "<1> \\\\{} <2>"
fmts["^"]   = "{<1>}^{<2>}"
fmts["ap"]  = "<1> <2>"
fmts["."]   = "<1> <2>"
fmts["m"]   = "<1> <2>"
fmts["mul"] = "<1> <2>"
fmts["num"] = "<1>"
fmts["var"] = "<1>"
fmts["call"] = "\\<1> "
fmts["fun"]  = "<funs[o[1]] or o[1]>"
funs["ln"]   = "\\ln "
funs["sin"]  = "\\sin "
funs["cos"]  = "\\cos "
funs["sqrt"] = "\\sqrt "
funs["ddx"]  = "\\frac{d}{dx} "

-- «fmts-tests»  (to ".fmts-tests")
-- See: (find-angg "LUA/ToTeX1.lua" "totexer-tests")
--[[
* (show2-use "/tmp/")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "CME3.lua"
o = mkast("ap", mkast("fun", "sin"), mkast("//", "a", "b"))
= o
= o:show00()
= o:show {em=1}
* (etv)

--]]



-- «verbatim»  (to ".verbatim")
usepackages.edrx21 = true
Verbatim.__index.topict0 = function (vb) return Pict {vb.o} end
Verbatim.__index.topict  = function (vb)
    return vb:act("e h c p v bg"):topict0():myvcenter()
  end
CME_totex = function (str) return CME_parse(str):totex() end
CME_tovbt = function (str) return CME_parsec(str):toverbatim():topict() end

-- «verbatim-tests»  (to ".verbatim-tests")
--[==[
* (show2-use "~/LATEX/")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "CME3.lua"

= CME_totex "1//x"
= CME_tovbt "1//x"

=   CME_parsec "(f(3mu+4)) s [x := 42]"
o = CME_parse  "(f(3mu+4)) s [x := 42]"
= o:totex()

=  CME_parsec("ddx(f(sin(x^4) + ln x))")
=  CME_parsec("f'(sin(x^4) + ln x) m (ddx(sin(x^4) + ln x))")
=  CME_parsec("f'(sin(x^4) + ln x) m (ddx(sin(x^4)) + ddx(ln x))")
=  CME_parsec("f'(sin(x^4) + ln x) m ((cos(x^4) * ddx(x^4)) + ddx(ln x))")
=  CME_parsec("f'(sin(x^4) + ln x) m ((cos(x^4) * ddx(x^4)) + {1//x})")
=  CME_parsec("f'(sin(x^4) + ln x) m ((cos(x^4) * 4 m x^3)  + {1//x})")
o = CME_parse("f'(sin(x^4) + ln x) m ((cos(x^4) * 4 m x^3)  + {1//x})")
PP(o)
= o:totex()

str = "f'(sin(x^4) + ln x) m ((cos(x^4) * 4 m x^3)  + {1//x})"
p = Pict { CME_totex(str), "=", CME_tovbt(str) }
= p:show0("", {scale=1})
= p:show ("", {scale=1})
* (etv)



= CME_totex(str)
= CME_tovbt(str)

= o:show  {em=1}
* (etv)

p = o:clean():toverbatim():topict()
= p
= p:tostring()
p2 = Pict {"a=", p}
PP(p)

= p2:show("",  {em=1})
* (etv)

= o
= o:clean()
= o:clean():toverbatim()
= o:clean():toverbatim():act("e h c p v bg")
= o:clean():toverbatim():act("e h c p v bg").o
= o:clean():toverbatim():act("e h c p v bg").o:show {em=1}
* (etv)
= string.show

=


= o:clean():toverbatim():pict()
= o:clean():toverbatim():pict():show("", {em=1})
* (etv)

    defvbt  = function (vb, name) return vb:act("e h c p v bg"):defvbt0(name) end,


:act("e h c p v bg")

--]==]




-- Local Variables:
-- coding:  utf-8-unix
-- End: