includes/std: Moved stdlib to std folder, added all builtin functions

This commit is contained in:
WerWolv
2022-01-30 17:53:48 +01:00
parent 00b7c912f2
commit 6325dbce0d
19 changed files with 190 additions and 19 deletions

37
includes/std/bit.pat Normal file
View File

@@ -0,0 +1,37 @@
#pragma once
namespace std::bit {
fn popcount(u128 x) {
x = (x & (std::limits::u128_max() / 3)) + ((x >> 1) & (std::limits::u128_max() / 3));
x = (x & (std::limits::u128_max() / 5)) + ((x >> 2) & (std::limits::u128_max() / 5));
x = (x & (std::limits::u128_max() / 17)) + ((x >> 4) & (std::limits::u128_max() / 17));
return x % 0xFF;
};
fn has_single_bit(u128 x) {
return x != 0 && (x & (x - 1)) == 0;
};
fn bit_ceil(u128 x) {
if (x == 0) return 0;
u8 i;
while ((1 << i) < x)
i = i + 1;
return 1 << i;
};
fn bit_floor(u128 x) {
if (x == 0) return 0;
u8 i;
while ((x >> i) > 0)
i = i + 1;
return 1 << (i - 1);
};
}

23
includes/std/cint.pat Normal file
View File

@@ -0,0 +1,23 @@
#pragma once
namespace std {
using uint8_t = u8;
using uint16_t = u16;
using uint32_t = u32;
using uint64_t = u64;
using uint128_t = u128;
using int8_t = s8;
using int16_t = s16;
using int32_t = s32;
using int64_t = s64;
using int128_t = s128;
using float32_t = float;
using float64_t = double;
using size_t = u64;
using ssize_t = s64;
}

53
includes/std/ctype.pat Normal file
View File

@@ -0,0 +1,53 @@
#pragma once
namespace std::ctype {
fn isdigit(char c) {
return c >= '0' && c <= '9';
};
fn isxdigit(char c) {
return std::ctype::isdigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
};
fn isupper(char c) {
return c >= 'A' && c <= 'Z';
};
fn islower(char c) {
return c >= 'a' && c <= 'z';
};
fn isalpha(char c) {
return std::ctype::isupper(c) || std::ctype::islower(c);
};
fn isalnum(char c) {
return std::ctype::isalpha(c) || std::ctype::isdigit(c);
};
fn isspace(char c) {
return (c >= 0x09 && c <= 0x0D) || c == 0x20;
};
fn isblank(char c) {
return c == 0x09 || c == ' ';
};
fn isprint(char c) {
return c >= '0' && c <= '~';
};
fn iscntrl(char c) {
return !std::ctype::isprint(c);
};
fn isgraph(char c) {
return std::ctype::isprint(c) && !std::ctype::isspace(c);
};
fn ispunct(char c) {
return std::ctype::isgraph(c) && !std::ctype::isalnum(c);
};
}

49
includes/std/file.pat Normal file
View File

@@ -0,0 +1,49 @@
#pragma once
namespace std::file {
using Handle = s32;
enum Mode : u8 {
Read = 1,
Write = 2,
Create = 3
};
fn open(str path, Mode mode) {
return builtin::std::file::open(path, mode);
};
fn close(Handle handle) {
builtin::std::file::close(handle);
};
fn read(Handle handle, u64 size) {
return builtin::std::file::read(handle, size);
};
fn write(Handle handle, str data) {
return builtin::std::file::write(handle, data);
};
fn size(Handle handle) {
return builtin::std::file::size(handle);
};
fn resize(Handle handle, u64 size) {
builtin::std::file::resize(handle, size);
};
fn flush(Handle handle) {
builtin::std::file::remove(handle);
};
fn remove(Handle handle) {
builtin::std::file::remove(handle);
};
}

35
includes/std/fxpt.pat Normal file
View File

