Skip to content

Commit

Permalink
Interface based streams. Fix for initializing with a force unwrap ins…
Browse files Browse the repository at this point in the history
…ide. Allow $define to take a list. Allow $define to return error on argument type mismatch in call. Fixed broken bit operations on boolean vectors.
  • Loading branch information
lerno committed Oct 30, 2023
1 parent e4c1328 commit ee34644
Show file tree
Hide file tree
Showing 42 changed files with 776 additions and 877 deletions.
10 changes: 5 additions & 5 deletions lib/std/core/allocators/temp_allocator.c3
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,21 @@ fn void*! TempAllocator.acquire(&self, usz size, bool clear, usz alignment, usz
return &page.data[0];
}

fn void TempAllocator.print_pages(&self, File f)
fn void! TempAllocator.print_pages(&self, File f)
{
TempAllocatorPage *last_page = self.last_page;
if (!last_page)
{
f.printf("No pages.\n");
io::fprintf(&f, "No pages.\n")!;
return;
}
f.printf("---Pages----\n");
io::fprintf(&f, "---Pages----\n")!;
uint index = 0;
while (last_page)
{
bool is_not_aligned = !(last_page.size & (1u64 << 63));
f.printf("%d. Alloc: %d %d at %p%s\n", ++index,
last_page.size & ~(1u64 << 63), last_page.mark, &last_page.data[0], is_not_aligned ? "" : " [aligned]");
io::fprintf(&f, "%d. Alloc: %d %d at %p%s\n", ++index,
last_page.size & ~(1u64 << 63), last_page.mark, &last_page.data[0], is_not_aligned ? "" : " [aligned]")!;
last_page = last_page.prev_page;
}
}
40 changes: 20 additions & 20 deletions lib/std/core/builtin.c3
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,33 @@ fn bool print_backtrace(String message, int backtraces_to_ignore) @if(env::DARWI
BacktraceList! backtrace = darwin::backtrace_load(mem::temp());
if (catch backtrace) return false;
if (backtrace.len() <= backtraces_to_ignore) return false;
(void)io::stderr().print("\nERROR: '");
(void)io::stderr().print(message);
io::printn("'");
io::eprint("\nERROR: '");
io::eprint(message);
io::eprintn("'");
foreach (i, &trace : backtrace)
{
if (i < backtraces_to_ignore) continue;
if (trace.is_unknown())
{
(void)io::stderr().printn(" in ???");
io::eprintn(" in ???");
continue;
}
if (trace.has_file())
{
(void)io::stderr().printfn(" in %s (%s:%d) [%s]", trace.function, trace.file, trace.line, trace.object_file);
io::eprintfn(" in %s (%s:%d) [%s]", trace.function, trace.file, trace.line, trace.object_file);
continue;
}
(void)io::stderr().printfn(" in %s (source unavailable) [%s]", trace.function, trace.object_file);
io::eprintfn(" in %s (source unavailable) [%s]", trace.function, trace.object_file);
}
return true;
};
}
fn void default_panic(String message, String file, String function, uint line) @if(env::DARWIN)
{
$if $defined(io::stderr) && $defined(Stream.printf):
$if $defined(io::stderr):
if (!print_backtrace(message, 2))
{
(void)io::stderr().printfn("\nERROR: '%s', in %s (%s:%d)", message, function, file, line);
io::eprintfn("\nERROR: '%s', in %s (%s:%d)", message, function, file, line);
return;
}
$endif
Expand All @@ -130,23 +130,23 @@ fn void default_panic(String message, String file, String function, uint line) @
fn void default_panic(String message, String file, String function, uint line) @if(!env::DARWIN)
{
CallstackElement* stack = $$stacktrace();
$if $defined(io::stderr) && $defined(Stream.printf):
$if $defined(io::stderr):
if (stack) stack = stack.prev;
if (stack)
{
(void)io::stderr().print("\nERROR: '");
(void)io::stderr().print(message);
(void)io::stderr().printn("'");
io::eprint("\nERROR: '");
io::eprint(message);
io::eprintn("'");
}
else
{
(void)io::stderr().print("\nERROR: '");
(void)io::stderr().print(message);
(void)io::stderr().printfn("', in %s (%s:%d)", function, file, line);
io::eprint("\nERROR: '");
io::eprint(message);
io::eprintfn("', in %s (%s:%d)", function, file, line);
}
while (stack)
{
(void)io::stderr().printfn(" in %s %s (%s:%d)", stack.location.name, stack.function, stack.file, stack.line);
io::eprintfn(" in %s %s (%s:%d)", stack.location.name, stack.function, stack.file, stack.line);
if (stack == stack.prev) break;
stack = stack.prev;
}
Expand Down Expand Up @@ -398,10 +398,10 @@ fn void sig_bus_error(CInt i)
$if !env::DARWIN:
sig_panic("Illegal memory access.");
$else
$if $defined(io::stderr) && $defined(Stream.printf):
$if $defined(io::stderr):
if (!print_backtrace("Illegal memory access.", 1))
{
(void)io::stderr().printn("\nERROR: 'Illegal memory access'.");
io::eprintn("\nERROR: 'Illegal memory access'.");
}
$endif
$endif
Expand All @@ -413,10 +413,10 @@ fn void sig_segmentation_fault(CInt i)
$if !env::DARWIN:
sig_panic("Out of bounds memory access.");
$else
$if $defined(io::stderr) && $defined(Stream.printf):
$if $defined(io::stderr):
if (!print_backtrace("Out of bounds memory access.", 1))
{
(void)io::stderr().printn("\nERROR: Memory error without backtrace, possible stack overflow.");
io::eprintn("\nERROR: Memory error without backtrace, possible stack overflow.");
}
$endif
$endif
Expand Down
4 changes: 2 additions & 2 deletions lib/std/core/builtin_comparison.c3
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ macro greater_eq(a, b) @builtin
macro bool equals(a, b) @builtin
{
$switch
$case $defined(a.equals):
$case $defined(a.equals, a.equals(b)):
return a.equals(b);
$case $defined(a.compare_to):
$case $defined(a.compare_to, a.compare_to(b)):
return a.compare_to(b) == 0;
$case $defined(a.less):
return !a.less(b) && !b.less(a);
Expand Down
22 changes: 17 additions & 5 deletions lib/std/core/dstring.c3
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module std::core::dstring;
import std::io;

distinct DString = void*;
distinct DString (OutStream) = void*;

const usz MIN_CAPACITY @private = 16;

Expand Down Expand Up @@ -79,9 +80,9 @@ fn usz DString.capacity(self)
return self.data().capacity;
}

fn usz DString.len(self)
fn usz DString.len(&self) @dynamic
{
if (!self) return 0;
if (!*self) return 0;
return self.data().len;
}

Expand Down Expand Up @@ -255,6 +256,17 @@ fn void DString.clear(self)
self.data().len = 0;
}

fn usz! DString.write(&self, char[] buffer) @dynamic
{
self.append_chars((String)buffer);
return buffer.len;
}

fn void! DString.write_byte(&self, char c) @dynamic
{
self.append_char(c);
}

fn void DString.append_char(&self, char c)
{
if (!*self)
Expand Down Expand Up @@ -387,9 +399,9 @@ fn void DString.reserve(&self, usz addition)
*self = (DString)realloc(data, StringData.sizeof + new_capacity, .using = data.allocator);
}

fn usz! DString.read_from_stream(&self, Stream* reader)
fn usz! DString.read_from_stream(&self, InStream* reader)
{
if (reader.supports_available())
if (&reader.available)
{
usz total_read = 0;
while (usz available = reader.available()!)
Expand Down
12 changes: 6 additions & 6 deletions lib/std/encoding/csv.c3
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import std::io;

struct CsvReader
{
Stream* stream;
InStream* stream;
String separator;
}

fn void CsvReader.init(&self, Stream* stream, String separator = ",")
fn void CsvReader.init(&self, InStream* stream, String separator = ",")
{
self.stream = stream;
self.separator = separator;
}

fn String[]! CsvReader.read_row(self, Allocator* using = mem::heap())
{
@pool()
@pool(using)
{
return self.stream.treadline().split(self.separator, .using = using);
return io::treadline(self.stream).split(self.separator, .using = using);
};
}

Expand All @@ -30,13 +30,13 @@ fn void! CsvReader.skip_row(self) @maydiscard
{
@pool()
{
self.stream.readline(mem::temp())!;
(void)io::treadline(self.stream);
};
}

macro CsvReader.@each_row(self, int rows = int.max; @body(String[] row))
{
Stream* stream = self.stream;
InputStream* stream = self.stream;
String sep = self.separator;
while (rows--)
{
Expand Down
4 changes: 2 additions & 2 deletions lib/std/encoding/json.c3
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fault JsonParsingError
INVALID_NUMBER,
}

fn Object*! parse(Stream* s, Allocator* using = mem::heap())
fn Object*! parse(InStream* s, Allocator* using = mem::heap())
{
JsonContext context = { .last_string = dstring::new_with_capacity(64, using), .stream = s, .allocator = using };
defer context.last_string.free();
Expand Down Expand Up @@ -44,7 +44,7 @@ enum JsonTokenType @local
struct JsonContext @local
{
uint line;
Stream* stream;
InStream* stream;
Allocator* allocator;
JsonTokenType token;
DString last_string;
Expand Down
18 changes: 6 additions & 12 deletions lib/std/io/bits.c3
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ module std::io;

struct BitReader
{
Stream reader;
InStream* reader;
uint bits;
uint len;
}

/**
* @require byte_reader.supports_read_byte()
**/
fn void BitReader.init(&self, Stream byte_reader)
fn void BitReader.init(&self, InStream* byte_reader)
{
*self = { .reader = byte_reader };
}
Expand Down Expand Up @@ -43,15 +40,12 @@ fn char! BitReader.read_bits(&self, uint nbits)

struct BitWriter
{
Stream writer;
OutStream* writer;
uint bits;
uint len;
}

/**
* @require byte_writer.supports_write_byte()
**/
fn void BitWriter.init(&self, Stream byte_writer)
fn void BitWriter.init(&self, OutStream* byte_writer)
{
*self = { .writer = byte_writer };
}
Expand All @@ -63,7 +57,7 @@ fn void! BitWriter.flush(&self)
uint n = (self.len + 7) / 8;
char[4] buffer;
bitorder::write(bits, &buffer, UIntBE);
self.writer.write_all(buffer[:n])!;
io::write_all(self.writer, buffer[:n])!;
self.len = 0;
}

Expand All @@ -83,7 +77,7 @@ fn void! BitWriter.write_bits(&self, uint bits, uint nbits)
lbits |= (ulong)(bits >> left) << (64 - (n - left));
char[8] buffer;
bitorder::write(lbits, &buffer, ULongBE);
self.writer.write_all(buffer[:to_write])!;
io::write_all(self.writer, buffer[:to_write])!;
}
self.bits <<= left;
self.bits |= bits & ((1 << left) - 1);
Expand Down
29 changes: 9 additions & 20 deletions lib/std/io/file.c3
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module std::io;
import libc;

struct File
struct File (InStream, OutStream)
{
inline Stream stream;
CFile file;
}

Expand All @@ -22,7 +21,7 @@ fn File! open_path(Path path, String mode)

fn File from_handle(CFile file)
{
return { .stream.fns = &FILESTREAM_INTERFACE, .file = file };
return { .file = file };
}

fn bool is_file(String path)
Expand All @@ -49,7 +48,7 @@ fn void! File.reopen(&self, String filename, String mode)
/**
* @require self.file != null
**/
fn usz! File.seek(&self, isz offset, Seek seek_mode = Seek.SET)
fn usz! File.seek(&self, isz offset, Seek seek_mode = Seek.SET) @dynamic
{
os::native_fseek(self.file, offset, seek_mode)!;
return os::native_ftell(self.file);
Expand All @@ -75,15 +74,15 @@ fn void! File.memopen(File* file, char[] data, String mode)
/**
* @require self.file != null
*/
fn void! File.write_byte(&self, char c)
fn void! File.write_byte(&self, char c) @dynamic
{
if (!libc::fputc(c, self.file)) return IoError.EOF?;
}

/**
* @param [&inout] self
*/
fn void! File.close(&self) @inline
fn void! File.close(&self) @inline @dynamic
{
if (self.file && libc::fclose(self.file))
{
Expand Down Expand Up @@ -117,7 +116,7 @@ fn bool File.eof(&self) @inline
/**
* @param [in] buffer
*/
fn usz! File.read(&self, char[] buffer)
fn usz! File.read(&self, char[] buffer) @dynamic
{
return os::native_fread(self.file, buffer);
}
Expand All @@ -126,13 +125,13 @@ fn usz! File.read(&self, char[] buffer)
* @param [out] buffer
* @require self.file `File must be initialized`
*/
fn usz! File.write(&self, char[] buffer)
fn usz! File.write(&self, char[] buffer) @dynamic
{
return os::native_fwrite(self.file, buffer);
}


fn char! File.read_byte(&self)
fn char! File.read_byte(&self) @dynamic
{
int c = libc::fgetc(self.file);
if (c == -1) return IoError.EOF?;
Expand All @@ -142,17 +141,7 @@ fn char! File.read_byte(&self)
/**
* @require self.file `File must be initialized`
*/
fn void! File.flush(&self)
fn void! File.flush(&self) @dynamic
{
libc::fflush(self.file);
}

const StreamInterface FILESTREAM_INTERFACE = {
.close_fn = (CloseStreamFn)&File.close,
.seek_fn = (SeekStreamFn)&File.seek,
.read_fn = (ReadStreamFn)&File.read,
.read_byte_fn = (ReadByteStreamFn)&File.read_byte,
.write_fn = (WriteStreamFn)&File.write,
.write_byte_fn = (WriteByteStreamFn)&File.write_byte,
.flush_fn = (FlushStreamFn)&File.flush
};
Loading

0 comments on commit ee34644

Please sign in to comment.