From 28437aecb635013972f8f70a117f4b2af3688d41 Mon Sep 17 00:00:00 2001 From: sofia Date: Tue, 22 Jul 2025 20:03:25 +0300 Subject: [PATCH] Fix indexing return type and casting to and from a char --- reid/lib/std.reid | 23 ++++++++++++++++++++--- reid/src/codegen.rs | 4 +++- reid/src/mir/implement.rs | 2 ++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/reid/lib/std.reid b/reid/lib/std.reid index b8991a6..8960af8 100644 --- a/reid/lib/std.reid +++ b/reid/lib/std.reid @@ -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; } \ No newline at end of file diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index bb353b8..0a3295b 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -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 diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index 4265ac1..d0d8fd3 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -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)) }