summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/mlib-lua.lua
diff options
context:
space:
mode:
authorHans Hagen <pragma@wxs.nl>2017-10-24 11:13:27 +0200
committerContext Git Mirror Bot <phg42.2a@gmail.com>2017-10-24 11:13:27 +0200
commitd91c37679b13162a4ead85abbe564090b2e1b51c (patch)
tree01b9c9e5a2437c7381b0f6d109cd51f0f6bed84e /tex/context/base/mkiv/mlib-lua.lua
parentcb218b728af372a1ed6c9188765022dc057799ac (diff)
downloadcontext-d91c37679b13162a4ead85abbe564090b2e1b51c.tar.gz
2017-10-24 10:36:00
Diffstat (limited to 'tex/context/base/mkiv/mlib-lua.lua')
-rw-r--r--tex/context/base/mkiv/mlib-lua.lua96
1 files changed, 73 insertions, 23 deletions
diff --git a/tex/context/base/mkiv/mlib-lua.lua b/tex/context/base/mkiv/mlib-lua.lua
index 3406d05db..83c1e49ee 100644
--- a/tex/context/base/mkiv/mlib-lua.lua
+++ b/tex/context/base/mkiv/mlib-lua.lua
@@ -18,7 +18,7 @@ local concat = table.concat
local lpegmatch = lpeg.match
local lpegpatterns = lpeg.patterns
-local P, S, Ct = lpeg.P, lpeg.S, lpeg.Ct
+local P, S, Ct, Cs, Cc, C = lpeg.P, lpeg.S, lpeg.Ct, lpeg.Cs, lpeg.Cc, lpeg.C
local report_luarun = logs.reporter("metapost","lua")
local report_message = logs.reporter("metapost")
@@ -87,7 +87,44 @@ local function mpprint(...)
end
end
-mp.print = mpprint
+local r = P('%') / "percent"
+ + P('"') / "dquote"
+ + P('\n') / "crlf"
+ -- + P(' ') / "space"
+local a = Cc("&")
+local q = Cc('"')
+local p = Cs(q * (r * a)^-1 * (a * r * (P(-1) + a) + P(1))^0 * q)
+
+local function mpvprint(...) -- variable print
+ for i=1,select("#",...) do
+ local value = select(i,...)
+ if value ~= nil then
+ n = n + 1
+ local t = type(value)
+ if t == "number" then
+ buffer[n] = f_numeric(value)
+ elseif t == "string" then
+ buffer[n] = lpegmatch(p,value)
+ elseif t == "table" then
+ local m = #t
+ if m == 2 then
+ buffer[n] = f_pair(unpack(t))
+ elseif m == 3 then
+ buffer[n] = f_triplet(unpack(t))
+ elseif m == 4 then
+ buffer[n] = f_quadruple(unpack(t))
+ else -- error
+ buffer[n] = ""
+ end
+ else -- boolean or whatever
+ buffer[n] = tostring(value)
+ end
+ end
+ end
+end
+
+mp.print = mpprint
+mp.vprint = mpvprint
-- We had this:
--
@@ -217,9 +254,11 @@ local function mpquoted(fmt,s,...)
if not find(fmt,"%%") then
fmt = lpegmatch(replacer,fmt)
end
- buffer[n] = '"' .. formatters[fmt](s,...) .. '"'
+ -- buffer[n] = '"' .. formatters[fmt](s,...) .. '"'
+ buffer[n] = lpegmatch(p,formatters[fmt](s,...))
elseif fmt then
- buffer[n] = '"' .. fmt .. '"'
+ -- buffer[n] = '"' .. fmt .. '"'
+ buffer[n] = '"' .. lpegmatch(p,fmt) .. '"'
else
-- something is wrong
end
@@ -275,24 +314,6 @@ end
-- endfor ;
-- \stopMPpage
--- function metapost.runscript(code)
--- local f = loadstring(f_code(code))
--- if f then
--- local result = f()
--- if result then
--- local t = type(result)
--- if t == "number" then
--- return f_numeric(result)
--- elseif t == "string" then
--- return result
--- else
--- return tostring(result)
--- end
--- end
--- end
--- return ""
--- end
-
local cache, n = { }, 0 -- todo: when > n then reset cache or make weak
function metapost.runscript(code)
@@ -340,7 +361,7 @@ function metapost.runscript(code)
report_luarun("no result")
end
else
- report_luarun("no result, invalid code")
+ report_luarun("no result, invalid code: %s",code)
end
return ""
end
@@ -650,3 +671,32 @@ do
end
end
+
+do
+local stores = { }
+
+ function mp.newstore(name)
+ stores[name] = { }
+ end
+
+ function mp.disposestore(name)
+ stores[name] = nil
+ end
+
+ function mp.tostore(name,key,value)
+ stores[name][key] = value
+ end
+
+ function mp.fromstore(name,key)
+ mp.vprint(stores[name][key]) -- type specific
+ end
+
+ interfaces.implement {
+ name = "getMPstored",
+ arguments = { "string", "string" },
+ actions = function(name,key)
+ context(stores[name][key])
+ end
+ }
+
+end