diff options
author | Hans Hagen <pragma@wxs.nl> | 2018-06-08 10:23:49 +0200 |
---|---|---|
committer | Context Git Mirror Bot <phg42.2a@gmail.com> | 2018-06-08 10:23:49 +0200 |
commit | 86bfea5faac983d15c47eec27f43ec56e75c69ef (patch) | |
tree | 69a37851a9b8beeb625110f4fc51ff1ef188d7e4 /tex/context/base/mkiv/mlib-lua.lua | |
parent | c2d2236242c95f4753e7b197d576102c6fc20446 (diff) | |
download | context-86bfea5faac983d15c47eec27f43ec56e75c69ef.tar.gz |
2018-06-08 09:38:00
Diffstat (limited to 'tex/context/base/mkiv/mlib-lua.lua')
-rw-r--r-- | tex/context/base/mkiv/mlib-lua.lua | 94 |
1 files changed, 54 insertions, 40 deletions
diff --git a/tex/context/base/mkiv/mlib-lua.lua b/tex/context/base/mkiv/mlib-lua.lua index 60ad9477b..1faefa68e 100644 --- a/tex/context/base/mkiv/mlib-lua.lua +++ b/tex/context/base/mkiv/mlib-lua.lua @@ -12,9 +12,9 @@ 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 concat, insert, remove = table.concat, table.insert, table.remove local formatters = string.formatters -local concat = table.concat local lpegmatch = lpeg.match local lpegpatterns = lpeg.patterns @@ -31,7 +31,10 @@ local be_tolerant = true directives.register("metapost.lua.tolerant",functi mp = mp or { } -- system namespace MP = MP or { } -- user namespace -local buffer, n, max = { }, 0, 10 -- we reuse upto max +local buffer = { } +local n = 0 +local max = 10 -- we reuse upto max +local nesting = 0 function mp._f_() if trace_enabled and trace_luarun then @@ -40,7 +43,7 @@ function mp._f_() buffer = { } end n = 0 - report_luarun("data: %s",result) + report_luarun("%i: data: %s",nesting,result) return result else if n == 0 then @@ -62,11 +65,17 @@ end local f_code = formatters["%s return mp._f_()"] -local f_numeric = formatters["%.16f"] local f_integer = formatters["%i"] -local f_pair = formatters["(%.16f,%.16f)"] -local f_triplet = formatters["(%.16f,%.16f,%.16f)"] -local f_quadruple = formatters["(%.16f,%.16f,%.16f,%.16f)"] + +-- local f_numeric = formatters["%.16f"] +-- local f_pair = formatters["(%.16f,%.16f)"] +-- local f_triplet = formatters["(%.16f,%.16f,%.16f)"] +-- local f_quadruple = formatters["(%.16f,%.16f,%.16f,%.16f)"] + +local f_numeric = formatters["%n"] +local f_pair = formatters["(%n,%n)"] +local f_triplet = formatters["(%n,%n,%n)"] +local f_quadruple = formatters["(%n,%n,%n,%n)"] local f_points = formatters["%p"] local f_pair_pt = formatters["(%p,%p)"] @@ -383,9 +392,11 @@ end -- end function metapost.runscript(code) + nesting = nesting + 1 + local trace = trace_enabled and trace_luarun if trace then - report_luarun("code: %s",code) + report_luarun("%i: code: %s",nesting,code) end runs = runs + 1 local f = loadstring(f_code(code)) @@ -393,6 +404,8 @@ function metapost.runscript(code) f = loadstring(code) end if f then + local _buffer_, _n_ = buffer, n + buffer, n = { }, 0 local result = f() if result then local t = type(result) @@ -402,53 +415,54 @@ function metapost.runscript(code) result = tostring(result) end if trace then - report_luarun("result: %s",result) + if #result == 0 then + report_luarun("%i: no result",nesting) + else + report_luarun("%i: result: %s",nesting,result) + end end + buffer, n = _buffer_, _n_ + nesting = nesting - 1 return result elseif trace then - report_luarun("no result") + report_luarun("%i: no result",nesting) end + buffer, n = _buffer_, _n_ else - report_luarun("no result, invalid code: %s",code) + report_luarun("%i: no result, invalid code: %s",nesting,code) end + + nesting = nesting - 1 return "" end --- function metapost.initializescriptrunner(mpx) --- mp.numeric = function(s) return mpx:get_numeric(s) end --- mp.string = function(s) return mpx:get_string (s) end --- mp.boolean = function(s) return mpx:get_boolean(s) end --- mp.number = mp.numeric --- end - -local get_numeric = mplib.get_numeric -local get_string = mplib.get_string -local get_boolean = mplib.get_boolean -local get_number = get_numeric +do --- function metapost.initializescriptrunner(mpx) --- mp.numeric = function(s) return get_numeric(mpx,s) end --- mp.string = function(s) return get_string (mpx,s) end --- mp.boolean = function(s) return get_boolean(mpx,s) end --- mp.number = mp.numeric --- end + local get_numeric = mplib.get_numeric + local get_string = mplib.get_string + local get_boolean = mplib.get_boolean + local get_number = get_numeric -local currentmpx = nil + local currentmpx = nil + local stack = { } + local getters = { } -local get = { } -mp.get = get + getters.numeric = function(s) return get_numeric(currentmpx,s) end + getters.string = function(s) return get_string (currentmpx,s) end + getters.boolean = function(s) return get_boolean(currentmpx,s) end + getters.number = mp.numeric -get.numeric = function(s) return get_numeric(currentmpx,s) end -get.string = function(s) return get_string (currentmpx,s) end -get.boolean = function(s) return get_boolean(currentmpx,s) end -get.number = mp.numeric + function metapost.pushscriptrunner(mpx) + insert(stack,mpx) + currentmpx = mpx + end -function metapost.initializescriptrunner(mpx,trialrun) - currentmpx = mpx - if trace_luarun then - report_luarun("type of run: %s", trialrun and "trial" or "final") + function metapost.popscriptrunner() + currentmpx = remove(stack,mpx) end - -- trace_enabled = not trialrun blocks too much + + mp.get = getters + end -- texts: |