From 52d6bc8533616dd642c96f8b6e72f459e1b4d465 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Mon, 17 Jul 2023 23:00:30 -0500 Subject: [PATCH] Standard lib rework --- README.md | 2 + build.sh | 4 +- examples/arguments.sloth | 2 +- examples/cgol.sloth | 2 +- examples/mandelbrot.sloth | 67 ++++++++++++----------- examples/read.sloth | 2 +- std/extern.sloth | 28 ++++++++++ std/stdio.sloth | 6 --- std/stdlib.sloth | 11 ---- std/stdmath.c | 6 +++ std/stdmath.sloth | 33 ++++++------ std/stdsocket.c | 110 +++++++++++++++++++------------------- std/stdsocket.sloth | 5 -- 13 files changed, 147 insertions(+), 131 deletions(-) create mode 100644 std/extern.sloth delete mode 100644 std/stdlib.sloth delete mode 100644 std/stdsocket.sloth diff --git a/README.md b/README.md index bee380d..d38f559 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/build.sh b/build.sh index 5c6c414..822a936 100755 --- a/build.sh +++ b/build.sh @@ -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}" . diff --git a/examples/arguments.sloth b/examples/arguments.sloth index 3e44dcc..9daaec7 100644 --- a/examples/arguments.sloth +++ b/examples/arguments.sloth @@ -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"); } diff --git a/examples/cgol.sloth b/examples/cgol.sloth index 6cfe72d..fadb9c7 100644 --- a/examples/cgol.sloth +++ b/examples/cgol.sloth @@ -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(" "); } diff --git a/examples/mandelbrot.sloth b/examples/mandelbrot.sloth index 391e86e..90c7f1c 100644 --- a/examples/mandelbrot.sloth +++ b/examples/mandelbrot.sloth @@ -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; } diff --git a/examples/read.sloth b/examples/read.sloth index cf947f8..df398bb 100644 --- a/examples/read.sloth +++ b/examples/read.sloth @@ -1,4 +1,4 @@ fn main() Int { - print(filer("examples/read.txt")); + print(filer("examples/server.sloth")); return 0; } diff --git a/std/extern.sloth b/std/extern.sloth new file mode 100644 index 0000000..c970541 --- /dev/null +++ b/std/extern.sloth @@ -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; diff --git a/std/stdio.sloth b/std/stdio.sloth index 37a2d07..978bcfb 100644 --- a/std/stdio.sloth +++ b/std/stdio.sloth @@ -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"); diff --git a/std/stdlib.sloth b/std/stdlib.sloth deleted file mode 100644 index 48a92bd..0000000 --- a/std/stdlib.sloth +++ /dev/null @@ -1,11 +0,0 @@ -foreign fn wait(x: Int) Int; -foreign fn print(str: String) Void; -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; diff --git a/std/stdmath.c b/std/stdmath.c index a650129..56e140c 100644 --- a/std/stdmath.c +++ b/std/stdmath.c @@ -1,6 +1,8 @@ #include #include #include +//#include +//float fmodf(float x, float y); int random_setup = 0; @@ -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))); +//} diff --git a/std/stdmath.sloth b/std/stdmath.sloth index 9de73ae..28e3d38 100644 --- a/std/stdmath.sloth +++ b/std/stdmath.sloth @@ -1,5 +1,3 @@ -foreign fn randGen(min: Int, max: Int) Int; - fn abs(x: Int) Int { if x < 0 { return -x; @@ -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; +} diff --git a/std/stdsocket.c b/std/stdsocket.c index 7f2a02c..94d2ebe 100644 --- a/std/stdsocket.c +++ b/std/stdsocket.c @@ -1,76 +1,76 @@ +#include "stdlib.c" #include +#include #include #include #include #include #include -#include -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); + } } diff --git a/std/stdsocket.sloth b/std/stdsocket.sloth deleted file mode 100644 index 68576a0..0000000 --- a/std/stdsocket.sloth +++ /dev/null @@ -1,5 +0,0 @@ -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;