Fix indexing return type and casting to and from a char

This commit is contained in:
Sofia 2025-07-22 20:03:25 +03:00
parent bbdfae081d
commit 28437aecb6
3 changed files with 25 additions and 4 deletions

View File

@ -31,6 +31,16 @@ 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
}
}
@ -42,13 +52,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 +71,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))
}