pub stdlib

This commit is contained in:
sfja 2024-12-31 00:24:09 +01:00
parent 170e153947
commit e56725dd4f

View File

@ -2,59 +2,59 @@
// stdlib.slg // stdlib.slg
#[builtin(Exit)] #[builtin(Exit)]
fn exit(status_code: int) {} pub fn exit(status_code: int) {}
#[builtin(Print)] #[builtin(Print)]
fn print(msg: string) {} pub fn print(msg: string) {}
msg + "\n" msg + "\n"
fn println(msg: string) { print) } pub fn println(msg: string) { print) }
#[builtin(IntToString)] #[builtin(IntToString)]
fn int_to_string(number: int) -> string {} pub fn int_to_string(number: int) -> string {}
#[builtin(StringPushChar)] #[builtin(StringPushChar)]
fn string_push_char(str: string, value: int) -> string {} pub fn string_push_char(str: string, value: int) -> string {}
#[builtin(StringCharAt)] #[builtin(StringCharAt)]
fn string_char_at(str: string, index: int) -> int {} pub fn string_char_at(str: string, index: int) -> int {}
#[builtin(StringLength)] #[builtin(StringLength)]
fn string_length(str: string) -> int {} pub fn string_length(str: string) -> int {}
#[builtin(StringToInt)] #[builtin(StringToInt)]
fn string_to_int(str: string) -> int {} pub fn string_to_int(str: string) -> int {}
#[builtin(ArrayNew)] #[builtin(ArrayNew)]
fn array_new<T>() -> [T] {} pub fn array_new<T>() -> [T] {}
#[builtin(ArrayPush)] #[builtin(ArrayPush)]
fn array_push<T>(array: [T], value: T) {} pub fn array_push<T>(array: [T], value: T) {}
#[builtin(ArrayLength)] #[builtin(ArrayLength)]
fn array_length<T>(array: [T]) -> int {} pub fn array_length<T>(array: [T]) -> int {}
#[builtin(ArrayAt)] #[builtin(ArrayAt)]
fn array_at<T>(array: [T], index: int) -> T {} pub fn array_at<T>(array: [T], index: int) -> T {}
#[builtin(FileOpen)] #[builtin(FileOpen)]
fn file_open(filename: string, mode: string) -> int {} pub fn file_open(filename: string, mode: string) -> int {}
#[builtin(FileClose)] #[builtin(FileClose)]
fn file_close(file: int) {} pub fn file_close(file: int) {}
#[builtin(FileWriteString)] #[builtin(FileWriteString)]
fn file_write_string(file: int, content: string) -> int {} pub fn file_write_string(file: int, content: string) -> int {}
#[builtin(FileReadChar)] #[builtin(FileReadChar)]
fn file_read_char(file: int) -> int {} pub fn file_read_char(file: int) -> int {}
#[builtin(FileReadToString)] #[builtin(FileReadToString)]
fn file_read_to_string(file: int) -> string {} pub fn file_read_to_string(file: int) -> string {}
#[builtin(FileFlush)] #[builtin(FileFlush)]
fn file_flush(file: int) {} pub fn file_flush(file: int) {}
#[builtin(FileEof)] #[builtin(FileEof)]
fn file_eof(file: int) -> bool {} pub fn file_eof(file: int) -> bool {}
#[builtin(IntToString)] #[builtin(IntToString)]
fn itos(number: int) -> string {} pub fn itos(number: int) -> string {}
#[builtin(StringToInt)] #[builtin(StringToInt)]
fn stoi(str: string) -> int {} pub fn stoi(str: string) -> int {}
fn stdin() -> int { 0 } pub fn stdin() -> int { 0 }
fn stdout() -> int { 1 } pub fn stdout() -> int { 1 }
fn stderr() -> int { 2 } pub fn stderr() -> int { 2 }
fn file_read_line(file: int) -> string { pub fn file_read_line(file: int) -> string {
let line = ""; let line = "";
loop { loop {
if file_eof(file) { if file_eof(file) {
@ -69,20 +69,20 @@ fn file_read_line(file: int) -> string {
line line
} }
fn read_text_file(filename: string) -> string { pub fn read_text_file(filename: string) -> string {
let file = file_open(filename, "r"); let file = file_open(filename, "r");
let text = file_read_to_string(file); let text = file_read_to_string(file);
file_close(file); file_close(file);
text text
} }
fn input(prompt: string) -> string { pub fn input(prompt: string) -> string {
print("> "); print("> ");
file_flush(stdout()); file_flush(stdout());
file_read_line(stdin()) file_read_line(stdin())
} }
fn string_abs(number: int) -> int { pub fn string_abs(number: int) -> int {
let result = number; let result = number;
if number < 0 { if number < 0 {
result = number - (number * 2); result = number - (number * 2);
@ -90,7 +90,7 @@ fn string_abs(number: int) -> int {
result result
} }
fn string_split(str: string, seperator: int) -> [string] { pub fn string_split(str: string, seperator: int) -> [string] {
let result = array_new::<string>(); let result = array_new::<string>();
let i = 0; let i = 0;
@ -112,7 +112,7 @@ fn string_split(str: string, seperator: int) -> [string] {
result result
} }
fn string_slice(str: string, from: int, to: int) -> string { pub fn string_slice(str: string, from: int, to: int) -> string {
let result = ""; let result = "";
let i = from; let i = from;
loop { loop {
@ -128,7 +128,7 @@ fn string_slice(str: string, from: int, to: int) -> string {
result result
} }
fn string_contains(str: string, ch: int) -> bool { pub fn string_contains(str: string, ch: int) -> bool {
let len = string_length(str); let len = string_length(str);
for (let i = 0; i < len; i += 1) { for (let i = 0; i < len; i += 1) {
if str[i] == ch { if str[i] == ch {
@ -138,7 +138,7 @@ fn string_contains(str: string, ch: int) -> bool {
false false
} }
fn array_clone<T>(array: [T]) -> [T] { pub fn array_clone<T>(array: [T]) -> [T] {
let len = array_length(array); let len = array_length(array);
let result = array_new::<T>(); let result = array_new::<T>();
let i = 0; let i = 0;
@ -150,7 +150,7 @@ fn array_clone<T>(array: [T]) -> [T] {
result result
} }
fn array_sort_mut(array: [int]) { pub fn array_sort_mut(array: [int]) {
let len = array_length(array); let len = array_length(array);
for (let i = 0; i < len; i += 1) { for (let i = 0; i < len; i += 1) {
for (let j = i + 1; j < len; j += 1) { for (let j = i + 1; j < len; j += 1) {
@ -163,7 +163,7 @@ fn array_sort_mut(array: [int]) {
} }
} }
fn array_to_sorted(array: [int]) -> [int] { pub fn array_to_sorted(array: [int]) -> [int] {
let cloned = array_clone(array); let cloned = array_clone(array);
array_sort_mut(array); array_sort_mut(array);
cloned cloned