package-type imports

This commit is contained in:
2026-06-10 00:08:33 +02:00
parent 087fdb45d5
commit d6e03b6f08
104 changed files with 1799 additions and 273 deletions
+64 -6
View File
@@ -4,24 +4,57 @@ import "core:fmt"
import "core:mem"
Span :: struct {
file: int,
start: int,
end: int,
}
Source :: struct {
id: int,
path: string,
text: string,
}
Store :: struct {
items: [dynamic]Source,
allocator: mem.Allocator,
}
Diagnostic :: struct {
span: Span,
message: string,
}
Diagnostics :: struct {
source: ^Source,
items: [dynamic]Diagnostic,
allocator: mem.Allocator,
source: ^Source,
store: ^Store,
items: [dynamic]Diagnostic,
allocator: mem.Allocator,
}
init_store :: proc(allocator := context.allocator) -> Store {
result: Store
result.allocator = allocator
result.items.allocator = allocator
return result
}
destroy_store :: proc(store: ^Store) {
for item in store.items {
delete(item.path, store.allocator)
delete(item.text, store.allocator)
}
delete(store.items)
}
add_source :: proc(store: ^Store, path, text: string) -> int {
id := len(store.items)
append(&store.items, Source{
id=id,
path=fmt.aprintf("%s", path, allocator=store.allocator),
text=fmt.aprintf("%s", text, allocator=store.allocator),
})
return id
}
init_diagnostics :: proc(source_file: ^Source, allocator := context.allocator) -> Diagnostics {
@@ -32,6 +65,14 @@ init_diagnostics :: proc(source_file: ^Source, allocator := context.allocator) -
return result
}
init_store_diagnostics :: proc(store: ^Store, allocator := context.allocator) -> Diagnostics {
result: Diagnostics
result.store = store
result.allocator = allocator
result.items.allocator = allocator
return result
}
destroy_diagnostics :: proc(diagnostics: ^Diagnostics) {
for diagnostic in diagnostics.items {
delete(diagnostic.message, diagnostics.allocator)
@@ -79,15 +120,32 @@ line_and_column :: proc(source_file: ^Source, offset: int) -> (line, column: int
return
}
source_for_span :: proc(diagnostics: ^Diagnostics, span: Span) -> ^Source {
if diagnostics.store != nil && span.file >= 0 && span.file < len(diagnostics.store.items) {
return &diagnostics.store.items[span.file]
}
return diagnostics.source
}
format :: proc(diagnostics: ^Diagnostics, id: int, allocator := context.allocator) -> string {
if id < 0 || id >= len(diagnostics.items) {
return fmt.aprintf("%s: compiler recovery error", diagnostics.source.path, allocator=allocator)
path := "<unknown>"
if diagnostics.source != nil {
path = diagnostics.source.path
} else if diagnostics.store != nil && len(diagnostics.store.items) > 0 {
path = diagnostics.store.items[0].path
}
return fmt.aprintf("%s: compiler recovery error", path, allocator=allocator)
}
diagnostic := diagnostics.items[id]
line, column := line_and_column(diagnostics.source, diagnostic.span.start)
source_file := source_for_span(diagnostics, diagnostic.span)
if source_file == nil {
return fmt.aprintf("<unknown>: error: %s", diagnostic.message, allocator=allocator)
}
line, column := line_and_column(source_file, diagnostic.span.start)
return fmt.aprintf(
"%s:%d:%d: error: %s",
diagnostics.source.path,
source_file.path,
line,
column,
diagnostic.message,