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

-- «.MaximaIO»		(to "MaximaIO")
-- «.MaximaIO-tests»	(to "MaximaIO-tests")
-- «.MaximaIOs»		(to "MaximaIOs")
-- «.MaximaIOs-tests»	(to "MaximaIOs-tests")
-- «.Lpeg»		(to "Lpeg")
-- «.Lpeg-tests»	(to "Lpeg-tests")
-- «.maxima-head»	(to "maxima-head")
-- «.Maxima»		(to "Maxima")
-- «.Maxima-tests»	(to "Maxima-tests")

require "ELpeg1"          -- (find-angg "LUA/ELpeg1.lua")
require "Show2"           -- (find-angg "LUA/Show2.lua")
require "Co1"             -- (find-angg "LUA/Co1.lua")
require "DeleteComments2" -- (find-angg "LUA/DeleteComments2.lua")

--  __  __            _                 ___ ___  
-- |  \/  | __ ___  _(_)_ __ ___   __ _|_ _/ _ \ 
-- | |\/| |/ _` \ \/ / | '_ ` _ \ / _` || | | | |
-- | |  | | (_| |>  <| | | | | | | (_| || | |_| |
-- |_|  |_|\__,_/_/\_\_|_| |_| |_|\__,_|___\___/ 
--                                               
-- «MaximaIO»  (to ".MaximaIO")
MaximaIO = Class {
  type  = "MaximaIO",
  from0 = function (tbl) return MaximaIO(tbl) end,
  from  = function (tbl) return MaximaIO(tbl):split() end,
  __tostring = function (mio) return mio[1].."\n"..(mio[2] or "(no mio[2])") end,
  __index = {
    pat = "^(%(%%[io]([%d+])%)) ?(.*)$",
    isplit = function (mio)
        local i,linenum,rest = mio[1]:match(mio.pat)
        local is = VTable {i=i, linenum=linenum, unpack(splitlines(rest))}
        mio.is = is
        return mio
      end,
    osplit = function (mio)
        local i,linenum,rest = mio[2]:match(mio.pat)
        local os = VTable {i=i, linenum=linenum, unpack(splitlines(rest))}
        mio.os = os
        return mio
      end,
    split = function (mio) return mio:isplit():osplit() end,
    --
    co = Co.new(" \\%", "^"),
    cot = function (mio, str) return mio.co:translate(str) end,
    iprefix  = function (mio) return mio.is.i end,
    ispace   = function (mio) return (" "):rep(#mio:iprefix()) end,
    iprefixi = function (mio, i) return (i==1) and mio:iprefix() or mio:ispace() end,
    ii  = function (mio, i) return mio.is[i] end,
    iii = function (mio, i) return mio:iprefixi(i).." "..mio:ii(i) end,
    iboxes = function (mio)
        local f = function (i)
            return format("\\maximablue{%s}", mio:cot(mio:iii(i)))
          end
        return mapconcat(f, seq(1, #mio.is), "%\n")
      end,
    oboxes = function (mio)
        local box = function (a,b) return format("\\maximared{%s}{%s}", a, b) end
        local i,linenum,rest = mio[2]:match(mio.pat)
        return box(mio:cot(i), "").."%\n"..box("", rest)
      end,
    tex = function (mio) return mio:iboxes().."%\n"..mio:oboxes() end,
  },
}

-- «MaximaIO-tests»  (to ".MaximaIO-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Maxima1.lua"
mio = MaximaIO.from {"(%i1) foo +\nbar;", "(%o1) a+b"}
= mio
= mio:cot("x^2")
= mio[1]
= mio[2]
= mio:split()
= mio:iboxes()
= mio:oboxes()
= mio:tex()

--]==]



--  __  __            _                 ___ ___      
-- |  \/  | __ ___  _(_)_ __ ___   __ _|_ _/ _ \ ___ 
-- | |\/| |/ _` \ \/ / | '_ ` _ \ / _` || | | | / __|
-- | |  | | (_| |>  <| | | | | | | (_| || | |_| \__ \
-- |_|  |_|\__,_/_/\_\_|_| |_| |_|\__,_|___\___/|___/
--                                                   
-- «MaximaIOs»  (to ".MaximaIOs")

MaximaIOs = Class {
  type = "MaximaIOs",
  lpat = nil,    -- redefined below
  from = function (bigstr)
      local tbl = MaximaIOs.lpat:match(bigstr)
      tbl.bigstr = bigstr
      return MaximaIOs(tbl)
    end,
  __tostring = function (mios)
      return mapconcat(tostring, mios, "\n")
    end,
  __index = {
    tex = function (mios)
        local f = function (mio) return mio:tex() end
        return mapconcat(f, mios, "%\n")
      end,
  },
}

-- «MaximaIOs-tests»  (to ".MaximaIOs-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Maxima1.lua"
bigstr = ee_readfile "/tmp/o"
= bigstr
mios = MaximaIOs.from(bigstr)
= mios
= mios.bigstr
= mios:tex()
= mios[1]:tex()

o = MaximaIOs.lpat:match(bigstr)
= o
PP(o)
= o[1]
= o[2]
= o[3]
= o[4]
= mapconcat(tostring, o, "\n")
= mapconcat(tostring, mios, "\n")
= o
PPPV(o)
= otype(o)

--]==]




--  _                      
-- | |    _ __   ___  __ _ 
-- | |   | '_ \ / _ \/ _` |
-- | |___| |_) |  __/ (_| |
-- |_____| .__/ \___|\__, |
--       |_|         |___/ 
--
-- «Lpeg»  (to ".Lpeg")
lpeg.C1 = function (pat) return pat:C() / function (s) return s end end

gr,V,VAST,VEXPECT,PEXPECT = Gram.new()

V.line0 = ((1 - S"\n")^0):Cs()
V.i = V.line0:Cmt(function (subj,pos,li)
    if li:match"^%(%%i" then return true,li end
  end)
V.o = V.line0:Cmt(function (subj,pos,li)
    if li:match"^%(%%o" then return true,li end
  end)
V.ni = V.line0:Cmt(function (subj,pos,li)
    if not li:match"^%(%%i" then return true,li end
  end)
V.no = V.line0:Cmt(function (subj,pos,li)
    if not li:match"^%(%%o" then return true,li end
  end)
V.bigi   = (V.i     * ("\n" * V.no   )^0):C1()
V.bigo   = (V.o     * ("\n" * V.ni   )^0):C1()
V.bigio  = (V.bigi  *  "\n" * V.bigo    ):Ct() / MaximaIO.from
V.bigio  = (V.bigi  *  "\n" * V.bigo^-1 ):Ct() / MaximaIO.from
V.bigios = (V.bigio * ("\n" * V.bigio)^0):Ct()

MaximaIOs.lpat = gr:compile("bigios")

-- «Lpeg-tests»  (to ".Lpeg-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Maxima1.lua"

--]]


