Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://anggtwu.net/LUA/Ast3.lua.html
--   http://anggtwu.net/LUA/Ast3.lua
--          (find-angg "LUA/Ast3.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun a  () (interactive) (find-angg "LUA/Ast3.lua"))
-- (defun a1 () (interactive) (find-angg "LUA/Ast1.lua"))
-- (defun a2 () (interactive) (find-angg "LUA/Ast2.lua"))
-- (defun a3 () (interactive) (find-angg "LUA/Ast3.lua"))
-- (defun c3 () (interactive) (find-angg "LUA/Caepro3.lua"))
-- (defun c4 () (interactive) (find-angg "LUA/Caepro4.lua"))
-- (defun g2 () (interactive) (find-angg "LUA/Gram2.lua"))
-- (defun g3 () (interactive) (find-angg "LUA/Gram3.lua"))

-- «.AST»		(to "AST")
-- «.E»			(to "E")
-- «.ToText»		(to "ToText")
-- «.totex»		(to "totex")
--   «.AST-tests»	(to "AST-tests")
--   «.E-tests»		(to "E-tests")
--   «.ToText-tests»	(to "ToText-tests")

require "Show1"   -- (find-angg "LUA/Show1.lua")
require "Gram2"   -- (find-angg "LUA/Gram2.lua")
lpeg.ptmatch = function (pat, str) PP(pat:Ct():match(str)) end
L = Code.L

--     _    ____ _____ 
--    / \  / ___|_   _|
--   / _ \ \___ \ | |  
--  / ___ \ ___) || |  
-- /_/   \_\____/ |_|  
--                     
-- «AST»  (to ".AST")
-- Based on: (find-angg "LUA/Gram2.lua" "AST")
-- See:      (find-angg "LUA/Show1.lua" "string.show-tests")
--
AST = Class {
  type = "AST",
  alttags = {},			     -- Override this!
  ify = function (o)                 -- "o" needs to be a (scratch) table.
      if o[0] and AST.alttags[o[0]]  -- In some cases
      then o[0] = AST.alttags[o[0]]  --  we change o[0],
      end                            -- and in all cases
      return AST(o)                  --  we change the metatable of o.
    end,
  __tostring = function (o) return tostring(SynTree.from(o)) end,
  __index = {
    totex0 = function (o) return totex0(o) end,   -- returns a tt
    totex  = function (o) return totex(o)  end,   -- returns linear text
    show   = function (o, ...) return totex(o):show(...) end
  },
}


--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Ast3.lua"
= AST     {2, 3, 4}
= AST     {[0]="mul", 2, 3, 4}
= AST     {[0]="mul", 2, 3, AST {4,5,6}}
= AST.ify {[0]="mul", 2, 3, AST {4,5,6}}
AST.alttags = {mul="*"}
= AST     {[0]="mul", 2, 3, AST {4,5,6}}
= AST.ify {[0]="mul", 2, 3, AST {4,5,6}} -- changes the "mul" to "*"

--]]






--  _____ 
-- | ____|
-- |  _|  
-- | |___ 
-- |_____|
--        
-- «E»  (to ".E")

entries = {}
E = setmetatable({}, {
    __call     = function (_,name) return entries[name] end,
    __index    = function (_,name) return entries[name] end,
    __newindex = function (_,name,o) entries[name] = o end,
  })



--  _____   _____         _   
-- |_   _|_|_   _|____  _| |_ 
--   | |/ _ \| |/ _ \ \/ / __|
--   | | (_) | |  __/>  <| |_ 
--   |_|\___/|_|\___/_/\_\\__|
--                            
-- Conventions:
--   "a tt0" means an object of the class ToText0
--   "a tt"  means an object of the class ToText
--   "an o"  means an AST or a string or a number
--   "tex"   means linear text (usually LaTeX code)
--
-- A tt0 contains static instructions for converting an o to tex.
-- A tt contains a tt0 plus an o.
-- totex (o) converts o to tex.
-- totex0(o) converts o to a tt.
-- totex00 is a global variable that holds a tt0.
--
-- The suffix "0" always means "lower level".
--
-- «ToText»  (to ".ToText")

