replace pointers with slices

This commit is contained in:
zongor 2026-07-10 18:45:21 -07:00
parent 8abe0ab545
commit 197e1542cb
1 changed files with 29 additions and 45 deletions

View File

@ -5,57 +5,31 @@ const nat U16_MAX = 65535;
byte[10000] heap; byte[10000] heap;
nat heap_allocated = 0; nat heap_allocated = 0;
function malloc(nat size) ref byte[] { subroutine alloc(nat size) byte[] {
nat pos = heap_allocated; nat pos = heap_allocated;
heap_allocated = heap_allocated + size; heap_allocated = heap_allocated + size;
return ref heap[pos]; return heap[pos:pos + size];
} }
function free() { subroutine heap_reset() {
heap_allocated = 0; 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 * Concatinates 2 strings
*/ */
function strcat(str src1, str src2) str { function strcat(str src1, str src2) str {
str result = malloc(src1.length + src2.length); str result = alloc(src1.length + src2.length) as str;
memcpy(ref result[0], ref src1[0]); result[0:src1.length] = src1;
memcpy((ref result[0] + src1.length), ref src2[0]); result[src1.length:] = src2;
return result; return result;
} }
/** /**
* finds index of string, -1 if not found * finds index of string, -1 if not found
*/ */
function stridx(str haystack, char needle) int { function stridx(str haystack, char needle) nat {
int i = -1; nat i = -1;
for (char c in haystack) { for (char c in haystack) {
i = i + 1; i = i + 1;
if (c == needle) { if (c == needle) {
@ -70,7 +44,7 @@ function stridx(str haystack, char needle) int {
*/ */
function streq(str src1, str src2) bool { function streq(str src1, str src2) bool {
if (src1.length != src2.length) return false; if (src1.length != src2.length) return false;
for (int i=0; src1.length; i++) { for (nat i=0; src1.length; i++) {
if (src1[i] != src2[i]) return false; if (src1[i] != src2[i]) return false;
} }
return true; return true;
@ -79,21 +53,31 @@ function streq(str src1, str src2) bool {
/** /**
* Slice string * Slice string
*/ */
function slice(str src, int start, int stop) str { function slice(str src, nat start, nat stop) str {
int len = stop - start; nat len = stop - start;
str result = malloc(len); str result = alloc(len) as str;
for (int i=start; i<stop; i++) { for (nat i=start; i<stop; i++) {
result[i] = src[i]; result[i] = src[i];
} }
return result; return result;
} }
/**
* Print with a newline
*/
function pln(str string) {
for (byte c in string) {
write = c;
}
write = '\n';
}
/** /**
* int to string * int to string
*/ */
function itos(int src) str { function itos(int src) str {
byte buffer[6]; byte buffer[6];
nat i = 6; nat i = 6;
bool neg = n < 0; bool neg = n < 0;
if (neg) n = -n; if (neg) n = -n;
@ -105,8 +89,8 @@ function itos(int src) str {
if(neg) buffer[--i] = '-'; if(neg) buffer[--i] = '-';
str result = malloc(6 - i); str result = alloc(6 - i) as str;
memcpy(result, &buffer[0] + i, 6 - i); result[0:] = buffer[i:6 - i];
return result; return result;
} }
@ -122,8 +106,8 @@ function ntos(nat src) str {
n /= 10; n /= 10;
} while(n > 0); } while(n > 0);
str result = malloc(5 - i); str result = alloc(6 - i) as str;
memcpy(result, &buffer[0] + i, 5 - i); result[0:] = buffer[i:6 - i];
return result; return result;
} }