summaryrefslogtreecommitdiff
path: root/tests/std/io.toc
blob: c274631fba667b5bd48b1b52bf9db25dd6cba745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* @TODO: use write / WriteFile */

#include "std/base.toc", base;

putchar ::= #foreign("putchar", base.libc) fn(#C int) #C int;
toc_putchar ::= fn(x: char) {
	putchar(x as #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) {
	for c := x {
		toc_putchar(c);
	}
}

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 {
				return -x; 
			} else {
				return 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');
}

printf ::= #foreign("printf", base.libc) fn (#C &"const char", #C ..) #C int;
writef ::= fn(x: float) {
	fmt := "%f\0";
	printf(&fmt[0], x);
}
putf ::= fn(x: float) {
	writef(x);
	toc_putchar('\n');
}