reid-llvm/reid/lib/std.reid

79 lines
1.7 KiB
Plaintext

extern fn puts(message: *char) -> i32;
extern fn malloc(size: u64) -> *u8;
extern fn free(ptr: *u8);
extern fn div(numerator: i32, denominator: i32) -> div_t;
struct div_t {
quotient: i32,
remainder: i32,
}
pub fn print(message: &String) {
puts(*message.inner);
}
pub fn int_div(numerator: i32, denominator: i32) -> div_t {
return div(numerator, denominator);
}
pub fn allocate(size: u64) -> *u8 {
malloc(size)
}
struct String {
inner: *char,
length: u64,
max_length: u64,
}
pub fn new_string() -> String {
String {
inner: allocate(0),
length: 0,
max_length: 0,
}
}
pub fn from_str(str: *char) -> String {
let length = str_length(str, 0);
String {
inner: str,
length: length,
max_length: length
}
}
pub fn add_char(string: &mut String, c: char) {
if ((*string).length + 1) >= (*string).max_length {
let new = allocate((*string).max_length + 4) as *char;
copy_bits((*string).inner, new, 0, (*string).max_length);
free((*string).inner as *u8);
(*string).max_length = (*string).max_length + 4;
(*string).inner = new;
}
(*string).inner[(*string).length] = c;
(((*string).inner) as *u8)[((*string).length + 1)] = 0;
(*string).length = (*string).length + 1;
}
pub fn free_string(string: &mut String) {
free((*string).inner as *u8);
}
fn copy_bits(from: *char, to: *char, pos: u64, max: u64) -> u8 {
if (pos >= max) {
return 0;
}
to[pos] = from[pos];
return copy_bits(from, to, pos + 1, max);
}
fn str_length(string: *char, position: u32) -> u32 {
if ((string[position] as u8) == 0) {
return 0;
}
return str_length(string, position + 1) + 1;
}