Create separate functions to generate titan and lua entry points
@@ -271,30 +271,7 @@ local function set_heap_slot(typ, dst, src, parent)
})
end
--- @param prog: (ast) Annotated AST for the whole module
--- @param modname: (string) Lua module name (for luaopen)
--- @return (string) C code for the whole module
-generate_program = function(prog, modname)
-
- -- Name all the function entry points
- for _, tl_node in ipairs(prog) do
- if tl_node._tag == ast.Toplevel.Func then
- tl_node._titan_entry_point =
- function_name(tl_node.name, "titan")
- tl_node._lua_entry_point =
- function_name(tl_node.name, "lua")
- end
- end
-
- -- Create toplevel function declarations
- local define_functions
- do
- local function_definitions = {}
- for _, tl_node in ipairs(prog) do
- if tl_node._tag == ast.Toplevel.Func then
- -- Titan entry point
- assert(#tl_node._type.rettypes <= 1)
-
+local function generate_titan_entry_point(tl_node)
local ret_ctype
if #tl_node._type.rettypes == 0 then
ret_ctype = "void"
@@ -337,8 +314,7 @@ generate_program = function(prog, modname)
table.insert(body, generate_stat(stat))
end
- table.insert(function_definitions,
- util.render([[
+ return util.render([[
static ${RET} ${NAME}(${PARAMS})
{
${BODY}
@@ -349,11 +325,10 @@ generate_program = function(prog, modname)
PARAMS = table.concat(titan_params, ", "),
BODY = table.concat(body, "\n"),
})
- )
+end
- -- Lua entry point
- assert(#tl_node._type.rettypes <= 1)
+local function generate_lua_entry_point(tl_node)
local args_decl = {}
local args = {}
table.insert(args, [[L]])
@@ -404,8 +379,7 @@ generate_program = function(prog, modname)
error("not implemented")
end
- table.insert(function_definitions,
- util.render([[
+ return util.render([[
static int ${LUA_ENTRY_POINT}(lua_State *L)
{
${ARGS_DECL}
@@ -422,7 +396,34 @@ generate_program = function(prog, modname)
SET_RET = set_ret,
NRET = c_integer(#tl_node._type.rettypes)
})
- )
+end
+
+-- @param prog: (ast) Annotated AST for the whole module
+-- @param modname: (string) Lua module name (for luaopen)
+-- @return (string) C code for the whole module
+generate_program = function(prog, modname)
+
+ -- Name all the function entry points
+ for _, tl_node in ipairs(prog) do
+ if tl_node._tag == ast.Toplevel.Func then
+ tl_node._titan_entry_point =
+ function_name(tl_node.name, "titan")
+ tl_node._lua_entry_point =
+ function_name(tl_node.name, "lua")
+ end
+ end
+
+ -- Create toplevel function declarations
+ local define_functions
+ do
+ local function_definitions = {}
+ for _, tl_node in ipairs(prog) do
+ if tl_node._tag == ast.Toplevel.Func then
+ assert(#tl_node._type.rettypes <= 1)
+ table.insert(function_definitions,
+ generate_titan_entry_point(tl_node))
+ table.insert(function_definitions,
+ generate_lua_entry_point(tl_node))
end
end
define_functions = table.concat(function_definitions, "\n")
