feat: initial support to generate global_env_def

This commit is contained in:
Aire-One 2023-10-08 16:59:07 +02:00
parent 086ab2a474
commit 989adaa62c
7 changed files with 51 additions and 8 deletions

View File

@ -1,3 +1,4 @@
return {
source_dir = ".",
global_env_def = "global_env_def",
}

View File

@ -1,18 +1,20 @@
local type Node = require("awesomewmdtl.types.Node")
local basic_nodes <total>: { Node.Token : function(name: string, ...: any): Node } = {
module = function(name: string, module_path: string): Node
module = function(name: string, module_path: string, global: boolean): Node
return {
token = "module",
global = global,
name = name,
module_path = module_path,
dependencies = {},
children = {},
}
end,
record = function(name: string): Node
record = function(name: string, global: boolean): Node
return {
token = "record",
global = global,
name = name,
children = {},
}

View File

@ -18,13 +18,18 @@ local function push_global_nodes(dag: Dag, nodes: { Node })
utils.spread(dag.global_nodes, nodes)
end
local function iter_modules(dag: Dag): function(): string, Node
local function iter_modules(dag: Dag): (function(): string, Node)
return pairs(dag.modules)
end
local function iter_global_nodes(dag: Dag): (function(): integer, Node)
return pairs(dag.global_nodes)
end
return {
new = new,
push_module = push_module,
push_global_nodes = push_global_nodes,
iter_modules = iter_modules,
iter_global_nodes = iter_global_nodes,
}

View File

@ -138,8 +138,24 @@ for _,root in dag.iter_modules(module_dag) do
end)
end
-- Build the global module from dag.global_nodes
--- TODO : todo
-- Build the global_env_def.d.tl module from module_dag.global_nodes
local global_nodes_by_record_kind <const>: { string : { Node } } = {}
for _, node in dag.iter_global_nodes(module_dag) do
local record_kind = node.name:gmatch("[^%.]*")() -- get the most left part of the lua name path representation
if not global_nodes_by_record_kind[record_kind] then
global_nodes_by_record_kind[record_kind] = {}
end
table.insert(global_nodes_by_record_kind[record_kind], node)
end
local global_module_ast <const> = ast.create_node("module", "global_env_def", "global_env_def", true)
for record_kind, nodes in pairs(global_nodes_by_record_kind) do
local record_kind_node = ast.create_node("record", record_kind, true)
for _, node in ipairs(nodes) do
node.name = node.name:gsub(record_kind .. "%.", "")
table.insert(record_kind_node.children, node)
end
table.insert(global_module_ast.children, record_kind_node)
end
--- TODO : this is fun, but we need to do something with it
-- Write the DAG to a file
@ -159,5 +175,12 @@ for module_path, root in dag.iter_modules(module_dag) do
)
end
-- Write global types definition to file
filesystem.file_writer.write(
printer.teal_type_definition.printer(global_module_ast),
property.out_directory .. "/global_env_def.d.tl"
)
do_module_init_definition(module_infos)
log:info("Module init files generated")

View File

@ -50,6 +50,11 @@ local print_children: function(node: Node): string
local node_printer <total>: { Node.Token : Node_Printer_Function } = {
["module"] = {
before_node = function(node: Node, indent_level: integer): string, integer
if node.global then
return render_code(
"-- This file was auto-generated.\n",
indent_level), indent_level
end
return render_code(
string.format(
"-- This file was auto-generated.\n%s\nlocal record %s",
@ -61,6 +66,9 @@ local node_printer <total>: { Node.Token : Node_Printer_Function } = {
return render_code(print_children(node), indent_level), indent_level
end,
after_node = function(node: Node, indent_level: integer): string, integer
if node.global then
return "", indent_level
end
return render_code("end", indent_level - 1) ..
"\n" ..
render_code(
@ -73,7 +81,8 @@ local node_printer <total>: { Node.Token : Node_Printer_Function } = {
before_node = function(node: Node, indent_level: integer): string, integer
return render_code(
string.format(
"record %s",
"%srecord %s",
node.global and "\nglobal " or "",
node.name),
indent_level), indent_level + 1
end,

View File

@ -77,7 +77,7 @@ local function extract_function_parameters(table_html: string, function_name: st
"%s_%s",
utils.capitalize(function_name),
utils.capitalize(name))
table.insert(parameters_types, ast.create_node("record", record_name))
table.insert(parameters_types, ast.create_node("record", record_name, false))
is_populating_parameters_types = true
field.types = { record_name }
end
@ -262,7 +262,7 @@ function module.get_doc_from_page(html: string, module_path: string): Node, { No
end
local record_name <const> = utils.capitalize((module_path:gsub(".*%.", "")))
local module_root <const> = ast.create_node("module", record_name, module_path)
local module_root <const> = ast.create_node("module", record_name, module_path, false)
local other_nodes <const>: { Node } = {}
local module_signals_node <const> = ast.create_node("enum", "Signal")

View File

@ -24,6 +24,9 @@ local record Node
-- for "module"
module_path: string
dependencies: { string : string } -- module_name -> module_path
-- for "module" and "record"
global: boolean
end
return Node