Debian Codegraph @

3rdparty/toluapp/src/bin/lua/code.lua

raw
1 -- tolua: code class
2 -- Written by Waldemar Celes
3 -- TeCGraf/PUC-Rio
4 -- Jul 1999
5 -- $Id: $
6
7 -- This code is free software; you can redistribute it and/or modify it.
8 -- The software provided hereunder is on an "as is" basis, and
9 -- the author has no obligation to provide maintenance, support, updates,
10 -- enhancements, or modifications.
11
12 -- global
13 code_n = 1
14
15 -- Code class
16 -- Represents Lua code to be compiled and included
17 -- in the initialization function.
18 -- The following fields are stored:
19 -- text = text code
20 classCode = {
21 text = '',
22 }
23 classCode.__index = classCode
24 setmetatable(classCode,classFeature)
25
26 -- register code
27 function classCode:register (pre)
28 pre = pre or ''
29 -- clean Lua code
30 local s = clean(self.text)
31 if not s then
32 --print(self.text)
33 error("parser error in embedded code")
34 end
35
36 -- get first line
37 local _, _, first_line=string.find(self.text, "^([^\n\r]*)")
38 if string.find(first_line, "^%s*%-%-") then
39 if string.find(first_line, "^%-%-##") then
40 first_line = string.gsub(first_line, "^%-%-##", "")
41 if flags['C'] then
42 s = string.gsub(s, "^%-%-##[^\n\r]*\n", "")
43 end
44 end
45 else
46 first_line = ""
47 end
48
49 -- pad to 16 bytes
50 local npad = 16 - (#s % 16)
51 local spad = ""
52 for i=1,npad do
53 spad = spad .. "-"
54 end
55 s = s..spad
56
57 -- convert to C
58 output('\n'..pre..'{ /* begin embedded lua code */\n')
59 output(pre..' int top = lua_gettop(tolua_S);')
60 output(pre..' static const unsigned char B[] = {\n ')
61 local t={n=0}
62
63 local b = gsub(s,'(.)',function (c)
64 local e = ''
65 t.n=t.n+1 if t.n==15 then t.n=0 e='\n'..pre..' ' end
66 return format('%3u,%s',strbyte(c),e)
67 end
68 )
69 output(b..strbyte(" "))
70 output('\n'..pre..' };\n')
71 if first_line and first_line ~= "" then
72 output(pre..' tolua_dobuffer(tolua_S,(char*)B,sizeof(B),"tolua embedded: '..first_line..'");')
73 else
74 output(pre..' tolua_dobuffer(tolua_S,(char*)B,sizeof(B),"tolua: embedded Lua code '..code_n..'");')
75 end
76 output(pre..' lua_settop(tolua_S, top);')
77 output(pre..'} /* end of embedded lua code */\n\n')
78 code_n = code_n +1
79 end
80
81
82 -- Print method
83 function classCode:print (ident,close)
84 print(ident.."Code{")
85 print(ident.." text = [["..self.text.."]],")
86 print(ident.."}"..close)
87 end
88
89
90 -- Internal constructor
91 function _Code (t)
92 setmetatable(t,classCode)
93 append(t)
94 return t
95 end
96
97 -- Constructor
98 -- Expects a string representing the code text
99 function Code (l)
100 return _Code {
101 text = l
102 }
103 end
104
105