summaryrefslogtreecommitdiff
path: root/tests/printf.toc
diff options
context:
space:
mode:
Diffstat (limited to 'tests/printf.toc')
-rw-r--r--tests/printf.toc37
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/printf.toc b/tests/printf.toc
new file mode 100644
index 0000000..e030af4
--- /dev/null
+++ b/tests/printf.toc
@@ -0,0 +1,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;
+ }
+ }
+ }
+ 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, 4);
+ tprintf("%d %d %d%%\n\0", 3, 4, 5);
+ tprintf("Hello!\n\0");
+};