From 65433ea6299c51a6f3868f7efe1ae68d56eb689e Mon Sep 17 00:00:00 2001 From: zongor Date: Mon, 6 Jul 2026 18:39:16 -0700 Subject: [PATCH] string test changes, add intial stdlib --- test/stdlib.ul | 206 +++++++++++++++++++++++++++++++++++++++++++++++++ test/string.ul | 21 +++-- 2 files changed, 221 insertions(+), 6 deletions(-) create mode 100644 test/stdlib.ul diff --git a/test/stdlib.ul b/test/stdlib.ul new file mode 100644 index 0000000..5a2cfb1 --- /dev/null +++ b/test/stdlib.ul @@ -0,0 +1,206 @@ +const int I16_MIN = -32768; +const int I16_MAX = 32767; +const nat U16_MAX = 65535; + +byte[10000] heap; +nat heap_allocated = 0; + +function malloc(nat size) ref byte[] { + nat pos = heap_allocated; + heap_allocated = heap_allocated + size; + return ref heap[pos]; +} + +function free() { + heap_allocated = 0; +} + +function memcpy(ref byte[] to, ref byte[] from, nat length) { + for (nat i = 0; i < length; i++) { + to[i] = from[i]; + } + return ptr; +} + +function mallocpy(ref byte[] from, nat length) ref byte[] { + ref byte[] ptr = malloc(length); + for (nat i = 0; i < length; i++) { + ptr[i] = from[i]; + } + return ptr; +} + + +/** + * Print with a newline + */ +function pln(str string) { + for (byte c in string) { + write = c; + } + write = '\n'; +} + +/** + * Concatinates 2 strings + */ +function strcat(str src1, str src2) str { + str result = malloc(src1.length + src2.length); + memcpy(ref result[0], ref src1[0]); + memcpy((ref result[0] + src1.length), ref src2[0]); + return result; +} + +/** + * finds index of string, -1 if not found + */ +function stridx(str haystack, char needle) int { + int i = -1; + for (char c in haystack) { + i = i + 1; + if (c == needle) { + break; + } + } + return i; +} + +/** + * checks if 2 strings are equal + */ +function streq(str src1, str src2) bool { + if (src1.length != src2.length) return false; + for (int i=0; src1.length; i++) { + if (src1[i] != src2[i]) return false; + } + return true; +} + +/** + * Slice string + */ +function slice(str src, int start, int stop) str { + int len = stop - start; + str result = malloc(len); + for (int i=start; i 0); + + if(neg) buffer[--i] = '-'; + + str result = malloc(6 - i); + memcpy(result, &buffer[0] + i, 6 - i); + return result; +} + +/** + * nat to string + */ +function ntos(nat src) str { + byte buffer[5]; + nat i = 5; + + do { + buffer[--i] = '0' + n mod 10; + n /= 10; + } while(n > 0); + + str result = malloc(5 - i); + memcpy(result, &buffer[0] + i, 5 - i); + return result; +} + +/** + * real to string + */ +function rtos(real src) str { + +} + +/** + * string to int + */ +function stoi(str src) int { + bool neg = false; + int n = 0; + nat idx = 0; + + while (is_space(src[idx])) { + if (length-- == 0) { return n; } + idx = idx + 1; + } + + if (src[idx] == '-') { + neg = true; + if (length-- == 0) { return n; } + idx = idx + 1; + } + + while (is_digit(src[idx]) and length-- > 0) { + int digit = (src[idx] - '0' as int); + idx = idx + 1; + + if (neg) { + if (n < (I16_MIN / 10) or (n == (I16_MIN / 10) and digit > -(I16_MIN mod 10))) { + return I16_MIN; + } + n = n * 10 - digit; + } else { + if (n > (I16_MAX / 10) or (n == (I16_MAX / 10) and digit > (I16_MAX mod 10))) { + return I16_MAX; + } + n = n * 10 + digit; + } + } + + return neg ? -n : n; +} + +/** + * string to nat + */ +function ston(str src) nat { + nat n = 0; + nat idx = 0; + + while (is_space(src[idx])) { + if (length-- == 0) { return n; } + idx = idx + 1; + } + + while (is_digit(src[idx]) and length-- > 0) { + nat digit = (src[idx] - '0' as nat); + idx = idx + 1; + + if (n > (U16_MAX / 10) or (n == (U16_MAX / 10) and digit > (U16_MAX mod 10))) { + return U16_MAX; + } + n = n * 10 + digit; + } + + return n; +} + +/** + * string to real + */ +function stor(str src) real { + +} diff --git a/test/string.ul b/test/string.ul index 62e7af2..cd8ab52 100755 --- a/test/string.ul +++ b/test/string.ul @@ -1,13 +1,22 @@ -str hello = "hello World!\n"; -str msg = " damage inflicted!\n"; +str hello = "hello World!"; +str msg = " damage inflicted!"; if (msg[1] == 'd') { - print("yes\n"); + pln("yes"); } -print(msg.length as str); -print(msg); +pln(msg.length as str); +pln(msg); hello[0] = hello[0] - ' '; -print(hello); +pln(hello); halt; + +function pln(str string) { + nat i = 0; + while (i < string.length) { + write = string[i]; + i = i + 1; + } + write = '\n'; +}