aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornic-gaffney <gaffney_nic@protonmail.com>2023-06-27 01:59:59 -0500
committernic-gaffney <gaffney_nic@protonmail.com>2023-06-27 01:59:59 -0500
commit29bdd10ee3621ed875bfa34a0faa42c35a1e39ed (patch)
tree97f7605b53fc77277d98d5ea8127391edefc4ab5
parent9c2d8f5a10b8affd604cec6e394d43514ef93ca1 (diff)
downloadsloth-29bdd10ee3621ed875bfa34a0faa42c35a1e39ed.tar.gz
Standard library methinks
-rwxr-xr-xbuild.sh13
-rw-r--r--examples/cgol.sloth73
-rw-r--r--examples/mandelbrot.sloth3
-rw-r--r--examples/mergesort.sloth4
-rwxr-xr-xmandelbrotbin0 -> 21024 bytes
-rw-r--r--mandelbrot.py22
-rw-r--r--std/stdlib.c2
-rw-r--r--std/stdlib.sloth2
-rw-r--r--std/stdmath.c6
9 files changed, 105 insertions, 20 deletions
diff --git a/build.sh b/build.sh
index d4d648d..b8a9b9c 100755
--- a/build.sh
+++ b/build.sh
@@ -2,16 +2,7 @@
cargo build
# Compile standard library
-./target/debug/sloth std/stdio.sloth
-mv output.o stdio.o
-./target/debug/sloth std/stdlib.sloth
-mv output.o stdlib.o
-./target/debug/sloth std/stdmath.sloth
-mv output.o stdmath.o
-
-# Compile user program
-./target/debug/sloth "$1"
-mv output.o main.o
+./target/debug/sloth std/stdio.sloth std/stdlib.sloth std/stdmath.sloth "$1"
# Generate binary
-clang stdio.o std/stdio.c stdlib.o std/stdlib.c stdmath.o std/stdmath.c main.o -o program
+clang output.o std/stdio.c std/stdlib.c std/stdmath.c -o program
diff --git a/examples/cgol.sloth b/examples/cgol.sloth
new file mode 100644
index 0000000..eea14c3
--- /dev/null
+++ b/examples/cgol.sloth
@@ -0,0 +1,73 @@
+foreign fn print(str: String);
+foreign fn randGen(min: Int, max: Int) Int;
+foreign fn wait(time: Float);
+foreign fn termpos(x: Int, y: Int);
+
+
+fn populate() [Int] {
+ var life: [Int] = [0];
+ vpopi(life);
+ var i: Int = 0;
+ while i < 100 {
+ n: Int = randGen(0,1);
+ vpushi(life, n);
+ }
+ return life;
+}
+
+fn coord(x: Int, y: Int) Int {
+ if x >= 0 && y >= 0 {
+ return y*10 + x;
+ }
+ return -1;
+
+}
+
+fn cval(x: Int, y: Int, life: [Int]) Int {
+ c: Int = coord(x, y);
+ if c < 0 {
+ return 0;
+ }
+ return vgeti(life, c);
+}
+
+fn update(life: [Int]) [Int] {
+ x: Int = 0;
+ while x < 10 Int {
+ y: Int = 0;
+ while y < 10 {
+ total: Int = cval(x-1, y-1) + cval(x-1, y) + cval(x-1, y+1) + cval(x, y-1) + cval(x, y+1) + cval(x+1, y-1) + cval(x+1, y) + cval(x+1, y+1);
+ if cval(x, y) == 1 && total < 2 || total > 3{
+ vseti(life, 0);
+ } else if total == 3 {
+ vseti(life, 1);
+ }
+ }
+ }
+ return life;
+}
+
+fn display(life: [Int]) {
+ x: Int = 0;
+ while x < 10 {
+ y: Int = 0;
+ while y < 10 {
+ alive: Bool = cval(x, y) == 1;
+ if alive {
+ termpos(x, y);
+ print("#");
+ }
+
+
+ }
+ }
+}
+
+fn main() Int {
+ var life: [Int] = populate();
+ while true {
+ life = update(life);
+ display(life);
+ wait(0.5);
+ }
+}
diff --git a/examples/mandelbrot.sloth b/examples/mandelbrot.sloth
index 1765e8e..6bf7727 100644
--- a/examples/mandelbrot.sloth
+++ b/examples/mandelbrot.sloth
@@ -5,11 +5,10 @@ foreign fn as_int(x: Float) Int;
foreign fn termpos(x: Int, y: Int) Void;
fn main() Int{
- var size: Float = 200.0;
+ var size: Float = 1000.0;
var maxVal: Float = 4.0;
var maxIter: Float = 50.0;
var plane: Float = 4.0;
- # lmao
var x: Float = 0.0;
while x < size {
var y: Float = 0.0;
diff --git a/examples/mergesort.sloth b/examples/mergesort.sloth
deleted file mode 100644
index f1e5901..0000000
--- a/examples/mergesort.sloth
+++ /dev/null
@@ -1,4 +0,0 @@
-fn merge_sort(list: List<Int>) {
- print(list);
-
-}
diff --git a/mandelbrot b/mandelbrot
new file mode 100755
index 0000000..1196b0f
--- /dev/null
+++ b/mandelbrot
Binary files differ
diff --git a/mandelbrot.py b/mandelbrot.py
new file mode 100644
index 0000000..82aa65c
--- /dev/null
+++ b/mandelbrot.py
@@ -0,0 +1,22 @@
+size = 200
+
+# constants:
+MaxValue = 4
+MaxIterations = 50
+planeWidth = 4
+
+for x in range(0, size):
+ for y in range(0, size): # for each pixel do:
+ cReal = (x * planeWidth / size) - 2
+ cImg = (y * planeWidth / size) - 2
+ zReal = 0
+ zImg = 0
+ count = 0
+ while (zReal*zReal + zImg*zImg) <= MaxValue and count < MaxIterations:
+ temp = (zReal * zReal) - (zImg * zImg) + cReal
+ zImg = 2 * zReal * zImg + cImg
+ zReal = temp
+ count += 1
+
+ if count == MaxIterations:
+ print(f"\x1b[{x};{y}H*")
diff --git a/std/stdlib.c b/std/stdlib.c
index 11ae42f..f405b6f 100644
--- a/std/stdlib.c
+++ b/std/stdlib.c
@@ -2,7 +2,7 @@
#include <stdlib.h>
#include <string.h>
-void wait(int x) {
+void wait(float x) {
sleep(x);
}
diff --git a/std/stdlib.sloth b/std/stdlib.sloth
index cc404f6..e15d4dc 100644
--- a/std/stdlib.sloth
+++ b/std/stdlib.sloth
@@ -1,4 +1,4 @@
-foreign fn wait(x: Int) Void;
+foreign fn wait(x: Float) Void;
foreign fn print(str: String) Void;
foreign fn slen(str: String) Int;
# foreign fn charAt(str: String) Char;
diff --git a/std/stdmath.c b/std/stdmath.c
index 37c81b0..f7f79c6 100644
--- a/std/stdmath.c
+++ b/std/stdmath.c
@@ -2,8 +2,12 @@
#include <stdlib.h>
#include <time.h>
+bool random_setup = false;
int randGen(int min, int max) {
- srandom((unsigned) time(NULL));
+ if random_setup == false {
+ srandom(time(NULL));
+ random_setup = true;
+ }
return random() % (max - min + 1) + min;
}