Compare commits

...

4 Commits

Author SHA1 Message Date
de803e9024 Fix from_str in stdlib, add concat_strings 2025-07-23 22:01:14 +03:00
d96fc51b9c Fix adding to string that was allocated earlier 2025-07-23 21:27:45 +03:00
0d3abe8e42 Fix add_num_to_string not printing 10 2025-07-23 21:18:38 +03:00
14537743ed Fix for-loops 2025-07-23 21:17:16 +03:00
4 changed files with 42 additions and 26 deletions

View File

@ -3,14 +3,20 @@ import std::from_str;
import std::add_char; import std::add_char;
import std::set_char; import std::set_char;
import std::free_string; import std::free_string;
import std::new_string;
import std::add_num_to_str; import std::add_num_to_str;
import std::concat_strings;
fn main() -> i32 { fn main() -> i32 {
let mut test = from_str("hello world"); let mut test = from_str("hello");
let mut other = from_str(" world");
concat_strings(&mut test, &other);
add_char(&mut test, '!'); add_char(&mut test, '!');
set_char(&mut test, 'B', 0); add_char(&mut test, '\n');
add_num_to_str(&mut test, 1234);
add_num_to_str(&mut test, 175);
print(&test); print(&test);

View File

@ -2,11 +2,15 @@
import std::print; import std::print;
import std::from_str; import std::from_str;
import std::add_num_to_str;
import std::free_string;
fn main() -> u32 { fn main() -> u32 {
let text = from_str("hello"); for i in 0 .. 15 {
for i in 0 .. 10 { let mut text = from_str("num: ");
print(&text) add_num_to_str(&mut text, i);
print(&text);
free_string(&mut text);
} }
let mut num = 0; let mut num = 0;

View File

@ -38,21 +38,22 @@ pub fn new_string() -> String {
} }
pub fn from_str(str: *char) -> String { pub fn from_str(str: *char) -> String {
let length = str_length(str, 0); let length = str_length(str);
let cloned = malloc(length as u64) as *char; let mut new = new_string();
copy_bits(str, cloned, 0, length as u64); let static = String {
return String { inner: str,
inner: cloned, length: length - 1,
length: length,
max_length: length, max_length: length,
must_be_freed: false, must_be_freed: false,
}; };
concat_strings(&mut new, &static);
return new;
} }
pub fn add_char(string: &mut String, c: char) { pub fn add_char(string: &mut String, c: char) {
if ((*string).length + 1) >= (*string).max_length { if ((*string).length + 1) >= (*string).max_length {
let new = allocate((*string).max_length + 4) as *char; let new = allocate((*string).max_length + 4) as *char;
copy_bits((*string).inner, new, 0, (*string).max_length); copy_bits((*string).inner, new, (*string).max_length);
if (*string).must_be_freed == true { if (*string).must_be_freed == true {
free((*string).inner as *u8); free((*string).inner as *u8);
@ -63,7 +64,7 @@ pub fn add_char(string: &mut String, c: char) {
} }
(*string).inner[(*string).length] = c; (*string).inner[(*string).length] = c;
(((*string).inner) as *u8)[((*string).length + 1)] = 0; (((*string).inner) as *u8)[(*string).length + 1] = 0;
(*string).length = (*string).length + 1; (*string).length = (*string).length + 1;
} }
@ -77,23 +78,22 @@ pub fn free_string(string: &mut String) {
free((*string).inner as *u8); free((*string).inner as *u8);
} }
fn copy_bits(from: *char, to: *char, pos: u64, max: u64) -> u8 { fn copy_bits(from: *char, to: *char, length: u64) {
if (pos >= max) { for i in 0 .. length {
return 0; to[i] = from[i];
} }
to[pos] = from[pos];
return copy_bits(from, to, pos + 1, max);
} }
fn str_length(string: *char, position: u32) -> u32 { fn str_length(string: *char) -> u32 {
if (string[position] == '\0') { let mut pos = 0;
return 0; while ((string[pos] == '\0') == false) {
pos = pos + 1;
} }
return str_length(string, position + 1) + 1; return pos + 1;
} }
pub fn add_num_to_str(string: &mut String, num: u64) { pub fn add_num_to_str(string: &mut String, num: u64) {
if num > 10 { if num >= 10 {
add_num_to_str(string, num / 10) add_num_to_str(string, num / 10)
} }
let rem = num % 10; let rem = num % 10;
@ -125,4 +125,10 @@ pub fn abs(f: f32) -> f32 {
return f * (0.0 - 1.0); return f * (0.0 - 1.0);
} }
return f; return f;
} }
pub fn concat_strings(destination: &mut String, source: &String) {
for i in 0 .. (str_length((*source).inner) - 1) {
add_char(destination, (*source).inner[i]);
}
}

View File

@ -186,7 +186,7 @@ impl ast::Block {
counter_range.as_meta(module_id), counter_range.as_meta(module_id),
); );
let mut block = block.into_mir(module_id); let mut block = block.into_mir(module_id);
block.statements.insert(0, set_new); block.statements.push(set_new);
( (
StmtKind::While(WhileStatement { StmtKind::While(WhileStatement {
condition: mir::Expression( condition: mir::Expression(