summaryrefslogtreecommitdiff
path: root/std/io.toc
blob: fac492185c4f8403609a8420ef33c06cd968a44e (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
get_type_with_size ::= fn(size :: i64) Type {
	if size == 1 { i8 }
	elif size == 2 { i16 }
	elif size == 4 { i32 }
	elif size == 8 { i64 }
	else { f32 }
};

get_utype_with_size ::= fn(size :: i64) Type {
	if size == 1 { u8 }
	elif size == 2 { u16 }
	elif size == 4 { u32 }
	elif size == 8 { u64 }
	else { f32 }
};

c_int ::= get_type_with_size(#builtin("sizeof int"));
c_size_t ::= get_utype_with_size(#builtin("sizeof size_t"));

c_putchar :: fn(c_int) c_int = #foreign "putchar", "libc.so.6";
toc_putchar ::= fn(x: char) {
		c_putchar(x as c_int);
};

c_fwrite :: fn(&u8, c_size_t, c_size_t, &u8) = #foreign "fwrite", "libc.so.6";

stdout_fwrite ::= fn(data: &u8, size: u64, nmemb: u64) {
	c_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');
};