if not modules then modules = { } end modules ['syst-con'] = {
    version   = 1.001,
    comment   = "companion to syst-con.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local tonumber, tostring = tonumber, tostring
local math = math
local utfchar = utf.char
local gsub = string.gsub
local concat, reverse, sort = table.concat, table.reverse, table.sort

converters       = converters or { }
local converters = converters

local context    = context
local commands   = commands
local implement  = interfaces.implement

local formatters = string.formatters

function converters.hexstringtonumber(n) tonumber(n,16) end
function converters.octstringtonumber(n) tonumber(n, 8) end

local f_lchexnumber  = formatters["%x"]
local f_uchexnumber  = formatters["%X"]
local f_lchexnumbers = formatters["%02x"]
local f_uchexnumbers = formatters["%02X"]
local f_octnumber    = formatters["%03o"]
local   nicenumber   = formatters["%0.6F"] -- or N

local lchexnumber  = function(n) if n < 0 then n = 0x100000000 + n end return f_lchexnumber (n) end
local uchexnumber  = function(n) if n < 0 then n = 0x100000000 + n end return f_uchexnumber (n) end
local lchexnumbers = function(n) if n < 0 then n = 0x100000000 + n end return f_lchexnumbers(n) end
local uchexnumbers = function(n) if n < 0 then n = 0x100000000 + n end return f_uchexnumbers(n) end
local octnumber    = function(n) if n < 0 then n = 0x100000000 + n end return f_octnumber   (n) end

converters.lchexnumber  = lchexnumber
converters.uchexnumber  = uchexnumber
converters.lchexnumbers = lchexnumbers
converters.uchexnumbers = uchexnumbers
converters.octnumber    = octnumber
converters.nicenumber   = nicenumber

implement { name = "hexstringtonumber", actions = { tonumber, context }, arguments = { "integer", 16 } }
implement { name = "octstringtonumber", actions = { tonumber, context }, arguments = { "integer",  8 } }

implement { name = "rawcharacter", actions = function(n) context(utfchar(0x110000+n)) end, arguments = "integer" }

implement { name = "lchexnumber",  actions = { lchexnumber,  context }, arguments = "integer" }
implement { name = "uchexnumber",  actions = { uchexnumber,  context }, arguments = "integer" }
implement { name = "lchexnumbers", actions = { lchexnumbers, context }, arguments = "integer" }
implement { name = "uchexnumbers", actions = { uchexnumbers, context }, arguments = "integer" }
implement { name = "octnumber",    actions = { octnumber,    context }, arguments = "integer" }

-- replaced by posits

implement { name = "sin",  actions = { math.sin,  nicenumber, context }, arguments = "number" }
implement { name = "cos",  actions = { math.cos,  nicenumber, context }, arguments = "number" }
implement { name = "tan",  actions = { math.tan,  nicenumber, context }, arguments = "number" }

implement { name = "sind", actions = { math.sind, nicenumber, context }, arguments = "number" }
implement { name = "cosd", actions = { math.cosd, nicenumber, context }, arguments = "number" }
implement { name = "tand", actions = { math.tand, nicenumber, context }, arguments = "number" }

-- only as commands

function commands.format(fmt,...) context((gsub(fmt,"@","%%")),...) end

implement {
    name          = "formatone", -- used as such so no name change here
    public        = true,
    semiprotected = true,
    arguments     = "2 strings",
    actions       = function(f,s) context((gsub(f,"@","%%")),s) end,
}

local function tobits(b,w,d)
    local t = { }
    if not w then
        w = 32
    end
    local m = d
    local k = 0
    for i=1,w do
        k = k + 1 ; t[k] = (b & 0x1) == 1 and "1" or "0"
        if m then
            m = m - 1
            if m == 0 then
                k = k + 1 ; t[k] = " "
                m = d
            end
        end
        b = b >> 1
    end
    if t[k] == " " then
        t[k] = nil
    end
    return concat(reverse(t))
end

implement {
    name      = "tobits",
    public    = true,
    arguments = "3 integers",
    actions   = function(w,d,n) context(tobits(n,w,d)) end,   -- fast enough
}

do

    local round, log = math.round, math.log

    implement {
        name      = "tobit",
        public    = true,
        arguments = "integer",
        actions   = function(n)
            context(n > 0 and (round(log(n,2)) + 1) or 0)
        end,
    }

end

implement {
    name      = "tohexa",
    public    = true,
    arguments = "2 integers",
    actions   = function(w,n) context("0x%0"..w.."X",n) end, -- somewhat slow
}

interfaces.implement {
    name      = "showbits",
    public    = true,
    arguments = { "string", "integer" },
    actions   = function(s,n)
        local b = tex[s]
        if type(b) == "table" then
            local t = { }
            for i=1,32 do
                if (n & 0x1) == 1 then
                    local f = b[1<<(i-1)]
                    if f then
                        t[#t+1] = tostring(f)
                    end
                end
                n = n >> 1
            end
            if #t > 0 then
                sort(t)
                context("% t",t)
            end
        end
    end,
}