ToText0 = Class {
  type  = "ToText0",
  __tostring = function (tt0) return mytostringv(tt0) end,
  __index = {
    with = function (tt0,o) return ToText {tt0=tt0, o=o} end,
  },
}

ToText = Class {
  type = "ToText",
  __tostring = function (tt) return tostring(tt.o) end,
  __index = {
    from = function (tt, o) return ToText {tt0=tt.tt0, o=o} end,
    atn  = function (tt, n) return tt:from(tt.o[n+0]) end,
    tag  = function (tt) return tt.o[0] end,
    fmt  = function (tt) return tt.tt0.fmts[tt:tag()] end,
    --
    eval = function (tt,fmt,a,b,c)
        return L("tt,tt0,o,a,b,c => "..fmt)(tt, tt.tt0, tt.o, a, b, c)
      end,
    subst = function (tt, fmt)
        local f = function (s)
            if s:match"^[0-9]+$" then return tt:atn(s):totext() end
            return tt:eval(s)
          end
        return (fmt:gsub("<(.-)>", f))
      end,
    totext = function (tt)
        if type(tt.o) == "string" then return tt.o end
        if type(tt.o) == "number" then return tostring(tt.o) end
        if not tt:tag() then return "[No tag]" end
        if not tt:fmt() then return "[No fmt: "..tt:tag().."]" end
        return tt:asttotext(tt.o)
      end,
    asttotext = function (tt) return tt:subst(tt:fmt()) end,
  },
}

-- «totex»  (to ".totex")

totex00 = ToText0 {fmts={}}    -- Override this!
totex0  = function (o) return totex00:with(o)          end  -- returns a tt
totex   = function (o) return totex00:with(o):totext() end  -- returns text


-- «E-tests»  (to ".E-tests")
-- «totex-tests»  (to ".totex-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Ast3.lua"
mkast = function (op, ...) return AST.ify {[0]=op, ...} end
  E["sin"]   = mkast("fun", "sin")
  E[    "x"] = mkast("var", "x")
  E["sin x"] = mkast("ap", E"sin", E"x")
= E["sin x"]
= E["sin x"][1]
= E["sin x"]:totex()

fmts = VTable {}
funs = VTable {}
totex00 = ToText0 {fmts=fmts, funs=funs}
fmts.ap  = "<1> <2>"
fmts.fun = "<1>"
fmts.var = "<1>"
funs.sin = "\\sin "

= E["sin x"]:totex()
fmts.fun = "<tt0.funs[o[1]] or o[1]>"
= E["sin x"]:totex()
= E["sin x"]:totex0()
= E["sin x"]:totex0().o
= E["sin x"]:totex0().tt0
= E["sin x"]:totex0().tt0.fmts
= E["sin x"]:totex0().tt0.fmts.ap
= E["sin x"]:totex0():eval("2+3")
= E["sin x"]:totex0():eval("tt0")
= E["sin x"]:totex0():eval("tt0.funs")
= E["sin x"]:totex0():eval("o")
= E["sin x"]:totex0():eval("tt")
= E["sin x"]:totex0():eval("otype(o)")
= E["sin x"]:totex0():eval("otype(tt)")
= E["sin x"]:totex0():eval("tt:tag()")
= E["sin x"]:totex0():eval("tt:fmt()")
= E["sin x"]:totex0():eval("tt:atn(1)")
= E["sin x"]:totex0():eval("tt:atn(1):totext()")
= E["sin x"]:totex0():eval("tt:atn(1):tag()")
= E["sin x"]:totex0():eval("tt:atn(1):fmt()")
= E["sin x"]:totex0():subst("<1>")
= E["sin x"]:totex0():subst("<2>")
= E["sin x"]:totex0():subst("<2+3>")
= E["sin x"]:totex0():subst("<tostring(tt)>")
= E["sin x"]:totex0():subst("<tostring(o)>")
= E["sin x"]:totex0():subst("<tostring(o[1])>")
= E["sin x"]:totex0():subst("<totex(o[1])>")

--]==]





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