feat: implement `"type"` Node type
ci/woodpecker/pr/docker-build Pipeline was successful Details
ci/woodpecker/pr/lint Pipeline was successful Details
ci/woodpecker/pr/test Pipeline failed Details
ci/woodpecker/pr/build-and-run Pipeline failed Details

This commit is contained in:
Aire-One 2023-10-08 17:34:16 +02:00
parent 989adaa62c
commit 544e3b5c71
4 changed files with 33 additions and 5 deletions

View File

@ -55,6 +55,14 @@ local basic_nodes <total>: { Node.Token : function(name: string, ...: any): Node
return_types = {},
}
end,
type = function(name: string, types: { string }, global: boolean): Node
return {
token = "type",
global = global,
name = name,
types = types,
}
end
}
local function create_node(token: Node.Token, name: string, ...: any): Node

View File

@ -148,6 +148,11 @@ for _, node in dag.iter_global_nodes(module_dag) do
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)
-- Declare global types
table.insert(global_module_ast.children, ast.create_node("type", "Color", { "any" }, true))
table.insert(global_module_ast.children, ast.create_node("type", "Surface", { "any" }, true))
table.insert(global_module_ast.children, ast.create_node("type", "Cairo_Context", { "any" }, true))
table.insert(global_module_ast.children, ast.create_node("type", "Image", { "any" }, 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

View File

@ -6,11 +6,14 @@ local utils = require("awesomewmdtl.utils")
local log = logger.log("scraper")
local function render_types(types: { string }): string
local function render_types(types: { string }, no_colon: boolean): string
if not types or #types == 0 then
return ""
end
return ": " .. table.concat(types, " | ")
return string.format(
"%s%s",
no_colon and "" or ": ",
table.concat(types, " | "))
end
local function dedent(str: string): string
@ -142,7 +145,18 @@ local node_printer <total>: { Node.Token : Node_Printer_Function } = {
on_node = function(): string, integer
log:warn("Metamethods are not supported yet")
end,
}
},
["type"] = {
on_node = function(node: Node, indent_level: integer): string, integer
return render_code(
string.format(
"%stype %s = %s",
node.global and "\nglobal " or "",
node.name,
render_types(node.types, true)),
indent_level), indent_level
end,
},
}
function print_teal(node: Node, indent_level: integer | nil): string

View File

@ -7,6 +7,7 @@ local record Node
"variable"
"function"
"metamethod"
"type"
end
token: Token
name: string
@ -14,7 +15,7 @@ local record Node
-- for "module", "record", "enum"
children: { Node }
-- for "variable"
-- for "variable" and "type"
types: { string }
-- for "function" and "metamethod"
@ -25,7 +26,7 @@ local record Node
module_path: string
dependencies: { string : string } -- module_name -> module_path
-- for "module" and "record"
-- for "module", "record", "type"
global: boolean
end