From 544e3b5c716c6320f1921954ce97ed4320c66c62 Mon Sep 17 00:00:00 2001 From: Aire-One Date: Sun, 8 Oct 2023 17:34:16 +0200 Subject: [PATCH] feat: implement `"type"` Node type --- src/awesomewmdtl/ast.tl | 8 ++++++++ src/awesomewmdtl/init.tl | 5 +++++ .../printer/teal_type_definition.tl | 20 ++++++++++++++++--- src/awesomewmdtl/types/Node.tl | 5 +++-- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/awesomewmdtl/ast.tl b/src/awesomewmdtl/ast.tl index 8c693f3..08e37a7 100644 --- a/src/awesomewmdtl/ast.tl +++ b/src/awesomewmdtl/ast.tl @@ -55,6 +55,14 @@ local basic_nodes : { 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 diff --git a/src/awesomewmdtl/init.tl b/src/awesomewmdtl/init.tl index c4cf800..05cf543 100644 --- a/src/awesomewmdtl/init.tl +++ b/src/awesomewmdtl/init.tl @@ -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 = 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 diff --git a/src/awesomewmdtl/printer/teal_type_definition.tl b/src/awesomewmdtl/printer/teal_type_definition.tl index dd232f3..a28ba05 100644 --- a/src/awesomewmdtl/printer/teal_type_definition.tl +++ b/src/awesomewmdtl/printer/teal_type_definition.tl @@ -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 : { 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 diff --git a/src/awesomewmdtl/types/Node.tl b/src/awesomewmdtl/types/Node.tl index cedfce8..87f5047 100644 --- a/src/awesomewmdtl/types/Node.tl +++ b/src/awesomewmdtl/types/Node.tl @@ -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