Compare commits
4 Commits
5831b06af5
...
de803e9024
Author | SHA1 | Date | |
---|---|---|---|
de803e9024 | |||
d96fc51b9c | |||
0d3abe8e42 | |||
14537743ed |
@ -3,14 +3,20 @@ 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 world");
|
||||
let mut test = from_str("hello");
|
||||
let mut other = from_str(" world");
|
||||
|
||||
concat_strings(&mut test, &other);
|
||||
|
||||
add_char(&mut test, '!');
|
||||
set_char(&mut test, 'B', 0);
|
||||
add_num_to_str(&mut test, 1234);
|
||||
add_char(&mut test, '\n');
|
||||
|
||||
add_num_to_str(&mut test, 175);
|
||||
|
||||
print(&test);
|
||||
|
||||
|
@ -2,11 +2,15 @@
|
||||
|
||||
import std::print;
|
||||
import std::from_str;
|
||||
import std::add_num_to_str;
|
||||
import std::free_string;
|
||||
|
||||
fn main() -> u32 {
|
||||
let text = from_str("hello");
|
||||
for i in 0 .. 10 {
|
||||
print(&text)
|
||||
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 mut num = 0;
|
||||
|
@ -38,21 +38,22 @@ pub fn new_string() -> String {
|
||||
}
|
||||
|
||||
pub fn from_str(str: *char) -> String {
|
||||
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,
|
||||
let length = str_length(str);
|
||||
let mut new = new_string();
|
||||
let static = String {
|
||||
inner: str,
|
||||
length: length - 1,
|
||||
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, 0, (*string).max_length);
|
||||
copy_bits((*string).inner, new, (*string).max_length);
|
||||
|
||||
if (*string).must_be_freed == true {
|
||||
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) as *u8)[((*string).length + 1)] = 0;
|
||||
(((*string).inner) as *u8)[(*string).length + 1] = 0;
|
||||
(*string).length = (*string).length + 1;
|
||||
}
|
||||
|
||||
@ -77,23 +78,22 @@ 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;
|
||||
fn copy_bits(from: *char, to: *char, length: u64) {
|
||||
for i in 0 .. length {
|
||||
to[i] = from[i];
|
||||
}
|
||||
to[pos] = from[pos];
|
||||
return copy_bits(from, to, pos + 1, max);
|
||||
}
|
||||
|
||||
fn str_length(string: *char, position: u32) -> u32 {
|
||||
if (string[position] == '\0') {
|
||||
return 0;
|
||||
fn str_length(string: *char) -> u32 {
|
||||
let mut pos = 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) {
|
||||
if num > 10 {
|
||||
if num >= 10 {
|
||||
add_num_to_str(string, num / 10)
|
||||
}
|
||||
let rem = num % 10;
|
||||
@ -125,4 +125,10 @@ 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]);
|
||||
}
|
||||
}
|
||||
|
@ -186,7 +186,7 @@ impl ast::Block {
|
||||
counter_range.as_meta(module_id),
|
||||
);
|
||||
let mut block = block.into_mir(module_id);
|
||||
block.statements.insert(0, set_new);
|
||||
block.statements.push(set_new);
|
||||
(
|
||||
StmtKind::While(WhileStatement {
|
||||
condition: mir::Expression(
|
||||
|
Loading…
Reference in New Issue
Block a user