From 5d688b4da97da2c2f684940147478f12d1f2baba Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Fri, 19 Jul 2024 03:21:39 -0400 Subject: switch tokenization scheme --- tnslc/utils/algo.tnsl | 2 +- tnslc/utils/c_wrap_linux.tnsl | 26 +++++++++++++++++++++++++- tnslc/utils/file.tnsl | 22 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) (limited to 'tnslc/utils') diff --git a/tnslc/utils/algo.tnsl b/tnslc/utils/algo.tnsl index a08c773..73cfb7f 100644 --- a/tnslc/utils/algo.tnsl +++ b/tnslc/utils/algo.tnsl @@ -194,7 +194,7 @@ return str ;/ -/; strclone(~uint8 cstr) [~uint8] +/; strcpy(~uint8 cstr) [~uint8] Vector out out.from_cstr(cstr) return out.as_cstr() diff --git a/tnslc/utils/c_wrap_linux.tnsl b/tnslc/utils/c_wrap_linux.tnsl index 814ec1c..1e3155e 100644 --- a/tnslc/utils/c_wrap_linux.tnsl +++ b/tnslc/utils/c_wrap_linux.tnsl @@ -1,5 +1,5 @@ # Must be included at the top of the file -asm "extern malloc, realloc, free, printf, open, close, read, write" +asm "extern malloc, realloc, free, printf, open, close, read, write, fseek" {}uint8 _alert = "Alert!\n\0" {}uint8 _dec = "%d\n\0" @@ -202,6 +202,30 @@ asm "extern malloc, realloc, free, printf, open, close, read, write" return out ;/ +/; _fseek (~void handle, uint pos) [int] + int out + + # align stack + asm "mov rax, rsp" + asm "xor rdx, rdx" + asm "mov rcx, 16" + asm "div rcx" + asm "sub rsp, rdx" + # add buffer zone to stack + asm "sub rsp, 128" + + # Call c func + asm "mov rdi, r10" + asm "mov rsi, r11" + asm "mov rdx, 0" # standard value for SEEK_SET as per GNU libc + asm "call fseek wrt ..plt" + + # get return value + asm "mov r12, rax" + + return out +;/ + /; _write_byte (~void handle, ~uint8 byte) [int] int out diff --git a/tnslc/utils/file.tnsl b/tnslc/utils/file.tnsl index 978f31b..22b11f1 100644 --- a/tnslc/utils/file.tnsl +++ b/tnslc/utils/file.tnsl @@ -1,6 +1,7 @@ struct File { Artifact path, ~void handle, + uint pos, bool at_end } @@ -14,6 +15,7 @@ struct File { self.path.split_cstr(str, '/') self.handle = NULL self.at_end = false + self.pos = 0 ;/ /; relative (~uint8 path) [File] @@ -75,15 +77,35 @@ struct File { ;/ /; read [uint8] + /; if (self.at_end == true) + return 0 + ;/ + uint8 out int bytes = _read_byte(self.handle, ~out) + self.pos = self.pos + 1 + /; if (bytes == 0) self.at_end = true return 0 ;/ + return out ;/ + /; unread + /; if (self.pos < 1) + return + ;/ + + _fseek(self.handle, self.pos - 1) + self.pos = self.pos - 1 + + /; if (self.at_end == true) + self.at_end = false + ;/ + ;/ + /; write (uint8 byte) int written = _write_byte(self.handle, ~byte) /; if (written == 0) -- cgit v1.2.3