aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornic-gaffney <gaffney_nic@protonmail.com>2023-09-30 00:18:27 -0500
committernic-gaffney <gaffney_nic@protonmail.com>2023-09-30 00:18:27 -0500
commitc748aedbc265fdc7d62768d368161a1f1d88b9a4 (patch)
tree2371ae4c1ce98e72c9e414f0e4176c3d8bf35778
parent84c77656e1d5f0414050d8a2dae6c86b022bf125 (diff)
downloadsloth-c748aedbc265fdc7d62768d368161a1f1d88b9a4.tar.gz
spacing
-rw-r--r--examples/cgol.sloth136
-rw-r--r--examples/mandelbrot.sloth55
-rw-r--r--output425
-rw-r--r--std/extern.sloth1
-rw-r--r--std/stdlib.c3
5 files changed, 521 insertions, 99 deletions
diff --git a/examples/cgol.sloth b/examples/cgol.sloth
index a0e1878..3700395 100644
--- a/examples/cgol.sloth
+++ b/examples/cgol.sloth
@@ -1,74 +1,66 @@
fn populate() [Int]
{
- # Initialize life vector
- var life: [Int] = [0];
- vpopi(life);
+ # Initialize life vector
+ var life: [Int] = [0];
+ vpopi(life);
- # Fill the vector with random values
- var i: Int = 0;
- while i < 57600
+ # Fill the vector with random values
+ var i: Int = 0;
+ while i < 57600
{
- var n: Int = randGen(0,1);
- vpushi(life, n);
- i = i+1;
- }
+ var n: Int = randGen(0,1);
+ vpushi(life, n);
+ i = i+1;
+ }
- return life;
+ return life;
}
-fn coord(x: Int, y: Int) Int
+fn coord(x: Int, y: Int) Int
{
- var res: Int = -1;
- # Calculate index based on coordinates
- if x >= 0 && y >= 0
+ var res: Int = -1;
+ # Calculate index based on coordinates
+ if x >= 0 && y >= 0
{
- res = y*240+ x;
- }
- # if coordinate is invalid, return -1
- return res;
-}
-
-fn cval(x: Int, y: Int, life: [Int]) Int
-{
- # Check to make sure index exists before returning
- var res: Int = 0;
- var c: Int = coord(x, y);
- if c >= 0 {
- res = vgeti(life, c);
- }
- return res;
+ res = y*240+ x;
+ }
+ # if coordinate is invalid, return -1
+ return res;
}
-fn gol(total: Int, alive: Bool) Int
+fn cval(x: Int, y: Int, life: [Int]) Int
{
-
- if !alive && total == 3 {
- return 1;
+ # Check to make sure index exists before returning
+ var res: Int = 0;
+ var c: Int = coord(x, y);
+ if c >= 0 {
+ res = vgeti(life, c);
}
- if alive && ()
- return 0;
+ return res;
}
+
+
fn update(life: [Int], new: [Int])
{
- # Iterate through life
- for x in 0..64
+ # Iterate through life
+ for x in 0..64
{
- for y in 0..240
+ for y in 0..240
{
- # Calculate total score around selected cell
- var total: Int =
+ # Calculate total score around selected cell
+ var total: Int =
cval(x-1, y-1, life) + # Top Left
- cval(x-1, y , life) +
- cval(x-1, y+1, life) +
- cval(x , y-1, life) +
- cval(x , y+1, life) +
- cval(x+1, y-1, life) +
- cval(x+1, y , life) +
+ cval(x-1, y , life) +
+ cval(x-1, y+1, life) +
+ cval(x , y-1, life) +
+ cval(x , y+1, life) +
+ cval(x+1, y-1, life) +
+ cval(x+1, y , life) +
cval(x+1, y+1, life);
- # Apply game of life rules
-
+ # Apply game of life rules
+
var idx: Int = coord(x, y);
if cval(x, y, life) == 1
@@ -93,36 +85,36 @@ fn update(life: [Int], new: [Int])
vseti(new, idx, 0);
}
}
- }
- }
+ }
+ }
}
fn display(life: [Int]) {
- # Iterate through life
- for x in 3..62 {
- for y in 0..240 {
- termpos(x-3, y);
- if cval(x-3, y, life) == 1 {
- print("█");
- } else {
- print(" ");
- }
- }
- }
+ # Iterate through life
+ for x in 3..62 {
+ for y in 0..240 {
+ termpos(x-3, y);
+ if cval(x-3, y, life) == 1 {
+ print("█");
+ } else {
+ print(" ");
+ }
+ }
+ }
}
fn main() Int {
- # Populate
- var life: [Int] = populate();
- display(life);
+ # Populate
+ var life: [Int] = populate();
+ display(life);
curshide();
- # Play forever
- while true {
+ # Play forever
+ while true {
var new: [Int] = populate();
- update(life, new);
- display(new);
+ update(life, new);
+ display(new);
life = new;
- wait(100);
- }
- return 0;
+ wait(100);
+ }
+ return 0;
}
diff --git a/examples/mandelbrot.sloth b/examples/mandelbrot.sloth
index 275d32b..09286bd 100644
--- a/examples/mandelbrot.sloth
+++ b/examples/mandelbrot.sloth
@@ -1,3 +1,26 @@
+fn printMan(size: Float, maxVal: Float, maxIter: Float, plane: Float, x: Int, y: Int)
+{
+ var cReal = (as_float(x) * plane / size) - 2.0;
+ var cImg = (as_float(y) * plane / size) - 2.0;
+ var zReal = 0.0;
+ var zImg = 0.0;
+ var count = 0.0;
+
+ # Calculate
+ while (zReal * zReal + zImg * zImg) <= maxVal && count < maxIter {
+ var temp = (zReal * zReal) - (zImg * zImg) + cReal;
+ zImg = 2.0 * zReal * zImg + cImg;
+ zReal = temp;
+ count = count + 1.0;
+
+ # Check
+ if as_int(count) == as_int(maxIter) {
+ termpos(x, y);
+ print("█");
+ }
+ }
+}
+
fn main() Int {
# Configure
var size = 1000.0;
@@ -6,35 +29,13 @@ fn main() Int {
var plane = 4.0;
# loop over coordinates
- var x = 0.0;
- while x < size {
- var y = 0.0;
- while y < size {
- # Initialize
- var cReal = (x * plane / size) - 2.0;
- var cImg = (y * plane / size) - 2.0;
- var zReal = 0.0;
- var zImg = 0.0;
- var count = 0.0;
-
- # Calculate
- while (zReal * zReal + zImg * zImg) <= maxVal && count < maxIter {
- var temp = (zReal * zReal) - (zImg * zImg) + cReal;
- zImg = 2.0 * zReal * zImg + cImg;
- zReal = temp;
- count = count + 1.0;
- }
- # Check
- if as_int(count) == as_int(maxIter) {
- termpos(as_int(x), as_int(y));
- print("█");
- }
-
- y = y + 1.0;
+ for x in 0..as_int(size) {
+ for y in 0..as_int(size) {
+ # Initialize
+ printMan(size, maxVal, maxIter, plane, x, y);
}
- x = x + 1.0;
+
}
return 0;
}
-
diff --git a/output b/output
new file mode 100644
index 0000000..d22bb30
--- /dev/null
+++ b/output
@@ -0,0 +1,425 @@
+declare void @print(ptr %0)
+declare ptr @readln()
+declare ptr @filer(ptr %0)
+declare void @curshide()
+declare void @cursshow()
+declare i32 @wait(i32 %0)
+declare i32 @slen(ptr %0)
+declare i32 @parse_int(ptr %0)
+declare void @termpos(i32 %0, i32 %1)
+declare i32 @as_int(float %0)
+declare float @as_float(i32 %0)
+declare ptr @istr(i32 %0)
+declare i32 @system(ptr %0)
+declare i1 @sequals(ptr %0, ptr %1)
+declare void @termclear()
+declare i32 @randGen(i32 %0, i32 %1)
+declare i32 @serversock(i32 %0, ptr %1, i32 %2)
+declare i32 @clientsock(i32 %0, ptr %1)
+declare void @closesock(i32 %0, i1 %1)
+declare void @sendsock(ptr %0, i32 %1)
+declare ptr @recvsock(i32 %0)
+define i32 @abs(i32 %0) {
+entrypoint:
+ %x = alloca i32, align 4
+ store i32 %0, ptr %x, align 4
+ %1 = load i32, ptr %x, align 4
+ %lt = icmp slt i32 %1, 0
+ br i1 %lt, label %then, label %else
+
+then: ; preds = %entrypoint
+ %2 = load i32, ptr %x, align 4
+ %neg = sub i32 0, %2
+ ret i32 %neg
+ br label %continue
+
+else: ; preds = %entrypoint
+ br label %continue
+
+continue: ; preds = %else, %then
+ %3 = load i32, ptr %x, align 4
+ ret i32 %3
+}
+define float @fabs(float %0) {
+entrypoint:
+ %x = alloca float, align 4
+ store float %0, ptr %x, align 4
+ %1 = load float, ptr %x, align 4
+ %lt = fcmp olt float %1, 0.000000e+00
+ br i1 %lt, label %then, label %else
+
+then: ; preds = %entrypoint
+ %2 = load float, ptr %x, align 4
+ %neg = fneg float %2
+ ret float %neg
+ br label %continue
+
+else: ; preds = %entrypoint
+ br label %continue
+
+continue: ; preds = %else, %then
+ %3 = load float, ptr %x, align 4
+ ret float %3
+}
+define i32 @max(i32 %0, i32 %1) {
+entrypoint:
+ %y = alloca i32, align 4
+ %x = alloca i32, align 4
+ store i32 %0, ptr %x, align 4
+ store i32 %1, ptr %y, align 4
+ %2 = load i32, ptr %x, align 4
+ %3 = load i32, ptr %y, align 4
+ %gt = icmp sgt i32 %2, %3
+ br i1 %gt, label %then, label %else
+
+then: ; preds = %entrypoint
+ %4 = load i32, ptr %x, align 4
+ ret i32 %4
+ br label %continue
+
+else: ; preds = %entrypoint
+ br label %continue
+
+continue: ; preds = %else, %then
+ %5 = load i32, ptr %y, align 4
+ ret i32 %5
+}
+define i32 @min(i32 %0, i32 %1) {
+entrypoint:
+ %y = alloca i32, align 4
+ %x = alloca i32, align 4
+ store i32 %0, ptr %x, align 4
+ store i32 %1, ptr %y, align 4
+ %2 = load i32, ptr %x, align 4
+ %3 = load i32, ptr %y, align 4
+ %lt = icmp slt i32 %2, %3
+ br i1 %lt, label %then, label %else
+
+then: ; preds = %entrypoint
+ %4 = load i32, ptr %x, align 4
+ ret i32 %4
+ br label %continue
+
+else: ; preds = %entrypoint
+ br label %continue
+
+continue: ; preds = %else, %then
+ %5 = load i32, ptr %y, align 4
+ ret i32 %5
+}
+define float @fmax(float %0, float %1) {
+entrypoint:
+ %y = alloca float, align 4
+ %x = alloca float, align 4
+ store float %0, ptr %x, align 4
+ store float %1, ptr %y, align 4
+ %2 = load float, ptr %x, align 4
+ %3 = load float, ptr %y, align 4
+ %gt = fcmp ogt float %2, %3
+ br i1 %gt, label %then, label %else
+
+then: ; preds = %entrypoint
+ %4 = load float, ptr %x, align 4
+ ret float %4
+ br label %continue
+
+else: ; preds = %entrypoint
+ br label %continue
+
+continue: ; preds = %else, %then
+ %5 = load float, ptr %y, align 4
+ ret float %5
+}
+define float @fmin(float %0, float %1) {
+entrypoint:
+ %y = alloca float, align 4
+ %x = alloca float, align 4
+ store float %0, ptr %x, align 4
+ store float %1, ptr %y, align 4
+ %2 = load float, ptr %x, align 4
+ %3 = load float, ptr %y, align 4
+ %lt = fcmp olt float %2, %3
+ br i1 %lt, label %then, label %else
+
+then: ; preds = %entrypoint
+ %4 = load float, ptr %x, align 4
+ ret float %4
+ br label %continue
+
+else: ; preds = %entrypoint
+ br label %continue
+
+continue: ; preds = %else, %then
+ %5 = load float, ptr %y, align 4
+ ret float %5
+}
+define float @pow(float %0, float %1) {
+entrypoint:
+ %power = alloca float, align 4
+ %y = alloca float, align 4
+ %x = alloca float, align 4
+ store float %0, ptr %x, align 4
+ store float %1, ptr %y, align 4
+ %2 = load float, ptr %x, align 4
+ store float %2, ptr %power, align 4
+ br label %loop
+
+loop: ; preds = %"loop body", %entrypoint
+ %3 = load float, ptr %y, align 4
+ %gt = fcmp ogt float %3, 1.000000e+00
+ br i1 %gt, label %"loop body", label %"after loop"
+
+"loop body": ; preds = %loop
+ %4 = load float, ptr %power, align 4
+ %5 = load float, ptr %x, align 4
+ %mul = fmul float %4, %5
+ store float %mul, ptr %x, align 4
+ %6 = load float, ptr %y, align 4
+ %sub = fsub float %6, 1.000000e+00
+ store float %sub, ptr %y, align 4
+ br label %loop
+
+"after loop": ; preds = %loop
+ %7 = load float, ptr %x, align 4
+ ret float %7
+}
+define i32 @floor(float %0) {
+entrypoint:
+ %x = alloca float, align 4
+ store float %0, ptr %x, align 4
+ %1 = load float, ptr %x, align 4
+ %2 = load float, ptr %x, align 4
+ %mod = frem float %2, 1.000000e+00
+ %3 = call float @fabs(float %mod)
+ %sub = fsub float %1, %3
+ %4 = call i32 @as_int(float %sub)
+ ret i32 %4
+}
+define i32 @ceil(float %0) {
+entrypoint:
+ %x = alloca float, align 4
+ store float %0, ptr %x, align 4
+ %1 = load float, ptr %x, align 4
+ %lt = fcmp olt float %1, 0.000000e+00
+ br i1 %lt, label %then, label %else
+
+then: ; preds = %entrypoint
+ %2 = load float, ptr %x, align 4
+ %3 = call i32 @floor(float %2)
+ %sub = sub i32 %3, 1
+ ret i32 %sub
+ br label %continue
+
+else: ; preds = %entrypoint
+ br label %continue
+
+continue: ; preds = %else, %then
+ %4 = load float, ptr %x, align 4
+ %5 = call i32 @floor(float %4)
+ %add = add i32 %5, 1
+ ret i32 %add
+}
+define i32 @round(float %0) {
+entrypoint:
+ %ret = alloca i32, align 4
+ %x = alloca float, align 4
+ store float %0, ptr %x, align 4
+ %1 = load float, ptr %x, align 4
+ %2 = call i32 @floor(float %1)
+ store i32 %2, ptr %ret, align 4
+ %3 = load float, ptr %x, align 4
+ %mod = frem float %3, 1.000000e+00
+ %4 = call float @fabs(float %mod)
+ %gt = fcmp oge float %4, 5.000000e-01
+ br i1 %gt, label %then, label %else
+
+then: ; preds = %entrypoint
+ %5 = load float, ptr %x, align 4
+ %6 = call i32 @ceil(float %5)
+ store i32 %6, ptr %ret, align 4
+ br label %continue
+
+else: ; preds = %entrypoint
+ br label %continue
+
+continue: ; preds = %else, %then
+ %7 = load i32, ptr %ret, align 4
+ ret i32 %7
+}
+define void @println(ptr %0) {
+entrypoint:
+ %str = alloca ptr, align 8
+ store ptr %0, ptr %str, align 8
+ %1 = load ptr, ptr %str, align 8
+ call void @print(ptr %1)
+ %str1 = tail call ptr @malloc(i32 mul (i32 ptrtoint (ptr getelementptr (i8, ptr null, i32 1) to i32), i32 7))
+ store [2 x i8] c"\0A\00", ptr %str1, align 1
+ call void @print(ptr %str1)
+ ret void
+}
+define void @printMan(float %0, float %1, float %2, float %3, i32 %4, i32 %5) {
+entrypoint:
+ %temp = alloca float, align 4
+ %count = alloca float, align 4
+ %zImg = alloca float, align 4
+ %zReal = alloca float, align 4
+ %cImg = alloca float, align 4
+ %cReal = alloca float, align 4
+ %y = alloca i32, align 4
+ %x = alloca i32, align 4
+ %plane = alloca float, align 4
+ %maxIter = alloca float, align 4
+ %maxVal = alloca float, align 4
+ %size = alloca float, align 4
+ store float %0, ptr %size, align 4
+ store float %1, ptr %maxVal, align 4
+ store float %2, ptr %maxIter, align 4
+ store float %3, ptr %plane, align 4
+ store i32 %4, ptr %x, align 4
+ store i32 %5, ptr %y, align 4
+ %6 = load i32, ptr %x, align 4
+ %7 = call float @as_float(i32 %6)
+ %8 = load float, ptr %plane, align 4
+ %mul = fmul float %7, %8
+ %9 = load float, ptr %size, align 4
+ %div = fdiv float %mul, %9
+ %sub = fsub float %div, 2.000000e+00
+ store float %sub, ptr %cReal, align 4
+ %10 = load i32, ptr %y, align 4
+ %11 = call float @as_float(i32 %10)
+ %12 = load float, ptr %plane, align 4
+ %mul1 = fmul float %11, %12
+ %13 = load float, ptr %size, align 4
+ %div2 = fdiv float %mul1, %13
+ %sub3 = fsub float %div2, 2.000000e+00
+ store float %sub3, ptr %cImg, align 4
+ store float 0.000000e+00, ptr %zReal, align 4
+ store float 0.000000e+00, ptr %zImg, align 4
+ store float 0.000000e+00, ptr %count, align 4
+ br label %loop
+
+loop: ; preds = %continue, %entrypoint
+ %14 = load float, ptr %zReal, align 4
+ %15 = load float, ptr %zReal, align 4
+ %mul4 = fmul float %14, %15
+ %16 = load float, ptr %zImg, align 4
+ %17 = load float, ptr %zImg, align 4
+ %mul5 = fmul float %16, %17
+ %add = fadd float %mul4, %mul5
+ %18 = load float, ptr %maxVal, align 4
+ %le = fcmp ole float %add, %18
+ %19 = load float, ptr %count, align 4
+ %20 = load float, ptr %maxIter, align 4
+ %lt = fcmp olt float %19, %20
+ %logand = and i1 %le, %lt
+ br i1 %logand, label %"loop body", label %"after loop"
+
+"loop body": ; preds = %loop
+ %21 = load float, ptr %zReal, align 4
+ %22 = load float, ptr %zReal, align 4
+ %mul6 = fmul float %21, %22
+ %23 = load float, ptr %zImg, align 4
+ %24 = load float, ptr %zImg, align 4
+ %mul7 = fmul float %23, %24
+ %sub8 = fsub float %mul6, %mul7
+ %25 = load float, ptr %cReal, align 4
+ %add9 = fadd float %sub8, %25
+ store float %add9, ptr %temp, align 4
+ %26 = load float, ptr %zReal, align 4
+ %mul10 = fmul float 2.000000e+00, %26
+ %27 = load float, ptr %zImg, align 4
+ %mul11 = fmul float %mul10, %27
+ %28 = load float, ptr %cImg, align 4
+ %add12 = fadd float %mul11, %28
+ store float %add12, ptr %zImg, align 4
+ %29 = load float, ptr %temp, align 4
+ store float %29, ptr %zReal, align 4
+ %30 = load float, ptr %count, align 4
+ %add13 = fadd float %30, 1.000000e+00
+ store float %add13, ptr %count, align 4
+ %31 = load float, ptr %count, align 4
+ %32 = call i32 @as_int(float %31)
+ %33 = load float, ptr %maxIter, align 4
+ %34 = call i32 @as_int(float %33)
+ %35 = icmp eq i32 %32, %34
+ br i1 %35, label %then, label %else
+
+"after loop": ; preds = %loop
+ ret void
+
+then: ; preds = %"loop body"
+ %36 = load i32, ptr %x, align 4
+ %37 = load i32, ptr %y, align 4
+ call void @termpos(i32 %36, i32 %37)
+ %str = tail call ptr @malloc(i32 mul (i32 ptrtoint (ptr getelementptr (i8, ptr null, i32 1) to i32), i32 9))
+ store [4 x i8] c"\E2\96\88\00", ptr %str, align 1
+ call void @print(ptr %str)
+ br label %continue
+
+else: ; preds = %"loop body"
+ br label %continue
+
+continue: ; preds = %else, %then
+ br label %loop
+}
+define i32 @main() {
+entrypoint:
+ %range4 = alloca { i32, i32 }, align 8
+ %range = alloca { i32, i32 }, align 8
+ %plane = alloca float, align 4
+ %maxIter = alloca float, align 4
+ %maxVal = alloca float, align 4
+ %size = alloca float, align 4
+ store float 1.000000e+03, ptr %size, align 4
+ store float 4.000000e+00, ptr %maxVal, align 4
+ store float 5.000000e+01, ptr %maxIter, align 4
+ store float 4.000000e+00, ptr %plane, align 4
+ %0 = load float, ptr %size, align 4
+ %1 = call i32 @as_int(float %0)
+ store { i32, i32 } { i32 0, i32 %1 }, ptr %range, align 4
+ %current = getelementptr inbounds { i32, i32 }, ptr %range, i32 0, i32 0
+ %end = getelementptr inbounds { i32, i32 }, ptr %range, i32 0, i32 1
+ br label %loop
+
+loop: ; preds = %"after loop3", %entrypoint
+ %2 = load i32, ptr %current, align 4
+ %3 = load i32, ptr %end, align 4
+ %4 = icmp slt i32 %2, %3
+ br i1 %4, label %"loop body", label %"after loop"
+
+"loop body": ; preds = %loop
+ %5 = load float, ptr %size, align 4
+ %6 = call i32 @as_int(float %5)
+ store { i32, i32 } { i32 0, i32 %6 }, ptr %range4, align 4
+ %current5 = getelementptr inbounds { i32, i32 }, ptr %range4, i32 0, i32 0
+ %end6 = getelementptr inbounds { i32, i32 }, ptr %range4, i32 0, i32 1
+ br label %loop1
+
+"after loop": ; preds = %loop
+ ret i32 0
+
+loop1: ; preds = %"loop body2", %"loop body"
+ %7 = load i32, ptr %current5, align 4
+ %8 = load i32, ptr %end6, align 4
+ %9 = icmp slt i32 %7, %8
+ br i1 %9, label %"loop body2", label %"after loop3"
+
+"loop body2": ; preds = %loop1
+ %10 = load float, ptr %size, align 4
+ %11 = load float, ptr %maxVal, align 4
+ %12 = load float, ptr %maxIter, align 4
+ %13 = load float, ptr %plane, align 4
+ %14 = load i32, ptr %current, align 4
+ %15 = load i32, ptr %current5, align 4
+ call void @printMan(float %10, float %11, float %12, float %13, i32 %14, i32 %15)
+ %16 = load i32, ptr %current5, align 4
+ %17 = add i32 %16, 1
+ store i32 %17, ptr %current5, align 4
+ br label %loop1
+
+"after loop3": ; preds = %loop1
+ %18 = load i32, ptr %current, align 4
+ %19 = add i32 %18, 1
+ store i32 %19, ptr %current, align 4
+ br label %loop
+}
diff --git a/std/extern.sloth b/std/extern.sloth
index c970541..a31c2c1 100644
--- a/std/extern.sloth
+++ b/std/extern.sloth
@@ -12,6 +12,7 @@ foreign fn slen(str: String) Int;
foreign fn parse_int(str: String) Int;
foreign fn termpos(x: Int, y: Int);
foreign fn as_int(x: Float) Int;
+foreign fn as_float(x: Int) Float;
foreign fn istr(x: Int) String;
foreign fn system(cmd: String) Int;
foreign fn sequals(a: String, b: String) Bool;
diff --git a/std/stdlib.c b/std/stdlib.c
index ed848c3..8636f11 100644
--- a/std/stdlib.c
+++ b/std/stdlib.c
@@ -41,6 +41,9 @@ int as_int(float x) {
return (int) x;
}
+float as_float(int x) {
+ return (float) x;
+}
bool sequals(char* a, char* b) {
if (strlen(a) != strlen(b)) {
return false;