|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://anggtwu.net/LUA/Freeze1.lua.html
-- http://anggtwu.net/LUA/Freeze1.lua
-- (find-angg "LUA/Freeze1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
-- Version: 2023nov25
--
-- This file contains some classes that were part of ELpeg1.lua until
-- nov/2023.
--
-- (defun e () (interactive) (find-angg "LUA/Freeze1.lua"))
-- «.totex» (to "totex")
-- «.totex-tests» (to "totex-tests")
-- «.Globals» (to "Globals")
-- «.Globals-tests» (to "Globals-tests")
-- «.Parser.freeze» (to "Parser.freeze")
-- «.Parser.freeze-tests» (to "Parser.freeze-tests")
-- _ _
-- | |_ ___ | |_ _____ __
-- | __/ _ \| __/ _ \ \/ /
-- | || (_) | || __/> <
-- \__\___/ \__\___/_/\_\
--
-- This is the basic way to convert ASTs to TeX code.
-- I moved its code to Subst1.lua, that is "require"d. See:
-- (find-angg "LUA/Subst1.lua" "Subst")
-- (find-angg "LUA/Subst1.lua" "totex")
-- «totex» (to ".totex")
-- «totex-tests» (to ".totex-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "ELpeg1.lua"
fmts = VTable {}
fmts["+"] = "<1> + <2>"
fmts["*"] = "<1> \\cdot <2>"
o = mkast("+", "a", mkast("*", 2, 3.4567))
= o
= totex (o)
= totex0 (o)
= totex00(o)
= totex00(o).o
= totex00(o):tag()
= totex00(o):fmt()
= totex00(o):getn(1)
= totex00(o):getn(1).o
= totex00(o):getn(2)
= totex00(o):getn(2).o
= totex00(o):getn(2):fmt()
= totex00(o):getn(2):getn(2)
= totex00(o):getn(2):getn(2).o
= totex00()
= totex00():gsub("a<2+3>b")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "ELpeg1.lua"
fmts = VTable {}
funs = VTable {}
-- totex00 = ToTeX0 {fmts=fmts, funs=funs}
fmts.ap = "<1> <2>"
fmts.fun = "<1>"
fmts.var = "<1>"
funs.sin = "\\sin "
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()
= E["sin x"]:totex()
fmts.fun = "<funs[o[1]] or o[1]>"
= E["sin x"]:totex()
* (code-etv2 "SHOW2DIR" "/tmp/" "Show2")
** (find-code-etv2 "SHOW2DIR" "/tmp/" "Show2")
= E["sin x"]:totex():show()
= E["sin x"]:totex():show(4)
= E["sin x"]:show(2.5)
* (etv)
--]==]
-- ____ _ _ _
-- / ___| | ___ | |__ __ _| |___
-- | | _| |/ _ \| '_ \ / _` | / __|
-- | |_| | | (_) | |_) | (_| | \__ \
-- \____|_|\___/|_.__/ \__,_|_|___/
--
-- I moved this code to Globals1.lua, that is "require"d.
-- See: (find-angg "LUA/Globals1.lua")
--
-- «Globals» (to ".Globals")
-- «Globals-tests» (to ".Globals-tests")
-- ____ __
-- | _ \ __ _ _ __ ___ ___ _ __ / _|_ __ ___ ___ _______
-- | |_) / _` | '__/ __|/ _ \ '__| | |_| '__/ _ \/ _ \_ / _ \
-- | __/ (_| | | \__ \ __/ | _ | _| | | __/ __// / __/
-- |_| \__,_|_| |___/\___|_|(_) |_| |_| \___|\___/___\___|
--
-- Work in progress!
-- See: (find-angg "LUA/ELpeg-cme1.lua" "Freeze")
-- «Parser.freeze» (to ".Parser.freeze")
Parser = Class {
type = "Parser",
freeze = function (top)
local p = Parser({top=top}):compile():save()
return p:matcher(),p
end,
__index = {
compile = function (p) grc = gr:compile(p.top); return p end,
save = function (p) p.globals = p:save0(); return p end,
save0 = function (p) return Globals.save "gr grc fmts funs" end,
restore = function (p) p.globals:restore(); return p end,
match = function (p, subj, pos) p:restore(); return p:match0(subj, pos) end,
match0 = function (p, subj, pos)
if type(pos) == "string" then pos = subj:match(pos) end
return grc:match(subj, pos)
end,
matcher = function (p) return function (...) return p:match(...) end end,
},
}
-- «Parser.freeze-tests» (to ".Parser.freeze-tests")
--[[
-- (find-angg "LUA/Globals1.lua" "Globals-tests")
-- These tests are broken
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "ELpeg1.lua"
dofile "Globals1.lua"
lambda_globals = Globals.save "fmts funs gr"
lambda_totex = lambda_globals:fun(function (...) totex (...) end)
lambda_totex0 = lambda_globals:fun(function (...) totex0 (...) end)
lambda_totex00 = lambda_globals:fun(function (...) totex00(...) end)
lambda_totex = lambda_globals:fun(totex)
lambda_totex0 = lambda_globals:fun(totex0)
lambda_totex00 = lambda_globals:fun(totex00)
--]]
-- Local Variables:
-- coding: utf-8-unix
-- End: