libtcc_test.lua
local ffi = require("ffi")
local tcc = require("tcc").load()
local function add(a, b)
return a + b
end
local my_program = [[
int fib(int n)
{
if (n <= 2)
return 1;
else
return fib(n-1) + fib(n-2);
}
int foo(int n)
{
printf("Hello World!\n");
printf("fib(%d) = %d\n", n, fib(n));
printf("add(%d, %d) = %d\n", n, 2 * n, add(n, 2 * n));
return 0;
}
]]
local function main()
local s = tcc.new()
if not s then
io.stderr:write("Could not create tcc state\n")
return 1
end
s:set_output_type(tcc.OUTPUT.MEMORY)
if not s:compile_string(my_program) then
return 1
end
s:add_symbol("add", ffi.cast("double (*)(double, double)", add))
if not s:relocate(tcc.RELOCATE.AUTO) then
return 1
end
local func = s:get_symbol("foo")
if not func then
return 1
end
ffi.cast("int (*)(int)", func)(32)
return 0
end
os.exit(main())