summaryrefslogtreecommitdiff
path: root/tex/context/base/core-env.lua
diff options
context:
space:
mode:
Diffstat (limited to 'tex/context/base/core-env.lua')
-rw-r--r--tex/context/base/core-env.lua347
1 files changed, 180 insertions, 167 deletions
diff --git a/tex/context/base/core-env.lua b/tex/context/base/core-env.lua
index a3a87b7f7..866176171 100644
--- a/tex/context/base/core-env.lua
+++ b/tex/context/base/core-env.lua
@@ -13,6 +13,9 @@ if not modules then modules = { } end modules ['core-env'] = {
local P, C, S, Cc, lpegmatch, patterns = lpeg.P, lpeg.C, lpeg.S, lpeg.Cc, lpeg.match, lpeg.patterns
+local newtoken = newtoken or token
+local context = context
+
local texgetcount = tex.getcount
local texsetcount = tex.setcount
@@ -20,8 +23,6 @@ local allocate = utilities.storage.allocate
local setmetatableindex = table.setmetatableindex
local setmetatablecall = table.setmetatablecall
-local context = context
-
tex.modes = allocate { }
tex.systemmodes = allocate { }
tex.constants = allocate { }
@@ -32,204 +33,216 @@ tex.isdefined = allocate { }
local modes = { }
local systemmodes = { }
--- if newtoken then -- we keep the old code for historic reasons
+-- we could use the built-in tex.is[count|dimen|skip|toks] here but caching
+-- at the lua en dis not that bad (and we need more anyway)
- -- undefined: mode == 0 or cmdname = "undefined_cs"
+-- undefined: mode == 0 or cmdname = "undefined_cs"
- local create = newtoken.create
+local create = newtoken.create
- local cache = table.setmetatableindex(function(t,k)
- local v = create(k)
- t[k] = v
- return v
- end)
+local cache = table.setmetatableindex(function(t,k)
+ local v = create(k)
+ t[k] = v
+ return v
+end)
- -- we can have a modes cache too
+-- we can have a modes cache too
- local iftrue = cache["iftrue"].mode
- local undefined = cache["*undefined*crap*"].mode -- is this ok?
+local iftrue = cache["iftrue"].mode
+local undefined = cache["*undefined*crap*"].mode -- is this ok?
- setmetatableindex(tex.modes, function(t,k)
- local m = modes[k]
- if m then
- return m()
+setmetatableindex(tex.modes, function(t,k)
+ local m = modes[k]
+ if m then
+ return m()
+ else
+ local n = "mode>" .. k
+ if cache[n].mode == 0 then
+ return false
else
- local n = "mode>" .. k
- if cache[n].mode == 0 then
- return false
- else
- modes[k] = function() return texgetcount(n) == 1 end
- return texgetcount(n) == 1 -- 2 is prevented
- end
+ modes[k] = function() return texgetcount(n) == 1 end
+ return texgetcount(n) == 1 -- 2 is prevented
end
- end)
+ end
+end)
- setmetatableindex(tex.systemmodes, function(t,k)
- local m = systemmodes[k]
- if m then
- return m()
+setmetatableindex(tex.systemmodes, function(t,k)
+ local m = systemmodes[k]
+ if m then
+ return m()
+ else
+ local n = "mode>*" .. k
+ if cache[n].mode == 0 then
+ return false
else
- local n = "mode>*" .. k
- if cache[n].mode == 0 then
- return false
- else
- systemmodes[k] = function() return texgetcount(n) == 1 end
- return texgetcount(n) == 1 -- 2 is prevented
- end
+ systemmodes[k] = function() return texgetcount(n) == 1 end
+ return texgetcount(n) == 1 -- 2 is prevented
end
- end)
-
- setmetatableindex(tex.constants, function(t,k)
- return cache[k].mode ~= 0 and texgetcount(k) or 0
- end)
-
- setmetatableindex(tex.conditionals, function(t,k) -- 0 == true
- return cache[k].mode ~= 0 and texgetcount(k) == 0
- end)
-
- table.setmetatableindex(tex.ifs, function(t,k)
- -- local mode = cache[k].mode
- -- if mode == 0 then
- -- return nil
- -- else
- -- return mode == iftrue
- -- end
- return cache[k].mode == iftrue
- end)
-
- setmetatableindex(tex.isdefined, function(t,k)
- return k and cache[k].mode ~= 0
- end)
-
- setmetatablecall(tex.isdefined, function(t,k)
- return k and cache[k].mode ~= 0
- end)
-
- local dimencode = cache["scratchdimen" ].command
- local countcode = cache["scratchcounter"].command
- local tokencode = cache["scratchtoken" ].command
- local skipcode = cache["scratchskip" ].command
-
- local types = {
- [dimencode] = "dimen",
- [countcode] = "count",
- [tokencode] = "token",
- [skipcode ] = "skip",
- }
-
- function tex.isdimen(name)
- return cache[name].command == dimencode
end
+end)
+
+setmetatableindex(tex.constants, function(t,k)
+ return cache[k].mode ~= 0 and texgetcount(k) or 0
+end)
+
+setmetatableindex(tex.conditionals, function(t,k) -- 0 == true
+ return cache[k].mode ~= 0 and texgetcount(k) == 0
+end)
+
+table.setmetatableindex(tex.ifs, function(t,k)
+ -- local mode = cache[k].mode
+ -- if mode == 0 then
+ -- return nil
+ -- else
+ -- return mode == iftrue
+ -- end
+ return cache[k].mode == iftrue
+end)
+
+setmetatableindex(tex.isdefined, function(t,k)
+ return k and cache[k].mode ~= 0
+end)
+
+setmetatablecall(tex.isdefined, function(t,k)
+ return k and cache[k].mode ~= 0
+end)
+
+local dimencode = cache["scratchdimen"] .command
+local countcode = cache["scratchcounter"] .command
+local tokencode = cache["scratchtoken"] .command
+local skipcode = cache["scratchskip"] .command
+local muskipcode = cache["scratchmuskip"] .command
+---- attributecode = cache["scratchattribute"].command
+
+local types = {
+ [dimencode] = "dimen",
+ [countcode] = "count",
+ [tokencode] = "token",
+ [skipcode] = "skip",
+ [muskipcode] = "muskip",
+ -- [attributecode] = "attribute",
+}
- function tex.iscount(name)
- return cache[name].command == countcode
- end
+function tex.isdimen(name)
+ local hit = cache[name]
+ return hit.command == dimencode and hit.index or true
+end
- function tex.istoken(name)
- return cache[name].command == tokencode
- end
+function tex.iscount(name)
+ local hit = cache[name]
+ return hit.command == countcode and hit.index or true
+end
- function tex.isskip(name)
- return cache[name].command == skipcode
- end
+function tex.istoken(name)
+ local hit = cache[name]
+ return hit.command == tokencode and hit.index or true
+end
- function tex.type(name)
- return types[cache[name].command] or "macro"
- end
+function tex.isskip(name)
+ local hit = cache[name]
+ return hit.command == skipcode and hit.index or true
+end
--- else
---
--- local csname_id = token.csname_id
--- local create = token.create
+function tex.ismuskip(name)
+ local hit = cache[name]
+ return hit.command == muskipcode and hit.index or true
+end
+
+function tex.type(name)
+ return types[cache[name].command] or "macro"
+end
+
+-- -- old token code
--
--- local undefined = csname_id("*undefined*crap*")
--- local iftrue = create("iftrue")[2] -- inefficient hack
+-- local csname_id = token.csname_id
+-- local create = token.create
--
--- setmetatableindex(tex.modes, function(t,k)
--- local m = modes[k]
--- if m then
--- return m()
--- else
--- local n = "mode>" .. k
--- if csname_id(n) == undefined then
--- return false
--- else
--- modes[k] = function() return texgetcount(n) == 1 end
--- return texgetcount(n) == 1 -- 2 is prevented
--- end
--- end
--- end)
+-- local undefined = csname_id("*undefined*crap*")
+-- local iftrue = create("iftrue")[2] -- inefficient hack
--
--- setmetatableindex(tex.systemmodes, function(t,k)
--- local m = systemmodes[k]
--- if m then
--- return m()
--- else
--- local n = "mode>*" .. k
--- if csname_id(n) == undefined then
--- return false
--- else
--- systemmodes[k] = function() return texgetcount(n) == 1 end
--- return texgetcount(n) == 1 -- 2 is prevented
--- end
--- end
--- end)
+-- setmetatableindex(tex.modes, function(t,k)
+-- local m = modes[k]
+-- if m then
+-- return m()
+-- else
+-- local n = "mode>" .. k
+-- if csname_id(n) == undefined then
+-- return false
+-- else
+-- modes[k] = function() return texgetcount(n) == 1 end
+-- return texgetcount(n) == 1 -- 2 is prevented
+-- end
+-- end
+-- end)
--
--- setmetatableindex(tex.constants, function(t,k)
--- return csname_id(k) ~= undefined and texgetcount(k) or 0
--- end)
+-- setmetatableindex(tex.systemmodes, function(t,k)
+-- local m = systemmodes[k]
+-- if m then
+-- return m()
+-- else
+-- local n = "mode>*" .. k
+-- if csname_id(n) == undefined then
+-- return false
+-- else
+-- systemmodes[k] = function() return texgetcount(n) == 1 end
+-- return texgetcount(n) == 1 -- 2 is prevented
+-- end
+-- end
+-- end)
--
--- setmetatableindex(tex.conditionals, function(t,k) -- 0 == true
--- return csname_id(k) ~= undefined and texgetcount(k) == 0
--- end)
+-- setmetatableindex(tex.constants, function(t,k)
+-- return csname_id(k) ~= undefined and texgetcount(k) or 0
+-- end)
--
--- setmetatableindex(tex.ifs, function(t,k)
--- -- k = "if" .. k -- better not
--- return csname_id(k) ~= undefined and create(k)[2] == iftrue -- inefficient, this create, we need a helper
--- end)
+-- setmetatableindex(tex.conditionals, function(t,k) -- 0 == true
+-- return csname_id(k) ~= undefined and texgetcount(k) == 0
+-- end)
--
--- setmetatableindex(tex.isdefined, function(t,k)
--- return k and csname_id(k) ~= undefined
--- end)
--- setmetatablecall(tex.isdefined, function(t,k)
--- return k and csname_id(k) ~= undefined
--- end)
+-- setmetatableindex(tex.ifs, function(t,k)
+-- -- k = "if" .. k -- better not
+-- return csname_id(k) ~= undefined and create(k)[2] == iftrue -- inefficient, this create, we need a helper
+-- end)
--
--- local lookuptoken = token.lookup
+-- setmetatableindex(tex.isdefined, function(t,k)
+-- return k and csname_id(k) ~= undefined
+-- end)
+-- setmetatablecall(tex.isdefined, function(t,k)
+-- return k and csname_id(k) ~= undefined
+-- end)
--
--- local dimencode = lookuptoken("scratchdimen" )[1]
--- local countcode = lookuptoken("scratchcounter")[1]
--- local tokencode = lookuptoken("scratchtoken" )[1]
--- local skipcode = lookuptoken("scratchskip" )[1]
+-- local lookuptoken = token.lookup
--
--- local types = {
--- [dimencode] = "dimen",
--- [countcode] = "count",
--- [tokencode] = "token",
--- [skipcode ] = "skip",
--- }
+-- local dimencode = lookuptoken("scratchdimen" )[1]
+-- local countcode = lookuptoken("scratchcounter")[1]
+-- local tokencode = lookuptoken("scratchtoken" )[1]
+-- local skipcode = lookuptoken("scratchskip" )[1]
--
--- function tex.isdimen(name)
--- return lookuptoken(name)[1] == dimencode
--- end
+-- local types = {
+-- [dimencode] = "dimen",
+-- [countcode] = "count",
+-- [tokencode] = "token",
+-- [skipcode ] = "skip",
+-- }
--
--- function tex.iscount(name)
--- return lookuptoken(name)[1] == countcode
--- end
+-- function tex.isdimen(name)
+-- return lookuptoken(name)[1] == dimencode
+-- end
--
--- function tex.istoken(name)
--- return lookuptoken(name)[1] == tokencode
--- end
+-- function tex.iscount(name)
+-- return lookuptoken(name)[1] == countcode
+-- end
--
--- function tex.isskip(name)
--- return lookuptoken(name)[1] == skipcode
--- end
+-- function tex.istoken(name)
+-- return lookuptoken(name)[1] == tokencode
+-- end
--
--- function tex.type(name)
--- return types[lookuptoken(name)[1]] or "macro"
--- end
+-- function tex.isskip(name)
+-- return lookuptoken(name)[1] == skipcode
+-- end
--
--- end
+-- function tex.type(name)
+-- return types[lookuptoken(name)[1]] or "macro"
+-- end
function context.setconditional(name,value)
if value then