diff options
-rw-r--r-- | tnslc/ast/ast.tnsl | 48 | ||||
-rw-r--r-- | tnslc/ast/block.tnsl | 15 | ||||
-rw-r--r-- | tnslc/ast/list.tnsl | 75 | ||||
-rw-r--r-- | tnslc/ast/statement.tnsl | 95 | ||||
-rw-r--r-- | tnslc/ast/tree.tnsl | 51 | ||||
-rw-r--r-- | tnslc/ast/value.tnsl | 55 | ||||
-rw-r--r-- | tnslc/compile/compile.tnsl | 127 | ||||
-rw-r--r-- | tnslc/flags/defaults.tnsl | 24 | ||||
-rw-r--r-- | tnslc/flags/flags.tnsl | 48 | ||||
-rw-r--r-- | tnslc/inc.tnsl | 4 |
10 files changed, 80 insertions, 462 deletions
diff --git a/tnslc/ast/ast.tnsl b/tnslc/ast/ast.tnsl deleted file mode 100644 index 71470f6..0000000 --- a/tnslc/ast/ast.tnsl +++ /dev/null @@ -1,48 +0,0 @@ -/## - Copyright 2021-2022 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 -#/ - -/; export module ast - :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 -} - diff --git a/tnslc/ast/block.tnsl b/tnslc/ast/block.tnsl deleted file mode 100644 index 8fa0f27..0000000 --- a/tnslc/ast/block.tnsl +++ /dev/null @@ -1,15 +0,0 @@ -/## - Copyright 2021-2022 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 deleted file mode 100644 index b65b336..0000000 --- a/tnslc/ast/list.tnsl +++ /dev/null @@ -1,75 +0,0 @@ -/## - Copyright 2021-2022 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 -;/ - -/; tree_list_type [{}Node] - ;{}Node out = {} - ;return out -;/ - -/; tree_list_statement (~{}Token tok, ~int cur) [{}Node] - ;{}Node out = {} - - /; loop (cur` < len tok`) [cur` = cur` + 1] - ;Node stmt = tree_statement(tok, cur) - ;out.append(stmt) - - ;Token tmp = tok`{cur`} - /; if (string_equate(tmp.data, "]") || string_equate(tmp.data, ")")) - ;cur` = cur` + 1 - ;break - ;; else if (string_equate(tmp.data, ";") == false) - ;create_panic("Unexpected token in list of statements") - ;/ - ;/ - - ;return out -;/ - -/; tree_list_params (~{}Token tok, ~int cur) [{}Node] - ;{}Node out = {} - - /; if (string_equate(tok`{cur`}.data, ")")) - ;return out - ;/ - - /; if (type_then_name(tok, cur) == false) - ;create_panic("Parameter list must start with a type and name combo.") - ;/ - - /; loop (cur` < len tok`) [cur` = cur` + 1] - ;Node tmp - /; if (type_then_name(tok, cur)) - ;tmp = tree_type(tok, cur) - ;out.append(tmp) - ;/ - - ;tmp = tree_value(tok, cur) - - /; if (string_equate(tok`{cur`}.data, ")")) - ;cur` = cur` + 1 - ;break - ;; else if (string_equate(tok`{cur`}.data, ",") == false) - ;create_panic("A value in a list of params must be followed with a comma") - ;/ - ;/ - - ;return out -;/ diff --git a/tnslc/ast/statement.tnsl b/tnslc/ast/statement.tnsl deleted file mode 100644 index 15f6e26..0000000 --- a/tnslc/ast/statement.tnsl +++ /dev/null @@ -1,95 +0,0 @@ -/## - Copyright 2021-2022 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_closing (Token t) [bool] - /; if (t.token_type == TOKEN_TYPE.DELIMIT) - ;return string_equate(t.data`, ")") || string_equate(t.data`, "]") || string_equate(t.data`, "}") - ;/ - ;return false -;/ - -/; get_closing (~{}Token tokens, int i) [int] - ;{}charp end = ")" - /; if (string_equate(tokens`{i}.data`, "[")) - ;end = "]" - ;; else if (string_equate(tokens`{i}.data`, "{")) - ;end = "}" - ;/ - - ;int delims = 0 - - /; loop (i++; i < len tokens`) [i++] - /; if (is_closing(tokens`{i})) - /; if (delims > 0) - ;delims-- - ;; else if (string_equate(end, tokens`{i}.data`)) - ;return i - ;; else - ;return -1 - ;/ - ;; else if (tokens`{i}.token_type == TOKEN_TYPE.DELIMIT) - ;delims++ - ;/ - ;/ - - ;return -1 -;/ - - -/; is_definition (~{}Token tokens, ~int i) [bool] - - /; loop (int j = i`; j < len tokens`) [j++] - /; if (tokens`{j}.token_type == TOKEN_TYPE.KEYTYPE) - - ;; else if () - ;/ - ;/ - ;return false -;/ - -/; tree_keyword_statement (~{}Token tokens, ~int i) [Node] - ;Token def_tok = {TOKEN_TYPE.KEYWORD, 0, 0, ~CNULL} - ;{}Node def_sub = {} - - ;return {def_tok, ~def_sub} -;/ - -/; 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 deleted file mode 100644 index 0e8f9dc..0000000 --- a/tnslc/ast/tree.tnsl +++ /dev/null @@ -1,51 +0,0 @@ -/## - Copyright 2021-2022 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] - ;Token null_tok = {0, 0, 0, ~CNULL} - ;{}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 deleted file mode 100644 index 59655c7..0000000 --- a/tnslc/ast/value.tnsl +++ /dev/null @@ -1,55 +0,0 @@ -/## - Copyright 2021-2022 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 (~{}Token tok, ~int cur) [Node] - ;Token type_tok = {NODE_TYPE.TYPE, 0, 0, ~CNULL} - ;{}Node type_sub = {} - - # Pre type keyword checks - /; loop (tok`{cur`}.token_type !== TOKEN_TYPE.DEFWORD && tok`{cur`}.token_type !== TOKEN_TYPE.KEYTYPE) - /; if (tok`{cur`}.token_type == TOKEN_TYPE.KEYWORD) - - ;/ - ;/ - - /; - - ;/ - - ;return {type_tok, ~type_sub} -;/ - -/; tree_value [Node] - ;Token def_tok = {NODE_TYPE.VALUE, 0, 0, ~CNULL} - ;{}Node def_sub = {} - - ;return {def_tok, ~def_sub} -;/ - -/; 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 diff --git a/tnslc/compile/compile.tnsl b/tnslc/compile/compile.tnsl index c884cca..921e6cd 100644 --- a/tnslc/compile/compile.tnsl +++ b/tnslc/compile/compile.tnsl @@ -28,14 +28,15 @@ {}charp name, - {}{}charp sub_types + {}VType sub_types, + {}{}charp sub_names } /; method VType /; get_sub_type({}charp name) [VType] - /; loop (int i = 0; i < len (self.sub_types)) [i = i + 2] - /; if (string_equate(~name, ~(self.sub_types{i + 1}))) - ;return vtype_by_name(self.sub_types{i}) + /; loop (int i = 0; i < len (self.sub_types)) [i++] + /; if (string_equate(~name, ~(self.sub_names{i}))) + ;return self.sub_types{i} ;/ ;/ ;return NT @@ -53,6 +54,15 @@ ;/ ;return -1 ;/ + + /; print + ;tnsl.io.print("Size: ") + ;tnsl.io.print(self._size) + ;tnsl.io.print(" | Ptr: ") + ;tnsl.io.print(self.ptr) + ;tnsl.io.print(" | Name: ") + ;tnsl.io.println(self.name) + ;/ ;/ # Tracks defined variables in a block @@ -118,7 +128,7 @@ # returns the type of the named variable /; get_vtype ({}charp name) [VType] /; loop (int i = 0; i < len (self.on_stack)) [i++] - /; if (string_equate(~name, ~(self.sym_names{i}))) + /; if (string_equate(name, self.sym_names{i})) ;return (self.sym_types{i}) ;/ ;/ @@ -127,25 +137,25 @@ # Sizes of items ;{}VType type_table = { - {1, 0, "int8", {}}, - {2, 0, "int16", {}}, - {4, 0, "int32", {}}, - {8, 0, "int64", {}}, - {8, 0, "int", {}}, - {1, 0, "uint8", {}}, - {2, 0, "uint16", {}}, - {4, 0, "uint32", {}}, - {8, 0, "uint64", {}}, - {8, 0, "uint", {}}, - {4, 0, "float32", {}}, - {8, 0, "float64", {}}, - {8, 0, "float", {}}, - {1, 0, "bool", {}}, - {8, 0, "void", {}} + {1, 0, "int8", {}, {}}, + {2, 0, "int16", {}, {}}, + {4, 0, "int32", {}, {}}, + {8, 0, "int64", {}, {}}, + {8, 0, "int", {}, {}}, + {1, 0, "uint8", {}, {}}, + {2, 0, "uint16", {}, {}}, + {4, 0, "uint32", {}, {}}, + {8, 0, "uint64", {}, {}}, + {8, 0, "uint", {}, {}}, + {4, 0, "float32", {}, {}}, + {8, 0, "float64", {}, {}}, + {8, 0, "float", {}, {}}, + {1, 0, "bool", {}, {}}, + {8, 0, "void", {}, {}} } # Null type -;VType NT = {0, 0, "null", {}} +;VType NT = {0, 0, "null", {}, {}} # Returns an index in the vtrack for a given variable name /; name_to_index ({}charp name, ~VTrack tab) [int] @@ -231,6 +241,17 @@ ;return out ;/ +# For use when constructing module labels +/; construct_path_label({}{}charp path) + ;{}charp out = "_." + /; loop (int i = 0; i < len path) [i++] + ;add_strings(~out, ~(path{i})) + ;out.append('.') + ;/ + ;out{len out - 1} = ':' + ;return out +;/ + # Using the given offset (in bytes), return an asm value of form ".quad <offset>" /; construct_value (int size, int offset) [{}charp] ;{}charp out = ".byte " @@ -263,7 +284,7 @@ # Parse a struct and add it to the table /; def_struct (~int cur, ~{}Token data, ~{}charp dsec) [VType] - ;VType out = {0, 0, "", {}} + ;VType out = {0, 0, "", {}, {}} ;cur`++ ;out.name = data`{cur`}.data` @@ -290,26 +311,14 @@ /; if (token_is(cur, data, ",") || token_is(cur, data, "}")) # Use ctype ;cur`-- - ;{}charp l = construct_offset_label(out.name, data`{cur`}.data`) - ;l.append(':') - ;l.append(' ') - ;{}charp p = construct_value(8, out._size) /; if (ctype.ptr > 0) - ;p = construct_value(8, 8) ;out._size = out._size + 8 ;; else ;out._size = out._size + ctype._size ;/ - - ;out.sub_types.append(ctype.name) - ;out.sub_types.append(data`{cur`}.data`) - - ;p.append('\n') - ;add_strings(~l, ~p) - # add "_type.member: .qword #offset_value" to data section - - ;add_strings(dsec, ~l) + ;out.sub_types.append(ctype) + ;out.sub_names.append(data`{cur`}.data`) ;; else # Get type @@ -317,8 +326,9 @@ ;ctype = get_vtype(cur, data) ;/ ;/ - + ;out.print() ;type_table.append(out) + ;return out ;/ @@ -559,15 +569,27 @@ -/; compile_block (~int cur, ~{}Token data, ~VTrack gsc, ~{}charp hsec, csec, dsec) +/; compile_block (~int cur, ~{}Token data, ~VTrack gsc, ~{}charp hsec, csec, dsec, {}{}charp mod_path, Path rel) ;VTrack tab = { {}, {}, {} } - ;VType out_type = NT + ;VType out_type = tnslc.type_table{14} ;{}charp name = "" ;bool r = false /; loop (cur`++; cur` < len data`) [cur`++] + /; if (token_is(cur, data, "module")) + ;mod_path.append(data`{cur` + 1}.data`) + ;cur` = cur` + 2 + ;break + ;/ /; if (data`{cur`}.token_type == TOKEN_TYPE.DEFWORD && len name == 0) ;name = data`{cur`}.data` + /; if (len mod_path > 0) + ;{}charp frs = "_." + ;{}charp jn = join(mod_path, '.') + ;add_strings(~frs, ~jn) + ;add_strings(csec, ~frs) + ;csec`.append('.') + ;/ ;add_strings(csec, ~name) ;csec`.append(':') ;csec`.append('\n') @@ -592,21 +614,26 @@ ;bool ret = false /; loop (cur` < len data` && !ret) - /; if (string_equate(data`{cur`}.data`, ";/")) + /; if (token_is(cur, data, ";/")) /; if (!r) ;tail_guard(csec) ;/ ;add_strings(csec, ~(tnslc.COMMON_ASM{0})) ;break - ;; else if (string_equate(data`{cur`}.data`, "/;")) + ;; else if (token_is(cur, data, "/;")) ;bool ch = true /; loop (ch) - ;compile_block(cur, data, gsc, hsec, csec, dsec) + ;compile_block(cur, data, gsc, hsec, csec, dsec, mod_path) /; if (cur` !< len data`) ;break ;/ - ;ch = string_equate(data`{cur`}.data`, ";;") - ;/ + ;ch = token_is(cur, data, ";;") + ;/ + ;; else if (string_equate(data`{cur`}.data`, ":")) + ;cur` = cur` + 2 + ;Path inc = rel.rel_file(unquote_string(data`{cur`}.data`)) + ;compile_include(inc, gsc, hsec, csec, dsec, mod_path) + ;cur`++ ;; else if (string_equate(data`{cur`}.data`, ";")) ;ret = compile_statement(cur, data, ~tab, gsc, hsec, csec, dsec) ;; else @@ -620,7 +647,7 @@ ;csec`.append('\n') ;/ -/; compile_include (Path file_path, ~VTrack global, ~{}charp hsec, csec, dsec) +/; compile_include (Path file_path, ~VTrack global, ~{}charp hsec, csec, dsec, {}{}charp mod_path) # Autocomplete in the case of module syntax ;bool d = file_path.extension_is("tnsl") /; if (!d) @@ -641,22 +668,22 @@ ;tnsl.io.print(len data`) ;tnsl.io.println(" tokens parsed.") - ;compile_file(file_path, data, global, hsec, csec, dsec) + ;compile_file(file_path, data, global, hsec, csec, dsec, mod_path) ;/ -/; compile_file (Path rel, ~{}Token data, ~VTrack global, ~{}charp hsec, csec, dsec) +/; compile_file (Path rel, ~{}Token data, ~VTrack global, ~{}charp hsec, csec, dsec, {}{}charp mod_path) ;int j = len data` /; loop (int i = 0; i < j) [i++] /; if (string_equate(data`{i}.data`, "/;")) - ;compile_block(~i, data, global, hsec, csec, dsec) + ;compile_block(~i, data, global, hsec, csec, dsec, mod_path, rel) ;; else if (string_equate(data`{i}.data`, ";")) ;compile_global(~i, data, global, hsec, csec, dsec) ;; else if (string_equate(data`{i}.data`, ":")) ;i = i + 2 ;Path inc = rel.rel_file(unquote_string(data`{i}.data`)) - ;compile_include(inc, global, hsec, csec, dsec) + ;compile_include(inc, global, hsec, csec, dsec, mod_path) ;; else ;break ;/ @@ -671,7 +698,7 @@ ;VTrack global_scope = {{}, {}} - ;tnslc.compile_include(rel, ~global_scope, ~hsec, ~csec, ~dsec) + ;tnslc.compile_include(rel, ~global_scope, ~hsec, ~csec, ~dsec, {}) ;tnsl.io.File out = tnsl.io.writeFile(file_out) diff --git a/tnslc/flags/defaults.tnsl b/tnslc/flags/defaults.tnsl deleted file mode 100644 index b1d4e13..0000000 --- a/tnslc/flags/defaults.tnsl +++ /dev/null @@ -1,24 +0,0 @@ -/## - Copyright 2021-2022 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 -#/ - -/# defaults.tnsl - # This file provides the default settings for the compiler - # if the user has not provided flags which overrides them. -#/ - -/; get_defaults [Settings] - -;/
\ No newline at end of file diff --git a/tnslc/flags/flags.tnsl b/tnslc/flags/flags.tnsl deleted file mode 100644 index 26192a3..0000000 --- a/tnslc/flags/flags.tnsl +++ /dev/null @@ -1,48 +0,0 @@ -/## - Copyright 2021-2022 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 -#/ - -/; module flags - :include "flags/defaults.tnsl" -;/ - -;enum ISA [uint] { - X86 = 0, - X64 = 1, - ARM = 2, - ARM64 = 3, - RISCV = 4 -} - -;enum FORMATS [uint] { - ELF = 0 - PE = 1 -} - -;struct Settings { - - # ISA settings - uint isa, - bool littleEndian, - - # Output format settings - uint format -} - -/; parse_flags () [Settings] - ;Settings out = flags.get_defaults() - - ;return out -;/ diff --git a/tnslc/inc.tnsl b/tnslc/inc.tnsl index f9029f0..6e17855 100644 --- a/tnslc/inc.tnsl +++ b/tnslc/inc.tnsl @@ -1 +1,3 @@ -:include "dummy.tnsl"
\ No newline at end of file +/; module main + :include "dummy.tnsl" +;/
\ No newline at end of file |