From ee7f98646598d47f02c640bfffcc75fe9617be5c Mon Sep 17 00:00:00 2001 From: Aire-One Date: Sun, 29 Jan 2023 19:19:20 +0100 Subject: [PATCH] feat(types): introduce new data structures --- src/awesomewm.d.tl/ast.tl | 63 ++++++++++++++++++++++++++++++ src/awesomewm.d.tl/dag.tl | 27 +++++++++++++ src/awesomewm.d.tl/types/Dag.d.tl | 8 ++++ src/awesomewm.d.tl/types/Node.d.tl | 25 ++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 src/awesomewm.d.tl/ast.tl create mode 100644 src/awesomewm.d.tl/dag.tl create mode 100644 src/awesomewm.d.tl/types/Dag.d.tl create mode 100644 src/awesomewm.d.tl/types/Node.d.tl diff --git a/src/awesomewm.d.tl/ast.tl b/src/awesomewm.d.tl/ast.tl new file mode 100644 index 0000000..b4a469f --- /dev/null +++ b/src/awesomewm.d.tl/ast.tl @@ -0,0 +1,63 @@ +local type Node = require("types.Node") + +local basic_nodes : { Node.Token : function(name: string): Node } = { + module = function(name: string): Node + return { + token = "module", + name = name, + children = {}, + } + end, + record = function(name: string): Node + return { + token = "record", + name = name, + children = {}, + } + end, + enum = function(name: string): Node + return { + token = "enum", + name = name, + children = {}, + } + end, + identifier = function(name: string): Node + return { + token = "identifier", + name = name, + } + end, + variable = function(name: string): Node + return { + token = "variable", + name = name, + types = {}, + } + end, + ["function"] = function(name: string): Node + return { + token = "function", + name = name, + parameters = {}, + return_types = {}, + } + end, + metamethod = function(name: string): Node + return { + token = "metamethod", + name = name, + parameters = {}, + return_types = {}, + } + end, +} + +local function create_node(token: Node.Token, name: string): Node + local node = basic_nodes[token](name) + return node +end + +return { + create_node = create_node, +} diff --git a/src/awesomewm.d.tl/dag.tl b/src/awesomewm.d.tl/dag.tl new file mode 100644 index 0000000..b2ff5bf --- /dev/null +++ b/src/awesomewm.d.tl/dag.tl @@ -0,0 +1,27 @@ +local type Dag = require("types.Dag") +local type Node = require("types.Node") + +local function init(): Dag + local dag : Dag = { + nodes_by_module_name = {}, + global_nodes = {}, + } + return dag +end + +local function insert(dag: Dag, module_name: string, node: Node) + if not dag.nodes_by_module_name[module_name] then + dag.nodes_by_module_name[module_name] = {} + end + table.insert(dag.nodes_by_module_name[module_name], node) +end + +local function insert_global(dag: Dag, node: Node) + table.insert(dag.global_nodes, node) +end + +return { + init = init, + insert = insert, + insert_global = insert_global, +} diff --git a/src/awesomewm.d.tl/types/Dag.d.tl b/src/awesomewm.d.tl/types/Dag.d.tl new file mode 100644 index 0000000..c6d38bc --- /dev/null +++ b/src/awesomewm.d.tl/types/Dag.d.tl @@ -0,0 +1,8 @@ +local type Node = require("redo.ast.Node") + +local record Dag + nodes_by_module_name: { string : { Node } } + global_nodes: { Node } +end + +return Dag diff --git a/src/awesomewm.d.tl/types/Node.d.tl b/src/awesomewm.d.tl/types/Node.d.tl new file mode 100644 index 0000000..b0440aa --- /dev/null +++ b/src/awesomewm.d.tl/types/Node.d.tl @@ -0,0 +1,25 @@ +local record Node + enum Token + "module" -- file root node, it is always a record and the generated .d.tl file returns it + "record" + "enum" + "identifier" -- blank token with only a name (used for enum values) + "variable" + "function" + "metamethod" + end + token: Token + name: string + + -- for "module", "record", "enum" + children: { Node } + + -- for "variable" + types: { string } + + -- for "function" and "metamethod" + parameters: { Node } + return_types: { string } +end + +return Node