aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authornic-gaffney <gaffney_nic@protonmail.com>2023-12-05 06:51:38 -0600
committernic-gaffney <gaffney_nic@protonmail.com>2023-12-05 06:51:38 -0600
commitf655a862106a8a05bc432c2df957979f3fcc399b (patch)
tree28134136bdbbaa5dc423403f0985ccf8fb60e861 /std
parentc748aedbc265fdc7d62768d368161a1f1d88b9a4 (diff)
parentd767828cee67bae17b811062949653234aa14b28 (diff)
downloadsloth-f655a862106a8a05bc432c2df957979f3fcc399b.tar.gz
merge
Diffstat (limited to 'std')
-rw-r--r--std/extern.sloth5
-rw-r--r--std/stdio.c60
-rw-r--r--std/stdmem.c20
-rw-r--r--std/stdmem.sloth0
4 files changed, 49 insertions, 36 deletions
diff --git a/std/extern.sloth b/std/extern.sloth
index a31c2c1..89676c7 100644
--- a/std/extern.sloth
+++ b/std/extern.sloth
@@ -27,3 +27,8 @@ 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;
+
+#stdmem
+foreign fn memalloc(size: Int) Int;
+foreign fn drefi(loc: Int) Int;
+foreign fn assignrefi(loc: Int, num: Int);
diff --git a/std/stdio.c b/std/stdio.c
index b5f1dd0..e9cefd0 100644
--- a/std/stdio.c
+++ b/std/stdio.c
@@ -1,48 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
-char* readln() {
- char* str = malloc(128);
- scanf("%127s", str);
- return str;
+char *readln() {
+ char *str = malloc(128);
+ fgets(str, 127, stdin);
+ return str;
}
-void print(char *str) {
- fputs(str, stdout);
-}
+void print(char *str) { fputs(str, stdout); }
-void termpos(int x, int y) {
- printf("\x1b[%d;%dH", x, y);
-}
+void termpos(int x, int y) { printf("\x1b[%d;%dH", x, y); }
-void termclear() {
- printf("\x1b[2J\x1b[H");
-}
+void termclear() { printf("\x1b[2J\x1b[H"); }
-void curshide() {
- print("\x1b[?25l");
-}
+void curshide() { print("\x1b[?25l"); }
-void cursshow() {
- print("\x1b[?25h");
-}
+void cursshow() { print("\x1b[?25h"); }
-char* filer(char* path) {
- 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);
-
- contents = malloc(size);
- fread(contents, 1, size, fptr);
- fclose(fptr);
-
- return contents;
-}
+char *filer(char *path) {
+ 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);
+ contents = malloc(size);
+ fread(contents, 1, size, fptr);
+ fclose(fptr);
+
+ return contents;
+}
diff --git a/std/stdmem.c b/std/stdmem.c
new file mode 100644
index 0000000..c3d0300
--- /dev/null
+++ b/std/stdmem.c
@@ -0,0 +1,20 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+char *heap[100000];
+int heaploc = 0;
+
+int memalloc(int size) {
+ const int chunk = heaploc;
+ heap[heaploc++] = malloc(size);
+ printf("MEMALLOC: heap[%d]\n", chunk);
+ return chunk;
+}
+int drefi(int loc) {
+ printf("DREF: *heap[%d] = %d\n", loc, *heap[loc]);
+ return *heap[loc];
+}
+void assignrefi(int loc, int num) {
+ *heap[loc] = num;
+ printf("ASSREF: *heap[%d] = %d\n", loc, *heap[loc]);
+}
diff --git a/std/stdmem.sloth b/std/stdmem.sloth
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/std/stdmem.sloth