@@ -0,0 +1,35 @@
#pragma once
namespace std::fxpt {
using fixed = s128;
fn to_float(fixed fxt, u32 precision) {
return double(fxt) / double((1 << precision));
};
fn to_fixed(double flt, u32 precision) {
return fixed((flt * (1 << precision)));
};
fn change_precision(fixed value, u32 start_precision, u32 end_precision) {
return std::fxpt::to_fixed(std::fxpt::to_float(value, start_precision), end_precision);
};
fn add(fixed a, fixed b, u32 precision) {
return a + b;
};
fn subtract(fixed a, fixed b, u32 precision) {
return a - b;
};
fn multiply(fixed a, fixed b, u32 precision) {
return (a * b) / (1 << precision);
};
fn divide(fixed a, fixed b, u32 precision) {
return (a << precision) / b;
};
}

27
includes/std/hash.pat Normal file
View File

@@ -0,0 +1,27 @@
namespace std::hash {
fn crc32(u128 address, u64 size, u32 init, u32 poly) {
u8 byte;
u32 crc, mask;
crc = init;
u64 i;
while (i < size) {
byte = std::mem::read_unsigned(address + i, 1);
crc = crc ^ byte;
u8 j;
while (j < 8) {
mask = u32(-(crc & 1));
crc = (crc >> 1) ^ (poly & mask);
j = j + 1;
}
i = i + 1;
}
return u32(~crc);
};
}

7
includes/std/http.pat Normal file
View File

@@ -0,0 +1,7 @@
namespace std::http {
fn get(str url) {
return builtin::std::http::get(url);
};
}

22
includes/std/io.pat Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
namespace std {
fn print(str fmt, auto ... args) {
builtin::std::print(fmt, args);
};
fn format(str fmt, auto ... args) {
return builtin::std::format(fmt, args);
};
fn error(str message) {
builtin::std::error(message);
};
fn warning(str message) {
builtin::std::warning(message);
};
}

85
includes/std/limits.pat Normal file
View File

@@ -0,0 +1,85 @@
#pragma once
namespace std::limits {
fn u8_min() {
return u8(0);
};
fn u8_max() {
return u8(-1);
};
fn s8_min() {
return -s8((std::limits::u8_max() / 2)) - 1;
};
fn s8_max() {
return s8((u8_max() / 2));
};
fn u16_min() {
return u16(0);
};
fn u16_max() {
return u16(-1);
};
fn s16_min() {
return -s16((std::limits::u16_max() / 2)) - 1;
};
fn s16_max() {
return s16((u16_max() / 2));
};
fn u32_min() {
return u32(0);
};
fn u32_max() {
return u32(-1);
};
fn s32_min() {
return -s32((std::limits::u32_max() / 2)) - 1;
};
fn s32_max() {
return s32((u32_max() / 2));
};
fn u64_min() {
return u64(0);
};
fn u64_max() {
return u64(-1);
};
fn s64_min() {
return -s64((std::limits::u64_max() / 2)) - 1;
};
fn s64_max() {
return s64((u64_max() / 2));
};
fn u128_min() {
return u128(0);
};
fn u128_max() {
return u128(-1);
};
fn s128_min() {
return -s128((std::limits::u128_max() / 2)) - 1;
};
fn s128_max() {
return s128((u128_max() / 2));
};
}

64
includes/std/math.pat Normal file
View File

@@ -0,0 +1,64 @@
#pragma once
namespace std::math {
fn min(auto a, auto b) {
return a < b ? a : b;
};
fn max(auto a, auto b) {
return a > b ? a : b;
};
fn clamp(auto x, auto min, auto max) {
return (x < min) ? min : ((x > max) ? max : x);
};
fn abs(auto x) {
if (x < 0) return -x;
else return x;
};
fn sign(auto x) {
if (x > 0)
return 1;
else if (x < 0)
return -1;
else
return 0;
};
fn copy_sign(auto x, auto y) {
if (y >= 0)
return std::math::abs(x);
else
return -std::math::abs(x);
};
fn factorial(u128 x) {
u128 result;
result = x;
while (x > 1) {
x = x - 1;
result = result * x;
}
return result;
};
fn comb(u128 n, u128 k) {
if (k > n)
return 0;
else
return std::math::factorial(n) / (std::math::factorial(k) * std::math::factorial(n - k));
};
fn perm(u128 n, u128 k) {
if (k > n)
return 0;
else
return std::math::factorial(n) / std::math::factorial(n - k);
};
}

41
includes/std/mem.pat Normal file
View File

