Compare commits

..

No commits in common. "de803e90246a28369b371c00dd7768c170869b30" and "5831b06af544702b8fe0e89d41d9e17dcae289d5" have entirely different histories.

4 changed files with 26 additions and 42 deletions

View File

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

View File

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

View File

@ -38,22 +38,21 @@ pub fn new_string() -> String {
}
pub fn from_str(str: *char) -> String {
let length = str_length(str);
let mut new = new_string();
let static = String {
inner: str,
length: length - 1,
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,
must_be_freed: false,
};
concat_strings(&mut new, &static);
return new;
}
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;
copy_bits((*string).inner, new, (*string).max_length);
copy_bits((*string).inner, new, 0, (*string).max_length);
if (*string).must_be_freed == true {
free((*string).inner as *u8);
@ -64,7 +63,7 @@ pub fn add_char(string: &mut String, c: char) {
}
(*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;
}
@ -78,22 +77,23 @@ pub fn free_string(string: &mut String) {
free((*string).inner as *u8);
}
fn copy_bits(from: *char, to: *char, length: u64) {
for i in 0 .. length {
to[i] = from[i];
fn copy_bits(from: *char, to: *char, pos: u64, max: u64) -> u8 {
if (pos >= max) {
return 0;
}
to[pos] = from[pos];
return copy_bits(from, to, pos + 1, max);
}
fn str_length(string: *char) -> u32 {
let mut pos = 0;
while ((string[pos] == '\0') == false) {
pos = pos + 1;
fn str_length(string: *char, position: u32) -> u32 {
if (string[position] == '\0') {
return 0;
}
return pos + 1;
return str_length(string, position + 1) + 1;
}
pub fn add_num_to_str(string: &mut String, num: u64) {
if num >= 10 {
if num > 10 {
add_num_to_str(string, num / 10)
}
let rem = num % 10;
@ -125,10 +125,4 @@ pub fn abs(f: f32) -> f32 {
return f * (0.0 - 1.0);
}
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),
);
let mut block = block.into_mir(module_id);
block.statements.push(set_new);
block.statements.insert(0, set_new);
(
StmtKind::While(WhileStatement {
condition: mir::Expression(