Skip to content

Commit

Permalink
Standard lib rework
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Jul 18, 2023
1 parent ee2133a commit 52d6bc8
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 131 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ In order to build sloth you will need a valid install of LLVM 15.0.1, you can do

After acquiring LLVM just run `cargo build` and you will have your own version of the sloth compiler!

You can also run `./build.sh {PATH TO SLOTH FILE} and it will build your sloth code!

## Disclaimer
Sloth is in very early development is NOT meant to be used for actual projects yet. Feel free to contribute to the project via Pull Request and open issues if you can. Thank you for using sloth!
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
cargo build
FILENAME="$1"
# Compile standard library
./target/debug/sloth std/stdio.sloth std/stdlib.sloth std/stdmath.sloth $FILENAME
./target/debug/sloth std/extern.sloth std/stdio.sloth std/stdmath.sloth $FILENAME

# Generate binary
clang output.o std/stdio.c std/stdlib.c std/stdmath.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}" .
2 changes: 1 addition & 1 deletion examples/arguments.sloth
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn main(argc: Int, argv: [String]) Int {
if argc == 2 {
print("The argument supplied is ");
println(argv);
println(vgets(argv, 1));
} else {
println("Wrong # of args");
}
Expand Down
2 changes: 1 addition & 1 deletion examples/cgol.sloth
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn display(life: [Int]) {
# if the cell is alive, print
termpos(x, y);
if cval(x, y, life) == 1{
print("#");
print("");
} else {
print(" ");
}
Expand Down
67 changes: 35 additions & 32 deletions examples/mandelbrot.sloth
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
#foreign fn print(str: String);
#foreign fn printint(i: Int);
#foreign fn as_int(x: Float) Int;
fn main() Int{
# Configure
var size: Float = 1000.0;
var maxVal: Float = 4.0;
var maxIter: Float = 50.0;
var plane: Float = 4.0;

#foreign fn termpos(x: Int, y: Int) Void;
# loop over coordinates
var x: Float = 0.0;
while x < size {
var y: Float = 0.0;
while y < size {
# Initialize
var cReal: Float = (x * plane / size) - 2.0;
var cImg: Float = (y * plane / size) - 2.0;
var zReal: Float = 0.0;
var zImg: Float = 0.0;
var count: Float = 0.0;

fn main() Int{
var size: Float = 1000.0;
var maxVal: Float = 4.0;
var maxIter: Float = 50.0;
var plane: Float = 4.0;
var x: Float = 0.0;
while x < size {
var y: Float = 0.0;
while y < size {
var cReal: Float = (x * plane / size) - 2.0;
var cImg: Float = (y * plane / size) - 2.0;
var zReal: Float = 0.0;
var zImg: Float = 0.0;
var count: Float = 0.0;
while (zReal * zReal + zImg * zImg) <= maxVal && count < maxIter {
var temp: Float = (zReal * zReal) - (zImg * zImg) + cReal;
zImg = 2.0 * zReal * zImg + cImg;
zReal = temp;
count = count + 1.0;
}
if as_int(count) == as_int(maxIter) {
termpos(as_int(x), as_int(y));
print("*");
}
y = y + 1.0;
}
x = x + 1.0;
# Calculate
while (zReal * zReal + zImg * zImg) <= maxVal && count < maxIter {
var temp: Float = (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;
}
return 0;
x = x + 1.0;
}
return 0;
}
2 changes: 1 addition & 1 deletion examples/read.sloth
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() Int {
print(filer("examples/read.txt"));
print(filer("examples/server.sloth"));
return 0;
}
28 changes: 28 additions & 0 deletions std/extern.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# stdio
foreign fn print(str: String) Void;
foreign fn readln() String;
foreign fn filer(path: String) String;
foreign fn curshide();
foreign fn cursshow();

# stdlib
foreign fn wait(x: Int) Int;
foreign fn slen(str: String) Int;
# foreign fn charAt(str: String) Char;
foreign fn parse_int(str: String) Int;
foreign fn termpos(x: Int, y: Int);
foreign fn as_int(x: Float) Int;
foreign fn istr(x: Int) String;
foreign fn system(cmd: String) Int;
foreign fn sequals(a: String, b: String) Bool;
foreign fn termclear() Void;

#stdmath
foreign fn randGen(min: Int, max: Int) Int;

#stdsocket
foreign fn serversock(port: Int, addr: String, backlog: Int) Int;
foreign fn clientsock(port: Int, addr: String) Int;
foreign fn closesock(soc: Int, server:Bool);
foreign fn sendsock(msg: String, soc: Int);
foreign fn recvsock(soc: Int) String;
6 changes: 0 additions & 6 deletions std/stdio.sloth
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
foreign fn print(str: String) Void;
foreign fn readln() String;
foreign fn filer(path: String) String;
foreign fn curshide();
foreign fn cursshow();

fn println(str: String) Void {
print(str);
print("\n");
Expand Down
11 changes: 0 additions & 11 deletions std/stdlib.sloth

This file was deleted.

6 changes: 6 additions & 0 deletions std/stdmath.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#include <math.h>
//float fmodf(float x, float y);

int random_setup = 0;

Expand All @@ -11,3 +13,7 @@ int randGen(int min, int max) {
}
return random() % (max - min + 1) + min;
}

//int slothfloor(float x) {
// return (int) (x - fabs(fmodf(x, (float) 1)));
//}
33 changes: 16 additions & 17 deletions std/stdmath.sloth
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
foreign fn randGen(min: Int, max: Int) Int;

fn abs(x: Int) Int {
if x < 0 {
return -x;
Expand Down Expand Up @@ -51,20 +49,21 @@ fn pow(x: Float, y: Float) Float {
return x;
}

#fn floor(x: Float) Int {
# return x - fabs(x % 1);
#}
fn floor(x: Float) Int{
return as_int(x - fabs(x % 1.0));
}

#fn ceil(x: Float) Int {
# if x < 0.0 {
# return floor(x) - 1;
# }
# return floor(x) + 1;
#}
fn ceil(x: Float) Int {
if x < 0.0 {
return floor(x) - 1;
}
return floor(x) + 1;
}

#fn round(x: Float) Float {
# if fabs(x % 1.0) >= 0.5 {
# return ceil(x);
# }
# return floor(x);
#}
fn round(x: Float) Int {
var ret: Int = floor(x);
if fabs(x % 1.0) >= 0.5 {
ret = ceil(x);
}
return ret;
}
110 changes: 55 additions & 55 deletions std/stdsocket.c
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
#include "stdlib.c"
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdbool.h>

int serversock(int PORT, char* addr, int backlog) {
int opt = 1;
int sock, new_sock;
struct sockaddr_in address;
int addrlen = sizeof(address);
int serversock(int PORT, char *addr, int backlog) {
int opt = 1;
int sock, new_sock;
struct sockaddr_in address;
int addrlen = sizeof(address);

if((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}

if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) < 0 ) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt,
sizeof(opt)) < 0) {
perror("setsockopt");
exit(EXIT_FAILURE);
}

address.sin_family = AF_INET;
if (sequals(addr, "auto")) {
address.sin_addr.s_addr = INADDR_ANY;
} else {
inet_aton(addr, &address.sin_addr.s_addr);
}
address.sin_port = htons(PORT);
address.sin_family = AF_INET;
if (sequals(addr, "auto")) {
address.sin_addr.s_addr = INADDR_ANY;
} else {
inet_aton(addr, &address.sin_addr.s_addr);
}
address.sin_port = htons(PORT);

if (bind(sock, (struct sockaddr*)&address, sizeof(address)) < 0) {
perror("bind");
exit(EXIT_FAILURE);
}
if (listen(sock, backlog) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}

if ((new_sock = accept(sock, (struct sockaddr*)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
return new_sock;
if (bind(sock, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind");
exit(EXIT_FAILURE);
}
if (listen(sock, backlog) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}

if ((new_sock = accept(sock, (struct sockaddr *)&address,
(socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
return new_sock;
}

int clientsock(int PORT, char *server_ip) {
struct sockaddr_in serv_addr;
int sock = socket(PF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

int clientsock(int PORT, char* server_ip) {
struct sockaddr_in serv_addr;
int sock = socket(PF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

inet_pton(AF_INET, server_ip, &serv_addr.sin_addr);
int status = connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
return sock;
inet_pton(AF_INET, server_ip, &serv_addr.sin_addr);
int status = connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
return sock;
}

char* recvsock(int soc) {
char* buf = malloc(1024);
int valread = read(soc, buf, 1024);
return buf;
char *recvsock(int soc) {
char *buf = malloc(1024);
int valread = read(soc, buf, 1024);
return buf;
}

void sendsock(char* msg, int soc) {
send(soc, msg, strlen(msg), 0);
}
void sendsock(char *msg, int soc) { send(soc, msg, strlen(msg), 0); }

void closesock(int soc, bool server) {
close(soc);
if (server) {
shutdown(soc, SHUT_RDWR);
}
close(soc);
if (server) {
shutdown(soc, SHUT_RDWR);
}
}
5 changes: 0 additions & 5 deletions std/stdsocket.sloth

This file was deleted.

0 comments on commit 52d6bc8

Please sign in to comment.