summaryrefslogtreecommitdiff
path: root/tests/std
diff options
context:
space:
mode:
Diffstat (limited to 'tests/std')
-rw-r--r--tests/std/arr.toc29
-rw-r--r--tests/std/base.toc23
-rw-r--r--tests/std/io.toc30
-rw-r--r--tests/std/mem.toc6
4 files changed, 82 insertions, 6 deletions
diff --git a/tests/std/arr.toc b/tests/std/arr.toc
new file mode 100644
index 0000000..a2d202b
--- /dev/null
+++ b/tests/std/arr.toc
@@ -0,0 +1,29 @@
+// this could be made quite a bit faster with realloc
+
+Arr ::= struct (t :: Type) {
+ data : []t;
+ cap : int;
+};
+
+resv ::= fn(t ::=, a : &Arr(t), n: int) {
+ if a.cap >= n {
+ return;
+ }
+ a.cap = n;
+ new_data := new(t, a.cap);
+ new_data.len = a.data.len;
+ for x, i := &new_data {
+ *x = a.data[i];
+ }
+ a.data = new_data;
+};
+
+add ::= fn(t ::=, a : &Arr(t), x : t) {
+ if a.data.len >= a.cap {
+ resv(a, a.cap * 2 + 2);
+ }
+ a.data.len += 1;
+ a.data[a.data.len-1] = x;
+};
+
+len ::= fn(t ::=, a : Arr(t)) int { a.data.len };
diff --git a/tests/std/base.toc b/tests/std/base.toc
new file mode 100644
index 0000000..bdb91e0
--- /dev/null
+++ b/tests/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/tests/std/io.toc b/tests/std/io.toc
index 2c82dc9..994cd2a 100644
--- a/tests/std/io.toc
+++ b/tests/std/io.toc
@@ -1,12 +1,22 @@
-putchar ::= #foreign("putchar", "libc.so.6") fn(#C int) #C int;
+/* @TODO: use write / WriteFile */
+
+#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;
+
+/*
+unfortunately, we can't use fwrite because MSVC doesn't like it when you
+use a file handle that's not from the DLL. (i.e. you can't pass your stdout
+to the imported version of fwrite)
+*/
writes ::= fn(x: []char) {
- printf_strfmt := "%s\0";
- printf(&printf_strfmt[0], &x[0]);
+ for c := x {
+ toc_putchar(c);
+ }
}
puts ::= fn(x: []char) {
@@ -41,3 +51,15 @@ puti ::= fn(x: int) {
toc_putchar('\n');
}
+/* @TODO: write your own version of this */
+printf ::= #foreign("printf", libc) fn(#C &"const char", #C ..) #C int;
+writef ::= fn(x: float) {
+ fmt := "%f\0";
+ printf(&fmt[0], x as f64);
+};
+putf ::= fn(x: float) {
+ writef(x);
+ toc_putchar('\n');
+}
+
+
diff --git a/tests/std/mem.toc b/tests/std/mem.toc
index 6d16895..e283364 100644
--- a/tests/std/mem.toc
+++ b/tests/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)