aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbuild.sh2
-rw-r--r--examples/cgol.sloth107
-rw-r--r--examples/hello.sloth5
-rw-r--r--examples/mandelbrot.sloth1
-rw-r--r--std/stdio.c30
5 files changed, 69 insertions, 76 deletions
diff --git a/build.sh b/build.sh
index 760f2ab..aa1bd42 100755
--- a/build.sh
+++ b/build.sh
@@ -5,7 +5,7 @@ FILENAME="$1"
./target/release/sloth std/extern.sloth std/stdmath.sloth std/stdio.sloth $FILENAME
# Generate binary
-clang -lm output.o std/stdsocket.c std/stdio.c std/stdlib.c std/stdmath.c std/stdmem.c -o "${FILENAME%.sloth}"
+clang -lm output.o std/stdio.c std/stdlib.c std/stdmath.c -o "${FILENAME%.sloth}"
# Move file
mv "${FILENAME%.sloth}" out/
diff --git a/examples/cgol.sloth b/examples/cgol.sloth
index 3700395..e885a66 100644
--- a/examples/cgol.sloth
+++ b/examples/cgol.sloth
@@ -1,55 +1,56 @@
-fn populate() [Int]
-{
- # Initialize life vector
- var life: [Int] = [0];
- vpopi(life);
+fn populate() [Int] {
+ # Initialize life vector
+ var life: [Int] = ["Hello World"];
+ vpopi(life);
- # 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;
- }
+ # 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;
+ }
return life;
}
-fn coord(x: Int, y: Int) Int
-{
- 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 coord(x: Int, y: Int) Int {
+ 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;
+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;
}
+#fn gol(total: Int, alive: Bool) Int
+#{
+ #if !alive && total == 3
+ #{
+ #return 1;
+ #}
+ #if alive && ()
+ #return 0;
+ #}
-fn update(life: [Int], new: [Int])
-{
- # Iterate through life
- for x in 0..64
- {
- for y in 0..240
- {
- # Calculate total score around selected cell
- var total: Int =
+fn update(life: [Int], new: [Int]) {
+ # Iterate through life
+ for x in 0..64 {
+ for y in 0..240 {
+ # 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) +
@@ -59,29 +60,23 @@ fn update(life: [Int], new: [Int])
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
- {
- if total < 2 || total > 3
- {
+ if cval(x, y, life) == 1 {
+ if total < 2 || total > 3 {
vseti(new, idx, 0);
}
- else
- {
+ else {
vseti(new, idx, 1);
}
}
- else
- {
- if total == 3
- {
+ else {
+ if total == 3 {
vseti(new, idx, 1);
}
- else
- {
+ else {
vseti(new, idx, 0);
}
}
@@ -95,7 +90,7 @@ fn display(life: [Int]) {
for y in 0..240 {
termpos(x-3, y);
if cval(x-3, y, life) == 1 {
- print("█");
+ print("█");
} else {
print(" ");
}
@@ -107,7 +102,7 @@ fn main() Int {
# Populate
var life: [Int] = populate();
display(life);
- curshide();
+ curshide();
# Play forever
while true {
var new: [Int] = populate();
diff --git a/examples/hello.sloth b/examples/hello.sloth
index 5aa54e9..641ed1a 100644
--- a/examples/hello.sloth
+++ b/examples/hello.sloth
@@ -1,7 +1,6 @@
fn main() Int {
- var i: Int = 10;
- for j in 0..i {
- print(istr(j));
+ while true {
+ println("Hello World!");
}
return 0;
}
diff --git a/examples/mandelbrot.sloth b/examples/mandelbrot.sloth
index 09286bd..4d93e3d 100644
--- a/examples/mandelbrot.sloth
+++ b/examples/mandelbrot.sloth
@@ -29,7 +29,6 @@ fn main() Int {
var plane = 4.0;
# loop over coordinates
-
for x in 0..as_int(size) {
for y in 0..as_int(size) {
# Initialize
diff --git a/std/stdio.c b/std/stdio.c
index e9cefd0..f584850 100644
--- a/std/stdio.c
+++ b/std/stdio.c
@@ -2,9 +2,9 @@
#include <stdlib.h>
char *readln() {
- char *str = malloc(128);
- fgets(str, 127, stdin);
- return str;
+ char *str = malloc(128);
+ fgets(str, 127, stdin);
+ return str;
}
void print(char *str) { fputs(str, stdout); }
@@ -18,19 +18,19 @@ void curshide() { print("\x1b[?25l"); }
void cursshow() { print("\x1b[?25h"); }
char *filer(char *path) {
- FILE *fptr = fopen(path, "rb");
- char *contents = 0;
+ FILE *fptr = fopen(path, "rb");
+ char *contents = 0;
- if (fptr == NULL) {
- return "File not found";
- }
- fseek(fptr, 0, SEEK_END);
- long size = ftell(fptr);
- fseek(fptr, 0, SEEK_SET);
+ if (fptr == NULL) {
+ return "File not found";
+ }
+ fseek(fptr, 0, SEEK_END);
+ long size = ftell(fptr);
+ fseek(fptr, 0, SEEK_SET);
- contents = malloc(size);
- fread(contents, 1, size, fptr);
- fclose(fptr);
+ contents = malloc(size);
+ fread(contents, 1, size, fptr);
+ fclose(fptr);
- return contents;
+ return contents;
}