mirror of
https://git.sfja.dk/Mikkel/slige.git
synced 2025-01-18 08:06:30 +00:00
advent of code example
This commit is contained in:
parent
783138c036
commit
9ef7ec9751
4
dev-env/Dockerfile
Normal file
4
dev-env/Dockerfile
Normal file
@ -0,0 +1,4 @@
|
||||
FROM archlinux
|
||||
RUN pacman -Syu git base-devel deno --noconfirm
|
||||
WORKDIR /workspace
|
||||
ENTRYPOINT ["/bin/bash"]
|
3
dev-env/run.sh
Normal file
3
dev-env/run.sh
Normal file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
docker build -t slige-dev-env dev-env
|
||||
docker run --name dev-env --rm -it --mount type=bind,source="$(pwd)"/,target=/workspace slige-dev-env
|
@ -76,7 +76,7 @@
|
||||
"operators": {
|
||||
"patterns": [
|
||||
{
|
||||
"match": "\\+|\\-|\\*|\\/|=|(+=)|(-=)|(==)|(!=)|<|>|(<=)|(>=)|\\.|:|(\\->)|(::)|(::<)",
|
||||
"match": "\\+|\\-|\\*|\\/|=|(\\+=)|(\\-=)|(==)|(!=)|<|>|(<=)|(>=)|\\.|:|(\\->)|(::)|(::<)",
|
||||
"name": "keyword.operator.slige"
|
||||
}
|
||||
]
|
||||
|
181
examples/advent_of_code/day1.slg
Normal file
181
examples/advent_of_code/day1.slg
Normal file
@ -0,0 +1,181 @@
|
||||
fn print(msg: string) #[builtin(Print)] {}
|
||||
fn println(msg: string) { print(msg + "\n") }
|
||||
|
||||
fn int_to_string(number: int) -> string #[builtin(IntToString)] {}
|
||||
|
||||
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 string_to_int(str: string) -> int #[builtin(StringToInt)] {}
|
||||
|
||||
fn string_array_new() -> [string] #[builtin(ArrayNew)] {}
|
||||
fn string_array_push(array: [string], value: string) #[builtin(ArrayPush)] {}
|
||||
fn string_array_length(array: [string]) -> int #[builtin(ArrayLength)] {}
|
||||
fn string_array_at(array: [string], index: int) -> string #[builtin(ArrayAt)] {}
|
||||
|
||||
fn int_array_new() -> [int] #[builtin(ArrayNew)] {}
|
||||
fn int_array_push(array: [int], value: int) #[builtin(ArrayPush)] {}
|
||||
fn int_array_length(array: [int]) -> int #[builtin(ArrayLength)] {}
|
||||
fn int_array_at(array: [int], index: int) -> int #[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 stdin() -> int { 0 }
|
||||
fn stdout() -> int { 1 }
|
||||
fn stderr() -> int { 2 }
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
fn read_text_file(filename: string) -> string {
|
||||
let file = file_open(filename, "r");
|
||||
let text = file_read_to_string(file);
|
||||
file_close(file);
|
||||
text
|
||||
}
|
||||
|
||||
fn input(prompt: string) -> string {
|
||||
print("> ");
|
||||
file_flush(stdout());
|
||||
file_read_line(stdin())
|
||||
}
|
||||
|
||||
fn char(ch: string) -> int {
|
||||
ch[0]
|
||||
}
|
||||
|
||||
fn abs(number: int) -> int {
|
||||
let result = number;
|
||||
if number < 0 {
|
||||
result = number - (number * 2);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn split(str: string, seperator: int) -> [string] {
|
||||
let result: [string] = string_array_new();
|
||||
|
||||
let i = 0;
|
||||
let current_str = "";
|
||||
loop {
|
||||
if i >= string_length(str) {
|
||||
break;
|
||||
}
|
||||
let char = str[i];
|
||||
if char == seperator {
|
||||
string_array_push(result, current_str);
|
||||
current_str = "";
|
||||
} else {
|
||||
current_str = string_push_char(current_str, char);
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
string_array_push(result, current_str);
|
||||
result
|
||||
}
|
||||
|
||||
fn slice(str: string, from: int, to: int) -> string {
|
||||
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
|
||||
}
|
||||
|
||||
fn array_clone(array: [int]) -> [int] {
|
||||
let len = int_array_length(array);
|
||||
let result = int_array_new();
|
||||
let i = 0;
|
||||
loop {
|
||||
if i >= len { break; }
|
||||
int_array_push(result, array[i]);
|
||||
i = 1 + 1;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn array_sort_mut(array: [int]) {
|
||||
let len = int_array_length(array);
|
||||
for (let i = 0; i < len - 1; i += 1) {
|
||||
let min_i = i;
|
||||
let min_val = array[i];
|
||||
for (let j = i + 1; j < len; j += 1) {
|
||||
if array[j] < min_val {
|
||||
min_i = j;
|
||||
min_val = array[j];
|
||||
}
|
||||
}
|
||||
|
||||
let tmp = array[i];
|
||||
array[i] = array[min_i];
|
||||
array[min_i] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
fn array_sort_mut_triangle(array: [int]) {
|
||||
let len = int_array_length(array);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn array_to_sorted(array: [int]) -> [int] {
|
||||
let cloned = array_clone(array);
|
||||
array_sort_mut(array);
|
||||
cloned
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let location_ids = read_text_file("examples/advent_of_code/day1.txt");
|
||||
let location_ids_split = split(location_ids, char("\n"));
|
||||
let i = 0;
|
||||
let left_ids: [int] = int_array_new();
|
||||
let right_ids: [int] = int_array_new();
|
||||
let location_len = string_array_length(location_ids_split);
|
||||
for (let i = 0; i < location_len; i += 1) {
|
||||
int_array_push(left_ids, string_to_int(slice(location_ids_split[i], 0, 5)));
|
||||
int_array_push(right_ids, string_to_int(slice(location_ids_split[i], 8, 13)));
|
||||
}
|
||||
array_sort_mut(left_ids);
|
||||
array_sort_mut(right_ids);
|
||||
let sum = 0;
|
||||
let left_ids_len = int_array_length(left_ids);
|
||||
for (let i = 0; i < left_ids_len; i += 1) {
|
||||
let difference = abs(left_ids[i] - right_ids[i]);
|
||||
sum += difference;
|
||||
}
|
||||
println(int_to_string(sum));
|
||||
}
|
1000
examples/advent_of_code/day1.txt
Normal file
1000
examples/advent_of_code/day1.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,190 +0,0 @@
|
||||
|
||||
|
||||
fn print(msg: string) #[builtin(Print)] {}
|
||||
fn println(msg: string) { print(msg + "\n") }
|
||||
|
||||
fn int_to_string(number: int) -> string #[builtin(IntToString)] {}
|
||||
|
||||
fn string_push_char(str: string, value: int) -> string #[builtin(StringPushChar)] {}
|
||||
fn string_length(str: string) -> int #[builtin(StringLength)] {}
|
||||
fn string_to_int(str: string) -> int #[builtin(StringToInt)] {}
|
||||
|
||||
fn array_new_string() -> [string] #[builtin(ArrayNew)] {}
|
||||
fn array_new_int() -> [int] #[builtin(ArrayNew)] {}
|
||||
fn array_push_string(array: [string], value: string) #[builtin(ArrayPush)] {}
|
||||
fn array_push_int(array: [int], value: int) #[builtin(ArrayPush)] {}
|
||||
fn array_length_string(array: [string]) -> int #[builtin(ArrayLength)] {}
|
||||
fn array_length_int(array: [int]) -> int #[builtin(ArrayLength)] {}
|
||||
|
||||
fn char(ch: string) -> int {
|
||||
ch[0]
|
||||
}
|
||||
|
||||
fn abs(number: int) -> int {
|
||||
let result = number;
|
||||
if number < 0 {
|
||||
result = number - (number * 2);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn split(str: string, seperator: int) -> [string] {
|
||||
let result: [string] = array_new_string();
|
||||
|
||||
let i = 0;
|
||||
let current_str = "";
|
||||
loop {
|
||||
if i >= string_length(str) {
|
||||
break;
|
||||
}
|
||||
let char = 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;
|
||||
}
|
||||
array_push_string(result, current_str);
|
||||
result
|
||||
}
|
||||
|
||||
fn slice(str: string, from: int, to: int) -> string {
|
||||
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
|
||||
}
|
||||
|
||||
// fn triangle_sort(array: [int]) -> [int] {
|
||||
// let result: [int] = array_new_int();
|
||||
// let i = 0;
|
||||
// loop {
|
||||
// if i >= array_length_int(array) {
|
||||
// break;
|
||||
// }
|
||||
// let j = i;
|
||||
// let current_lowest_int = array[0];
|
||||
// loop {
|
||||
// if j >= array_length_int(array) {
|
||||
// break;
|
||||
// }
|
||||
// let current_int = array[j];
|
||||
// if current_int < current_lowest_int {
|
||||
// current_lowest_int = current_int;
|
||||
// }
|
||||
// j = j + 1;
|
||||
// }
|
||||
// array_push_int(result, current_lowest_int);
|
||||
// i = i + 1;
|
||||
// }
|
||||
// result
|
||||
// }
|
||||
|
||||
fn array_clone(array: [int]) -> [int] {
|
||||
let len = array_length_int(array);
|
||||
let result = array_new_int();
|
||||
let i = 0;
|
||||
loop {
|
||||
if i >= len { break; }
|
||||
result[i] = array[i];
|
||||
i = 1 + 1;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn array_sort_mut(array: [int]) {
|
||||
let len = array_length_int(array);
|
||||
let i = 0;
|
||||
loop {
|
||||
if i >= len { break; }
|
||||
let j = i + 1;
|
||||
loop {
|
||||
if j >= len { break; }
|
||||
if array[j] < array[i] {
|
||||
let tmp = array[j];
|
||||
array[j] = array[i];
|
||||
array[i] = tmp;
|
||||
}
|
||||
j = j + 1;
|
||||
}
|
||||
i = 1 + 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn array_to_sorted(array: [int]) -> [int] {
|
||||
let cloned = array_clone(array);
|
||||
array_sort_mut(array);
|
||||
cloned
|
||||
}
|
||||
|
||||
fn location_ids() -> string {
|
||||
"49744 57964
|
||||
20738 85861
|
||||
20319 65072
|
||||
79568 74248
|
||||
78194 83454
|
||||
48701 94102
|
||||
69552 26808
|
||||
62781 67392
|
||||
85323 47428
|
||||
99344 72568
|
||||
27523 97243
|
||||
48039 36600
|
||||
91532 31571
|
||||
21306 31571
|
||||
52409 10805
|
||||
33901 31571
|
||||
80772 38756
|
||||
13849 54584
|
||||
72294 28326
|
||||
86065 65553
|
||||
93987 72533
|
||||
81640 39741
|
||||
25701 89912
|
||||
98611 57082
|
||||
80949 94974
|
||||
84717 61876
|
||||
31599 57082
|
||||
87119 65871
|
||||
56659 22897"
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let location_ids = split(location_ids(), char("\n"));
|
||||
let i = 0;
|
||||
let left_ids: [int] = array_new_int();
|
||||
let right_ids: [int] = array_new_int();
|
||||
loop {
|
||||
if i >= array_length_string(location_ids) {
|
||||
break;
|
||||
}
|
||||
array_push_int(left_ids, string_to_int(slice(location_ids[i], 0, 5)));
|
||||
array_push_int(right_ids, string_to_int(slice(location_ids[i], 8, 13)));
|
||||
i = i + 1;
|
||||
}
|
||||
|
||||
let sorted_left_ids: [int] = array_to_sorted(left_ids);
|
||||
// let sorted_right_ids: [int] = array_to_sorted(right_ids);
|
||||
// i = 0;
|
||||
// let sum = 0;
|
||||
// loop {
|
||||
// if i >= array_length_int(left_ids) {
|
||||
// break;
|
||||
// }
|
||||
// let difference = abs(sorted_left_ids[i] - sorted_right_ids[i]);
|
||||
// let sum = sum + difference;
|
||||
// i = i + 1;
|
||||
// }
|
||||
// println(int_to_string(sum))
|
||||
}
|
24
examples/array_set.slg
Normal file
24
examples/array_set.slg
Normal file
@ -0,0 +1,24 @@
|
||||
|
||||
fn print(msg: string) #[builtin(Print)] {}
|
||||
fn println(msg: string) { print(msg + "\n") }
|
||||
|
||||
fn int_to_string(number: int) -> string #[builtin(IntToString)] {}
|
||||
|
||||
fn string_push_char(str: string, value: int) -> string #[builtin(StringPushChar)] {}
|
||||
fn string_length(str: string) -> int #[builtin(StringLength)] {}
|
||||
fn string_to_int(str: string) -> int #[builtin(StringToInt)] {}
|
||||
|
||||
fn array_new_string() -> [string] #[builtin(ArrayNew)] {}
|
||||
fn array_new_int() -> [int] #[builtin(ArrayNew)] {}
|
||||
fn array_push_string(array: [string], value: string) #[builtin(ArrayPush)] {}
|
||||
fn array_push_int(array: [int], value: int) #[builtin(ArrayPush)] {}
|
||||
fn array_length_string(array: [string]) -> int #[builtin(ArrayLength)] {}
|
||||
fn array_length_int(array: [int]) -> int #[builtin(ArrayLength)] {}
|
||||
|
||||
|
||||
|
||||
fn main() {
|
||||
let array = array_new_int();
|
||||
array_push_int(array, 22);
|
||||
array[0] = 5;
|
||||
}
|
@ -4,6 +4,6 @@ WORKDIR /workdir
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN make
|
||||
RUN make RELEASE=1
|
||||
|
||||
ENTRYPOINT [ "./build/sliger", "run", "out.slgbc" ]
|
@ -10,6 +10,7 @@ ifeq ($(RELEASE),1)
|
||||
CXX_FLAGS = \
|
||||
-std=c++23 \
|
||||
-O3 \
|
||||
-static -static-libgcc -static-libstdc++ \
|
||||
-pedantic -pedantic-errors \
|
||||
-Wall -Wextra -Wpedantic -Wconversion -Werror\
|
||||
|
||||
|
@ -399,8 +399,8 @@ void VM::run_array_builtin(Builtin builtin_id)
|
||||
auto index = stack_pop().as_int().value;
|
||||
auto array_ptr = stack_pop().as_ptr().value;
|
||||
auto value = stack_pop();
|
||||
auto array = this->heap.at(array_ptr).val()->as_array();
|
||||
array.at(index) = value;
|
||||
|
||||
this->heap.at(array_ptr).val()->as_array().at(index) = value;
|
||||
stack_push(Null());
|
||||
break;
|
||||
}
|
||||
@ -451,8 +451,8 @@ void VM::run_file_builtin(Builtin builtin_id)
|
||||
auto filename = stack_pop().as_string().value;
|
||||
FILE* fp = std::fopen(filename.c_str(), mode.c_str());
|
||||
if (fp == nullptr) {
|
||||
stack_push(Int(0));
|
||||
break;
|
||||
std::cerr << std::format("error: could not open file '{}'\n", filename);
|
||||
std::exit(1);
|
||||
}
|
||||
auto file_id = this->file_id_counter;
|
||||
this->file_id_counter += 1;
|
||||
@ -510,12 +510,13 @@ void VM::run_file_builtin(Builtin builtin_id)
|
||||
}
|
||||
auto content = std::string();
|
||||
while (true) {
|
||||
constexpr size_t buf_size = 128;
|
||||
constexpr size_t buf_size = 129;
|
||||
char buf[buf_size] = "";
|
||||
auto res = std::fread(buf, 1, buf_size, fp->second);
|
||||
auto res = std::fread(buf, 1, buf_size - 1, fp->second);
|
||||
if (res == 0) {
|
||||
break;
|
||||
}
|
||||
buf[res] = '\0';
|
||||
content.append(std::string(buf));
|
||||
}
|
||||
stack_push(String(std::move(content)));
|
||||
|
94
stdlib.slg
94
stdlib.slg
@ -1,7 +1,12 @@
|
||||
fn print(msg: string) #[builtin(Print)] {}
|
||||
fn println(msg: string) { print(msg + "\n") }
|
||||
|
||||
fn int_to_string(number: int) -> string #[builtin(IntToString)] {}
|
||||
|
||||
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 string_to_int(str: string) -> int #[builtin(StringToInt)] {}
|
||||
|
||||
fn string_array_new() -> [string] #[builtin(ArrayNew)] {}
|
||||
fn string_array_push(array: [string], value: string) #[builtin(ArrayPush)] {}
|
||||
@ -21,6 +26,7 @@ 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 stdin() -> int { 0 }
|
||||
fn stdout() -> int { 1 }
|
||||
fn stderr() -> int { 2 }
|
||||
@ -40,8 +46,12 @@ fn file_read_line(file: int) -> string {
|
||||
line
|
||||
}
|
||||
|
||||
fn print(msg: string) #[builtin(Print)] {}
|
||||
fn println(msg: string) { print(msg + "\n") }
|
||||
fn read_text_file(filename: string) -> string {
|
||||
let file = file_open(filename, "r");
|
||||
let text = file_read_to_string(file);
|
||||
file_close(file);
|
||||
text
|
||||
}
|
||||
|
||||
fn input(prompt: string) -> string {
|
||||
print("> ");
|
||||
@ -49,3 +59,83 @@ fn input(prompt: string) -> string {
|
||||
file_read_line(stdin())
|
||||
}
|
||||
|
||||
fn char(ch: string) -> int {
|
||||
ch[0]
|
||||
}
|
||||
|
||||
fn abs(number: int) -> int {
|
||||
let result = number;
|
||||
if number < 0 {
|
||||
result = number - (number * 2);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn split(str: string, seperator: int) -> [string] {
|
||||
let result: [string] = string_array_new();
|
||||
|
||||
let i = 0;
|
||||
let current_str = "";
|
||||
loop {
|
||||
if i >= string_length(str) {
|
||||
break;
|
||||
}
|
||||
let char = str[i];
|
||||
if char == seperator {
|
||||
string_array_push(result, current_str);
|
||||
current_str = "";
|
||||
} else {
|
||||
current_str = string_push_char(current_str, char);
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
string_array_push(result, current_str);
|
||||
result
|
||||
}
|
||||
|
||||
fn slice(str: string, from: int, to: int) -> string {
|
||||
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
|
||||
}
|
||||
|
||||
fn array_clone(array: [int]) -> [int] {
|
||||
let len = int_array_length(array);
|
||||
let result = int_array_new();
|
||||
let i = 0;
|
||||
loop {
|
||||
if i >= len { break; }
|
||||
int_array_push(result, array[i]);
|
||||
i = 1 + 1;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn array_sort_mut(array: [int]) {
|
||||
let len = int_array_length(array);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn array_to_sorted(array: [int]) -> [int] {
|
||||
let cloned = array_clone(array);
|
||||
array_sort_mut(array);
|
||||
cloned
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user