summaryrefslogtreecommitdiff
path: root/src/lua/lua2c.lua
blob: 3f8d1716e66790e7d71997d81c6e9f6edaf34399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-- lua2c.lua
-- embed lua code into C source
-- celetecgraf.puc-rio.br
-- dez 2000

function embed (code)

 -- clean Lua code
 local s = clean(code)
 if not s then
  error("parser error in embedded code")  
 end
 
 -- convert to C
 output('\n { /* begin embedded lua code */\n')
 output('  static unsigned char B[] = {\n   ')
 local t={n=0}
 local b = gsub(s,'(.)',function (c) 
                         local e = '' 
                         %t.n=%t.n+1 if %t.n==15 then %t.n=0 e='\n   ' end 
                         return format('%3u,%s',strbyte(c),e) 
                        end
               )
 output(b..strbyte(" "))
 output('\n  };\n')
 output('  lua_dobuffer(tolua_S,(char*)B,sizeof(B),"'..fn..': embedded Lua code");')
 output(' } /* end of embedded lua code */\n\n')
end