From bf647e430e3a3642c8199dc81a411d5e1586cabb Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Sun, 25 Jun 2023 18:55:58 -0500 Subject: [PATCH 1/7] Standard library beginnings --- examples/mandelbrot.sloth | 22 ++++++------- std/stdio.c | 12 +++++++ std/stdio.sloth | 9 +++++ std/stdlib.c | 5 +++ std/stdlib.sloth | 14 ++++++++ std/stdmath.sloth | 69 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 std/stdio.c create mode 100644 std/stdio.sloth create mode 100644 std/stdlib.c create mode 100644 std/stdlib.sloth create mode 100644 std/stdmath.sloth diff --git a/examples/mandelbrot.sloth b/examples/mandelbrot.sloth index bc95e2f..fb22b88 100644 --- a/examples/mandelbrot.sloth +++ b/examples/mandelbrot.sloth @@ -1,23 +1,23 @@ -val size: int = 200; -val maxVal: float = 4.0; -val maxIter: int = 50; -val plane: float = 4.0; +val size: Int = 200; +val maxVal: Float = 4.0; +val maxIter: Int = 50; +val plane: Float = 4.0; for x in 0 .. size { for y in 0 .. size { - var cReal: float = (x * plane / size) - 2; - var cImg: float = (y * plane / size) - 2; - var zReal: float = 0; - var zImg: float = 0; - var count: float = 0; + var cReal: Float = (x * plane / size) - 2; + var cImg: Float = (y * plane / size) - 2; + var zReal: Float = 0; + var zImg: Float = 0; + var count: Float = 0; while (zReal * zReal + zImg * zImg) <= maxVal && count < 4{ - var temp: float = (zReal * zReal) - (zImg * zImg) + cReal; + var temp: Float = (zReal * zReal) - (zImg * zImg) + cReal; zImg = 2 * zReal * zImg + cImg; zReal = temp; count += 1; } if count == maxIter { - term_setpos(x, y); + termpos(x, y); print("*"); } } diff --git a/std/stdio.c b/std/stdio.c new file mode 100644 index 0000000..d1bc69a --- /dev/null +++ b/std/stdio.c @@ -0,0 +1,12 @@ +#include +#include + +char* readln() { + char* str = malloc(128); + scanf("%127s", str); + return str; +} + +void print(char *str) { + puts(str); +} diff --git a/std/stdio.sloth b/std/stdio.sloth new file mode 100644 index 0000000..c28d474 --- /dev/null +++ b/std/stdio.sloth @@ -0,0 +1,9 @@ +foreign fn print(str: String) Void; +foreign fn readln() String; + +fn println(str: String) Void { + print(str); + print("\n"); +} + + diff --git a/std/stdlib.c b/std/stdlib.c new file mode 100644 index 0000000..b50c0c5 --- /dev/null +++ b/std/stdlib.c @@ -0,0 +1,5 @@ +#include + +void wait(long long x) { + sleep(x); +} diff --git a/std/stdlib.sloth b/std/stdlib.sloth new file mode 100644 index 0000000..7b6e2f9 --- /dev/null +++ b/std/stdlib.sloth @@ -0,0 +1,14 @@ +foreign fn wait(x: Int) Void; +foreign fn print(str: String) Void; + +fn termpos(x: int, y: int) Void { + print("\x1b["); + print(x); + print(";"); + print(y); + print("H"); +} + +fn termclear() Void { + print("\x1b[2J\x1b[H"); +} diff --git a/std/stdmath.sloth b/std/stdmath.sloth new file mode 100644 index 0000000..ebf9a7c --- /dev/null +++ b/std/stdmath.sloth @@ -0,0 +1,69 @@ +foreign fn rand() Int; + +fn abs(x: Int) Int { + if x < 0 { + return -x; + } + return x; +} + +fn fabs(x: Float) Float { + if x < 0 { + return -x; + } + return x; +} + +fn max(x: Int, y: Int) Int { + if x > y { + return x; + } + return y; +} + +fn min(x: Int, y: Int) Int { + if x < y { + return x; + } + return y; +} + +fn fmax(x: Float, y: Float) Float { + if x > y { + return x; + } + return y; +} + +fn fmin(x: Float, y: Float) Float { + if x < y { + return x; + } + return y; +} + +fn pow(x: Int, y: Int) Int { + while y > 1 { + x = x*x; + y = y-1; + } + return x; +} + +fn floor(x: Float) Float { + return x - abs(x % 1.0); +} + +fn ceil(x: Float) Float { + if x < 0.0 { + return floor(x) - 1.0; + } + return floor(x) + 1.0; +} + +fn round(x: Float) Float { + if fabs(x % 1.0) >= 0.5 { + return ceil(x); + } + return floor(x); +} From 28ab9c8ba094d86dbcb9abb853dddd83dca028cc Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Sun, 25 Jun 2023 19:41:03 -0500 Subject: [PATCH 2/7] Standard library updated and llvm-sys added to cargo.toml --- Cargo.lock | 1 + examples/mergesort.sloth | 4 ++++ sloth/Cargo.toml | 1 + std/stdlib.c | 8 ++++++++ std/stdlib.sloth | 2 ++ std/stdmath.c | 11 +++++++++++ std/stdmath.sloth | 2 +- 7 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 examples/mergesort.sloth create mode 100644 std/stdmath.c diff --git a/Cargo.lock b/Cargo.lock index 1d886a8..8652bed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -254,6 +254,7 @@ version = "0.1.0" dependencies = [ "inkwell", "itertools", + "llvm-sys", "rand", "thiserror", ] diff --git a/examples/mergesort.sloth b/examples/mergesort.sloth new file mode 100644 index 0000000..f1e5901 --- /dev/null +++ b/examples/mergesort.sloth @@ -0,0 +1,4 @@ +fn merge_sort(list: List) { + print(list); + +} diff --git a/sloth/Cargo.toml b/sloth/Cargo.toml index 1146e8e..f58f40e 100644 --- a/sloth/Cargo.toml +++ b/sloth/Cargo.toml @@ -6,6 +6,7 @@ version.workspace = true edition.workspace = true [dependencies] +llvm-sys = "150" inkwell = { version = "0.2.0", features = ["llvm15-0"] } itertools = "0.10.5" rand = "0.8.5" diff --git a/std/stdlib.c b/std/stdlib.c index b50c0c5..f8df9e8 100644 --- a/std/stdlib.c +++ b/std/stdlib.c @@ -3,3 +3,11 @@ void wait(long long x) { sleep(x); } + +int len(char *str) { + return strlen(str); +} + +char charAt(char *str, int) { + return str[int]; +} diff --git a/std/stdlib.sloth b/std/stdlib.sloth index 7b6e2f9..36f3879 100644 --- a/std/stdlib.sloth +++ b/std/stdlib.sloth @@ -1,5 +1,7 @@ foreign fn wait(x: Int) Void; foreign fn print(str: String) Void; +foreign fn len(str: String) Int; +foreign fn charAt(str: String) Char; fn termpos(x: int, y: int) Void { print("\x1b["); diff --git a/std/stdmath.c b/std/stdmath.c new file mode 100644 index 0000000..c9a91c3 --- /dev/null +++ b/std/stdmath.c @@ -0,0 +1,11 @@ +#include +#include +#include + +int randGen(int min, int max) { + time_t t; + + srand((unsigned) time(&t)); + + return rand() % (max - min + 1) + min; +} diff --git a/std/stdmath.sloth b/std/stdmath.sloth index ebf9a7c..0f967eb 100644 --- a/std/stdmath.sloth +++ b/std/stdmath.sloth @@ -1,4 +1,4 @@ -foreign fn rand() Int; +foreign fn randGen(min: Int, max: Int) Int; fn abs(x: Int) Int { if x < 0 { From 0de05c7033940966eac811557c3eb85f1ff4e21b Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Sun, 25 Jun 2023 20:12:15 -0500 Subject: [PATCH 3/7] stdmath updated --- std/stdmath.sloth | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/std/stdmath.sloth b/std/stdmath.sloth index 0f967eb..7ff5c82 100644 --- a/std/stdmath.sloth +++ b/std/stdmath.sloth @@ -8,7 +8,7 @@ fn abs(x: Int) Int { } fn fabs(x: Float) Float { - if x < 0 { + if x < 0.0 { return -x; } return x; @@ -51,7 +51,7 @@ fn pow(x: Int, y: Int) Int { } fn floor(x: Float) Float { - return x - abs(x % 1.0); + return x - fabs(x % 1.0); } fn ceil(x: Float) Float { From debb3b9a9c2cab0791396f008af7021968121001 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Sun, 25 Jun 2023 20:18:08 -0500 Subject: [PATCH 4/7] stdlib --- std/stdlib.c | 2 +- std/stdlib.sloth | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/std/stdlib.c b/std/stdlib.c index f8df9e8..d27b36f 100644 --- a/std/stdlib.c +++ b/std/stdlib.c @@ -4,7 +4,7 @@ void wait(long long x) { sleep(x); } -int len(char *str) { +int slen(char *str) { return strlen(str); } diff --git a/std/stdlib.sloth b/std/stdlib.sloth index 36f3879..b1849e3 100644 --- a/std/stdlib.sloth +++ b/std/stdlib.sloth @@ -1,6 +1,6 @@ foreign fn wait(x: Int) Void; foreign fn print(str: String) Void; -foreign fn len(str: String) Int; +foreign fn slen(str: String) Int; foreign fn charAt(str: String) Char; fn termpos(x: int, y: int) Void { From a7454f42bab35400eb8a651e4dfb8ef4a596e713 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Sun, 25 Jun 2023 22:26:53 -0500 Subject: [PATCH 5/7] removed old standard libarby --- sloth/src/sloth_std.rs | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 sloth/src/sloth_std.rs diff --git a/sloth/src/sloth_std.rs b/sloth/src/sloth_std.rs deleted file mode 100644 index 7e10297..0000000 --- a/sloth/src/sloth_std.rs +++ /dev/null @@ -1,26 +0,0 @@ -use std::ffi::{c_char, CStr}; - -use rand::Rng; - -#[no_mangle] -pub extern "C" fn rand(a: i64, b: i64) -> i64 { - rand::thread_rng().gen_range(a..b) -} - -#[no_mangle] -/// # Safety -/// -/// Function is unsafe if passed pointer is not a valid CStr -pub unsafe extern "C" fn println(s: *const c_char) { - let s = unsafe { CStr::from_ptr(s) }.to_str().unwrap(); - println!("{s}"); -} - -#[no_mangle] -/// # Safety -/// -/// Function is unsafe if passed pointer is not a valid CStr -pub unsafe extern "C" fn print(s: *const c_char) { - let s = unsafe { CStr::from_ptr(s) }.to_str().unwrap(); - print!("{s}"); -} From 578229132e35075ef191ee960460d237c3ace3b8 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Sun, 25 Jun 2023 22:33:13 -0500 Subject: [PATCH 6/7] fixed a bug i think --- build.sh | 1 + sloth/src/main.rs | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100755 build.sh diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..178169f --- /dev/null +++ b/build.sh @@ -0,0 +1 @@ +cargo build --features=llvm-sys/prefer-dynamic diff --git a/sloth/src/main.rs b/sloth/src/main.rs index bdcb4cb..957145d 100644 --- a/sloth/src/main.rs +++ b/sloth/src/main.rs @@ -11,7 +11,6 @@ pub mod analysis; pub mod codegen; pub mod lexer; pub mod parser; -pub mod sloth_std; pub mod symtable; use std::{env, fs}; @@ -24,7 +23,7 @@ use parser::AstParser; use symtable::{Symbol, SymbolTable}; use crate::analysis::analyze; -use crate::parser::graph::GraphBuilder; + use crate::symtable::Type; fn main() { From c3bb1751d7f62a13a46f28c1eb67d9e20d8d6f7f Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Sun, 25 Jun 2023 23:21:02 -0500 Subject: [PATCH 7/7] Fixed standard library --- std/stdio.c | 2 +- std/stdlib.c | 14 ++++++++++---- std/stdlib.sloth | 1 + std/stdmath.c | 10 ++++------ 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/std/stdio.c b/std/stdio.c index d1bc69a..1a4d98c 100644 --- a/std/stdio.c +++ b/std/stdio.c @@ -8,5 +8,5 @@ char* readln() { } void print(char *str) { - puts(str); + fputs(str, stdout); } diff --git a/std/stdlib.c b/std/stdlib.c index d27b36f..7e77385 100644 --- a/std/stdlib.c +++ b/std/stdlib.c @@ -1,13 +1,19 @@ #include +#include +#include void wait(long long x) { sleep(x); } -int slen(char *str) { - return strlen(str); +long long slen(char *str) { + return (long long) strlen(str); } -char charAt(char *str, int) { - return str[int]; +char charAt(char *str, long long x) { + return str[x]; +} + +long long parse_int(char *str) { + return (long long) atoi(str); } diff --git a/std/stdlib.sloth b/std/stdlib.sloth index b1849e3..d7ddeff 100644 --- a/std/stdlib.sloth +++ b/std/stdlib.sloth @@ -2,6 +2,7 @@ foreign fn wait(x: Int) Void; foreign fn print(str: String) Void; foreign fn slen(str: String) Int; foreign fn charAt(str: String) Char; +foreign fn parse_int(str: String) Int; fn termpos(x: int, y: int) Void { print("\x1b["); diff --git a/std/stdmath.c b/std/stdmath.c index c9a91c3..dad292f 100644 --- a/std/stdmath.c +++ b/std/stdmath.c @@ -2,10 +2,8 @@ #include #include -int randGen(int min, int max) { - time_t t; - - srand((unsigned) time(&t)); - - return rand() % (max - min + 1) + min; + +long long randGen(long long min, long long max) { + srandom((unsigned) time(NULL)); + return random() % (max - min + 1) + min; }