diff options
Diffstat (limited to 'tnslc/utils')
| -rw-r--r-- | tnslc/utils/algo.tnsl | 36 | ||||
| -rw-r--r-- | tnslc/utils/c_wrap_linux.tnsl | 8 | ||||
| -rw-r--r-- | tnslc/utils/c_wrap_windows.tnsl | 8 | ||||
| -rw-r--r-- | tnslc/utils/vector.tnsl | 13 |
4 files changed, 47 insertions, 18 deletions
diff --git a/tnslc/utils/algo.tnsl b/tnslc/utils/algo.tnsl index 35ac35c..31d9290 100644 --- a/tnslc/utils/algo.tnsl +++ b/tnslc/utils/algo.tnsl @@ -1,5 +1,5 @@ -/; strcmp(~uint8 a, b) [bool] +/; strcmp (~uint8 a, b) [bool] /; loop (a` == b` && a` !== 0 && b` !== 0) [a++; b++] ;/ /; if (b` == 0 && a` == 0) @@ -9,12 +9,31 @@ ;/ # Length of a cstr -/; strlen(~uint8 str) [int] +/; strlen (~uint8 str) [int] int out = 0 /; loop (str` !== 0) [str++; out++] ;/ return out ;/ +/; ends_with (~uint8 str, suffix) [bool] + int chk = strlen(suffix) + int counter = 0 + + /; loop (str` !== 0) [str++] + /; if (suffix{counter} !== 0) + /; if (suffix{counter} == str`) + counter++ + ;; else + counter = 0 + ;/ + ;; else + counter = 0 + ;/ + ;/ + + return chk == counter +;/ + /; base_for_char (uint8 c) [int] /; if (c == 'b' || c == 'B') return 2 @@ -45,15 +64,14 @@ /; if (ch !< '0' && ch !> '9') return ch - '0' ;; else if (ch !< 'a' && ch !> 'f') - return ch - 'a' + return ch - 'a' + 10 ;; else if (ch !< 'A' && ch !> 'F') - return ch - 'A' + return ch - 'A' + 10 ;/ return 0 ;/ /; decode_dec (uint8 ch) [int] - _print_num(NUM_STR, ch) /; if (ch !< '0' && ch !> '9') return ch - '0' ;/ @@ -78,8 +96,6 @@ out = out * base int decoded = 0 - _print_num(NUM_STR, start) - _print_num(NUM_STR, str{start}) /; if (base == 2) decoded = decode_bin(str{start}) ;; else if (base == 8) @@ -150,7 +166,7 @@ /; loop (i > 0) [i = i / 16] int n = i % 16 /; if (n > 9) - out.push_char('a' + n - 10) + out.push_char('A' + n - 10) ;; else out.push_char('0' + n) ;/ @@ -231,7 +247,7 @@ return cha` ;/ -/; unquote_str(~uint8 str) [~uint8] +/; unquote_str(~uint8 str) [Vector] Vector out out.init(1) @@ -244,6 +260,6 @@ out.push(~buf) ;/ - return out.as_cstr() + return out ;/ diff --git a/tnslc/utils/c_wrap_linux.tnsl b/tnslc/utils/c_wrap_linux.tnsl index 97a93ba..71a5b47 100644 --- a/tnslc/utils/c_wrap_linux.tnsl +++ b/tnslc/utils/c_wrap_linux.tnsl @@ -1,9 +1,9 @@ # Must be included at the top of the file asm "extern malloc, realloc, free, printf, putchar, open, close, read, write, lseek, perror" -{}uint8 _alert = "Alert!\n\0" -{}uint8 _dec = "%d\n\0" -{}uint8 _ptr = "%p\n\0" +~uint8 _alert = "Alert!\n\0" +~uint8 _dec = "%d\n\0" +~uint8 _ptr = "%p\n\0" ~void NULL = 0 @@ -275,5 +275,5 @@ asm "extern malloc, realloc, free, printf, putchar, open, close, read, write, ls ;/ /; print_alert - _printf(~_alert{0}) + _printf(_alert) ;/ diff --git a/tnslc/utils/c_wrap_windows.tnsl b/tnslc/utils/c_wrap_windows.tnsl index 577ca25..839c976 100644 --- a/tnslc/utils/c_wrap_windows.tnsl +++ b/tnslc/utils/c_wrap_windows.tnsl @@ -10,9 +10,9 @@ asm "extern ReadFile" asm "extern WriteFile" asm "extern CloseHandle" -{}uint8 _alert = "Alert!\n\0" -{}uint8 _dec = "%d\n\0" -{}uint8 _ptr = "%p\n\0" +~uint8 _alert = "Alert!\n\0" +~uint8 _dec = "%d\n\0" +~uint8 _ptr = "%p\n\0" /; _alloc (uint size) [~void] ~void out @@ -280,5 +280,5 @@ asm "extern CloseHandle" ;/ /; print_alert - _printf(~_alert{0}) + _printf(_alert) ;/ diff --git a/tnslc/utils/vector.tnsl b/tnslc/utils/vector.tnsl index 38c45fc..b38978f 100644 --- a/tnslc/utils/vector.tnsl +++ b/tnslc/utils/vector.tnsl @@ -56,6 +56,19 @@ uint VECTOR_MAX_GROW = 256 self.count++ ;/ + /; replace (int index, ~void el) + ~uint8 start = self.get(index) + /; if (start == NULL) + return + ;/ + + /; loop (int i = 0; i < self._elsz) [i++] + ~uint8 to = start + i + ~uint8 from = el + i + to` = from` + ;/ + ;/ + /; _shrink(uint i) /; if (i !< self.size) self.size = 1 |