From 32059682eeb7b8be3a0c4bd61d7ab389bbc77384 Mon Sep 17 00:00:00 2001 From: sofia Date: Tue, 22 Jul 2025 20:27:29 +0300 Subject: [PATCH] Make add_char and set_char work --- reid/lib/std.reid | 18 +++++++++++++++--- reid/src/codegen.rs | 2 +- reid_src/hello_world.reid | 4 ++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/reid/lib/std.reid b/reid/lib/std.reid index 4749871..753f323 100644 --- a/reid/lib/std.reid +++ b/reid/lib/std.reid @@ -25,6 +25,7 @@ struct String { inner: *char, length: u64, max_length: u64, + must_be_freed: bool, } pub fn new_string() -> String { @@ -32,6 +33,7 @@ pub fn new_string() -> String { inner: allocate(0), length: 0, max_length: 0, + must_be_freed: true, } } @@ -42,7 +44,8 @@ pub fn from_str(str: *char) -> String { return String { inner: cloned, length: length, - max_length: length + max_length: length, + must_be_freed: false, }; } @@ -51,9 +54,12 @@ pub fn add_char(string: &mut String, c: char) { let new = allocate((*string).max_length + 4) as *char; copy_bits((*string).inner, new, 0, (*string).max_length); - free((*string).inner as *u8); + if (*string).must_be_freed == true { + free((*string).inner as *u8); + } (*string).max_length = (*string).max_length + 4; (*string).inner = new; + (*string).must_be_freed = true; } (*string).inner[(*string).length] = c; @@ -61,6 +67,12 @@ pub fn add_char(string: &mut String, c: char) { (*string).length = (*string).length + 1; } +pub fn set_char(string: &mut String, c: char, position: u64) { + if position <= (*string).length { + (*string).inner[position] = c; + } +} + pub fn free_string(string: &mut String) { free((*string).inner as *u8); } @@ -74,7 +86,7 @@ fn copy_bits(from: *char, to: *char, pos: u64, max: u64) -> u8 { } fn str_length(string: *char, position: u32) -> u32 { - if ((string[position] as u8) == 0) { + if (string[position] == '\0') { return 0; } return str_length(string, position + 1) + 1; diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 0a3295b..b4adffa 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -1135,7 +1135,7 @@ impl mir::IfExpression { if let Some(debug) = &scope.debug { let before_location = self.0 .1.into_debug(scope.tokens, debug.scope).unwrap(); let before_v = debug.info.location(&debug.scope, before_location); - scope.block.set_terminator_location(before_v).unwrap(); + scope.block.set_terminator_location(before_v).ok(); let then_location = self .1 diff --git a/reid_src/hello_world.reid b/reid_src/hello_world.reid index 6dbce11..dcd4f5d 100644 --- a/reid_src/hello_world.reid +++ b/reid_src/hello_world.reid @@ -1,10 +1,14 @@ import std::print; import std::from_str; import std::add_char; +import std::set_char; import std::free_string; fn main() -> i32 { let mut test = from_str("hello world"); + + add_char(&mut test, '!'); + set_char(&mut test, 'B', 0); print(&test);