From 5044062793b482155fb7995881e6e6dac326b96a Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Tue, 7 Jul 2020 17:42:33 -0400 Subject: oops fclose fixed --- .gitignore | 5 ++++- std/io.toc | 34 +++++++++++++++++++--------------- test.toc | 13 ++----------- types.c | 2 +- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 450a731..5cbc15f 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,7 @@ std/*.o *.obj *.exe .vs -test.txt +test*.txt +c_test +toc_test +test.c diff --git a/std/io.toc b/std/io.toc index 932cf45..daa060f 100644 --- a/std/io.toc +++ b/std/io.toc @@ -20,7 +20,7 @@ raw_stdout - standard output raw_stderr - standard error raw_file_write - write to a raw file raw_file_open_write - open a raw file for writing -raw_file_close - flush and close a raw file +raw_file_close - close a raw file */ @@ -37,16 +37,16 @@ raw_file_close - flush and close a raw file O_CREAT ::= 0x40; RawFile ::= int; - raw_file_write ::= fn(file: RawFile, buf: &void, size: int) bool { + raw_file_write ::= fn(file: RawFile, buf: &void, size: int) err := FILE_ERR_OK { while size > 0 { bytes_written := write(file as #C int, buf, size as #C size_t); if bytes_written < 0 { - return false; + err = FILE_ERR_MISC; // @TODO + return; } size -= bytes_written; buf += bytes_written; } - return true; } raw_stdout ::= fn() RawFile { return 1; @@ -121,42 +121,46 @@ fopen_write ::= fn(name: []char) f: &File, error: FileError { } } -flush ::= fn(use f: &File) { - raw_file_write(raw, &buffer[0], buffer_used); +flush ::= fn(use f: &File) err: FileError { + err = raw_file_write(raw, &buffer[0], buffer_used); buffer_used = 0; } fclose ::= fn(f: &File) err: FileError { + flush(f); err = raw_file_close(f.raw); mem.del(f); } -fwrites ::= fn(use f: &File, s : []char) { +fwrites ::= fn(use f: &File, s : []char) FileError { if f.nobuffer { - raw_file_write(raw, &s[0], s.len); - return; + return raw_file_write(raw, &s[0], s.len); } if buffer_used + s.len > BUFSZ { flush(f); if s.len > BUFSZ { - raw_file_write(raw, &s[0], s.len); + return raw_file_write(raw, &s[0], s.len); } else { mem.mem_copy(&buffer[0], &s[0], s.len); buffer_used = s.len; + return FILE_ERR_OK; } } else { mem.mem_copy(&buffer[buffer_used], &s[0], s.len); buffer_used += s.len; + return FILE_ERR_OK; } } -fputs ::= fn(f: &File, s: []char) { - fwrites(f, s); - fwrites(f, "\n"); +fputs ::= fn(f: &File, s: []char) err : FileError { + err = fwrites(f, s); + if !err { + err = fwrites(f, "\n"); + } } -puts ::= fn(s: []char) { - fputs(&std_out, s); +puts ::= fn(s: []char) FileError { + return fputs(&std_out, s); } diff --git a/test.toc b/test.toc index 8523940..79eb3b3 100644 --- a/test.toc +++ b/test.toc @@ -6,17 +6,8 @@ main ::= fn() { if err { base.error("Couldn't open file!"); } - io.fputs(file, "This file has some stuff in it."); - io.puts("Hello!"); - io.puts("yes"); - io.fputs(file, "here is more stuff for the file."); - for i := 1..1000 { - io.fwrites(file, "Here's a line in the file"); - for j := 1..i { - io.fwrites(file, "!"); - } - io.fwrites(file, "\n"); + for i := 1..100000000 { + io.fputs(file, "Hello"); } io.fclose(file); } -main(); diff --git a/types.c b/types.c index f50bebd..f181f30 100644 --- a/types.c +++ b/types.c @@ -3732,7 +3732,7 @@ top: r->expr.type = tr->fn->ret_type; } } else { - if (!type_is_void(&tr->fn->ret_type) || tr->fn->ret_decls) { + if (!type_is_void(&tr->fn->ret_type) && !tr->fn->ret_decls) { err_print(s->where, "No return value in non-void function."); return false; } -- cgit v1.2.3