summaryrefslogtreecommitdiff
path: root/std/io.toc
blob: 17c4bbb236dec50c18ef56dd7adbe0c9c962f4c2 (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
putchar ::= #foreign("putchar", "libc.so.6") fn(#C int) #C int;
toc_putchar ::= fn(x: char) {
	putchar(x as #C int);
};

fwrite ::= #foreign("fwrite", "libc.so.6") fn(#C &"void", #C size_t, #C size_t, #C &"void") #C size_t;

stdout_fwrite ::= fn(data: &u8, size: u64, nmemb: u64) {
	fwrite(data, size as #C size_t, nmemb as #C size_t, #builtin("stdout"));
};

puts ::= fn(x: []char) {
	stdout_fwrite(&x[0] as &u8, 1, x.len as u64);
	toc_putchar('\n');
};

puti ::= 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;
		}
	}
	toc_putchar('\n');
};