PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/gendoc.lua

http://github.com/zhuomingliang/luatcc
Lua | 307 lines | 245 code | 40 blank | 22 comment | 18 complexity | 543da32ef8ff59a6fe268423b84c479e MD5 | raw file
  1. local charset = ([[
  2. vi: encoding=utf-8
  3. ]]):sub(14, -2):upper()
  4. require 'markdown'
  5. local file_index = "index.html"
  6. local lastversion = "1.0.0"
  7. function print(...)
  8. local t = {...}
  9. for i=1,select('#', ...) do
  10. t[i] = tostring(t[i])
  11. end
  12. io.write(table.concat(t, '\t')..'\n')
  13. end
  14. local function manlink(name)
  15. return '<a href="http://www.lua.org/manual/5.1/manual.html#pdf-'..name..'"><code>'..name..'</code></a>'
  16. end
  17. local function manclink(name)
  18. return '<a href="http://www.lua.org/manual/5.1/manual.html#'..name..'"><code>'..name..'</code></a>'
  19. end
  20. function header()
  21. print[[
  22. <?xml version="1.0" encoding="UTF-8"?>
  23. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  24. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  25. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
  26. lang="en">
  27. <head>
  28. <title>Luatcc</title>
  29. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  30. <link rel="stylesheet" href="doc.css" type="text/css"/>
  31. </head>
  32. <body>
  33. ]]
  34. print([[
  35. <div class="chapter" id="header">
  36. <img width="128" height="128" alt="luatcc" src="luatcc.png"/>
  37. <p>Inline C for the Lua language</p>
  38. <p class="bar">
  39. <a href="]]..file_index..[[">home</a> &middot;
  40. <a href="]]..file_index..[[#download">download</a> &middot;
  41. <a href="]]..file_index..[[#installation">installation</a> &middot;
  42. <a href="]]..file_index..[[#manual">manual</a>
  43. </p>
  44. </div>
  45. ]])
  46. end
  47. function footer()
  48. print([[
  49. <div class="chapter" id="footer">
  50. <small>Last update: ]]..os.date"%Y-%m-%d %H:%M:%S %Z"..[[</small>
  51. </div>
  52. ]])
  53. print[[
  54. </body>
  55. </html>
  56. ]]
  57. end
  58. local chapterid = 0
  59. function chapter(title, text, sections, raw)
  60. chapterid = chapterid+1
  61. local text = text:gsub("%%chapterid%%", tostring(chapterid))
  62. if not raw then
  63. text = markdown(text)
  64. end
  65. if sections then
  66. for _,section in ipairs(sections) do
  67. section = section:gsub("%%chapterid%%", tostring(chapterid))
  68. text = text..[[
  69. <div class="section">
  70. ]]..markdown(section)..[[
  71. </div>]]
  72. end
  73. end
  74. print([[
  75. <div class="chapter">
  76. <h1>]]..tostring(chapterid).." - "..title..[[</h1>
  77. ]]..text..[[
  78. </div>
  79. ]])
  80. end
  81. function chapterp(title, text) chapter(title, "<p>"..text.."</p>") end
  82. ------------------------------------------------------------------------------
  83. io.output(file_index)
  84. header()
  85. chapter("About Luatcc", [[
  86. Luatcc is a Lua binding for libtcc, which is the core library of [TCC, the
  87. Tiny C Compiler](http://fabrice.bellard.free.fr/tcc/). Primary goal of this
  88. module is to be an illustration for the Gem *Interpreted C Modules* from the
  89. book [Lua Programming Gems](http://www.lua.org/gems/). As this module may be
  90. of interest independently from the book, I made it available here.
  91. Luatcc features a module loader that allows to load C modules directly from
  92. sources. To activate the loader you just have to load the tcc.loader module,
  93. which will install itself in Lua 5.1 package system.
  94. libtcc binding is not complete. Only the functions necessary to implement the
  95. C source module loader have been bound. Also it is very low level (it maps
  96. directly to libtcc C API). However I'm open to suggestions or requests to add
  97. more bindings or to implement a higher level interface in the future. Just ask
  98. :-)
  99. ## Support
  100. All support is done through the [Lua mailing list](http://www.lua.org/lua-l.html).
  101. If the traffic becomes too important a specialized mailing list will be
  102. created.
  103. Feel free to ask for further developments. I can't guarantee that I'll develop
  104. everything you ask, but as this module currently exists only for pedagogical
  105. purpose and is not actually used there is very little chance that I develop it
  106. anymore if you don't ask me to.
  107. ## Credits
  108. This module is written and maintained by [Jérôme Vuarand](mailto:jerome.vuarand@gmail.com).
  109. It is originally based on [lua-tcc](http://luaforge.net/projects/lua-tcc/)
  110. module by Javier Guerra and has been extended to support a bigger part of
  111. libtcc API and to work as a Lua module loader.
  112. This website and Luatcc downloadable packages are generously hosted by
  113. [Luaforge.net](http://luaforge.net/). Consider making a donation.
  114. Luatcc is available under a [MIT-style license](LICENSE.txt).
  115. ]])
  116. chapter('<a name="download">Download</a>', [[
  117. Luatcc sources are available in its Mercurial repository:
  118. hg clone http://piratery.net/hg/luatcc/
  119. Tarballs of the latest code can be downloaded directly from there: as
  120. [gz](http://piratery.net/hg/luatcc/archive/tip.tar.gz),
  121. [bz2](http://piratery.net/hg/luatcc/archive/tip.tar.bz2) or
  122. [zip](http://piratery.net/hg/luatcc/archive/tip.zip).
  123. Alternatively Luatcc is available on its [Luaforge project page](http://luaforge.net/frs/?group_id=255).
  124. ]])
  125. chapter('<a name="installation">Installation</a>', [[
  126. ## Build instructions
  127. To build Luatcc edit Makefile to configure the install directories and
  128. options, then run make in the top directory:
  129. $ vi Makefile
  130. $ make
  131. $ make test
  132. $ make install
  133. ## TCC
  134. To use Luatcc you need TCC. The recommended version is 0.9.25. Luatcc does
  135. work with 0.9.24 and 0.9.23, but you need to uncomment a line in the Makefile
  136. because there is no way to detect the 0.9.25 API change automatically. TCC is
  137. available at:
  138. - [http://bellard.org/tcc/](http://bellard.org/tcc/)
  139. ##Testing the module
  140. In the test subdirectory you will find some test programs to check that
  141. everything is working properly. They are run by the "make test" target,
  142. look at each testx.lua file to have an idea of what each test does.
  143. ]])
  144. local functions = { {
  145. name = "functions";
  146. title = "Module functions";
  147. doc = "These functions are global to the module.";
  148. functions = { {
  149. name = "tcc.new";
  150. parameters = {};
  151. doc = [[
  152. Returns a new TCC compiling context. This context can be used to compile C code and dynamically execute it.
  153. <pre>
  154. local context = tcc.new()
  155. </pre>
  156. ]];
  157. }
  158. } }, {
  159. name = "context_methods";
  160. title = "Context methods";
  161. doc = "These functions are the method of the compiler context returned by <code>tcc.new</code>.";
  162. functions = { {
  163. name = "context:add_include_path";
  164. parameters = {"path"};
  165. doc = [[Adds an include path to the context. <code>#include</code> directives in compiled sources will look for header files in paths provided by that function and some system-wide paths defined when compiling TCC.]];
  166. },{
  167. name = "context:compile";
  168. parameters = {"source [", "chunkname]"};
  169. doc = [=[Compiles a C source file inside the context. Optionnal <code>chunkname</code> argument can be a string that will be displayed in error messages (typically it will be the source file from which <code>source</code> is extracted, if any).
  170. <pre>
  171. context:compile[[
  172. #include &lt;lua.h&gt;
  173. int hello(lua_State* L)
  174. {
  175. lua_pushstring(L, "Hello World!");
  176. return 1;
  177. }
  178. ]]
  179. </pre>
  180. ]=];
  181. },{
  182. name = "context:add_library_path";
  183. parameters = {"path"};
  184. doc = [[Adds a library path to the context. <code>context:add_library</code> will look for libraries in the paths provided by that function and in some system-wide paths defined when compiling TCC.]];
  185. },{
  186. name = "context:add_library";
  187. parameters = {"libraryname"};
  188. doc = [[Adds a library to the context. This can be used to load static libraries or import libraries for dynamically loaded libraries. <code>libraryname</code> is the short name of the library (for example short name of liblua51.a is <code>"lua51"</code>).]];
  189. },{
  190. name = "context:relocate";
  191. parameters = {};
  192. doc = [[Performs the actual linking of the compiled sources and libraries present in the context. This function returns nothing, to access externally accessible symbols from the linked code use <code>context:get_symbol</code>.
  193. <pre>
  194. context:relocate()
  195. </pre>
  196. ]];
  197. },{
  198. name = "context:get_symbol";
  199. parameters = {"symbolname"};
  200. doc = [[This function looks for a symbol in the compiled code present in the context. The symbol is cast to a ]]..manclink('lua_CFunction')..[[, and returned as a <code>function</code>. You have to make sure that the symbol matches ]]..manclink('lua_CFunction')..[[, otherwise calling the returned function has unexpected behaviour (which is likely to be a crash).
  201. <pre>
  202. local hello = context:get_symbol("hello")
  203. print(hello()) -- Should output "Hello World!"
  204. </pre>
  205. ]];
  206. }
  207. } }, {
  208. name = "module_loader";
  209. title = "C source module loader";
  210. doc = [[
  211. <p>Luatcc features a new module loader. That loader will load C modules directly from source files. To activate the loader, you juste have to load the <code>tcc.loader</code> submodule:
  212. <pre>
  213. require("tcc.loader")
  214. </pre>
  215. Loading the <code>tcc.loader</code> submodule automatically installs the loader in Lua package system. Any further call to <code>require</code> will use the loader.</p>
  216. <p>The C source module loader looks for C source files according to the path in <code>package.tccpath</code>, which default value is <code>"./?.c"</code>. Just like for ]]..manlink('package.path')..[[ and ]]..manlink('package.cpath')..[[, the interrogation marks will be replaced with the module name, in which all dots have been replaced with a "directory separator" (such as "<code>/</code>" in Unix).</p>
  217. <p>Error reporting is done the same way than with the predefined module loaders. Also the C source module loader has the lowest priority, it is invoked last if no other loader can locate a module. To change loader priority you have to manually alter the <code>package.loaders</code> table.</p>
  218. ]];
  219. functions = {};
  220. } }
  221. local funcstr = ""
  222. for sectionid,section in ipairs(functions) do
  223. funcstr = funcstr..[[
  224. <div class="section">
  225. <h2><a name="]]..section.name..[[">%chapterid%.]]..tostring(sectionid).." - "..section.title..[[</a></h2>
  226. ]]..section.doc..[[
  227. ]]
  228. for _,func in ipairs(section.functions) do
  229. funcstr = funcstr..[[
  230. <div class="function">
  231. <h3><a name="]]..func.name..[["><code>]]..func.name..' ('..table.concat(func.parameters, ", ")..[[)</code></a></h3>
  232. <p>]]..func.doc..[[</p>
  233. </div>
  234. ]]
  235. end
  236. funcstr = funcstr..[[
  237. </div>
  238. ]]
  239. end
  240. chapter('<a name="manual">Manual</a>', [[
  241. <p>Here you can find a list of the functions present in the module and how to use them. Luatcc main module follows Lua 5.1 package system, see the <a href="http://www.lua.org/manual/5.1/">Lua 5.1 manual</a> for further explanations.</p>
  242. ]]..funcstr, nil, true)
  243. footer()
  244. --[[
  245. Copyright (c) 2009-2010 Jérôme Vuarand
  246. Permission is hereby granted, free of charge, to any person obtaining a copy
  247. of this software and associated documentation files (the "Software"), to deal
  248. in the Software without restriction, including without limitation the rights
  249. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  250. copies of the Software, and to permit persons to whom the Software is
  251. furnished to do so, subject to the following conditions:
  252. The above copyright notice and this permission notice shall be included in
  253. all copies or substantial portions of the Software.
  254. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  255. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  256. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  257. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  258. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  259. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  260. THE SOFTWARE.
  261. ]]