summaryrefslogtreecommitdiff
path: root/tests/std
diff options
context:
space:
mode:
Diffstat (limited to 'tests/std')
-rw-r--r--tests/std/io.toc43
-rw-r--r--tests/std/mem.toc23
2 files changed, 66 insertions, 0 deletions
diff --git a/tests/std/io.toc b/tests/std/io.toc
new file mode 100644
index 0000000..2c82dc9
--- /dev/null
+++ b/tests/std/io.toc
@@ -0,0 +1,43 @@
+putchar ::= #foreign("putchar", "libc.so.6") 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;
+
+writes ::= fn(x: []char) {
+ printf_strfmt := "%s\0";
+ printf(&printf_strfmt[0], &x[0]);
+}
+
+puts ::= fn(x: []char) {
+ writes(x);
+ toc_putchar('\n');
+}
+
+writei ::= fn(x: int) {
+ if x < 0 {
+ toc_putchar('-');
+ // NOTE: don't do x = -x; here to make sure I64_MIN works
+ }
+ if x == 0 {
+ toc_putchar('0');
+ } else {
+ abs ::= fn(x: int) int { if x < 0 { -x } else { x } };
+ scan_digit := 1000000000000000000;
+ started := false;
+ while scan_digit > 0 {
+ digit := abs((x / scan_digit) % 10);
+ if digit > 0 { started = true; }
+ if started {
+ toc_putchar((('0' as int) + digit) as char);
+ }
+ scan_digit /= 10;
+ }
+ }
+}
+
+puti ::= fn(x: int) {
+ writei(x);
+ toc_putchar('\n');
+}
+
diff --git a/tests/std/mem.toc b/tests/std/mem.toc
new file mode 100644
index 0000000..6d16895
--- /dev/null
+++ b/tests/std/mem.toc
@@ -0,0 +1,23 @@
+// 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");
+
+new ::= fn(t :: Type) &t {
+ calloc(1, (sizeof t) as #C size_t)
+}
+
+news ::= fn(t :: Type, n : int) []t {
+ s: []t;
+ s.data = calloc(n as #C size_t, (sizeof t) as #C size_t);
+ s.len = n;
+ s
+}
+
+// TODO(eventually): use type information to make this just one function
+del ::= fn(t::=, x: &t) {
+ free(x);
+}
+
+dels ::= fn(t::=, x: []t) {
+ free(x.data);
+}