From 7b98ba81c90e1b751d7073b9e8bdc8fe3fbf1588 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Thu, 8 Aug 2024 04:53:08 -0400 Subject: [tnslc] prelim method block parsing --- tnslc/parse/ast.tnsl | 91 ++++++++++++++++++++-- tnslc/test.tnsl | 211 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 274 insertions(+), 28 deletions(-) (limited to 'tnslc') diff --git a/tnslc/parse/ast.tnsl b/tnslc/parse/ast.tnsl index f90428e..5b525f7 100644 --- a/tnslc/parse/ast.tnsl +++ b/tnslc/parse/ast.tnsl @@ -798,8 +798,87 @@ struct Node { # Method blocks +/; _block_helper_method(~utils.File fin, ~Node mod, ~Token first) + Token blf = first` + + /; loop (first`.eq("/;\0") == true || first`.eq(";;\0") == true) + Token tmp = produce_next_token(fin, first`) + /; if (first`.eq("/;\0") == false) + first`.end() + ;/ + first` = tmp + + /; if (first`._type == TTYPE_USRWD) + _ast_function(fin, mod, first) + ;; else + _ast_print_err(first, "Expected function or operator in method block\0") + ;/ + + /; loop (int deep = 1; deep > 0 && first`._type !== TTYPE_ERR) + /; if (first`.eq(";/\0") == true) + deep = deep - 1 + ;; else if (first`.eq("/;\0") == true) + deep = deep + 1 + ;; else if (first`.eq(";;\0") == true && deep == 1) + deep = deep - 1 + ;/ + + /; if (deep > 0) + tmp = produce_next_token(fin, first`) + first`.end() + first` = tmp + ;/ + ;/ + ;/ + + /; if (_advance_check(fin, first`, ";/\0") == false) + _ast_print_err(~blf, "Could not find closing ;/ for function in method block\0") + ;/ + blf.end() +;/ + /; _ast_method (~utils.File fin, ~Node mod, ~Token first) + Token blf = first` + + /; if (first`._type !== TTYPE_USRWD) + _ast_print_err(first, "Expected identifier of struct after 'method'\0") + return + ;/ + Node mth + mth.init(NTYPE_METHOD, first`.data) + first` = produce_next_token(fin, first`) + + /; loop (bool run = true; run == true && first`._type == TTYPE_DELIM) + /; if (first`.eq("/;\0") == true) + _block_helper_method(fin, ~mth, first) + ;; else + /; if (first`.data` !== ';') + _ast_print_err(first, "Method block being skipped. Parsing will resume after the end\0") + ;/ + run = false + ;/ + ;/ + + /; loop (int deep = 1; deep > 0 && first`._type !== TTYPE_ERR) + /; if (first`.eq(";/\0") == true) + deep = deep - 1 + ;; else if (first`.eq("/;\0") == true) + deep = deep + 1 + ;/ + + /; if (deep > 0) + Token tmp = produce_next_token(fin, first`) + first`.end() + first` = tmp + ;/ + ;/ + + /; if (_advance_check(fin, first`, ";/\0") == false) + _ast_print_err(~blf, "Could not find closing ;/ for method block\0") + ;/ + + mod`.sub.push(~mth) ;/ @@ -834,9 +913,9 @@ struct Node { ;/ /; loop (int deep = 1; deep > 0 && first`._type !== TTYPE_ERR) - /; if (first`.eq(";/\0")) + /; if (first`.eq(";/\0") == true) deep = deep - 1 - ;; else if (first`.eq("/;\0")) + ;; else if (first`.eq("/;\0") == true) deep = deep + 1 ;/ @@ -869,11 +948,11 @@ struct Node { fn.init(NTYPE_FUNCTION, first`.data) first` = produce_next_token(fin, first`) - /; if (first`.eq("(\0")) + /; if (first`.eq("(\0") == true) _ast_list_decl(fin, ~fn, first) ;/ - /; if (first`.eq("[\0")) + /; if (first`.eq("[\0") == true) _ast_list_type(fin, ~fn, first) ;/ @@ -1099,9 +1178,9 @@ struct Node { ;/ /; loop (int deep = 1; deep > 0 && first`._type !== TTYPE_ERR) - /; if (first`.eq(";/\0")) + /; if (first`.eq(";/\0") == true) deep = deep - 1 - ;; else if (first`.eq("/;\0")) + ;; else if (first`.eq("/;\0") == true) deep = deep + 1 ;/ diff --git a/tnslc/test.tnsl b/tnslc/test.tnsl index 4412e70..d00d698 100644 --- a/tnslc/test.tnsl +++ b/tnslc/test.tnsl @@ -1,34 +1,201 @@ -# should not be included +struct Vector { + ~void data, + uint + count, + size, + _elsz +} -~uint lmao +uint VECTOR_MIN_ELEMENTS = 4 +uint VECTOR_MAX_GROW = 256 -~{2}user c +~uint8 NUM_STR = "Num %d\n\0" -struct user { - ~int abcd, - ~Geko hij -} +/; method Vector + + /; init (uint elsz) + self._elsz = elsz + self.size = VECTOR_MIN_ELEMENTS + self.count = 0 + self.data = _alloc(self.size * self._elsz) + ;/ -struct Geko { - int i -} + /; from_cstr(~uint8 cstr) + self.init(1) + self.push_cstr(cstr) + ;/ + + /; _grow (uint i) + self.size = self.size + i + self.data = _realloc(self.data, self.size * self._elsz) + ;/ + + /; get (uint index) [~void] + /; if (index !< self.count) + return NULL + ;/ + + return self.data + index * self._elsz + ;/ + + /; push (~void el) + /; if (self.size == self.count + 1) + /; if (self.size < VECTOR_MAX_GROW) + self._grow(self.size) + ;; else + self._grow(VECTOR_MAX_GROW) + ;/ + ;/ + + ~void start = self.data + self.count * self._elsz + /; loop (int i = 0; i < self._elsz) [i++] + ~uint8 to = start + i + ~uint8 from = el + i + to` = from` + ;/ + self.count++ + ;/ -/; module mod - int i = 0 - /; whatev (~uint8 a) [uint8] - return a{0} + /; _shrink(uint i) + /; if (i !< self.size) + self.size = 1 + ;; else + self.size = self.size - i + ;/ + + self.data = _realloc(self.data, self.size * self._elsz) + ;/ + + /; pop + self.count-- + + /; if (self.count < self.size / 2) + self._shrink(self.size / 3) + ;/ + ;/ + + /; push_char (uint8 ch) + self.push(~ch) + ;/ + + /; push_cstr(~uint8 ch) + /; loop (ch` !== 0) [ch++] + self.push(ch) + ;/ + ;/ + + /; as_cstr [~uint8] + ~uint8 z = self.data + self.count + z` = 0 + return self.data + ;/ + + /; end + self.count = 0 + self.size = 0 + self._elsz = 0 + _delete(self.data) ;/ ;/ -int i = {a, b, c} * (whatev + 1)[int] -bool j = _op_order(f`.data) < _op_order(g`.data) +# Artifacts + +struct Artifact { + ~~uint8 strings, + uint + size, + count +} + +/; method Artifact + + /; init + self.size = VECTOR_MIN_ELEMENTS + self.count = 0 + self.strings = _alloc(len self.strings * self.size) + ;/ + + /; split_cstr (~uint8 str, uint8 split) + Vector track + track.init(1) + + /; loop (str` !== 0) [str++] + /; if (str` == split) + self.push(track.as_cstr()) + track.init(1) + ;; else + track.push(str) + ;/ + ;/ + + self.push(track.as_cstr()) + ;/ + + /; _grow (uint i) + self.size = self.size + i + self.strings = _realloc(self.strings, self.size * len self.strings) + ;/ + + /; push (~uint8 str) + /; if (self.size == self.count + 1) + /; if (self.size < VECTOR_MAX_GROW) + self._grow(self.size) + ;; else + self._grow(VECTOR_MAX_GROW) + ;/ + ;/ + + self.strings{self.count} = str + self.count++ + ;/ -/; main [int] - # ~uint8 a = "asdf\0" - #whatev(a) - #whatev("asdf\0") - return 0 + /; _shrink(uint i) + /; if (i !< self.size) + self.size = 1 + ;; else + self.size = self.size - i + ;/ + + self.strings = _realloc(self.strings, self.size * len self.strings) + ;/ + + /; pop + _delete(self.get(self.count - 1)) + self.count-- + + /; if (self.count < self.size / 2) + self._shrink(self.size / 3) + ;/ + ;/ + + /; get (uint index) [~uint8] + /; if (index !< self.count) + return NULL + ;/ + + return self.strings{index} + ;/ + + /; to_cstr (uint8 join) [~uint8] + Vector out + out.init(1) + + /; loop (int i = 0; i < self.count) [i++] + out.push_cstr(self.get(i)) + /; if (i < self.count - 1) + out.push(~join) + ;/ + ;/ + + return out.as_cstr() + ;/ + + /; end + /; loop (int i = 0; i < self.count) [i++] + _delete(self.get(i)) + ;/ + _delete(self.strings) + ;/ ;/ -~uint8 printf = "Unmatched closing delimiter at {line %d, col %d, %s}" -- cgit v1.2.3