summaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-05-03 15:19:45 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2020-05-03 15:19:45 -0400
commit9661c9839d6635181b81e0a1061dfa566f7660fe (patch)
treec9298875e57c08985f376af36bf346bb048befbd /std
parent3fcabe8edbe4213ab1d5a65c584ee332c1b26baf (diff)
trying to make std/*.toc portable, fwrite not working for some reason
Diffstat (limited to 'std')
-rw-r--r--std/base.toc23
-rw-r--r--std/io.toc9
-rw-r--r--std/mem.toc6
3 files changed, 32 insertions, 6 deletions
diff --git a/std/base.toc b/std/base.toc
new file mode 100644
index 0000000..bdb91e0
--- /dev/null
+++ b/std/base.toc
@@ -0,0 +1,23 @@
+PLATFORM_OTHER ::= 0;
+PLATFORM_LINUX ::= 1;
+PLATFORM_WINDOWS ::= 2;
+PLATFORM_OSX ::= 3;
+PLATFORM_FREEBSD ::= 4;
+PLATFORM_OPENBSD ::= 5;
+PLATFORM_MISC_UNIX ::= 6;
+
+PLATFORM ::= #builtin("platform");
+#if PLATFORM == PLATFORM_LINUX {
+ libc ::= "libc.so.6";
+} elif PLATFORM == PLATFORM_WINDOWS {
+ libc ::= "msvcrt.dll";
+} elif PLATFORM == PLATFORM_OSX {
+ libc ::= "libc.dylib";
+} elif PLATFORM == PLATFORM_FREEBSD || PLATFORM == PLATFORM_OPENBSD {
+ libc ::= "libc.so";
+} else {
+ /* maybe it's non-linux gnu? */
+ libc ::= "libc.so.6";
+}
+
+
diff --git a/std/io.toc b/std/io.toc
index 2c82dc9..c3d8f71 100644
--- a/std/io.toc
+++ b/std/io.toc
@@ -1,12 +1,13 @@
-putchar ::= #foreign("putchar", "libc.so.6") fn(#C int) #C int;
+#include "std/base.toc";
+
+putchar ::= #foreign("putchar", libc) fn(#C int) #C int;
toc_putchar ::= fn(x: char) {
putchar(x as #C int);
}
-printf ::= #foreign("printf", "libc.so.6") fn(#C &"char const", #C ..) #C int;
+fwrite ::= #foreign("fwrite", libc) fn(#C &"const void", #C size_t, #C size_t, #C &"FILE") #C size_t;
writes ::= fn(x: []char) {
- printf_strfmt := "%s\0";
- printf(&printf_strfmt[0], &x[0]);
+ fwrite(&x[0], 1, x.len as #C size_t, #builtin("stdout"));
}
puts ::= fn(x: []char) {
diff --git a/std/mem.toc b/std/mem.toc
index 6d16895..e283364 100644
--- a/std/mem.toc
+++ b/std/mem.toc
@@ -1,6 +1,8 @@
+#include "std/base.toc";
+
// TODO: check for failed calloc
-calloc ::= #foreign("calloc", "libc.so.6") fn(#C size_t, #C size_t) #C &"void";
-free ::= #foreign("free", "libc.so.6") fn(#C &"void");
+calloc ::= #foreign("calloc", libc) fn(#C size_t, #C size_t) #C &"void";
+free ::= #foreign("free", libc) fn(#C &"void");
new ::= fn(t :: Type) &t {
calloc(1, (sizeof t) as #C size_t)