aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
Diffstat (limited to 'std')
-rw-r--r--std/extern.sloth28
-rw-r--r--std/stdio.sloth6
-rw-r--r--std/stdlib.sloth11
-rw-r--r--std/stdmath.c6
-rw-r--r--std/stdmath.sloth33
-rw-r--r--std/stdsocket.c110
-rw-r--r--std/stdsocket.sloth5
7 files changed, 105 insertions, 94 deletions
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 <stdio.h>
#include <stdlib.h>
#include <time.h>
+//#include <math.h>
+//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 <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);
+ }
}
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;