summaryrefslogtreecommitdiff
path: root/tests/printf.toc
blob: a6ef348f074f85cb913a978c69a12b6263ac03d7 (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
printf ::= #foreign("printf","libc.so.6") fn(#C &"const char", #C ..) #C int;


// NOTE: this doesn't work (e.g. "%%%")
tprintf_valid ::= fn(fmt :: []char, nargs: int) bool {
	if fmt[fmt.len-1] != '\0' {
		return false;
	}
	count := 0;
	for x, i := fmt {
		if x == '%' {
			if i == fmt.len-1 {
				count += 1;
			} elif fmt[i+1] != '%' {
				count += 1;
			} else {
				count -= 1;
			}
		}
	}
	return count == nargs;
};
	

tprintf ::= fn(fmt :: []char, args: ..) {
	#if !tprintf_valid(fmt, args.len) {
		#error "Invalid printf format";
	}
	f := fmt;
	printf(&f[0], args);
};

main ::= fn() {
	 tprintf("%d %d%%\n\0", 3 as #C int, 4 as #C int);
	 tprintf("%d %d %d%%\n\0", 3 as #C int, 4 as #C int, 5 as #C int);
	 tprintf("Hello!\n\0");
};