Skip to content

Commit

Permalink
Standard library beginnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Jun 25, 2023
1 parent 6a59bf6 commit bf647e4
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 11 deletions.
22 changes: 11 additions & 11 deletions examples/mandelbrot.sloth
Original file line number Diff line number Diff line change
@@ -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("*");
}
}
Expand Down
12 changes: 12 additions & 0 deletions std/stdio.c
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 9 additions & 0 deletions std/stdio.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
foreign fn print(str: String) Void;
foreign fn readln() String;

fn println(str: String) Void {
print(str);
print("\n");
}


5 changes: 5 additions & 0 deletions std/stdlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <unistd.h>

void wait(long long x) {
sleep(x);
}
14 changes: 14 additions & 0 deletions std/stdlib.sloth
Original file line number Diff line number Diff line change
@@ -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");
}
69 changes: 69 additions & 0 deletions std/stdmath.sloth
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit bf647e4

Please sign in to comment.