diff options
Diffstat (limited to 'tnslc/utils')
-rw-r--r-- | tnslc/utils/c_wrap_linux.tnsl | 38 | ||||
-rw-r--r-- | tnslc/utils/file.tnsl | 21 |
2 files changed, 44 insertions, 15 deletions
diff --git a/tnslc/utils/c_wrap_linux.tnsl b/tnslc/utils/c_wrap_linux.tnsl index 62c3962..70b50f0 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, lseek" +asm "extern malloc, realloc, free, printf, open, close, read, write, lseek, perror" {}uint8 _alert = "Alert!\n\0" {}uint8 _dec = "%d\n\0" @@ -119,8 +119,8 @@ asm "extern malloc, realloc, free, printf, open, close, read, write, lseek" # Create file for writing (overwrite) -/; _create_file (~void name) [~void] - ~void out +/; _create_file (~void name) [int] + int out asm "mov rax, rsp" asm "xor rdx, rdx" @@ -143,8 +143,8 @@ asm "extern malloc, realloc, free, printf, open, close, read, write, lseek" ;/ # Open file for reading or writing (no overwrite) -/; _open_file (~void name) [~void] - ~void out +/; _open_file (~void name) [int] + int out asm "mov rax, rsp" asm "xor rdx, rdx" @@ -166,7 +166,7 @@ asm "extern malloc, realloc, free, printf, open, close, read, write, lseek" return out ;/ -/; _close_file (~void handle) +/; _close_file (int handle) asm "mov rax, rsp" asm "xor rdx, rdx" @@ -180,7 +180,7 @@ asm "extern malloc, realloc, free, printf, open, close, read, write, lseek" asm "call close wrt ..plt" ;/ -/; _read_byte (~void handle, ~uint8 byte) [int] +/; _read_byte (int handle, ~uint8 byte) [int] int out asm "mov rax, rsp" @@ -202,7 +202,7 @@ asm "extern malloc, realloc, free, printf, open, close, read, write, lseek" return out ;/ -/; _fseek (~void handle, uint pos) [int] +/; _fseek (int handle, uint pos) [int] int out # align stack @@ -226,7 +226,7 @@ asm "extern malloc, realloc, free, printf, open, close, read, write, lseek" return out ;/ -/; _write_byte (~void handle, ~uint8 byte) [int] +/; _write_byte (int handle, ~uint8 byte) [int] int out asm "mov rax, rsp" @@ -247,6 +247,26 @@ asm "extern malloc, realloc, free, printf, open, close, read, write, lseek" return out ;/ +/; _perror (~void str) + + asm "mov rax, rsp" + asm "xor rdx, rdx" + asm "mov rcx, 16" + asm "div rcx" + asm "sub rsp, rdx" + asm "sub rsp, 128" + + # setup call by clearing most values + asm "mov rdi, r10" + asm "xor rsi, rsi" + asm "xor rax, rax" + + # do call + asm "call perror wrt ..plt" + + # there's no more to do 'cause printf returns nothing +;/ + /; print_alert _printf(~_alert{0}) ;/ diff --git a/tnslc/utils/file.tnsl b/tnslc/utils/file.tnsl index 1d8a1e9..f6a0643 100644 --- a/tnslc/utils/file.tnsl +++ b/tnslc/utils/file.tnsl @@ -1,6 +1,6 @@ struct File { Artifact path, - ~void handle, + int32 handle, uint pos, bool at_end } @@ -13,7 +13,8 @@ struct File { /; init (~uint8 str) self.path.init() self.path.split_cstr(str, '/') - self.handle = NULL + self.handle = 0 + self.handle-- self.at_end = false self.pos = 0 ;/ @@ -43,16 +44,18 @@ struct File { ;/ /; end - self.path.end() /; if (self.handle + 1 !== 0) _close_file(self.handle) - self.handle = NULL + self.handle = 0 + self.handle-- ;/ + self.path.end() ;/ /; open ~uint8 p = self.path.to_cstr('/') self.handle = _open_file(p) + /; if (self.handle + 1 == 0) self.at_end = true ;/ @@ -71,7 +74,8 @@ struct File { /; close /; if (self.handle + 1 !== 0) _close_file(self.handle) - self.handle = NULL + self.handle = 0 + self.handle-- self.at_end = false ;/ ;/ @@ -84,10 +88,15 @@ struct File { uint8 out int bytes = _read_byte(self.handle, ~out) self.pos = self.pos + 1 - + /; if (bytes == 0) self.at_end = true return 0 + ;; else if (bytes !== 1) + _perror("Error reading from file\0") + _print_num("FD: %ld\n\0", self.handle) + self.at_end = true + return 0 ;/ return out |