diff options
-rw-r--r-- | cgen.c | 5 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | test.toc | 54 |
3 files changed, 35 insertions, 26 deletions
@@ -1337,11 +1337,6 @@ static void cgen_expr(CGenerator *g, Expression *e) { cgen_write(g, "%.16Lf", (long double)e->floatl); break; case EXPR_LITERAL_INT: - /* make sure it's the right type (for variadic foreign functions) */ - cgen_write(g, "("); - cgen_type_pre(g, &e->type); - cgen_type_post(g, &e->type); - cgen_write(g, ")"); cgen_write(g, U64_FMT, e->intl); break; case EXPR_LITERAL_STR: { @@ -8,9 +8,9 @@ /* TODO: -cast varargs, not integer literals in cgen - make new(s) and del functions! defer +use &&, || start making a standard library... (printf; stringbuilder would be nice to have) switch @@ -1,23 +1,37 @@ -#include "std/io.toc", io; +printf ::= #foreign("printf","libc.so.6") fn(#C &"const char", #C ..) #C int; -Point3D ::= struct { - x, y, z: f32; -} + +// 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() { - p: Point3D; - io.puti(#sizeof Point3D); - // io.puti(#sizeof p); - io.puti(#sizeof typeof p); - io.puti(sizeof Point3D); - // io.puti(sizeof p); - io.puti(sizeof typeof p); - - io.puti(#alignof Point3D); - // io.puti(#alignof p); - io.puti(#alignof typeof p); - io.puti(alignof Point3D); - // io.puti(alignof p); - io.puti(alignof typeof p); -} -main();
\ No newline at end of file + tprintf("%d %d%%\n\0", 3, 4); + tprintf("%d %d %d%%\n\0", 3, 4, 5); + tprintf("Hello!\n\0"); +}; |