diff options
-rw-r--r-- | decls_cgen.c | 5 | ||||
-rw-r--r-- | parse.c | 1 | ||||
-rw-r--r-- | std/base.toc | 4 | ||||
-rw-r--r-- | std/io.toc | 94 | ||||
-rw-r--r-- | test.toc | 5 |
5 files changed, 56 insertions, 53 deletions
diff --git a/decls_cgen.c b/decls_cgen.c index 5a85b09..97c63ec 100644 --- a/decls_cgen.c +++ b/decls_cgen.c @@ -277,7 +277,10 @@ static void cgen_fn_decl(CGenerator *g, FnExpr *f, Type *t) { cgen_ctype(g, csub); } } - cgen_write(g, ") = %s;", foreign_name); + cgen_write(g, ")"); + if (ctypes[0].kind == CTYPE_NONE) + cgen_type_post(g, &fn_types[0]); + cgen_write(g, "= %s;", foreign_name); } cgen_nl(g); return; @@ -823,6 +823,7 @@ static Status parse_block(Parser *p, Block *b, U8 flags) { Block *prev_block = p->block; b->flags = 0; b->kind = BLOCK_OTHER; + b->uses = NULL; b->ret_expr = NULL; assert(p->block != b); b->parent = p->block; diff --git a/std/base.toc b/std/base.toc index bdb91e0..08c5e53 100644 --- a/std/base.toc +++ b/std/base.toc @@ -20,4 +20,8 @@ PLATFORM ::= #builtin("platform"); libc ::= "libc.so.6"; } +PLATFORM_IS_UNIX :: bool = PLATFORM == PLATFORM_LINUX || PLATFORM == PLATFORM_OSX || PLATFORM == PLATFORM_FREEBSD + || PLATFORM == PLATFORM_OPENBSD || PLATFORM == PLATFORM_MISC_UNIX; +// @TODO: write your own +memcpy ::= #foreign("memcpy", libc) fn(&void, #C &"const void", #C size_t) &void; @@ -1,61 +1,57 @@ -/* @TODO: use write / WriteFile */ - #include "std/base.toc", base; -putchar ::= #foreign("putchar", base.libc) fn(#C int) #C int; -toc_putchar ::= fn(x: char) { - putchar(x as #C int); -} -printf ::= #foreign("printf", base.libc) fn (#C &"const char", #C ..) #C int; - -/* -unfortunately, we can't use fwrite because MSVC doesn't like it when you -use a file handle that's not from the DLL. (i.e. you can't pass your stdout -to the imported version of fwrite) -*/ -writes ::= fn(x: []char) { - for c := x { - toc_putchar(c); +#if base.PLATFORM_IS_UNIX { + // @TODO: use syscall instead + write ::= #foreign("write", "libc.so.6") fn(#C int, #C &"const void", #C size_t) #C long; + RawFile ::= int; + raw_file_write ::= fn(file: RawFile, buf: &void, size: int) int { + write(file as #C int, buf, size as #C size_t) as int + } + get_stdout ::= fn() RawFile { + 1 + } +} else { + // @TODO: on windows, use WriteFile + fwrite ::= #foreign("fwrite", base.libc) fn(#C &"const void", #C size_t, #C size_t, &void) #C size_t; + RawFile ::= &void; + raw_file_write ::= fn(file: RawFile, buf: &void, size: int) int { + fwrite(buf, 1, size, file); + } + get_stdout ::= fn() RawFile { + #builtin("stdout") } } -puts ::= fn(x: []char) { - writes(x); - toc_putchar('\n'); -} -writei ::= 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; - } - } +File ::= struct { + BUFSZ ::= 4096; + raw : RawFile; + written : int; // ranges from 0 to FILE_BUFSZ-1 + buffer : [BUFSZ]char; } -puti ::= fn(x: int) { - writei(x); - toc_putchar('\n'); +raw_file_to_file ::= fn(raw : RawFile) f: File { + f.raw = raw; } -writef ::= fn(x: float) { - fmt := "%f\0"; - printf(&fmt[0], x as f64); +std_out := raw_file_to_file(get_stdout()); + +file_flush ::= fn(use f: &File) { + raw_file_write(raw, &buffer[0], written); + written = 0; } -putf ::= fn(x: float) { - writef(x); - toc_putchar('\n'); + +file_writes ::= fn(use f: &File, s : []char) { + if written + s.len > BUFSZ { + file_flush(f); + if s.len > BUFSZ { + raw_file_write(raw, &s[0], s.len); + } else { + base.memcpy(&buffer[0], &s[0], s.len as #C size_t); + written = s.len; + } + } else { + base.memcpy(&buffer[written], &s[0], s.len as #C size_t); + written += s.len; + } } @@ -1,7 +1,6 @@ #include "std/io.toc", io; -#include "std/base.toc", base; main ::= fn() { - io.putf(3.7); + io.file_writes(&io.std_out, "hello!\n"); + io.file_flush(&io.std_out); } -main(); |