From 3a9f7e6c9e1f44385c1950edc6267af8eea56a7e Mon Sep 17 00:00:00 2001 From: Fabrice Date: Tue, 3 Mar 2026 09:42:37 +0100 Subject: formatting of tokens --- src/source.cc | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'src/source.cc') diff --git a/src/source.cc b/src/source.cc index a393213..bf00afc 100644 --- a/src/source.cc +++ b/src/source.cc @@ -1,19 +1,35 @@ #ifndef SOURCE_CC #define SOURCE_CC +#include #include "common.cc" #include "memory.cc" -typedef u32 Source_Id; - struct Span { String file; usize start, end; Span() : file(), start(0), end(0) {} - Span(String file, usize start, usize end) : file(file), start(start), end(end) {} + Span(String file, usize start, usize end) + : file(file), start(start), end(end) {} }; +bool span_write(const Span* span, FILE* stream) { + assert_neq(span, nullptr); + assert_neq(stream, nullptr); + + i32 rc = fprintf(stream, "Span { file: "); + if(unlikely(rc < 0)) return false; + + bool status = string_write(&span->file, stream); + if(unlikely(!status)) return false; + + rc = fprintf(stream, ", start: %zu, end: %zu }", span->start, span->end); + if(unlikely(rc < 0)) return false; + + return true; +} + struct Buffer { String file; String content; @@ -22,15 +38,17 @@ struct Buffer { usize cursor; }; -bool buffer_init(const Allocator* allocator, - const String* content_in, const String* file_in, Buffer** out) { +bool buffer_init(const Allocator* allocator, const String* content_in, + const String* file_in, Buffer** out) { assert_neq(allocator, nullptr); assert_neq(content_in, nullptr); assert_neq(file_in, nullptr); assert_neq(out, nullptr); + //TODO: use defer!! + Buffer* buffer = allocate(allocator, 1); - if(buffer == nullptr) return false; + if (buffer == nullptr) return false; memset(buffer, 0, sizeof(*buffer)); String content, file; @@ -59,8 +77,8 @@ void buffer_deinit(Buffer* buffer) { assert_neq(buffer, nullptr); const Allocator* allocator = buffer->allocator; - if(allocator == nullptr) return; - + if (allocator == nullptr) return; + slice_deallocate(buffer->allocator, &buffer->file); slice_deallocate(buffer->allocator, &buffer->content); -- cgit v1.2.3