diff options
Diffstat (limited to 'test.toc')
-rw-r--r-- | test.toc | 43 |
1 files changed, 20 insertions, 23 deletions
@@ -1,33 +1,30 @@ printf ::= #foreign("printf","libc.so.6") fn(#C &"const char", #C ..) #C int; -tprintf ::= fn(fmt: []char, args: ..) { - printf(&fmt[0], args); -}; - -sum ::= fn(x: ..) int { - total := 0; - for a, i := x { - total += a + i - i + 1; +tprintf_valid ::= fn(fmt :: []char, nargs: int) bool { + if fmt[fmt.len-1] != '\0' { + return false; } - total - x.len -}; - -sumc ::= fn(x:: ..) int { - total := 0; - for a, i := x { - total += a + i - i + 1; + count := 0; + for x, i := fmt { + if x == '%' { + if i == fmt.len-1 { + count += 1; + } elif fmt[i+1] != '%' { + count += 1; + } else { + count -= 1; + } + } } - total - x.len + count == nargs }; + -do_printing ::= fn(x::..) { - tprintf("%ld\n",sum(x)); - tprintf("%ld\n",sumc(x)); +tprintf ::= fn(fmt :: []char, args: ..) where tprintf_valid(fmt, args.len) { + f := fmt; + printf(&f[0], args); }; main ::= fn() { - do_printing(); - do_printing(1,2,3); - do_printing(4); - do_printing(1,10,100,1000,10000); + tprintf("%d %d%%\n\0", 3, 4); }; |