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 { }