From 46aa6b65376ea62deb1d5ea1611b59dc222a5141 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Mon, 2 May 2022 02:57:49 -0400 Subject: [TNSLC] AST Updates - Change how string_equate works - Call AST Node generator from main - Flush out ast.tnsl - Add a few initial methods to AST - Move some code from token.tnsl to parse.tnsl --- tnslc/ast/ast.tnsl | 30 +++++++++++++++++++++++++++- tnslc/ast/block.tnsl | 15 ++++++++++++++ tnslc/ast/list.tnsl | 21 +++++++++++++++++++ tnslc/ast/node.tnsl | 26 ------------------------ tnslc/ast/statement.tnsl | 48 ++++++++++++++++++++++++++++++++++++++++++++ tnslc/ast/tree.tnsl | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ tnslc/ast/value.tnsl | 40 +++++++++++++++++++++++++++++++++++++ 7 files changed, 205 insertions(+), 27 deletions(-) create mode 100644 tnslc/ast/block.tnsl create mode 100644 tnslc/ast/list.tnsl delete mode 100644 tnslc/ast/node.tnsl create mode 100644 tnslc/ast/statement.tnsl create mode 100644 tnslc/ast/tree.tnsl create mode 100644 tnslc/ast/value.tnsl (limited to 'tnslc/ast') diff --git a/tnslc/ast/ast.tnsl b/tnslc/ast/ast.tnsl index 466fe08..bece84e 100644 --- a/tnslc/ast/ast.tnsl +++ b/tnslc/ast/ast.tnsl @@ -15,5 +15,33 @@ #/ /; export module ast - :include "ast/node.tnsl" + :include "ast/block.tnsl" + :include "ast/list.tnsl" + :include "ast/statement.tnsl" + :include "ast/tree.tnsl" + :include "ast/value.tnsl" ;/ + +;{}charp CNULL = "" + +# AST node (non-block) +;struct Node { + tnslc.Token + # associated token to the node + tok, + + ~{}Node + # sub-nodes + sub +} + +;enum NODE_TYPE [int] { + FILE = 9, + TYPE = 10, + VALUE = 11, + DEFN = 12, + + #... + + PREP = 1000 +} \ No newline at end of file diff --git a/tnslc/ast/block.tnsl b/tnslc/ast/block.tnsl new file mode 100644 index 0000000..17a9e22 --- /dev/null +++ b/tnslc/ast/block.tnsl @@ -0,0 +1,15 @@ +/## + Copyright 2021 Kyle Gunger + + This file is licensed under the CDDL 1.0 (the License) + and may only be used in accordance with the License. + You should have received a copy of the License with this + software/source code. If you did not, a copy can be found + at the following URL: + + https://opensource.org/licenses/CDDL-1.0 + + THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO + WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE + EXPRESS OR IMPLIED +#/ \ No newline at end of file diff --git a/tnslc/ast/list.tnsl b/tnslc/ast/list.tnsl new file mode 100644 index 0000000..ee5d0cf --- /dev/null +++ b/tnslc/ast/list.tnsl @@ -0,0 +1,21 @@ +/## + Copyright 2021 Kyle Gunger + + This file is licensed under the CDDL 1.0 (the License) + and may only be used in accordance with the License. + You should have received a copy of the License with this + software/source code. If you did not, a copy can be found + at the following URL: + + https://opensource.org/licenses/CDDL-1.0 + + THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO + WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE + EXPRESS OR IMPLIED +#/ + +/; tree_list_value [{}Node] + ;{}Node out = {} + + ;return out +;/ diff --git a/tnslc/ast/node.tnsl b/tnslc/ast/node.tnsl deleted file mode 100644 index d1db325..0000000 --- a/tnslc/ast/node.tnsl +++ /dev/null @@ -1,26 +0,0 @@ -/## - Copyright 2021 Kyle Gunger - - This file is licensed under the CDDL 1.0 (the License) - and may only be used in accordance with the License. - You should have received a copy of the License with this - software/source code. If you did not, a copy can be found - at the following URL: - - https://opensource.org/licenses/CDDL-1.0 - - THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO - WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE - EXPRESS OR IMPLIED -#/ - -# AST node (non-block) -;struct Node { - tnslc.parse.Token - # associated token to the node - tok, - - ~{}Node - # sub-nodes - sub -} diff --git a/tnslc/ast/statement.tnsl b/tnslc/ast/statement.tnsl new file mode 100644 index 0000000..d8b4877 --- /dev/null +++ b/tnslc/ast/statement.tnsl @@ -0,0 +1,48 @@ +/## + Copyright 2021 Kyle Gunger + + This file is licensed under the CDDL 1.0 (the License) + and may only be used in accordance with the License. + You should have received a copy of the License with this + software/source code. If you did not, a copy can be found + at the following URL: + + https://opensource.org/licenses/CDDL-1.0 + + THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO + WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE + EXPRESS OR IMPLIED +#/ + +/; is_keyword_statement ({}charp word) [bool] + ;return string_equate(word, "struct") || string_equate(word, "interface") || string_equate(word, "enum") || + string_equate(word, "continue") || string_equate(word, "break") || string_equate(word, "label") || + string_equate(word, "goto") || string_equate(word, "virtual") || string_equate(word, "asm") || + string_equate(word, "delete") +;/ + +/; is_definition (~{}Token tokens, ~int i) [bool] ;/ + +/; tree_keyword_statement (~{}Token tokens, ~int i) [Node] + +;/ + +/; tree_statement (~{}Token tokens, ~int i) [Node] + ;i`++ + /; if (i` !< len tokens`) + ;return make_null_node() + + ;; else if (is_keyword_statement(tokens`{i`}.data`)) + ;return tree_keyword_statement(tokens, i) + + ;; else if (is_definition(tokens, i)) + ;return tree_definition(tokens, i) + + ;/ + + ;return tree_value(tokens, i) +;/ + +/; tree_preproc [Node] + +;/ \ No newline at end of file diff --git a/tnslc/ast/tree.tnsl b/tnslc/ast/tree.tnsl new file mode 100644 index 0000000..fe6baab --- /dev/null +++ b/tnslc/ast/tree.tnsl @@ -0,0 +1,52 @@ +/## + Copyright 2021 Kyle Gunger + + This file is licensed under the CDDL 1.0 (the License) + and may only be used in accordance with the License. + You should have received a copy of the License with this + software/source code. If you did not, a copy can be found + at the following URL: + + https://opensource.org/licenses/CDDL-1.0 + + THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO + WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE + EXPRESS OR IMPLIED +#/ + +/; make_null_node [Node] + ;{}charp null_str = "" + ;Token null_tok = {0, 0, 0, ~null_str} + ;{}Node sub = {} + ;return {null_tok, ~sub} +;/ + +/# + # This should be the main entry point into the AST module + # +#; make_tree (~{}Token tokens, {}charp name) [Node] + ;Token root = {0, 0, NODE_TYPE.FILE, ~name} + ;{}Node sub = {} + + /; loop (int i = 0; i < len tokens`) [i++] + ;{}charp to_check = tokens`{i}.data` + + /; if (string_equate(to_check, ";")) + ;sub.append(tree_statement(tokens, ~i)) + + ;; else if (string_equate(to_check, ":")) + ;sub.append(tree_preproc(tokens, ~i)) + + ;; else if (string_equate(to_check, "/;")) + ;sub.append(tree_block(tokens, ~i)) + + ;; else if (string_equate(to_check, "/:")) + ;sub.append(tree_preblock(tokens, ~i)) + + ;; else + ;break + ;/ + ;/ + + ;return {root, ~sub} +;/ diff --git a/tnslc/ast/value.tnsl b/tnslc/ast/value.tnsl new file mode 100644 index 0000000..2cefb62 --- /dev/null +++ b/tnslc/ast/value.tnsl @@ -0,0 +1,40 @@ +/## + Copyright 2021 Kyle Gunger + + This file is licensed under the CDDL 1.0 (the License) + and may only be used in accordance with the License. + You should have received a copy of the License with this + software/source code. If you did not, a copy can be found + at the following URL: + + https://opensource.org/licenses/CDDL-1.0 + + THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO + WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE + EXPRESS OR IMPLIED +#/ + +/; tree_type [Node] + ;Token type_tok = {NODE_TYPE.TYPE, 0, 0, ~CNULL} + ;{}Node type_sub = {} + ;return {type_tok, ~type_sub} +;/ + +/; tree_value [Node] + +;/ + +/; tree_definition [Node] + ;Token def_tok = {NODE_TYPE.DEFN, 0, 0, ~CNULL} + ;{}Node def_sub = {} + + ;def_sub.append(tree_type()) + + ;{}Node vals = tree_list_value() + + /; loop (int i = 0; i < len vals) [i++] + ;def_sub.append(vals{i}) + ;/ + + ;return {def_tok, ~def_sub} +;/ \ No newline at end of file -- cgit v1.2.3