From 391153b533c9de61e801ac1815d6dcf2204e6bdb Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Tue, 25 Jun 2024 02:36:43 -0400 Subject: load imported tnsl files --- tnslc/compile/ast.tnsl | 58 ++++++++++++++++++++++++++++++++++++++++---------- tnslc/utils/algo.tnsl | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 11 deletions(-) (limited to 'tnslc') diff --git a/tnslc/compile/ast.tnsl b/tnslc/compile/ast.tnsl index ebe44cb..ebb4037 100644 --- a/tnslc/compile/ast.tnsl +++ b/tnslc/compile/ast.tnsl @@ -35,22 +35,56 @@ struct Node { ;/ -/; build_struct (utils.Iterator it) +/; build_struct (~utils.Iterator it, ~Node mod, ~utils.File fin) ;/ -/; build_module () +/; build_module (~utils.Iterator it, ~Node mod, ~utils.File fin) ;/ -/; build_block (utils.Iterator it) +/; build_block (~utils.Iterator it, ~Node mod, ~utils.File fin) + it`.next() + /; if (it`.at_end() == true) + return + ;/ + + ~Token cur = it`.get() ;/ -/; build_preproc (utils.Iterator it) +/; build_preproc (~utils.Iterator it, ~Node mod, ~utils.File fin) + it`.next() + /; if (it`.at_end() == true) + return + ;/ + + ~Token cur = it`.get() + /; if (utils.strcmp(cur`.data, "import\0") == true) + # get file path + it`.next() + cur = it`.get() + + # gen new file struct + ~uint8 frel = utils.unquote_str(cur`.data) + _printf("\nReading file: \0") + _printf(frel) + _printf("\n\0") + utils.File fnew = fin`.relative(frel) + + # file import + build_file(~fnew, mod) + + # cleanup + _delete(frel) + fnew.end() + ;; else + # unknown preproc + return + ;/ ;/ -/; build_vardef (utils.Iterator it) +/; build_vardef (~utils.Iterator it, ~Node mod, ~utils.File fin) ;/ -/; at_defn (utils.Iterator it) [bool] +/; at_defn (~utils.Iterator it) [bool] return false ;/ @@ -65,18 +99,18 @@ struct Node { /; loop (tokit.at_end() == false) ~Token t = tokit.get() - /; if (utils.strcmp(t`.data, "/;\0") == true) + /; if (utils.strcmp(t`.data, "/;\0") || utils.strcmp(t`.data, ";;\0")) _printf("Block detected!\n\0") - build_block(tokit) + build_block(~tokit, mod, fin) ;; else if (utils.strcmp(t`.data, "struct\0") == true) _printf("Struct detected!\n\0") - build_struct(tokit) + build_struct(~tokit, mod, fin) ;; else if (utils.strcmp(t`.data, ":\0") == true) _printf("Preproc detected!\n\0") - build_preproc(tokit) + build_preproc(~tokit, mod, fin) ;; else if (at_defn(tokit) == true) _printf("Defn detected!\n\0") - build_vardef(tokit) + build_vardef(~tokit, mod, fin) ;; else # _printf("Error detected!\n\0") # TODO: ERROR @@ -84,5 +118,7 @@ struct Node { tokit.next() ;/ + + free_token_list(~tokens) ;/ diff --git a/tnslc/utils/algo.tnsl b/tnslc/utils/algo.tnsl index 8426f9d..a08c773 100644 --- a/tnslc/utils/algo.tnsl +++ b/tnslc/utils/algo.tnsl @@ -200,3 +200,43 @@ return out.as_cstr() ;/ +/; unquote_cha(~uint8 cha) [uint8] + /; if (cha` !== '\\') + return cha` + ;/ + + cha++ + + /; if (cha` == '\"') + return '\"' + ;; else if (cha` == '\'') + return '\'' + ;; else if (cha` == '\\') + return '\\' + ;; else if (cha` == 'n') + return '\n' + ;; else if (cha` == 't') + return '\t' + ;; else if (cha` == '0') + return 0 + ;/ + + return cha` +;/ + +/; unquote_str(~uint8 str) [~uint8] + Vector out + out.init(1) + + /; loop (str++; str` !== 0 && str` !== '\"') [str++] + uint8 buf = str` + /; if (str` == '\\') + buf = unquote_cha(str) + str++ + ;/ + out.push(~buf) + ;/ + + return out.as_cstr() +;/ + -- cgit v1.2.3