diff options
author | Hans Hagen <pragma@wxs.nl> | 2018-03-15 16:04:31 +0100 |
---|---|---|
committer | Context Git Mirror Bot <phg42.2a@gmail.com> | 2018-03-15 16:04:31 +0100 |
commit | a4e07f30e880ab27c2918f81f136e257475b7729 (patch) | |
tree | 02db002d3001a49777a049f9a98fdc872a5e1ad1 /tex/context/base/mkiv/mlib-lua.lua | |
parent | cbc37c39432e0ebe38e0922fc6d14c2955ab3ba2 (diff) | |
download | context-a4e07f30e880ab27c2918f81f136e257475b7729.tar.gz |
2018-03-15 15:36:00
Diffstat (limited to 'tex/context/base/mkiv/mlib-lua.lua')
-rw-r--r-- | tex/context/base/mkiv/mlib-lua.lua | 143 |
1 files changed, 105 insertions, 38 deletions
diff --git a/tex/context/base/mkiv/mlib-lua.lua b/tex/context/base/mkiv/mlib-lua.lua index 9831efc20..5e6be614b 100644 --- a/tex/context/base/mkiv/mlib-lua.lua +++ b/tex/context/base/mkiv/mlib-lua.lua @@ -13,11 +13,12 @@ if not modules then modules = { } end modules ['mlib-lua'] = { local type, tostring, select, loadstring = type, tostring, select, loadstring local find, match, gsub, gmatch = string.find, string.match, string.gsub, string.gmatch -local formatters = string.formatters -local concat = table.concat -local lpegmatch = lpeg.match +local formatters = string.formatters +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") @@ -67,7 +68,7 @@ local f_pair = formatters["(%.16f,%.16f)"] local f_triplet = formatters["(%.16f,%.16f,%.16f)"] local f_quadruple = formatters["(%.16f,%.16f,%.16f,%.16f)"] -local function mpprint(...) +local function mpprint(...) -- we can optimize for n=1 for i=1,select("#",...) do local value = select(i,...) if value ~= nil then @@ -86,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: -- @@ -204,7 +242,7 @@ local replacer = lpeg.replacer("@","%%") function mp.fprint(fmt,...) n = n + 1 - if not find(fmt,"%%") then + if not find(fmt,"%",1,true) then fmt = lpegmatch(replacer,fmt) end buffer[n] = formatters[fmt](...) @@ -213,12 +251,14 @@ end local function mpquoted(fmt,s,...) n = n + 1 if s then - if not find(fmt,"%%") then + if not find(fmt,"%",1,true) 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 @@ -230,8 +270,8 @@ function mp.n(t) return type(t) == "table" and #t or 0 end -local whitespace = lpeg.patterns.whitespace -local newline = lpeg.patterns.newline +local whitespace = lpegpatterns.whitespace +local newline = lpegpatterns.newline local setsep = newline^2 local comment = (S("#%") + P("--")) * (1-newline)^0 * (whitespace - setsep)^0 local value = (1-whitespace)^1 / tonumber @@ -274,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) @@ -339,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 @@ -479,13 +501,15 @@ function mp.prefix(str) mpquoted(match(str,"^(.-)[%d%[]") or str) end -function mp.dimensions(str) - local n = 0 - for s in gmatch(str,"%[?%-?%d+%]?") do --todo: lpeg - n = n + 1 - end - mpprint(n) -end +-- function mp.dimension(str) +-- local n = 0 +-- for s in gmatch(str,"%[?%-?%d+%]?") do --todo: lpeg +-- n = n + 1 +-- end +-- mpprint(n) +-- end + +mp.dimension = lpeg.counter(P("[") * lpegpatterns.integer * P("]") + lpegpatterns.integer,mpprint) -- faster and okay as we don't have many variables but probably only -- basename makes sense and even then it's not called that often @@ -647,3 +671,46 @@ do end end + +do + + local mpvprint = mp.vprint + + 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) + mpvprint(stores[name][key]) -- type specific + end + + interfaces.implement { + name = "getMPstored", + arguments = { "string", "string" }, + actions = function(name,key) + context(stores[name][key]) + end + } + +end + +do + + local mpprint = mp.print + local texmodes = tex.modes + + function mp.processingmode(s) + mpprint(tostring(texmodes[s])) + end + +end |