1if not modules then modules = { } end modules ['syst-con'] = {
2 version = 1.001,
3 comment = "companion to syst-con.mkiv",
4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5 copyright = "PRAGMA ADE / ConTeXt Development Team",
6 license = "see context related readme files"
7}
8
9local tonumber, tostring = tonumber, tostring
10local math = math
11local utfchar = utf.char
12local gsub = string.gsub
13local concat, reverse, sort = table.concat, table.reverse, table.sort
14
15converters = converters or { }
16local converters = converters
17
18local context = context
19local commands = commands
20local implement = interfaces.implement
21
22local formatters = string.formatters
23
24function converters.hexstringtonumber(n) tonumber(n,16) end
25function converters.octstringtonumber(n) tonumber(n, 8) end
26
27local f_lchexnumber = formatters["%x"]
28local f_uchexnumber = formatters["%X"]
29local f_lchexnumbers = formatters["%02x"]
30local f_uchexnumbers = formatters["%02X"]
31local f_octnumber = formatters["%03o"]
32local nicenumber = formatters["%0.6F"]
33
34local lchexnumber = function(n) if n < 0 then n = 0x100000000 + n end return f_lchexnumber (n) end
35local uchexnumber = function(n) if n < 0 then n = 0x100000000 + n end return f_uchexnumber (n) end
36local lchexnumbers = function(n) if n < 0 then n = 0x100000000 + n end return f_lchexnumbers(n) end
37local uchexnumbers = function(n) if n < 0 then n = 0x100000000 + n end return f_uchexnumbers(n) end
38local octnumber = function(n) if n < 0 then n = 0x100000000 + n end return f_octnumber (n) end
39
40converters.lchexnumber = lchexnumber
41converters.uchexnumber = uchexnumber
42converters.lchexnumbers = lchexnumbers
43converters.uchexnumbers = uchexnumbers
44converters.octnumber = octnumber
45converters.nicenumber = nicenumber
46
47implement { name = "hexstringtonumber", actions = { tonumber, context }, arguments = { "integer", 16 } }
48implement { name = "octstringtonumber", actions = { tonumber, context }, arguments = { "integer", 8 } }
49
50implement { name = "rawcharacter", actions = function(n) context(utfchar(0x110000+n)) end, arguments = "integer" }
51
52implement { name = "lchexnumber", actions = { lchexnumber, context }, arguments = "integer" }
53implement { name = "uchexnumber", actions = { uchexnumber, context }, arguments = "integer" }
54implement { name = "lchexnumbers", actions = { lchexnumbers, context }, arguments = "integer" }
55implement { name = "uchexnumbers", actions = { uchexnumbers, context }, arguments = "integer" }
56implement { name = "octnumber", actions = { octnumber, context }, arguments = "integer" }
57
58
59
60implement { name = "sin", actions = { math.sin, nicenumber, context }, arguments = "number" }
61implement { name = "cos", actions = { math.cos, nicenumber, context }, arguments = "number" }
62implement { name = "tan", actions = { math.tan, nicenumber, context }, arguments = "number" }
63
64implement { name = "sind", actions = { math.sind, nicenumber, context }, arguments = "number" }
65implement { name = "cosd", actions = { math.cosd, nicenumber, context }, arguments = "number" }
66implement { name = "tand", actions = { math.tand, nicenumber, context }, arguments = "number" }
67
68
69
70function commands.format(fmt,...) context((gsub(fmt,"@","%%")),...) end
71
72implement {
73 name = "formatone",
74 public = true,
75 semiprotected = true,
76 arguments = "2 strings",
77 actions = function(f,s) context((gsub(f,"@","%%")),s) end,
78}
79
80local function tobits(b,w,d)
81 local t = { }
82 if not w then
83 w = 32
84 end
85 local m = d
86 local k = 0
87 for i=1,w do
88 k = k + 1 ; t[k] = (b & 0x1) == 1 and "1" or "0"
89 if m then
90 m = m - 1
91 if m == 0 then
92 k = k + 1 ; t[k] = " "
93 m = d
94 end
95 end
96 b = b >> 1
97 end
98 if t[k] == " " then
99 t[k] = nil
100 end
101 return concat(reverse(t))
102end
103
104implement {
105 name = "tobits",
106 public = true,
107 arguments = "3 integers",
108 actions = function(w,d,n) context(tobits(n,w,d)) end,
109}
110
111do
112
113 local round, log = math.round, math.log
114
115 implement {
116 name = "tobit",
117 public = true,
118 arguments = "integer",
119 actions = function(n)
120 context(n > 0 and (round(log(n,2)) + 1) or 0)
121 end,
122 }
123
124end
125
126implement {
127 name = "tohexa",
128 public = true,
129 arguments = "2 integers",
130 actions = function(w,n) context("0x%0"..w.."X",n) end,
131}
132
133interfaces.implement {
134 name = "showbits",
135 public = true,
136 arguments = { "string", "integer" },
137 actions = function(s,n)
138 local b = tex[s]
139 if type(b) == "table" then
140 local t = { }
141 for i=1,32 do
142 if (n & 0x1) == 1 then
143 local f = b[1<<(i-1)]
144 if f then
145 t[#t+1] = tostring(f)
146 end
147 end
148 n = n >> 1
149 end
150 if #t > 0 then
151 sort(t)
152 context("% t",t)
153 end
154 end
155 end,
156}
157 |