Compare commits

...

2 Commits

Author SHA1 Message Date
64418635a5 Add from_str to stdlib 2025-07-22 20:07:42 +03:00
28437aecb6 Fix indexing return type and casting to and from a char 2025-07-22 20:03:25 +03:00
4 changed files with 29 additions and 11 deletions

View File

@ -31,9 +31,21 @@ 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);
let cloned = malloc(length as u64) as *char;
copy_bits(str, cloned, 0, length as u64);
return String {
inner: cloned,
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;
@ -42,13 +54,17 @@ pub fn add_char(string: &mut String, c: char) {
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;
@ -57,6 +73,9 @@ fn copy_bits(from: *char, to: *char, pos: u64, max: u64) -> u8 {
return copy_bits(from, to, pos + 1, max);
}
pub fn free_string(string: &mut String) {
free((*string).inner as *u8);
fn str_length(string: *char, position: u32) -> u32 {
if ((string[position] as u8) == 0) {
return 0;
}
return str_length(string, position + 1) + 1;
}

View File

@ -1083,7 +1083,9 @@ impl mir::Expression {
)),
_ => panic!(),
},
(TypeKind::UserPtr(_), TypeKind::UserPtr(_)) => Some(StackValue(
(TypeKind::UserPtr(_), TypeKind::UserPtr(_))
| (TypeKind::Char, TypeKind::U8)
| (TypeKind::U8, TypeKind::Char) => Some(StackValue(
val.0.derive(
scope
.block

View File

@ -394,6 +394,8 @@ impl Expression {
let expr_type = expression.return_type(refs, mod_id)?;
if let TypeKind::Array(elem_ty, _) = expr_type.1.resolve_weak(refs) {
Ok((ReturnKind::Soft, *elem_ty))
} else if let TypeKind::UserPtr(_) = expr_type.1.resolve_weak(refs) {
Ok((ReturnKind::Soft, expr_type.1))
} else {
Err(ReturnTypeOther::IndexingNonArray(expression.1))
}

View File

@ -1,15 +1,10 @@
import std::print;
import std::new_string;
import std::from_str;
import std::add_char;
import std::free_string;
fn main() -> i32 {
let mut test = new_string();
add_char(&mut test, 'h');
add_char(&mut test, 'e');
add_char(&mut test, 'l');
add_char(&mut test, 'l');
add_char(&mut test, 'o');
let mut test = from_str("hello world");
print(&test);