slige/examples/std.slg

61 lines
1.9 KiB
Plaintext
Raw Permalink Normal View History

2024-12-12 16:07:59 +01:00
fn print(msg: string) #[builtin(Print)] {}
2024-12-13 06:09:10 +01:00
fn println(msg: string) { print(msg + "\n") }
2024-12-12 16:07:59 +01:00
2024-12-13 06:09:10 +01:00
fn string_push_char(str: string, value: int) -> string #[builtin(StringPushChar)] {}
2024-12-12 16:07:59 +01:00
fn string_char_at(str: string, index: int) -> int #[builtin(StringCharAt)] {}
fn string_length(str: string) -> int #[builtin(StringLength)] {}
2024-12-13 06:09:10 +01:00
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)] {}
2024-12-12 16:07:59 +01:00
2024-12-13 20:05:27 +01:00
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)] {}
2024-12-13 20:29:06 +01:00
fn file_read_char(file: int) -> int #[builtin(FileReadChar)] {}
2024-12-13 20:05:27 +01:00
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)] {}
2024-12-12 16:07:59 +01:00
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 = "";
2024-12-13 09:55:09 +01:00
let str_length = string_length(str);
2024-12-12 16:07:59 +01:00
loop {
2024-12-13 09:55:09 +01:00
if i >= str_length {
2024-12-13 06:09:10 +01:00
break;
}
2024-12-12 16:07:59 +01:00
let char = string_char_at(str, i);
if char == seperator {
array_push_string(result, current_str);
current_str = "";
} else {
2024-12-13 09:55:09 +01:00
current_str = string_push_char(current_str, char);
2024-12-12 16:07:59 +01:00
}
i = i + 1;
}
result
}
fn main() {
2024-12-13 09:55:09 +01:00
let array = split("yaoisfjasoifjsaiofjsa", char("a"));
2024-12-12 16:07:59 +01:00
let i = 0;
2024-12-13 06:09:10 +01:00
let array_length = array_length_string(array);
2024-12-12 16:07:59 +01:00
loop {
2024-12-13 06:09:10 +01:00
if i >= array_length {
2024-12-12 16:07:59 +01:00
break;
}
2024-12-13 09:55:09 +01:00
println(array_at_string(array, i));
2024-12-13 06:09:10 +01:00
i = i + 1;
2024-12-12 16:07:59 +01:00
}
}