PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ldislin/processh.lua

https://bitbucket.org/kaendfinger/aur-mirror
Lua | 650 lines | 452 code | 42 blank | 156 comment | 91 complexity | 5ac6cc1a3e987503e78cbc38a3484ff6 MD5 | raw file
Possible License(s): LGPL-2.0, Unlicense, AGPL-1.0, BitTorrent-1.0, EPL-1.0, GPL-3.0, BSD-3-Clause, GPL-2.0, MIT, CC-BY-SA-3.0, BSD-2-Clause, MPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, JSON, AGPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0, LGPL-2.1, ISC, CC-BY-3.0, WTFPL, 0BSD, CC0-1.0, LGPL-3.0, Cube, Apache-2.0
  1. --[[ processh.lua
  2. * Lua pre-processor for DISLIN header file
  3. * created October 17, 2006 by e
  4. *
  5. * Copyright (c) 2006 Doug Currie, Londonderry, NH
  6. * All rights reserved.
  7. *
  8. Permission is hereby granted, free of charge, to any person obtaining a
  9. copy of this software and associated documentation files (the
  10. "Software"), to deal in the Software without restriction, including
  11. without limitation the rights to use, copy, modify, merge, publish,
  12. distribute, and/or sell copies of the Software, and to permit persons
  13. to whom the Software is furnished to do so, provided that the above
  14. copyright notice(s) and this permission notice appear in all copies of
  15. the Software and that both the above copyright notice(s) and this
  16. permission notice appear in supporting documentation.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
  20. OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  21. HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  22. INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  23. FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  24. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  25. WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26. ************************************************************************
  27. ]]--
  28. --[[
  29. TODO
  30. finish with quesimpl functions
  31. document all the handimpl functions
  32. README
  33. Most Lua functions match the signature of the DISLIN function. There are a
  34. few exceptions, listed here...
  35. LEGINI - since Lua strings are not mutable, LEGINI omits the first (string)
  36. argument, and returns a Lua userdata l_legend_t. This userdata is used as
  37. the first (char *cbuf) argument to the legend functions. So:
  38. cbuf = legini (nlin, nmaxln)
  39. legend (cbuf, ncor)
  40. leglin (cbuf, cstr, ilin);
  41. piegrf (cbuf, nlin, xray, nseg)
  42. x = nxlegn (cbuf)
  43. y = nyegn (cbuf)
  44. ITMCAT - is not implemented since it mutates a string; the Lua .. operator
  45. provides the same capability, so there is no need for itmcat.
  46. Other functions that mutate strings, or have Lua implementations already,
  47. are: fcha, intcha, and upstr.
  48. Binary I/O - DISLIN provides several binary I/O functions for FORTRAN. The
  49. Lua file functions work with full 8-bit data, so these functions are not
  50. needed and not implemented: closfl, openfl, posifl, readfl, skipfl, tellfl,
  51. writfl.
  52. The following functions were in earlier versions of DISLIN, but are now
  53. obsolete: dattim, digits (use LABDIG), lsechk, moment, scale (use AXSSCL),
  54. swgcb, swgmod, winini.
  55. UNIT is an old DISLIN routine for suppressing error messages. It should be
  56. replaced by the newer routines ERRMOD, ERRDEV and ERRFIL. Nevertheless, we
  57. implement UNIT(0) to accommodate the old idiom. GETUNI is not implemented.
  58. --]]
  59. local fn = arg[2] or "/usr/lib/dislin/dislin.h"
  60. local fo = arg[3] or "ldislin.txt.c"
  61. local fd = arg[4] or "ldislin.txt.pod"
  62. local fe = arg[5] or "ldislin.tx2.pod"
  63. local li32 = true -- longs and ints are both 32 bits in the Lua implementation
  64. local gsub = string.gsub
  65. local fmt = string.format
  66. --[[----------------------------------
  67. function orderedtable(t)
  68. local currentIndex = 1
  69. local metaTable = {}
  70. function metaTable:__newindex(key,value)
  71. rawset(self, key, value)
  72. rawset(self, currentIndex, key)
  73. currentIndex = currentIndex + 1
  74. end
  75. return setmetatable(t or {}, metaTable)
  76. end
  77. function ordered(t)
  78. local currentIndex = 0
  79. local function iter(t)
  80. currentIndex = currentIndex + 1
  81. local key = t[currentIndex]
  82. if key then return key, t[key] end
  83. end
  84. return iter, t
  85. end
  86. --]]----------------------------------
  87. local o = io.open (fo,"w")
  88. local rtncmalloc = {}
  89. -- these return (char *) malloc()'d by DISLIN (per Helmut), free()'d by ldislin
  90. rtncmalloc["itmstr"] = true
  91. rtncmalloc["dwgfil"] = true
  92. rtncmalloc["dwgtxt"] = true
  93. local hand = {} -- hand implemented
  94. local function handimpl (n,g) hand[n]=g end
  95. handimpl("bars", "v_EEEi") -- has in/out rays - sometimes!
  96. handimpl("bezier","v_DDiEEi") -- has out rays
  97. handimpl("colray","v_DNi") -- has out iray
  98. handimpl("conpts","v_DiDiDdEEiNiJ") -- has out irays
  99. handimpl("disini","v_v") -- must keep track of spline size
  100. handimpl("getlab","v_CCC") -- has out crays
  101. handimpl("getmat","v_DDDiEiidNE") -- has out rays
  102. handimpl("gwgfil","v_iC") -- has out cray[256]
  103. handimpl("gwgtxt","v_iC") -- has out cray[256]
  104. handimpl("histog","v_DiEEJ") -- has out irays
  105. handimpl("legend","v_Ci") -- needs legini
  106. handimpl("legini","v_Cii") -- makes userdata
  107. handimpl("leglin","v_CKi") -- needs legini
  108. handimpl("piegrf","v_CiDi") -- needs legini
  109. handimpl("nxlegn","i_C") -- needs legini
  110. handimpl("nylegn","i_C") -- needs legini
  111. handimpl("pdfbuf","i_Ci") -- big out buffer
  112. handimpl("rbfpng","i_Ci") -- big out buffer
  113. handimpl("setcbk","v_ZK") -- callback for user defined projection function
  114. handimpl("sortr1","v_EiK") -- has in/out ray
  115. handimpl("sortr2","v_EEiK") -- has in/out rays
  116. handimpl("spline","v_DDiEEJ") -- has out rays; output array size coordinated with SPLMOD and DISINI
  117. handimpl("spline","v_DDiEEJ") -- has out rays; output array size coordinated with SPLMOD and DISINI
  118. handimpl("splmod","v_ii") -- must keep track of spline size
  119. handimpl("surfun","v_Zidid") -- callback
  120. handimpl("surfcp","v_Zdddddd")-- callback
  121. handimpl("swgcbk","v_iZ") -- widget callback
  122. handimpl("trfco1","v_EiKK") -- has in/out ray
  123. handimpl("trfco2","v_EEiKK") -- has in/out rays
  124. handimpl("trfco3","v_EEEiKK") -- has in/out rays
  125. handimpl("trfmat","v_DiiEii") -- has out mat
  126. handimpl("trfrel","v_EEi") -- has in/out rays
  127. handimpl("triang","i_DDiNNNi")-- has out rays -- userdata candidate TRIANG
  128. handimpl("tripts","v_DDDiIIIidEEiNiJ") -- yow
  129. handimpl("unit","v_V") -- suppress error messages; legacy
  130. -- "confll" -- const rays, uses TRIANG
  131. -- "contri" -- const rays, uses TRIANG
  132. -- "crvtri" -- const rays, uses TRIANG
  133. -- "surtri" -- const rays, uses TRIANG
  134. local cast = {} -- hand checked type (e.g., scalar output type R or J)
  135. local function castimpl (n,g) cast[n]=g end
  136. castimpl("abs3pt","v_dddRR")
  137. castimpl("circ3p","v_ddddddRRR")
  138. castimpl("csrpos","i_JJ")
  139. castimpl("csrpt1","v_JJ")
  140. castimpl("getclp","v_JJJJ")
  141. castimpl("getdig","v_JJJ")
  142. castimpl("getgrf","v_RRRRK")
  143. castimpl("getind","v_iRRR")
  144. castimpl("getlen","v_JJJ")
  145. castimpl("getor","v_JJ")
  146. castimpl("getpag","v_JJ")
  147. castimpl("getpos","v_JJ")
  148. castimpl("getran","v_JJ")
  149. castimpl("getres","v_JJ")
  150. castimpl("getrgb","v_RRR")
  151. castimpl("getscl","v_JJJ")
  152. castimpl("getscr","v_JJ")
  153. castimpl("getsp1","v_JJJ")
  154. castimpl("getsp2","v_JJJ")
  155. castimpl("getsym","v_JJ")
  156. castimpl("gettcl","v_JJ")
  157. castimpl("gettic","v_JJJ")
  158. castimpl("getvk" ,"v_JJJ")
  159. castimpl("getwin","v_JJJJ")
  160. castimpl("gmxalf","i_KAA")
  161. castimpl("hsvrgb","v_dddRRR")
  162. castimpl("pos2pt","v_ddRR")
  163. castimpl("pos3pt","v_dddRRR")
  164. castimpl("rel3pt","v_dddRR")
  165. castimpl("rgbhsv","v_dddRRR")
  166. castimpl("rpixel","v_iiJ")
  167. castimpl("trfdat","v_iJJJ") -- (date calc)
  168. local noti = {} -- not implemented
  169. local function notbimpl (n) noti[n]=true end
  170. notbimpl "closfl" -- FORTRAN
  171. notbimpl "dattim" -- Obsolete per Helmut
  172. notbimpl "digits" -- Obsolete per Helmut -- use LABDIG
  173. notbimpl "fcha" -- has out cray (dtoa) -- use Lua
  174. notbimpl "getuni" -- returns (FILE *)
  175. notbimpl "intcha" -- has out cray (itoa) -- use Lua
  176. notbimpl "itmcat" -- side affects 1st arg (per Helmut) -- use Lua
  177. notbimpl "lsechk" -- Obsolete per Helmut
  178. notbimpl "moment" -- Obsolete per Helmut
  179. notbimpl "openfl" -- FORTRAN
  180. notbimpl "posifl" -- FORTRAN
  181. notbimpl "readfl" -- FORTRAN; and has out cray
  182. notbimpl "scale" -- Obsolete per Helmut -- use AXSSCL
  183. notbimpl "skipfl" -- FORTRAN
  184. notbimpl "swapi2" -- has in/out sray; rarely used per Helmut (not in Python wrapper)
  185. notbimpl "swapi4" -- has in/out iray; rarely used per Helmut (not in Python wrapper)
  186. notbimpl "swgcb" -- Obsolete per Helmut
  187. notbimpl "swgmod" -- Obsolete per Helmut
  188. notbimpl "tellfl" -- FORTRAN
  189. notbimpl "upstr" -- has in/out cray -- use Lua
  190. notbimpl "winini" -- Obsolete per Helmut
  191. notbimpl "writfl" -- FORTRAN
  192. local ques = {} -- not implemented
  193. local function quesimpl (n) ques[n]=true end
  194. quesimpl "csrmov" -- has out irays
  195. quesimpl "csrpos" -- has optional args
  196. quesimpl "csrpts" -- has out irays
  197. quesimpl "getind" -- has optional args
  198. quesimpl "rpixls" -- has out iray or cray?
  199. quesimpl "rpxrow" -- has out iray or cray?
  200. --[[
  201. =head2 C<dislin.csrpos>
  202. dislin.csrpos ()
  203. i_JJ -- this needs custom implementation for optional arguments!
  204. =head2 C<dislin.getind>
  205. dislin.getind (i)
  206. Returns the corresponding RGB coordinates stored in the current color table
  207. v_iRRR -- this needs custom implementation for optional arguments!
  208. ]]
  209. local ty =
  210. {
  211. ["const float *"] = "F ",
  212. ["float *"] = "G ",
  213. ["const double *"] = "D ",
  214. ["double *"] = "E ",
  215. ["const unsigned char *"] = "K ", -- only 3 funs; all take raw pixels
  216. ["unsigned char *"] = "U ",
  217. ["const char *"] = "K ",
  218. ["char *"] = "C ",
  219. ["const int *"] = "I ",
  220. ["int *"] = "N ",
  221. ["const long *"] = "L ", -- li32 and "I " or "L ", -- results in warning
  222. ["long *"] = "M ", -- li32 and "I " or "L ", -- results in warning
  223. ["short *"] = "S ",
  224. ["void *"] = "V ",
  225. ["double"] = "d ",
  226. ["float"] = "f ",
  227. ["long"] = li32 and "i " or "l ",
  228. ["int"] = "i ",
  229. ["short"] = "s ",
  230. ["void"] = "v "
  231. }
  232. --[[-- derived/pseudo types
  233. K -- constant char *
  234. Z -- function pointer
  235. J -- out (scalar result) int *
  236. A -- out (scalar result) char *
  237. R -- out (scalar result) double *
  238. --]]
  239. local sigcompat =
  240. {
  241. ["Z"] = {},
  242. -- double
  243. ["R"] = {["D"]=true,["E"]=true},
  244. ["D"] = {["R"]=true,["E"]=true},
  245. ["E"] = {["R"]=true,["D"]=true},
  246. -- long
  247. ["L"] = { ["M"]=true,["I"]=li32,["J"]=li32,["N"]=li32},
  248. ["M"] = {["L"]=true, ["I"]=li32,["J"]=li32,["N"]=li32},
  249. -- int
  250. ["I"] = {["J"]=true, ["N"]=true,["L"]=li32,["M"]=li32},
  251. ["N"] = {["J"]=true,["I"]=true, ["L"]=li32,["M"]=li32},
  252. ["J"] = { ["I"]=true,["N"]=true,["L"]=li32,["M"]=li32},
  253. --
  254. ["S"] = {},
  255. -- ["U"] = {},
  256. ["K"] = {["C"]=true},
  257. ["A"] = {["C"]=true},
  258. ["C"] = {["K"]=true,["A"]=true},
  259. ["V"] = {},
  260. ["d"] = {},
  261. ["f"] = {},
  262. ["l"] = {["i"]=li32},
  263. ["i"] = {["l"]=li32},
  264. ["s"] = {},
  265. ["v"] = {},
  266. }
  267. local function sigmatch (g1,g2)
  268. local n = string.len(g1)
  269. if n ~= string.len(g2) then return false end
  270. for i = 1,n do
  271. local c1 = string.sub (g1,i,i)
  272. local c2 = string.sub (g2,i,i)
  273. if c1 ~= c2 and not (sigcompat[c1])[c2] then
  274. return false
  275. end
  276. end
  277. return true
  278. end
  279. local funs = {}
  280. local sigs = {}
  281. local nope = {}
  282. local punt = {}
  283. local cust = {}
  284. local docs = {}
  285. local docn = {}
  286. local function line (str)
  287. --for k,v in pairs(ty) do s = gsub (s, k, v) end -- must be ordered
  288. --
  289. local s = str
  290. if s == "" then return end
  291. s = gsub (s, "([%w_]* [%w_]* [%w_]* %*)", ty)
  292. s = gsub (s, "([%w_]* [%w_]* %*)", ty)
  293. s = gsub (s, "([%w_]* %*)", ty)
  294. s = gsub (s, "([%w_]*)", ty)
  295. -- function arg
  296. -- (double (*zfun)(double x, double y, int i
  297. -- (void (*callbck) (double *x, double *y)
  298. s = gsub (s, "[%w_]*%s*%(%*[%w_]*%)%s*%([%w%s_,]*%)", "Z z")
  299. local ds = s
  300. --
  301. for i = 1,15 do
  302. s = gsub (s, "%(%s*([ZDELMINSUKCVAdflisv])%s*[%w_]*[,]?", "%1(")
  303. end
  304. s = gsub (s, "%(%)%;%s*", "")
  305. if string.find (s,"%(") then
  306. print ("what is "..s)
  307. table.insert (punt, str)
  308. return
  309. end
  310. --print (s)
  311. s = gsub (s, "^%s*([%w_]*)%s*([%w_]*)%s*([%w_]*)%s*$", --([.]*) --%s*
  312. function (r,n,a)
  313. if n == nil or n == "" or a == nil or a == "" then
  314. print ("#### "..s.." # "..r.." # "..n.." # "..a)
  315. else
  316. --if ischarsafe[n] then a = gsub (a,"C","K") end
  317. if r == "C" then if rtncmalloc[n] then r = "C" else r = "K" end end
  318. g = r.."_"..a
  319. if hand[n] then
  320. if not sigmatch(g,hand[n]) then
  321. print ("WARNING, signature mismatch:", n, g, hand[n])
  322. end
  323. funs[n] = "CUSTOM"
  324. table.insert (cust, str)
  325. docn[n] = ds
  326. elseif cast[n] then
  327. if sigmatch(g,cast[n]) then
  328. funs[n] = cast[n]
  329. sigs[cast[n]] = true
  330. else
  331. print ("mismatch:", n, g, cast[n])
  332. table.insert (punt, str)
  333. end
  334. elseif noti[n] or ques[n] then
  335. -- funs[n] = "notimp"
  336. table.insert (nope, str)
  337. elseif string.find (a, "[GEUCNMS]") then
  338. print ("non-const array in: "..str)
  339. table.insert (punt, str)
  340. elseif string.find (a, "Z") then
  341. print ("callback in: "..str)
  342. table.insert (punt, str)
  343. else
  344. funs[n] = g
  345. sigs[g] = true
  346. docs[n] = ds -- doc string
  347. end
  348. end
  349. end)
  350. end
  351. -- for s in io.lines(fn) do line (s) end
  352. local fa = io.open(fn):read("*a")
  353. -- remove the conditional code; all is C++ related
  354. fa = gsub (fa, "#ifdef", "<")
  355. fa = gsub (fa, "#endif", ">")
  356. fa = gsub (fa, "%b<>", "")
  357. -- remove comments
  358. fa = gsub (fa, "/%*", "<")
  359. fa = gsub (fa, "%*/", ">")
  360. fa = gsub (fa, "%b<>", "")
  361. -- -- debug
  362. -- -- od = io.open("pldebug.txt","w")
  363. -- remove spaces at starts of lines
  364. fa = gsub (fa, "[\n\r]%s*", "\n")
  365. -- -- od:write(fa)
  366. -- -- od:write("\n#############\n")
  367. -- remove newlines
  368. fa = gsub (fa, "[\n\r]", "")
  369. -- process what's left
  370. for s in string.gmatch (fa, "(%w[%s%w%(%),%*]*;)") do line (s) end
  371. -- -- od:close()
  372. -----------------------------------------------
  373. function __genOrderedIndex( t )
  374. local orderedIndex = {}
  375. for key,_ in pairs(t) do
  376. table.insert( orderedIndex, key )
  377. end
  378. table.sort( orderedIndex )
  379. return orderedIndex
  380. end
  381. function orderednext(t, state)
  382. if state == nil then
  383. -- the first time, generate the index
  384. t.__orderedIndex = __genOrderedIndex( t )
  385. key = t.__orderedIndex[1]
  386. return key, t[key]
  387. end
  388. -- fetch the next value
  389. key = nil
  390. for i = 1,#(t.__orderedIndex) do
  391. if t.__orderedIndex[i] == state then
  392. key = t.__orderedIndex[i+1]
  393. end
  394. end
  395. if key then
  396. return key, t[key]
  397. end
  398. -- no more value to return, cleanup
  399. t.__orderedIndex = nil
  400. return
  401. end
  402. function orderedpairs (t)
  403. -- equivalent to pairs(), but in order
  404. return orderednext, t, nil
  405. end
  406. ---------------------------------------------------------
  407. -- for k,_ in pairs(sigs) do print (k) end
  408. for k,_ in orderedpairs(sigs) do
  409. local qresults = 0
  410. local results = {}
  411. local as = ""
  412. o:write (fmt ("#define %s(nm) \\\n", k))
  413. o:write (fmt ("static int l_ ## nm (lua_State *L) { \\\n"))
  414. if string.sub (k,3,-1) == "v" then
  415. -- no args, nothing to do
  416. else for i = 3,#k do
  417. local a = "a"..(i-2)
  418. local c = string.sub (k,i,i) -- DLISUCVdflisv
  419. if c == "D" then
  420. o:write (fmt (" double *%s = magic_doublestar_function (L, %d); \\\n", a, i-2))
  421. elseif c == "L" then
  422. o:write (fmt (" long *%s = magic_longstar_function (L, %d); \\\n", a, i-2))
  423. elseif c == "I" then
  424. o:write (fmt (" int *%s = magic_intstar_function (L, %d); \\\n", a, i-2))
  425. elseif c == "S" then
  426. o:write (fmt (" short *%s = magic_shortstar_function (L, %d); \\\n", a, i-2))
  427. elseif c == "U" then
  428. o:write (fmt (" const unsigned char *%s = luaL_checkstring(L, %d); \\\n", a, i-2))
  429. elseif c == "C" or c == "K" then
  430. o:write (fmt (" const char *%s = luaL_checkstring(L, %d); \\\n", a, i-2))
  431. if c == "K" then a = "(char *)"..a end
  432. elseif c == "V" then
  433. o:write (fmt (" void *%s = magic_voidstar_function (L, %d); \\\n", a, i-2))
  434. elseif c == "d" or c == "f" then
  435. o:write (fmt (" lua_Number %s = luaL_checknumber (L, %d); \\\n", a, i-2))
  436. elseif c == "l" or c == "i" or c == "s" then
  437. o:write (fmt (" lua_Integer %s = luaL_checkinteger (L, %d); \\\n", a, i-2))
  438. elseif c == "J" then
  439. o:write (fmt (" lua_Integer %s; \\\n", a))
  440. results[a] = "J"
  441. a = "&"..a
  442. qresults = qresults + 1
  443. elseif c == "R" then
  444. o:write (fmt (" double %s; \\\n", a))
  445. results[a] = "R"
  446. a = "&"..a
  447. qresults = qresults + 1
  448. elseif c == "A" then
  449. o:write (fmt (" char %s[4] = {0,0,0,0}; \\\n", a))
  450. results[a] = "A"
  451. qresults = qresults + 1
  452. elseif c == "v" then
  453. error (fmt("sig format has void arg"))
  454. else
  455. error (fmt("unknown sig format char %s",c))
  456. end
  457. as = as..","..a
  458. end end -- else for
  459. as = string.sub (as,2)
  460. c = string.sub (k,1,1)
  461. if c == "D" then
  462. o:write (fmt (" double *r = nm (%s); \\\n", as))
  463. o:write (fmt (" magic_push_doublestar(L, r); \\\n", as))
  464. elseif c == "L" then
  465. o:write (fmt (" long *r = nm (%s); \\\n", as))
  466. o:write (fmt (" magic_push_longstar(L, r); \\\n", as))
  467. elseif c == "I" then
  468. o:write (fmt (" int *r = nm (%s); \\\n", as))
  469. o:write (fmt (" magic_push_intstar(L, r); \\\n", as))
  470. elseif c == "S" then
  471. o:write (fmt (" short *r = nm (%s); \\\n", as))
  472. o:write (fmt (" magic_push_shortstar(L, r); \\\n", as))
  473. elseif c == "K" or c == "C" or c == "U" then
  474. o:write (fmt (" char *r = nm (%s); \\\n", as))
  475. o:write (fmt (" lua_pushstring (L, r); \\\n", as))
  476. if c ~= "K" then
  477. o:write (fmt (" free (r); \\\n"))
  478. end
  479. elseif c == "V" then
  480. o:write (fmt (" void *r = nm (%s); \\\n", as))
  481. o:write (fmt (" magic_push_voidstar(L, r); \\\n", as))
  482. elseif c == "d" then
  483. o:write (fmt (" double r = nm (%s); \\\n", as))
  484. o:write (fmt (" lua_pushnumber (L, r); \\\n", as))
  485. elseif c == "f" then
  486. o:write (fmt (" float r = nm (%s); \\\n", as))
  487. o:write (fmt (" lua_pushnumber (L, r); \\\n", as))
  488. elseif c == "l" then
  489. o:write (fmt (" long r = nm (%s); \\\n", as))
  490. o:write (fmt (" lua_pushnumber (L, r); \\\n", as))
  491. elseif c == "i" or c == "s" then
  492. o:write (fmt (" int r = nm (%s); \\\n", as))
  493. o:write (fmt (" lua_pushinteger (L, r); \\\n", as))
  494. elseif c == "v" then
  495. o:write (fmt (" nm (%s); \\\n", as))
  496. -- nothing to push
  497. else
  498. error (fmt("unknown sig format char %s",c))
  499. end
  500. for r,c in orderedpairs(results) do
  501. if c == "J" then
  502. o:write (fmt (" lua_pushinteger (L, %s); \\\n", r))
  503. elseif c == "R" then
  504. o:write (fmt (" lua_pushnumber (L, %s); \\\n", r))
  505. elseif c == "A" then
  506. o:write (fmt (" lua_pushstring (L, %s); \\\n", r))
  507. else
  508. error (fmt("unknown res format char %s",c))
  509. end
  510. end
  511. if c ~= "v" then qresults = qresults + 1 end
  512. o:write (fmt (" return %d; \\\n", qresults))
  513. o:write (fmt ("}\n"))
  514. end
  515. o:write "\n#define CUSTOM(nm) static int l_ ## nm (lua_State *L); /* custom impl */\n\n"
  516. for n,g in orderedpairs(funs) do
  517. o:write (fmt ("%s(%s)\n", g, n))
  518. end
  519. o:write "\nstatic const luaL_reg ldislin_lib[] =\n{\n"
  520. local function ss (n)
  521. if 0 <= n and n <= 8 then
  522. return (({""," "," "," "," "," "," "," "," "})[n+1])
  523. else
  524. return " "
  525. end
  526. end
  527. for n,_ in orderedpairs(funs) do
  528. o:write (fmt (" {\"%s\",%s l_%s},\n", n, ss(7 - #n), n))
  529. end
  530. o:write " {NULL, NULL}\n};\n"
  531. o:write "\n"
  532. if #nope ~= 0 then
  533. o:write (fmt ("/* **** %d excluded functions ****\n", #nope))
  534. for _,v in ipairs(nope) do
  535. o:write (fmt ("** **** %s\n", v))
  536. end
  537. o:write (fmt ("** **** ******************* ****\n*/\n\n"))
  538. end
  539. if #punt ~= 0 then
  540. o:write (fmt ("/* **** %d punted functions ****\n", #punt))
  541. for _,v in ipairs(punt) do
  542. o:write (fmt ("** **** %s\n", v))
  543. end
  544. o:write (fmt ("** **** ******************* ****\n*/\n\n"))
  545. end
  546. if #cust ~= 0 then
  547. o:write (fmt ("/* **** %d custom functions ****\n", #cust))
  548. for _,v in ipairs(cust) do
  549. o:write (fmt ("** **** %s\n", v))
  550. end
  551. o:write (fmt ("** **** ******************* ****\n*/\n\n"))
  552. end
  553. local function doco (fn, tbl, ps)
  554. local q = 0
  555. local t2tn = {["d"]="number", ["i"]="integer", ["s"]="integer", ["C"]="string"}
  556. local o = io.open (fn,"w")
  557. for n,s in orderedpairs(tbl) do
  558. local t
  559. s = gsub (s,";","")
  560. s = gsub (s,"([ZDELMINSUKCVAdflisv])%s*([%w_]*)%s*%(",
  561. function (ty,nm)
  562. if n ~= nm then print ("OOPS ", n, nm) end
  563. t = ty
  564. return fmt("dislin.%s (", n)
  565. end)
  566. s = gsub (s, "%(%s*[ZDELMINSUKCVAdflisv]%s*", "(")
  567. s = gsub (s, "%,%s*[ZDELMINSUKCVAdflisv]%s*", ", ")
  568. o:write (fmt("=head2 C<dislin.%s>\n\n %s\n\n",n,s))
  569. if t ~= "v" then
  570. tn = t2tn[t]
  571. if tn == nil then print ("need", t); tn = t end
  572. o:write (fmt("Returns %s C<%s>.\n\n",tn,n))
  573. end
  574. q = q + 1
  575. end
  576. print ("making docs for", q, ps)
  577. o:close()
  578. end
  579. if fd ~= nil then
  580. doco (fd, docs, "simple functions")
  581. end
  582. if fe ~= nil then
  583. doco (fe, docn, "custom functions")
  584. end
  585. local function sz (t) local n = 0; for _,_ in pairs(t) do n = n + 1 end return n end
  586. print ("Nope",#nope,"Punt",#punt,"Cust",#cust,"Cast",sz(cast),"Simp",sz(docs),"Funs",sz(funs))