From 606ab9da9e4231564c91e4613bfbbb8883f7eb9d Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Fri, 29 Mar 2024 16:55:01 -0400 Subject: File read test --- tnslc/c_wrap_linux.tnsl | 2 + tnslc/compile/compile.tnsl | 1 + tnslc/compile/module.tnsl | 11 +++ tnslc/file.tnsl | 9 -- tnslc/tnslc.tnsl | 43 ++++++---- tnslc/utils/algo.tnsl | 10 +++ tnslc/utils/file.tnsl | 89 ++++++++++++++++++++ tnslc/utils/utils.tnsl | 6 ++ tnslc/utils/vector.tnsl | 201 +++++++++++++++++++++++++++++++++++++++++++++ tnslc/vector.tnsl | 166 ------------------------------------- 10 files changed, 347 insertions(+), 191 deletions(-) create mode 100644 tnslc/compile/module.tnsl delete mode 100644 tnslc/file.tnsl create mode 100644 tnslc/utils/algo.tnsl create mode 100644 tnslc/utils/file.tnsl create mode 100644 tnslc/utils/utils.tnsl create mode 100644 tnslc/utils/vector.tnsl delete mode 100644 tnslc/vector.tnsl diff --git a/tnslc/c_wrap_linux.tnsl b/tnslc/c_wrap_linux.tnsl index 4e7768a..814ec1c 100644 --- a/tnslc/c_wrap_linux.tnsl +++ b/tnslc/c_wrap_linux.tnsl @@ -5,6 +5,8 @@ asm "extern malloc, realloc, free, printf, open, close, read, write" {}uint8 _dec = "%d\n\0" {}uint8 _ptr = "%p\n\0" +~void NULL = 0 + /; _alloc (uint size) [~void] ~void out diff --git a/tnslc/compile/compile.tnsl b/tnslc/compile/compile.tnsl index 157969e..00fe081 100644 --- a/tnslc/compile/compile.tnsl +++ b/tnslc/compile/compile.tnsl @@ -1,4 +1,5 @@ /; module compile + :import "module.tnsl" :import "tokenizer.tnsl" :import "lexer.tnsl" :import "generator.tnsl" diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl new file mode 100644 index 0000000..b33ae58 --- /dev/null +++ b/tnslc/compile/module.tnsl @@ -0,0 +1,11 @@ +struct Module { + ~Module parent, + utils.Vector vars +} + +/; method Module + /; init (~Module parent) + self.parent = parent + self.vars.init(1) + ;/ +;/ diff --git a/tnslc/file.tnsl b/tnslc/file.tnsl deleted file mode 100644 index e17ec70..0000000 --- a/tnslc/file.tnsl +++ /dev/null @@ -1,9 +0,0 @@ -struct File { - -} - -/; method File - /; init (~uint8 str) - ;/ - -;/ diff --git a/tnslc/tnslc.tnsl b/tnslc/tnslc.tnsl index 0e4e04e..23d3531 100644 --- a/tnslc/tnslc.tnsl +++ b/tnslc/tnslc.tnsl @@ -1,10 +1,6 @@ :import "c_wrap_linux.tnsl" -/; module utils - :import "vector.tnsl" - :import "file.tnsl" -;/ - +:import "utils/utils.tnsl" :import "compile/compile.tnsl" ~uint8 DEFAULT_FOUT = "out.asm\0" @@ -17,7 +13,11 @@ usage: \0" -~uint8 split_test = "test/whatever/file.tnsl\0" +~uint8 FOPEN_ERR = "Error opening file\n\0" + +~uint8 char_str = "%c\0" +~uint8 newline = "\n\0" +~uint8 rel_pth = "../../tnslc.tnsl\0" /; main (int argc, ~~uint8 argv) [int] asm "mov r10, rdi" @@ -28,18 +28,29 @@ usage: return 1 ;/ - utils.Artifact art - - art.init() - # art.push(split_test) - # _printf(art.strings{0}) + utils.File fin + fin.init(argv{1}) + fin.open() + + /; if (fin.at_end == true) + _printf(FOPEN_ERR) + fin.end() + return 2 + ;/ + + /; loop (fin.at_end == false) + _print_num(char_str, fin.read()) + ;/ - art.split_cstr(split_test, '/') - ~uint8 file = art.to_cstr('/') - _printf(file) - _delete(file) + utils.File rel = fin.relative(rel_pth) + ~uint8 str = rel.path.to_cstr('/') + _printf(str) + _printf(newline) + _delete(str) + rel.end() - art.end() + fin.close() + fin.end() return 0 ;/ diff --git a/tnslc/utils/algo.tnsl b/tnslc/utils/algo.tnsl new file mode 100644 index 0000000..c501e35 --- /dev/null +++ b/tnslc/utils/algo.tnsl @@ -0,0 +1,10 @@ + +/; strcmp(~uint8 a, b) [bool] + /; loop (a` == b` && a` !== 0 && b` !== 0) [a++; b++] ;/ + + /; if (b` == 0 && a` == 0) + return true + ;/ + return false +;/ + diff --git a/tnslc/utils/file.tnsl b/tnslc/utils/file.tnsl new file mode 100644 index 0000000..a33e30c --- /dev/null +++ b/tnslc/utils/file.tnsl @@ -0,0 +1,89 @@ +struct File { + Artifact path, + ~void handle, + bool at_end +} + +~uint8 PT_HANDLE = "Handle: %p\n\0" +uint INVALID_HANDLE = 0xffffffff +~uint8 PARENT_DIR = ".." +~uint8 CURRENT_DIR = "." + +/; method File + /; init (~uint8 str) + self.path.init() + self.path.split_cstr(str, '/') + self.handle = NULL + self.at_end = false + ;/ + + /; relative (~uint8 path) [File] + ~uint8 current = self.path.to_cstr('/') + File out + out.init(current) + _delete(current) + out.path.pop() + + Artifact add + add.init() + add.split_cstr(path, '/') + + # Brunt of work here + /; loop (uint i = 0; i < add.count) [i++] + ~uint8 cur = add.get(i) + /; if (strcmp(cur, CURRENT_DIR) == false) + out.path.split_cstr(cur, '/') + ;/ + ;/ + + add.end() + + return out + ;/ + + /; end + self.path.end() + /; if (self.handle < INVALID_HANDLE && self.handle != NULL) + _close_file(self.handle) + self.handle = NULL + ;/ + ;/ + + /; open + ~uint8 p = self.path.to_cstr('/') + self.handle = _open_file(p) + _delete(p) + ;/ + + /; create + ~uint8 p = self.path.to_cstr('/') + self.handle = _create_file(p) + _delete(p) + ;/ + + /; close + /; if (self.handle !== INVALID_HANDLE && self.handle !== NULL) + _close_file(self.handle) + self.handle = NULL + self.at_end = false + ;/ + ;/ + + /; read [uint8] + uint8 out + int bytes = _read_byte(self.handle, ~out) + /; if (bytes == 0) + self.at_end = true + return 0 + ;/ + return out + ;/ + + /; write (uint8 byte) + int written = _write_byte(self.handle, ~byte) + /; if (written == 0) + self.at_end = true + ;/ + ;/ + +;/ diff --git a/tnslc/utils/utils.tnsl b/tnslc/utils/utils.tnsl new file mode 100644 index 0000000..aa7968a --- /dev/null +++ b/tnslc/utils/utils.tnsl @@ -0,0 +1,6 @@ +/; module utils + + :import "vector.tnsl" + :import "file.tnsl" + :import "algo.tnsl" +;/ diff --git a/tnslc/utils/vector.tnsl b/tnslc/utils/vector.tnsl new file mode 100644 index 0000000..d00d698 --- /dev/null +++ b/tnslc/utils/vector.tnsl @@ -0,0 +1,201 @@ +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 +} + +/; 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) + ;/ +;/ + diff --git a/tnslc/vector.tnsl b/tnslc/vector.tnsl deleted file mode 100644 index af0a665..0000000 --- a/tnslc/vector.tnsl +++ /dev/null @@ -1,166 +0,0 @@ -struct Vector { - ~void data, - uint - count, - size, - _elsz -} - -uint VECTOR_MIN_ELEMENTS = 4 -uint VECTOR_MAX_GROW = 256 -~void NULL = 0 - -~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++ - ;/ - - /; 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 -} - -/; 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()) - _print_num(NUM_STR, self.count) - ;/ - - /; _grow (uint i) - self.size = self.size + i - self.strings = _realloc(self.strings, len self.strings * self.size) - ;/ - - /; 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++ - ;/ - - /; 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) - ;/ -;/ - -- cgit v1.2.3