-- «maxima-head»  (to ".maxima-head")
-- (find-angg "LUA/Verbatim1.lua" "vbt-head")
registerhead = registerhead or function () return nop end
registerhead "%M" {
  name   = "maxima",
  action = function ()
      local i,j,origlines = tf:getblock()
      local f = function (li) return li:sub(2) end
      maxima_lines00 = origlines
      maxima_lines0  = map(f, origlines)
      maxima_lines   = mapconcat(f, origlines, "\n")
    end,
}

--  __  __            _                 
-- |  \/  | __ ___  _(_)_ __ ___   __ _ 
-- | |\/| |/ _` \ \/ / | '_ ` _ \ / _` |
-- | |  | | (_| |>  <| | | | | | | (_| |
-- |_|  |_|\__,_/_/\_\_|_| |_| |_|\__,_|
--                                      
-- «Maxima»  (to ".Maxima")

Maxima = Class {
  type = "Maxima",
  body0 = function () return maxima_lines end,
  body = function () return MaximaIOs.from(maxima_lines):tex() end,
  sa   = function (name) output(Maxima.sa0(name, Maxima.body())) end,
  sa0  = function (name, body)
      return format("\\sa{%s}{%%\n%s%%\n}", name, body)
    end,
  __index = {
  },
}

-- «Maxima-tests»  (to ".Maxima-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Maxima1.lua"
output = print
maxima_lines = "(%i1) foo\n(%o1) bar"
= Maxima.body()
= Maxima.sa("foo")
= Maxima.sa0("foo", "BAR")

--]==]





bigstr1 = [=[
(%i1) a : 2$

(%i2) a^3;
(%o2) 8
(%i3) a : 2$

(%i4) a^3;
(%o4) 8
(%i5) 
]=]

bigstr1 = [=[
(%i1) RP : 'diff(x^n,x) = n*x^(n-1);
(%o1) {{d}\over{d\,x}}\,x^{n}=n\,x^{n-1}
(%i2) subst([n=4], RP);
(%o2) {{d}\over{d\,x}}\,x^4=4\,x^3
(%i3) RE : 'diff(x^n,x) = x^(n-1)+10*n;
(%o3) {{d}\over{d\,x}}\,x^{n}=x^{n-1}+10\,n
(%i4) subst([n=42], RE);
(%o4) {{d}\over{d\,x}}\,x^{42}=x^{41}+420
(%i5) 
RC : 'diff(f(g(x)),x) = fp(g(x))*gp(x);
(%o5) {{d}\over{d\,x}}\,f\left(g\left(x\right)\right)=\mathrm{gp}\left(x\right)\,\mathrm{fp}\left(g\left(x\right)\right)
]=]


bigstr1 = [=[
(%i6) texput(fp, "f'")$

(%i7) texput(gp, "g'")$

(%i8) RC;
(%o8) {{d}\over{d\,x}}\,f\left(g\left(x\right)\right)=g'\left(x\right)\,f'\left(g\left(x\right)\right)
(%i9) 
sg  : lambda([x],42*x);
(%o9) \lambda\left(\left[ x \right]  , 42\,x\right)
(%i10) sgp : lambda([x],42);
(%o10) \lambda\left(\left[ x \right]  , 42\right)
(%i11) sf  : lambda([x],sin(x));
(%o11) \lambda\left(\left[ x \right]  , \sin x\right)
(%i12) sfp : lambda([x],cos(x));
(%o12) \lambda\left(\left[ x \right]  , \cos x\right)
(%i13) subst([g=sg,gp=sgp], RC);
(%o13) {{d}\over{d\,x}}\,f\left(42\,x\right)=42\,f'\left(42\,x\right)
(%i14) subst([f=sf,fp=sfp], RC);
(%o14) {{d}\over{d\,x}}\,\sin g\left(x\right)=g'\left(x\right)\,\cos g\left(x\right)
(%i15) subst([f=sf], RC);
(%o15) {{d}\over{d\,x}}\,\sin g\left(x\right)=g'\left(x\right)\,f'\left(g\left(x\right)\right)
(%i16) 
]=]



--[[
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("/usr/share/emacs/site-lisp/maxima/emaxima.lisp")$
display2d:'emaxima$
linenum:0;
f(x) := sin(x)^3;
define(F1(x), 'integrate(f(x), x));
define(F2(x), ev(f(x), 'integrate));

--]]

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Maxima1.lua"
-- bigstr = ee_readfile "/tmp/o"
bigstr = bigstr1
= bigstr
o = gr:cm0("bigio", bigstr)
o = gr:cm0("bigios", bigstr)
o = gr:cm0("bigi", bigstr)
o = gr:cm0("i", bigstr)
PP(o)
= o[1][1]
= o[1][2]
= o[2][1]
= o[2][2]
= o[3][1]
= o[3][2]
= o[4]

--]]













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