@@ -0,0 +1,41 @@
#pragma once
namespace std::mem {
fn eof() {
return $ >= std::mem::size();
};
fn align_to(u128 alignment, u128 value) {
u128 remainder = value % alignment;
return remainder != 0 ? value + (alignment - remainder) : value;
};
fn base_address() {
return builtin::std::mem::base_address();
};
fn size() {
return builtin::std::mem::size();
};
fn find_sequence(u128 occurrence_index, auto ... bytes) {
return builtin::std::mem::find_sequence(occurrence_index, bytes);
};
fn read_unsigned(u128 address, u8 size) {
return builtin::std::mem::read_unsigned(address, size);
};
fn read_signed(u128 address, u8 size) {
return builtin::std::mem::read_signed(address, size);
};
fn read_string(u128 address, u8 size) {
return builtin::std::mem::read_string(address, size);
};
}

17
includes/std/ptr.pat Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
namespace std::ptr {
fn relative_to_pointer(u128 offset) {
return $;
};
fn relative_to_parent(u128 offset) {
return addressof(parent);
};
fn relative_to_end(u128 offset) {
return std::mem::size() - offset * 2;
};
}

17
includes/std/rustint.pat Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
namespace std {
using i8 = s8;
using i16 = s16;
using i32 = s32;
using i64 = s64;
using i128 = s128;
using f32 = float;
using f64 = double;
using usize = u64;
using isize = s64;
}

131
includes/std/string.pat Normal file
View File

@@ -0,0 +1,131 @@
#pragma once
namespace std::string {
fn length(str string) {
return builtin::std::string::length(string);
};
fn at(str string, u32 index) {
return builtin::std::string::at(string, index);
};
fn substr(str string, u32 pos, u32 count) {
return builtin::std::string::substr(string, pos, count);
};
fn parse_int(str string, u8 base) {
return builtin::std::string::parse_int(string, base);
};
fn parse_float(str string) {
return builtin::std::string::parse_float(string);
};
fn to_string(auto x) {
return std::format("{}", x);
};
fn starts_with(str string, str part) {
return std::string::substr(string, 0, std::string::length(part)) == part;
};
fn ends_with(str string, str part) {
return std::string::substr(string, std::string::length(string) - std::string::length(part), std::string::length(part)) == part;
};
fn contains(str a, str b) {
s32 a_len, b_len;
a_len = std::string::length(a);
b_len = std::string::length(b);
s32 i;
while (i < a_len - b_len) {
if (std::string::substr(a, i, b_len) == b)
return true;
i = i + 1;
}
return false;
};
fn reverse(str string) {
str result;
s32 i;
i = std::string::length(string);
while (i > 0) {
i = i - 1;
result = result + std::string::at(string, i);
}
return result;
};
fn to_upper(str string) {
str result;
u32 i;
char c;
while (i < std::string::length(string)) {
c = std::string::at(string, i);
if (c >= 'a' && c <= 'z')
result = result + char(c - 0x20);
else
result = result + c;
i = i + 1;
}
return result;
};
fn to_lower(str string) {
str result;
u32 i;
char c;
while (i < std::string::length(string)) {
c = std::string::at(string, i);
if (c >= 'A' && c <= 'Z')
result = result + char(c + 0x20);
else
result = result + c;
i = i + 1;
}
return result;
};
fn replace(str string, str pattern, str replace) {
u32 string_len, pattern_len, replace_len;
string_len = std::string::length(string);
pattern_len = std::string::length(pattern);
replace_len = std::string::length(replace);
if (pattern_len > string_len)
return string;
str result;
u32 i;
while (i <= (string_len - pattern_len)) {
if (std::string::substr(string, i, pattern_len) == pattern) {
result = result + replace;
i = i + pattern_len;
} else {
result = result + std::string::at(string, i);
i = i + 1;
}
}
return result;
};
}

28
includes/std/sys.pat Normal file
View File

@@ -0,0 +1,28 @@
#pragma once
#include <std/io.pat>
namespace std {
fn assert(bool condition, str message) {
if (!condition) {
std::error(std::format("assertion failed '{0}'", message));
}
};
fn assert_warn(bool condition, str message) {
if (!condition) {
std::warning(std::format("assertion failed '{0}'", message));
}
};
fn env(str name) {
return builtin::std::env(name);
};
fn sizeof_pack(auto ... pack) {
return builtin::std::sizeof_pack(pack);
};
}