slige/std/lib.slg

171 lines
3.9 KiB
Plaintext
Raw Normal View History

// stdlib.slg
2024-12-31 00:16:10 +01:00
#[builtin(Exit)]
2024-12-31 00:24:09 +01:00
pub fn exit(status_code: int) {}
2024-12-26 01:51:05 +01:00
2024-12-31 00:16:10 +01:00
#[builtin(Print)]
2024-12-31 00:24:09 +01:00
pub fn print(msg: string) {}
2024-12-31 00:54:58 +01:00
pub fn println(msg: string) { print(msg + "\n") }
2024-12-16 14:37:24 +01:00
2024-12-31 00:16:10 +01:00
#[builtin(IntToString)]
2024-12-31 00:24:09 +01:00
pub fn int_to_string(number: int) -> string {}
2024-12-31 00:16:10 +01:00
#[builtin(StringPushChar)]
2024-12-31 00:24:09 +01:00
pub fn string_push_char(str: string, value: int) -> string {}
2024-12-31 00:16:10 +01:00
#[builtin(StringCharAt)]
2024-12-31 00:24:09 +01:00
pub fn string_char_at(str: string, index: int) -> int {}
2024-12-31 00:16:10 +01:00
#[builtin(StringLength)]
2024-12-31 00:24:09 +01:00
pub fn string_length(str: string) -> int {}
2024-12-31 00:16:10 +01:00
#[builtin(StringToInt)]
2024-12-31 00:24:09 +01:00
pub fn string_to_int(str: string) -> int {}
2024-12-31 00:16:10 +01:00
#[builtin(ArrayNew)]
2024-12-31 00:24:09 +01:00
pub fn array_new<T>() -> [T] {}
2024-12-31 00:16:10 +01:00
#[builtin(ArrayPush)]
2024-12-31 00:24:09 +01:00
pub fn array_push<T>(array: [T], value: T) {}
2024-12-31 00:16:10 +01:00
#[builtin(ArrayLength)]
2024-12-31 00:24:09 +01:00
pub fn array_length<T>(array: [T]) -> int {}
2024-12-31 00:16:10 +01:00
#[builtin(ArrayAt)]
2024-12-31 00:24:09 +01:00
pub fn array_at<T>(array: [T], index: int) -> T {}
2024-12-31 00:16:10 +01:00
#[builtin(FileOpen)]
2024-12-31 00:24:09 +01:00
pub fn file_open(filename: string, mode: string) -> int {}
2024-12-31 00:16:10 +01:00
#[builtin(FileClose)]
2024-12-31 00:24:09 +01:00
pub fn file_close(file: int) {}
2024-12-31 00:16:10 +01:00
#[builtin(FileWriteString)]
2024-12-31 00:24:09 +01:00
pub fn file_write_string(file: int, content: string) -> int {}
2024-12-31 00:16:10 +01:00
#[builtin(FileReadChar)]
2024-12-31 00:24:09 +01:00
pub fn file_read_char(file: int) -> int {}
2024-12-31 00:16:10 +01:00
#[builtin(FileReadToString)]
2024-12-31 00:24:09 +01:00
pub fn file_read_to_string(file: int) -> string {}
2024-12-31 00:16:10 +01:00
#[builtin(FileFlush)]
2024-12-31 00:24:09 +01:00
pub fn file_flush(file: int) {}
2024-12-31 00:16:10 +01:00
#[builtin(FileEof)]
2024-12-31 00:24:09 +01:00
pub fn file_eof(file: int) -> bool {}
2024-12-31 00:16:10 +01:00
#[builtin(IntToString)]
2024-12-31 00:24:09 +01:00
pub fn itos(number: int) -> string {}
2024-12-31 00:16:10 +01:00
#[builtin(StringToInt)]
2024-12-31 00:24:09 +01:00
pub fn stoi(str: string) -> int {}
2024-12-16 14:37:24 +01:00
2024-12-31 00:24:09 +01:00
pub fn stdin() -> int { 0 }
pub fn stdout() -> int { 1 }
pub fn stderr() -> int { 2 }
2024-12-31 00:24:09 +01:00
pub fn file_read_line(file: int) -> string {
let line = "";
loop {
if file_eof(file) {
break;
}
let ch = file_read_char(file);
if ch == "\n"[0] {
break;
}
line = string_push_char(line, ch);
}
line
}
2024-12-31 00:24:09 +01:00
pub fn read_text_file(filename: string) -> string {
2024-12-16 14:37:24 +01:00
let file = file_open(filename, "r");
let text = file_read_to_string(file);
file_close(file);
text
}
2024-12-31 00:24:09 +01:00
pub fn input(prompt: string) -> string {
print("> ");
file_flush(stdout());
file_read_line(stdin())
}
2024-12-31 00:24:09 +01:00
pub fn string_abs(number: int) -> int {
2024-12-16 14:37:24 +01:00
let result = number;
if number < 0 {
result = number - (number * 2);
}
result
}
2024-12-31 00:24:09 +01:00
pub fn string_split(str: string, seperator: int) -> [string] {
let result = array_new::<string>();
2024-12-16 14:37:24 +01:00
let i = 0;
let current_str = "";
loop {
if i >= string_length(str) {
break;
}
let char = str[i];
if char == seperator {
array_push(result, current_str);
2024-12-16 14:37:24 +01:00
current_str = "";
} else {
current_str = string_push_char(current_str, char);
}
i = i + 1;
}
array_push(result, current_str);
2024-12-16 14:37:24 +01:00
result
}
2024-12-31 00:24:09 +01:00
pub fn string_slice(str: string, from: int, to: int) -> string {
2024-12-16 14:37:24 +01:00
let result = "";
let i = from;
loop {
if i >= string_length(str) {
break;
}
if i >= to {
break;
}
result = string_push_char(result, str[i]);
i = i + 1;
}
result
}
2024-12-31 00:24:09 +01:00
pub fn string_contains(str: string, ch: int) -> bool {
let len = string_length(str);
for (let i = 0; i < len; i += 1) {
if str[i] == ch {
return true;
}
}
false
}
2024-12-31 00:24:09 +01:00
pub fn array_clone<T>(array: [T]) -> [T] {
let len = array_length(array);
let result = array_new::<T>();
2024-12-16 14:37:24 +01:00
let i = 0;
loop {
if i >= len { break; }
array_push(result, array[i]);
2024-12-16 14:37:24 +01:00
i = 1 + 1;
}
result
}
2024-12-31 00:24:09 +01:00
pub fn array_sort_mut(array: [int]) {
let len = array_length(array);
2024-12-16 14:37:24 +01:00
for (let i = 0; i < len; i += 1) {
for (let j = i + 1; j < len; j += 1) {
if array[j] < array[i] {
let tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}
}
}
}
2024-12-31 00:24:09 +01:00
pub fn array_to_sorted(array: [int]) -> [int] {
2024-12-16 14:37:24 +01:00
let cloned = array_clone(array);
array_sort_mut(array);
cloned
}