diff options
| author | nic-gaffney <gaffney_nic@protonmail.com> | 2023-06-25 18:55:58 -0500 |
|---|---|---|
| committer | nic-gaffney <gaffney_nic@protonmail.com> | 2023-06-25 18:55:58 -0500 |
| commit | bf647e430e3a3642c8199dc81a411d5e1586cabb (patch) | |
| tree | c95a82bcaa233e1653b702d9fa7ce54ca7dd95e6 | |
| parent | 6a59bf6d5345fbe2487e1cc36c36aa6884fcc39d (diff) | |
| download | sloth-bf647e430e3a3642c8199dc81a411d5e1586cabb.tar.gz | |
Standard library beginnings
| -rw-r--r-- | examples/mandelbrot.sloth | 22 | ||||
| -rw-r--r-- | std/stdio.c | 12 | ||||
| -rw-r--r-- | std/stdio.sloth | 9 | ||||
| -rw-r--r-- | std/stdlib.c | 5 | ||||
| -rw-r--r-- | std/stdlib.sloth | 14 | ||||
| -rw-r--r-- | std/stdmath.sloth | 69 |
6 files changed, 120 insertions, 11 deletions
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 <stdio.h> +#include <stdlib.h> + +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 <unistd.h> + +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); +} |
