summaryrefslogtreecommitdiff
path: root/src/lua/lua2c.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/lua2c.lua')
-rw-r--r--src/lua/lua2c.lua29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/lua/lua2c.lua b/src/lua/lua2c.lua
new file mode 100644
index 00000000..3f8d1716
--- /dev/null
+++ b/src/lua/lua2c.lua
@@ -0,0 +1,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
+