

fn print(msg: string) #[builtin(Print)] {}
fn println(msg: string) { print(msg + "\n") }

fn string_push_char(str: string, value: int) -> string #[builtin(StringPushChar)] {}
fn string_char_at(str: string, index: int) -> int #[builtin(StringCharAt)] {}
fn string_length(str: string) -> int #[builtin(StringLength)] {}

fn array_new_string() -> [string] #[builtin(ArrayNew)] {}
fn array_push_string(array: [string], value: string) #[builtin(ArrayPush)] {}
fn array_length_string(array: [string]) -> int #[builtin(ArrayLength)] {}
fn array_at_string(array: [string], index: int) -> string #[builtin(ArrayAt)] {}

fn file_open(filename: string, mode: string) -> int #[builtin(FileOpen)] {}
fn file_close(file: int) #[builtin(FileClose)] {}
fn file_write_string(file: int, content: string) -> int #[builtin(FileWriteString)] {}
fn file_read_char(file: int) -> int #[builtin(FileReadChar)] {}
fn file_read_to_string(file: int) -> string #[builtin(FileReadToString)] {}
fn file_flush(file: int) #[builtin(FileFlush)] {}
fn file_eof(file: int) -> bool #[builtin(FileEof)] {}

fn char(ch: string) -> int {
    string_char_at(ch, 0)
}

fn split(str: string, seperator: int) -> [string] {
    let result: [string] = array_new_string();

    let i = 0;
    let current_str = "";
    let str_length = string_length(str);
    loop {
        if i >= str_length {
            break;
        }
        let char = string_char_at(str, i);
        if char == seperator {
            array_push_string(result, current_str);
            current_str = "";
        } else {
            current_str = string_push_char(current_str, char);
        }
        i = i + 1;
    }
    result
}

fn main() {
    let array = split("yaoisfjasoifjsaiofjsa", char("a"));
    let i = 0;
    let array_length = array_length_string(array);
    loop {
        if i >= array_length {
            break;
        }
        println(array_at_string(array, i));
        i = i + 1;
    }
}
