summaryrefslogtreecommitdiff
path: root/tests/std/io.toc
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-04-25 16:10:45 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2020-04-25 16:10:45 -0400
commita161b75a01eee7249f989c3a85d92b69c1818370 (patch)
treefb65753b6e84b102787523a8948bc5507400f3b3 /tests/std/io.toc
parent7ee5fc2721e40471f01f9377dd901ded4b969a33 (diff)
stop copying whole fn body unless necessary when generating an instance
Diffstat (limited to 'tests/std/io.toc')
-rw-r--r--tests/std/io.toc43
1 files changed, 43 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');
+}
+