From 2f5551c7c9c60f7e8e45b4826df9890cfa28d3d9 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Thu, 29 Aug 2024 14:55:11 -0400 Subject: [tnslc] Framework for func helper --- tnslc/parse/ast.tnsl | 48 +++++++++++- tnslc/test.tnsl | 208 +++------------------------------------------------ 2 files changed, 56 insertions(+), 200 deletions(-) (limited to 'tnslc') diff --git a/tnslc/parse/ast.tnsl b/tnslc/parse/ast.tnsl index 943c044..26b57b8 100644 --- a/tnslc/parse/ast.tnsl +++ b/tnslc/parse/ast.tnsl @@ -393,7 +393,11 @@ struct Node { cur = cur`.sub.get(cur`.sub.count - 1) first` = produce_next_token(fin, first`) - run = _ast_value_bare(fin, cur, first) + /; if (utils.strcmp(bin.data, "is\0")) + _ast_type(fin, cur, first) + ;; else + run = _ast_value_bare(fin, cur, first) + ;/ ;; else run = false @@ -916,10 +920,46 @@ struct Node { blf.end() ;/ +/; _mhf_post (~utils.File fin, ~Node mod, ~Token first) [bool] + return false +;/ + +/; _mhf_transform (~Node _type) +;/ + +/; _mhf_finish_value (~utils.File fin, ~Node mod, ~Token first) +;/ + +/; _mhf_finish_decl (~utils.File fin, ~Node mod, ~Token first) +;/ + /; _maybe_helper_fun (~utils.File fin, ~Node mod, ~Token first) - Token next = produce_next_token(fin, first`) - first`.end() - first` = next + # Try parsing as a type first, and if we encounter something weird we will transform + # the output into a value before proceeding + Node out + out.init(NTYPE_TYPE, utils.strcpy("\0")) + + _type_helper_pre(fin, ~out, first) + + /; if (first`.eq("(\0")) + _mhf_transform(~out) + _mhf_finish_value(fin, ~out, first) + + ;; else if (first`._type == TTYPE_KEYTP) + _mhf_finish_decl(fin, ~out, first) + + ;; else if (first`._type !== TTYPE_USRWD) + _ast_print_err(first, "Unexpected token. Expected the completion of a declaration or value\0") + + ;; else if (_mhf_post(fin, ~out, first)) + _mhf_finish_decl(fin, ~out, first) + + ;; else + _mhf_transform(~out) + _mhf_finish_value(fin, ~out, first) + ;/ + + mod`.sub.push(~out) ;/ /; _ast_function (~utils.File fin, ~Node mod, ~Token first) diff --git a/tnslc/test.tnsl b/tnslc/test.tnsl index d00d698..0d54258 100644 --- a/tnslc/test.tnsl +++ b/tnslc/test.tnsl @@ -1,201 +1,17 @@ -struct Vector { - ~void data, - uint - count, - size, - _elsz -} - -uint VECTOR_MIN_ELEMENTS = 4 -uint VECTOR_MAX_GROW = 256 - -~uint8 NUM_STR = "Num %d\n\0" - -/; method Vector - - /; init (uint elsz) - self._elsz = elsz - self.size = VECTOR_MIN_ELEMENTS - self.count = 0 - self.data = _alloc(self.size * self._elsz) - ;/ - - /; 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++ - ;/ - - /; _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) - ;/ -;/ - -# Artifacts - -struct Artifact { - ~~uint8 strings, - uint - size, - count +struct Fin { + ~void fd, + int state } -/; 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++ - ;/ - - /; _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) - ;/ +/; main (int argc, {}{}uint8 argv) [int] + uint8 a = 1 + ~uint8 b = ~a + Fin c + ~Fin d = ~c + b` = 2 + c.state = a + d`.state = 0 + return c.state ;/ -- cgit v1.2.3