Warning: this is an htmlized version!
The original is across this link,
and the conversion rules are here.
-- treetex.lua: derivation trees and functions to convert them to TeX.
-- This file:
--   http://angg.twu.net/dednat6/treetex.lua.html
--   http://angg.twu.net/dednat6/treetex.lua
--                    (find-dn6 "treetex.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
-- Version: 2015aug22
-- License: GPL3
--

-- This is a rewrite of:
-- tatsuta        (find-dn4 "dednat4.lua" "tree-out")

require "eoo"       -- (find-dn6 "eoo.lua")
require "abbrevs"       -- (find-dn6 "eoo.lua")

TreeNode = Class {
  type    = "TreeNode",
  __index = {
    hasbar    = function (tn) return tn.bar ~= nil end,
    barchar   = function (tn) return tn.bar end,
    TeX_root  = function (tn) return tn[0] end,
    TeX_label = function (tn) return tn.label end,
    nhyps     = function (tn) return #tn end,
    hypslist  = function (tn) return tn end,
  },
}

TeX_subtree_tatsuta = function (tn, i_)
    if not tn:hasbar() then
      return i_.."\\mathstrut "..unabbrev(tn:TeX_root())
    else
      local r_ = tn:TeX_root()
      local b_ = tn:barchar()
      local l_ = tn:TeX_label()
      local h_ = tn:hypslist()
      local r  = "\\mathstrut "..unabbrev(r_)
      local b  = ({["-"]="", ["="]="=", [":"]="*"})[b_]
      local l  = (l_ and "[{"..unabbrev(l_).."}]") or ""
      local i  = i_.." "
      local f  = function (tn) return TeX_subtree_tatsuta(tn, i) end
      local h  = mapconcat(f, h_, " &\n")
      return i_.."\\infer"..b..l.."{ "..r.." }{\n"..h.." }"
    end
  end
TeX_deftree_tatsuta = function (tn, name, link)
    return "\\defded{"..name.."}{"..(link or "").."\n"..
           TeX_subtree_tatsuta(tn, " ").." }"
  end

TreeNode.__index.TeX_subtree = TeX_subtree_tatsuta
TreeNode.__index.TeX_deftree = TeX_deftree_tatsuta




--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
-- This test is broken
require "treetex"
require "treesegs"
r = treesegtest [[
%:  a  b 
%:  ----?
%:    c     
%:    
%:    ^foo     
]]
= r:totreenode():TeX_subtree()
PP(r)
PP(r:totreenode())

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
require "treetex"
TN = function (root, b, l, ...)
    return TreeNode {[0]=root, bar=b, label=l, ...}
  end
T = TN("f(1+2+3)", "-", "app",
       TN("f"),
       TN("1+2+3", "=", nil,
          TN"1",
          TN"2",
          TN"3"))
print(T:TeX_subtree("  "))
print(T:TeX_deftree("f(1+2+3)"))

-- Here's a typical TreeNode structure:
PPV(T)
-- {0="f(1+2+3)",
--    1={0="f"},
--    2={0="1+2+3",
--         1={0="1"},
--         2={0="2"},
--         3={0="3"},
--       "bar"="="},
--  "bar"="-",
--  "label"="app"}

* (ex "treetex-1")


-- 2015aug22: See: (find-dn6 "rect.lua" "dedtorect-tests")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
require "treetex"
require "rect"
addabbrev("->", "\\to")
TN = function (root, b, l, ...)
    return TreeNode {[0]=root, bar=b, label=l, ...}
  end
T = TN("Q->R", "-", "app",
       TN("P"),
       TN("P->(Q->R)"))
print(T:TeX_subtree("  "))
print(T:TeX_deftree("f(1+2+3)"))
= dedtorect(T)

* (ex "treetex-2")

--]==]


-- Local Variables:
-- coding:             raw-text-unix
-- ee-anchor-format:   "«%s»"
-- End: