intern identifiers

This commit is contained in:
2026-06-10 21:27:00 +02:00
parent d6e03b6f08
commit cdbe4fbc99
16 changed files with 497 additions and 182 deletions
+9 -9
View File
@@ -1,6 +1,7 @@
package ast
import "../source"
import "../symbol"
import "core:mem"
INVALID_ID :: -1
@@ -26,8 +27,8 @@ Expr_Kind :: enum {
Expr :: struct {
kind: Expr_Kind,
span: source.Span,
qualifier: string,
text: string,
qualifier: symbol.Id,
name: symbol.Id,
integer: i64,
left: int,
right: int,
@@ -36,7 +37,7 @@ Expr :: struct {
}
Param :: struct {
name: string,
name: symbol.Id,
span: source.Span,
type: Type_Syntax,
}
@@ -52,7 +53,7 @@ Stmt_Kind :: enum {
Stmt :: struct {
kind: Stmt_Kind,
span: source.Span,
name: string,
name: symbol.Id,
type: Type_Syntax,
immutable: bool,
expr: int,
@@ -61,7 +62,7 @@ Stmt :: struct {
Function :: struct {
span: source.Span,
name: string,
name: symbol.Id,
pkg: int,
file: int,
c_abi: bool,
@@ -73,7 +74,7 @@ Function :: struct {
Global :: struct {
span: source.Span,
name: string,
name: symbol.Id,
pkg: int,
file: int,
type: Type_Syntax,
@@ -84,7 +85,7 @@ Global :: struct {
Import :: struct {
span: source.Span,
alias: string,
alias: symbol.Id,
path: string,
pkg: int,
file: int,
@@ -101,7 +102,7 @@ File :: struct {
Package :: struct {
path: string,
name: string,
name: symbol.Id,
available: bool,
}
@@ -142,7 +143,6 @@ destroy_module :: proc(module: ^Module) {
}
for pkg in module.packages {
delete(pkg.path, module.allocator)
delete(pkg.name, module.allocator)
}
delete(module.exprs)
delete(module.statements)
+77 -65
View File
@@ -3,6 +3,7 @@ package checker
import "../ast"
import "../hir"
import "../source"
import "../symbol"
import "../types"
import "base:intrinsics"
import "core:fmt"
@@ -18,12 +19,12 @@ Spec :: struct {
}
Infer_Local :: struct {
name: string,
name: symbol.Id,
type: types.Type,
}
Build_Local :: struct {
name: string,
name: symbol.Id,
type: types.Type,
mutable: bool,
id: int,
@@ -43,13 +44,14 @@ Constant :: struct {
Symbol_Index_Entry :: struct {
scope: int,
name: string,
name: symbol.Id,
id: int,
}
Checker :: struct {
ast_module: ^ast.Module,
diagnostics: ^source.Diagnostics,
symbols: ^symbol.Table,
module: hir.Module,
specs: [dynamic]Spec,
function_index: []Symbol_Index_Entry,
@@ -57,9 +59,15 @@ Checker :: struct {
import_index: []Symbol_Index_Entry,
global_types: []types.Type,
constants: []Constant,
main_symbol: symbol.Id,
sink_symbol: symbol.Id,
allocator: mem.Allocator,
}
symbol_text :: proc(checker: ^Checker, id: symbol.Id) -> string {
return symbol.resolve(checker.symbols, id)
}
eval_constant :: proc(checker: ^Checker, expr_id: int) -> Constant {
if expr_id < 0 || expr_id >= len(checker.ast_module.exprs) {
return Constant{kind = .Not_Constant}
@@ -131,18 +139,18 @@ symbol_index_less :: proc(left, right: Symbol_Index_Entry) -> bool {
return left.scope < right.scope
}
if left.name != right.name {
return left.name < right.name
return int(left.name) < int(right.name)
}
return left.id < right.id
}
find_symbol :: proc(index: []Symbol_Index_Entry, scope: int, name: string) -> int {
find_symbol :: proc(index: []Symbol_Index_Entry, scope: int, name: symbol.Id) -> int {
low := 0
high := len(index)
for low < high {
middle := low + (high-low)/2
entry := index[middle]
if entry.scope < scope || entry.scope == scope && entry.name < name {
if entry.scope < scope || entry.scope == scope && int(entry.name) < int(name) {
low = middle + 1
} else {
high = middle
@@ -174,15 +182,15 @@ build_symbol_indexes :: proc(checker: ^Checker) {
slice.sort_by(checker.import_index, symbol_index_less)
}
find_template :: proc(checker: ^Checker, name: string, pkg := 0) -> int {
find_template :: proc(checker: ^Checker, name: symbol.Id, pkg := 0) -> int {
return find_symbol(checker.function_index, pkg, name)
}
find_global :: proc(checker: ^Checker, name: string, pkg := 0) -> int {
find_global :: proc(checker: ^Checker, name: symbol.Id, pkg := 0) -> int {
return find_symbol(checker.global_index, pkg, name)
}
find_import :: proc(checker: ^Checker, file: int, alias: string, mark_used := false) -> int {
find_import :: proc(checker: ^Checker, file: int, alias: symbol.Id, mark_used := false) -> int {
id := find_symbol(checker.import_index, file, alias)
if id >= 0 && mark_used {
checker.ast_module.imports[id].used = true
@@ -191,7 +199,7 @@ find_import :: proc(checker: ^Checker, file: int, alias: string, mark_used := fa
}
expr_package :: proc(checker: ^Checker, expr: ast.Expr, pkg, file: int, mark_used := false) -> (int, bool) {
if expr.qualifier == "" {
if !symbol.is_valid(expr.qualifier) {
return pkg, true
}
import_id := find_import(checker, file, expr.qualifier, mark_used)
@@ -208,44 +216,44 @@ expr_package :: proc(checker: ^Checker, expr: ast.Expr, pkg, file: int, mark_use
add_package_resolution_diagnostic :: proc(checker: ^Checker, expr: ast.Expr, file: int) -> int {
if find_import(checker, file, expr.qualifier) < 0 {
return source.addf(checker.diagnostics, expr.span, "unknown package alias '%s'", expr.qualifier)
return source.addf(checker.diagnostics, expr.span, "unknown package alias '%s'", symbol_text(checker, expr.qualifier))
}
return source.addf(checker.diagnostics, expr.span, "unavailable imported package '%s'", expr.qualifier)
return source.addf(checker.diagnostics, expr.span, "unavailable imported package '%s'", symbol_text(checker, expr.qualifier))
}
add_name_resolution_diagnostic :: proc(checker: ^Checker, expr: ast.Expr, target_pkg: int) -> int {
if find_template(checker, expr.text, target_pkg) >= 0 {
return source.addf(checker.diagnostics, expr.span, "'%s' is a function, not a global value", expr.text)
if find_template(checker, expr.name, target_pkg) >= 0 {
return source.addf(checker.diagnostics, expr.span, "'%s' is a function, not a global value", symbol_text(checker, expr.name))
}
if expr.qualifier != "" {
if symbol.is_valid(expr.qualifier) {
return source.addf(
checker.diagnostics,
expr.span,
"package '%s' has no member '%s'",
expr.qualifier,
expr.text,
symbol_text(checker, expr.qualifier),
symbol_text(checker, expr.name),
)
}
return source.addf(checker.diagnostics, expr.span, "unresolved global '%s'", expr.text)
return source.addf(checker.diagnostics, expr.span, "unresolved global '%s'", symbol_text(checker, expr.name))
}
add_call_resolution_diagnostic :: proc(checker: ^Checker, expr: ast.Expr, target_pkg: int) -> int {
if find_global(checker, expr.text, target_pkg) >= 0 {
return source.addf(checker.diagnostics, expr.span, "'%s' is a global, not a function", expr.text)
if find_global(checker, expr.name, target_pkg) >= 0 {
return source.addf(checker.diagnostics, expr.span, "'%s' is a global, not a function", symbol_text(checker, expr.name))
}
if expr.qualifier != "" {
if symbol.is_valid(expr.qualifier) {
return source.addf(
checker.diagnostics,
expr.span,
"package '%s' has no member '%s'",
expr.qualifier,
expr.text,
symbol_text(checker, expr.qualifier),
symbol_text(checker, expr.name),
)
}
return source.addf(checker.diagnostics, expr.span, "unresolved function '%s'", expr.text)
return source.addf(checker.diagnostics, expr.span, "unresolved function '%s'", symbol_text(checker, expr.name))
}
contains_name :: proc(names: []string, name: string) -> bool {
contains_name :: proc(names: []symbol.Id, name: symbol.Id) -> bool {
for existing in names {
if existing == name {
return true
@@ -261,11 +269,11 @@ mark_expr_imports_used :: proc(checker: ^Checker, expr_id, file: int) {
expr := checker.ast_module.exprs[expr_id]
switch expr.kind {
case .Name:
if expr.qualifier != "" {
if symbol.is_valid(expr.qualifier) {
_ = find_import(checker, file, expr.qualifier, true)
}
case .Call:
if expr.qualifier != "" {
if symbol.is_valid(expr.qualifier) {
_ = find_import(checker, file, expr.qualifier, true)
}
for arg in expr.args {
@@ -280,7 +288,7 @@ mark_expr_imports_used :: proc(checker: ^Checker, expr_id, file: int) {
validate_declarations :: proc(checker: ^Checker) {
for function in checker.ast_module.functions {
locals: [dynamic]string
locals: [dynamic]symbol.Id
locals.allocator = checker.allocator
for param in function.params {
if param.type == .Void {
@@ -295,7 +303,7 @@ validate_declarations :: proc(checker: ^Checker) {
checker.diagnostics,
param.span,
"duplicate parameter '%s'",
param.name,
symbol_text(checker, param.name),
)
}
append(&locals, param.name)
@@ -312,7 +320,7 @@ validate_declarations :: proc(checker: ^Checker) {
}
}
find_infer_local :: proc(locals: []Infer_Local, name: string) -> types.Type {
find_infer_local :: proc(locals: []Infer_Local, name: symbol.Id) -> types.Type {
for index := len(locals) - 1; index >= 0; index -= 1 {
if locals[index].name == name {
return locals[index].type
@@ -372,7 +380,7 @@ ensure_spec :: proc(checker: ^Checker, template: int, actual_args: []types.Type)
}
}
result := type_from_syntax(function.result)
if function.pkg == 0 && function.name == "main" && function.result == .Int {
if function.pkg == 0 && function.name == checker.main_symbol && function.result == .Int {
result = types.I32
}
index := len(checker.specs)
@@ -401,8 +409,8 @@ infer_expr :: proc(checker: ^Checker, expr_id: int, locals: []Infer_Local, pkg :
case .Integer:
return types.smallest_signed_for_literal(expr.integer)
case .Name:
if expr.qualifier == "" {
local_type := find_infer_local(locals, expr.text)
if !symbol.is_valid(expr.qualifier) {
local_type := find_infer_local(locals, expr.name)
if types.is_valid(local_type) {
return local_type
}
@@ -411,7 +419,7 @@ infer_expr :: proc(checker: ^Checker, expr_id: int, locals: []Infer_Local, pkg :
if !available {
return types.INVALID
}
global := find_global(checker, expr.text, target_pkg)
global := find_global(checker, expr.name, target_pkg)
if global >= 0 {
return checker.global_types[global]
}
@@ -425,7 +433,7 @@ infer_expr :: proc(checker: ^Checker, expr_id: int, locals: []Infer_Local, pkg :
if !available {
return types.INVALID
}
template := find_template(checker, expr.text, target_pkg)
template := find_template(checker, expr.name, target_pkg)
if template < 0 {
return types.INVALID
}
@@ -437,7 +445,7 @@ infer_expr :: proc(checker: ^Checker, expr_id: int, locals: []Infer_Local, pkg :
if !can_specialize(function, args) {
delete(args, checker.allocator)
declared := type_from_syntax(function.result)
if function.pkg == 0 && function.name == "main" && function.result == .Int {
if function.pkg == 0 && function.name == checker.main_symbol && function.result == .Int {
return types.I32
}
if declared.kind == .Concrete || declared.kind == .Void {
@@ -456,7 +464,7 @@ infer_spec_result :: proc(checker: ^Checker, spec_id: int) -> types.Type {
spec := checker.specs[spec_id]
function := checker.ast_module.functions[spec.template]
declared := type_from_syntax(function.result)
if function.pkg == 0 && function.name == "main" && function.result == .Int {
if function.pkg == 0 && function.name == checker.main_symbol && function.result == .Int {
declared = types.I32
}
@@ -525,7 +533,7 @@ infer_all :: proc(checker: ^Checker) {
}
}
main_template := find_template(checker, "main", 0)
main_template := find_template(checker, checker.main_symbol, 0)
if main_template >= 0 {
ensure_spec(checker, main_template, nil)
}
@@ -588,7 +596,7 @@ add_unique :: proc(values: ^[dynamic]int, value: int) {
append(values, value)
}
find_build_local :: proc(locals: []Build_Local, name: string) -> (Build_Local, bool) {
find_build_local :: proc(locals: []Build_Local, name: symbol.Id) -> (Build_Local, bool) {
for index := len(locals) - 1; index >= 0; index -= 1 {
if locals[index].name == name {
return locals[index], true
@@ -708,8 +716,8 @@ build_expr :: proc(
case .Integer:
unreachable()
case .Name:
if expr.qualifier == "" {
if local, ok := find_build_local(locals, expr.text); ok {
if !symbol.is_valid(expr.qualifier) {
if local, ok := find_build_local(locals, expr.name); ok {
return add_hir_expr(
checker,
hir.Expr {
@@ -729,7 +737,7 @@ build_expr :: proc(
id := add_package_resolution_diagnostic(checker, expr, file)
return invalid_hir_expr(checker, expr.span, id)
}
global := find_global(checker, expr.text, target_pkg)
global := find_global(checker, expr.name, target_pkg)
if global >= 0 {
add_unique(global_reads, global)
return add_hir_expr(
@@ -781,7 +789,7 @@ build_expr :: proc(
id := add_package_resolution_diagnostic(checker, expr, file)
return invalid_hir_expr(checker, expr.span, id)
}
template := find_template(checker, expr.text, target_pkg)
template := find_template(checker, expr.name, target_pkg)
if template < 0 {
id := add_call_resolution_diagnostic(checker, expr, target_pkg)
return invalid_hir_expr(checker, expr.span, id)
@@ -791,7 +799,7 @@ build_expr :: proc(
checker.diagnostics,
expr.span,
"function '%s' expects %d arguments, got %d",
expr.text,
symbol_text(checker, expr.name),
len(checker.ast_module.functions[template].params),
len(expr.args),
)
@@ -833,7 +841,7 @@ build_expr :: proc(
checker.diagnostics,
expr.span,
"could not resolve result type for specialization of '%s'",
expr.text,
symbol_text(checker, expr.name),
)
delete(built_args, checker.allocator)
return invalid_hir_expr(checker, expr.span, id)
@@ -862,14 +870,14 @@ build_expr :: proc(
make_link_name :: proc(checker: ^Checker, spec_id: int) -> string {
spec := checker.specs[spec_id]
function := checker.ast_module.functions[spec.template]
if function.pkg == 0 && function.name == "main" {
if function.pkg == 0 && function.name == checker.main_symbol {
return fmt.aprintf("main", allocator = checker.allocator)
}
builder := strings.builder_make(checker.allocator)
defer strings.builder_destroy(&builder)
strings.write_string(&builder, "bro_c__" if function.c_abi else "bro__")
fmt.sbprintf(&builder, "p%d__", function.pkg)
strings.write_string(&builder, function.name)
strings.write_string(&builder, symbol_text(checker, function.name))
for arg in spec.args {
strings.write_string(&builder, "__")
strings.write_string(&builder, types.name(arg))
@@ -891,7 +899,7 @@ build_function :: proc(checker: ^Checker, spec_id: int) {
checker.diagnostics,
function.span,
"could not resolve a concrete result type for '%s'",
function.name,
symbol_text(checker, function.name),
)
}
for arg in spec.args {
@@ -900,7 +908,7 @@ build_function :: proc(checker: ^Checker, spec_id: int) {
checker.diagnostics,
function.span,
"could not resolve a concrete parameter type for '%s'",
function.name,
symbol_text(checker, function.name),
)
break
}
@@ -984,7 +992,7 @@ build_function :: proc(checker: ^Checker, spec_id: int) {
checker.diagnostics,
statement.span,
"duplicate local '%s'",
statement.name,
symbol_text(checker, statement.name),
)
append(&body, len(checker.module.statements))
append(
@@ -1031,7 +1039,7 @@ build_function :: proc(checker: ^Checker, spec_id: int) {
)
problematic = problematic || checker.module.exprs[value].kind == .Invalid
case .Assignment:
if statement.name == "_" {
if statement.name == checker.sink_symbol {
value := build_expr(checker, statement.expr, locals[:], &global_reads, &calls, types.INVALID, function.pkg, function.file)
if checker.module.exprs[value].type.kind == .Void {
id := source.add(
@@ -1072,7 +1080,7 @@ build_function :: proc(checker: ^Checker, spec_id: int) {
checker.diagnostics,
statement.span,
"cannot assign unresolved local '%s'",
statement.name,
symbol_text(checker, statement.name),
)
append(&body, len(checker.module.statements))
append(
@@ -1093,7 +1101,7 @@ build_function :: proc(checker: ^Checker, spec_id: int) {
checker.diagnostics,
statement.span,
"cannot assign immutable local '%s'",
statement.name,
symbol_text(checker, statement.name),
)
append(&body, len(checker.module.statements))
append(
@@ -1265,7 +1273,7 @@ build_function :: proc(checker: ^Checker, spec_id: int) {
checker.diagnostics,
function.span,
"function '%s' does not return a value",
function.name,
symbol_text(checker, function.name),
)
append(&body, len(checker.module.statements))
append(
@@ -1280,8 +1288,8 @@ build_function :: proc(checker: ^Checker, spec_id: int) {
hir.Function {
name = function.name,
link_name = make_link_name(checker, spec_id),
c_abi = function.c_abi || (function.pkg == 0 && function.name == "main"),
is_main = function.pkg == 0 && function.name == "main",
c_abi = function.c_abi || (function.pkg == 0 && function.name == checker.main_symbol),
is_main = function.pkg == 0 && function.name == checker.main_symbol,
params = params[:],
result = spec.result,
locals = hir_locals[:],
@@ -1342,7 +1350,7 @@ build_globals :: proc(checker: ^Checker) {
checker.diagnostics,
global.span,
"could not resolve a concrete type for global '%s'",
global.name,
symbol_text(checker, global.name),
)
global_type = types.I64
expr = invalid_hir_expr(checker, global.span, diagnostic, global_type)
@@ -1507,7 +1515,7 @@ detect_global_cycles_visit :: proc(checker: ^Checker, global_id: int, states: []
checker.diagnostics,
checker.ast_module.globals[global_id].span,
"global initialization cycle involving '%s'",
checker.module.globals[global_id].name,
symbol_text(checker, checker.module.globals[global_id].name),
)
checker.module.globals[global_id].diagnostic = id
checker.module.globals[global_id].problematic = true
@@ -1537,7 +1545,7 @@ synthesize_trap_main :: proc(checker: ^Checker) {
append(
&checker.module.functions,
hir.Function {
name = "main",
name = checker.main_symbol,
link_name = fmt.aprintf("main", allocator = checker.allocator),
c_abi = true,
is_main = true,
@@ -1581,12 +1589,16 @@ replace_main_with_trap :: proc(checker: ^Checker, diagnostic: int) {
check :: proc(
ast_module: ^ast.Module,
diagnostics: ^source.Diagnostics,
symbols: ^symbol.Table,
allocator := context.allocator,
) -> hir.Module {
checker := Checker {
ast_module = ast_module,
diagnostics = diagnostics,
symbols = symbols,
module = hir.init_module(allocator),
main_symbol = symbol.intern(symbols, "main"),
sink_symbol = symbol.intern(symbols, "_"),
allocator = allocator,
}
checker.specs.allocator = allocator
@@ -1608,19 +1620,19 @@ check :: proc(
for function, index in ast_module.functions {
for previous in ast_module.functions[:index] {
if previous.pkg == function.pkg && previous.name == function.name {
source.addf(diagnostics, function.span, "duplicate function '%s'", function.name)
source.addf(diagnostics, function.span, "duplicate function '%s'", symbol_text(&checker, function.name))
}
}
for global in ast_module.globals {
if global.pkg == function.pkg && global.name == function.name {
source.addf(diagnostics, function.span, "package declaration '%s' conflicts with a global", function.name)
source.addf(diagnostics, function.span, "package declaration '%s' conflicts with a global", symbol_text(&checker, function.name))
}
}
}
for global, index in ast_module.globals {
for previous in ast_module.globals[:index] {
if previous.pkg == global.pkg && previous.name == global.name {
source.addf(diagnostics, global.span, "duplicate global '%s'", global.name)
source.addf(diagnostics, global.span, "duplicate global '%s'", symbol_text(&checker, global.name))
}
}
}
@@ -1634,10 +1646,10 @@ check :: proc(
resolve_call_targets(&checker)
propagate_global_reads(&checker)
main_template := find_template(&checker, "main", 0)
main_template := find_template(&checker, checker.main_symbol, 0)
main_declarations := 0
for function in ast_module.functions {
if function.pkg == 0 && function.name == "main" {
if function.pkg == 0 && function.name == checker.main_symbol {
main_declarations += 1
}
}
@@ -1666,7 +1678,7 @@ check :: proc(
propagate_problems(&checker)
for import_item in ast_module.imports {
if import_item.valid && !import_item.used {
source.addf(diagnostics, import_item.span, "unused import '%s'", import_item.alias)
source.addf(diagnostics, import_item.span, "unused import '%s'", symbol_text(&checker, import_item.alias))
}
}
return checker.module
+6 -2
View File
@@ -7,6 +7,7 @@ import "./loader"
import "./lower"
import "./opt"
import "./source"
import "./symbol"
import "core:fmt"
import vmem "core:mem/virtual"
import "core:os"
@@ -17,6 +18,8 @@ compile_package :: proc(input_path, output_path: string) -> int {
defer source.destroy_store(&sources)
diagnostics := source.init_store_diagnostics(&sources)
defer source.destroy_diagnostics(&diagnostics)
symbols := symbol.init_table()
defer symbol.destroy_table(&symbols)
lexer_arena: vmem.Arena
if err := vmem.arena_init_growing(&lexer_arena); err != nil {
@@ -47,6 +50,7 @@ compile_package :: proc(input_path, output_path: string) -> int {
input_path,
&sources,
&diagnostics,
&symbols,
vmem.arena_allocator(&lexer_arena),
vmem.arena_allocator(&parser_arena),
)
@@ -55,13 +59,13 @@ compile_package :: proc(input_path, output_path: string) -> int {
return 2
}
vmem.arena_free_all(&lexer_arena)
hir_module := checker.check(&ast_module, &diagnostics, vmem.arena_allocator(&checker_arena))
hir_module := checker.check(&ast_module, &diagnostics, &symbols, vmem.arena_allocator(&checker_arena))
vmem.arena_free_all(&parser_arena)
ir_module := lower.lower(&hir_module, vmem.arena_allocator(&lower_arena))
vmem.arena_free_all(&checker_arena)
opt.run(&ir_module)
llvm_text := llvm.emit(&ir_module, &diagnostics)
llvm_text := llvm.emit(&ir_module, &diagnostics, &symbols)
defer delete(llvm_text)
vmem.arena_free_all(&lower_arena)
llvm_path := fmt.tprintf("%s.brolang-%d.ll", output_path, os2.get_pid())
+4 -3
View File
@@ -1,6 +1,7 @@
package hir
import "../source"
import "../symbol"
import "../types"
import "core:mem"
@@ -29,7 +30,7 @@ Expr :: struct {
}
Local :: struct {
name: string,
name: symbol.Id,
type: types.Type,
mutable: bool,
parameter: bool,
@@ -53,7 +54,7 @@ Stmt :: struct {
}
Function :: struct {
name: string,
name: symbol.Id,
link_name: string,
c_abi: bool,
is_main: bool,
@@ -68,7 +69,7 @@ Function :: struct {
}
Global :: struct {
name: string,
name: symbol.Id,
type: types.Type,
expr: int,
static_value: i64,
+2 -4
View File
@@ -1,6 +1,7 @@
package ir
import "../source"
import "../symbol"
import "../types"
import "core:mem"
@@ -34,7 +35,6 @@ Instruction :: struct {
}
Function :: struct {
name: string,
link_name: string,
c_abi: bool,
is_main: bool,
@@ -45,7 +45,7 @@ Function :: struct {
}
Global :: struct {
name: string,
name: symbol.Id,
type: types.Type,
is_static: bool,
static_value: i64,
@@ -77,13 +77,11 @@ destroy_instructions :: proc(instructions: []Instruction, allocator: mem.Allocat
destroy_module :: proc(module: ^Module) {
for function in module.functions {
delete(function.name, module.allocator)
delete(function.link_name, module.allocator)
delete(function.param_types, module.allocator)
destroy_instructions(function.instructions, module.allocator)
}
for global in module.globals {
delete(global.name, module.allocator)
destroy_instructions(global.initializer, module.allocator)
}
delete(module.functions)
+14 -6
View File
@@ -1,6 +1,7 @@
package lexer
import "../source"
import "../symbol"
import "../token"
is_identifier_start :: proc(value: byte) -> bool {
@@ -33,12 +34,13 @@ append_token :: proc(
source_file: ^source.Source,
kind: token.Kind,
start, end: int,
id := symbol.INVALID,
diagnostic := -1,
) {
append(&stream.items, token.Token{
kind=kind,
span=source.Span{file=source_file.id, start=start, end=end},
text=source_file.text[start:end],
symbol=id,
diagnostic=diagnostic,
})
}
@@ -46,6 +48,7 @@ append_token :: proc(
lex :: proc(
source_file: ^source.Source,
diagnostics: ^source.Diagnostics,
symbols: ^symbol.Table,
allocator := context.allocator,
) -> token.Stream {
stream: token.Stream
@@ -73,7 +76,7 @@ lex :: proc(
append_token(&stream, source_file, .Colon_Colon, start, cursor)
} else {
id := source.add(diagnostics, source.Span{file=source_file.id, start=start, end=cursor}, "expected a second ':'")
append_token(&stream, source_file, .Invalid, start, cursor, id)
append_token(&stream, source_file, .Invalid, start, cursor, diagnostic=id)
}
case '=':
append_token(&stream, source_file, .Equal, cursor, cursor+1)
@@ -128,7 +131,7 @@ lex :: proc(
source.Span{file=source_file.id, start=start, end=cursor},
"unterminated import string",
)
append_token(&stream, source_file, .Invalid, start, cursor, id)
append_token(&stream, source_file, .Invalid, start, cursor, diagnostic=id)
}
case ';':
id := source.add(
@@ -136,7 +139,7 @@ lex :: proc(
source.Span{file=source_file.id, start=cursor, end=cursor+1},
"semicolons are invalid; terminate statements with a newline",
)
append_token(&stream, source_file, .Invalid, cursor, cursor+1, id)
append_token(&stream, source_file, .Invalid, cursor, cursor+1, diagnostic=id)
cursor += 1
case:
if value >= '0' && value <= '9' {
@@ -151,7 +154,12 @@ lex :: proc(
cursor += 1
}
text := source_file.text[start:cursor]
append_token(&stream, source_file, keyword_kind(text), start, cursor)
kind := keyword_kind(text)
id := symbol.INVALID
if kind == .Identifier || kind == .Underscore {
id = symbol.intern(symbols, text)
}
append_token(&stream, source_file, kind, start, cursor, id)
} else {
id := source.addf(
diagnostics,
@@ -159,7 +167,7 @@ lex :: proc(
"invalid source byte 0x%02x",
value,
)
append_token(&stream, source_file, .Invalid, cursor, cursor+1, id)
append_token(&stream, source_file, .Invalid, cursor, cursor+1, diagnostic=id)
cursor += 1
}
}
+14 -2
View File
@@ -2,6 +2,7 @@ package llvm
import "../ir"
import "../source"
import "../symbol"
import "../types"
import "core:fmt"
import "core:mem"
@@ -14,6 +15,7 @@ Trap_Message :: struct {
Emitter :: struct {
module: ^ir.Module,
diagnostics: ^source.Diagnostics,
symbols: ^symbol.Table,
builder: strings.Builder,
messages: [dynamic]Trap_Message,
allocator: mem.Allocator,
@@ -271,7 +273,11 @@ emit_global_accessors :: proc(emitter: ^Emitter) {
global_id,
)
strings.write_string(&emitter.builder, "check:\n %visiting = icmp eq i8 %state, 1\n br i1 %visiting, label %cycle, label %initialize\ncycle:\n")
message_text := fmt.aprintf("runtime trap: global initialization cycle involving '%s'", global.name, allocator=emitter.allocator)
message_text := fmt.aprintf(
"runtime trap: global initialization cycle involving '%s'",
symbol.resolve(emitter.symbols, global.name),
allocator=emitter.allocator,
)
message := register_message(emitter, message_text)
delete(message_text, emitter.allocator)
emit_trap_call(emitter, message)
@@ -366,10 +372,16 @@ emit_declarations :: proc(emitter: ^Emitter) {
)
}
emit :: proc(module: ^ir.Module, diagnostics: ^source.Diagnostics, allocator := context.allocator) -> string {
emit :: proc(
module: ^ir.Module,
diagnostics: ^source.Diagnostics,
symbols: ^symbol.Table,
allocator := context.allocator,
) -> string {
emitter := Emitter{
module=module,
diagnostics=diagnostics,
symbols=symbols,
builder=strings.builder_make(allocator),
allocator=allocator,
}
+14 -9
View File
@@ -4,6 +4,7 @@ import "../ast"
import "../lexer"
import "../parser"
import "../source"
import "../symbol"
import "core:mem"
import "core:os"
import "core:path/filepath"
@@ -14,6 +15,7 @@ State :: struct {
module: ^ast.Module,
sources: ^source.Store,
diagnostics: ^source.Diagnostics,
symbols: ^symbol.Table,
token_allocator: mem.Allocator,
allocator: mem.Allocator,
root_failed: bool,
@@ -53,7 +55,7 @@ add_placeholder :: proc(state: ^State, path: string) -> int {
id := len(state.module.packages)
append(&state.module.packages, ast.Package{
path=strings.clone(path, state.allocator),
name=strings.clone(filepath.base(path), state.allocator),
name=symbol.intern(state.symbols, filepath.base(path)),
available=false,
})
return id
@@ -130,7 +132,7 @@ load_package :: proc(state: ^State, path: string, import_span: source.Span, is_r
pkg_id := len(state.module.packages)
append(&state.module.packages, ast.Package{
path=canonical,
name=strings.clone(filepath.base(canonical), state.allocator),
name=symbol.intern(state.symbols, filepath.base(canonical)),
available=true,
})
files, files_ok := read_package_files(state, canonical)
@@ -159,8 +161,8 @@ load_package :: proc(state: ^State, path: string, import_span: source.Span, is_r
delete(bytes)
file_id := len(state.module.files)
append(&state.module.files, ast.File{source=source_id, pkg=pkg_id})
stream := lexer.lex(&state.sources.items[source_id], state.diagnostics, state.token_allocator)
parser.parse_into(&stream, state.diagnostics, state.module, pkg_id, file_id)
stream := lexer.lex(&state.sources.items[source_id], state.diagnostics, state.symbols, state.token_allocator)
parser.parse_into(&stream, &state.sources.items[source_id], state.diagnostics, state.module, pkg_id, file_id)
delete(stream.items)
}
os.file_info_slice_delete(files, state.allocator)
@@ -188,7 +190,7 @@ load_package :: proc(state: ^State, path: string, import_span: source.Span, is_r
return pkg_id
}
declaration_conflicts :: proc(module: ^ast.Module, pkg: int, name: string) -> bool {
declaration_conflicts :: proc(module: ^ast.Module, pkg: int, name: symbol.Id) -> bool {
for function in module.functions {
if function.pkg == pkg && function.name == name {
return true
@@ -204,11 +206,12 @@ declaration_conflicts :: proc(module: ^ast.Module, pkg: int, name: string) -> bo
validate_imports :: proc(state: ^State) {
for import_item, import_id in state.module.imports {
if import_item.alias == "" && import_item.target >= 0 {
if !symbol.is_valid(import_item.alias) && import_item.target >= 0 {
state.module.imports[import_id].alias = state.module.packages[import_item.target].name
}
alias := state.module.imports[import_id].alias
if !is_identifier(alias) {
alias_text := symbol.resolve(state.symbols, alias)
if !is_identifier(alias_text) {
state.module.imports[import_id].diagnostic = source.add(
state.diagnostics,
import_item.span,
@@ -221,7 +224,7 @@ validate_imports :: proc(state: ^State) {
state.diagnostics,
import_item.span,
"import alias '%s' conflicts with a package declaration",
alias,
alias_text,
)
state.module.imports[import_id].valid = false
}
@@ -231,7 +234,7 @@ validate_imports :: proc(state: ^State) {
state.diagnostics,
import_item.span,
"duplicate import alias '%s' in the same file",
alias,
alias_text,
)
state.module.imports[import_id].valid = false
break
@@ -244,6 +247,7 @@ load :: proc(
root_path: string,
sources: ^source.Store,
diagnostics: ^source.Diagnostics,
symbols: ^symbol.Table,
token_allocator := context.allocator,
allocator := context.allocator,
) -> (ast.Module, bool) {
@@ -252,6 +256,7 @@ load :: proc(
module=&module,
sources=sources,
diagnostics=diagnostics,
symbols=symbols,
token_allocator=token_allocator,
allocator=allocator,
}
+1 -2
View File
@@ -327,7 +327,7 @@ lower :: proc(hir_module: ^hir.Module, allocator := context.allocator) -> ir.Mod
module := ir.init_module(allocator)
for global in hir_module.globals {
append(&module.globals, ir.Global{
name=fmt.aprintf("%s", global.name, allocator=allocator),
name=global.name,
type=global.type,
is_static=global.is_static,
static_value=global.static_value,
@@ -342,7 +342,6 @@ lower :: proc(hir_module: ^hir.Module, allocator := context.allocator) -> ir.Mod
param_types[index] = function.locals[local_id].type
}
append(&module.functions, ir.Function{
name=fmt.aprintf("%s", function.name, allocator=allocator),
link_name=fmt.aprintf("%s", function.link_name, allocator=allocator),
c_abi=function.c_abi,
is_main=function.is_main,
+32 -18
View File
@@ -2,6 +2,7 @@ package parser
import "../ast"
import "../source"
import "../symbol"
import "../token"
import "core:fmt"
import "core:strconv"
@@ -9,6 +10,7 @@ import "core:strings"
Parser :: struct {
tokens: ^token.Stream,
source_file: ^source.Source,
diagnostics: ^source.Diagnostics,
module: ast.Module,
pkg: int,
@@ -17,6 +19,13 @@ Parser :: struct {
delimiter_depth: int,
}
token_text :: proc(parser: ^Parser, tok: token.Token) -> string {
if tok.span.start < 0 || tok.span.end < tok.span.start || tok.span.end > len(parser.source_file.text) {
return ""
}
return parser.source_file.text[tok.span.start:tok.span.end]
}
span_from :: proc(first, last: source.Span) -> source.Span {
return source.Span{file=first.file, start=first.start, end=last.end}
}
@@ -101,7 +110,7 @@ parse_type :: proc(parser: ^Parser) -> ast.Type_Syntax {
return .Invalid
}
parse_call :: proc(parser: ^Parser, qualifier: string, first, name: token.Token) -> int {
parse_call :: proc(parser: ^Parser, qualifier: symbol.Id, first, name: token.Token) -> int {
left_paren := advance(parser)
parser.delimiter_depth += 1
defer parser.delimiter_depth -= 1
@@ -126,7 +135,7 @@ parse_call :: proc(parser: ^Parser, qualifier: string, first, name: token.Token)
kind=.Call,
span=source.Span{file=name.span.file, start=first.span.start, end=right_paren.span.end},
qualifier=qualifier,
text=name.text,
name=name.symbol,
args=args[:],
left=ast.INVALID_ID,
right=ast.INVALID_ID,
@@ -139,7 +148,7 @@ parse_primary :: proc(parser: ^Parser) -> int {
#partial switch tok.kind {
case .Integer:
advance(parser)
value, ok := strconv.parse_i64(tok.text)
value, ok := strconv.parse_i64(token_text(parser, tok))
if !ok {
return invalid_expr(parser, tok.span, "integer literal does not fit in i64")
}
@@ -154,12 +163,12 @@ parse_primary :: proc(parser: ^Parser) -> int {
case .Identifier:
first := advance(parser)
name := first
qualifier := ""
qualifier := symbol.INVALID
if _, ok := allow(parser, .Dot); ok {
if current(parser).kind != .Identifier {
return invalid_expr(parser, current(parser).span, "expected a package member after '.'")
}
qualifier = first.text
qualifier = first.symbol
name = advance(parser)
}
if current(parser).kind == .Left_Paren {
@@ -169,7 +178,7 @@ parse_primary :: proc(parser: ^Parser) -> int {
kind=.Name,
span=span_from(first.span, name.span),
qualifier=qualifier,
text=name.text,
name=name.symbol,
left=ast.INVALID_ID,
right=ast.INVALID_ID,
diagnostic=-1,
@@ -260,7 +269,7 @@ parse_return :: proc(parser: ^Parser) -> int {
append(&parser.module.statements, ast.Stmt{
kind=.Return,
span=span_from(start.span, end.span),
name="_",
name=end.symbol,
expr=ast.INVALID_ID,
diagnostic=-1,
})
@@ -306,7 +315,7 @@ parse_statement :: proc(parser: ^Parser) -> int {
append(&parser.module.statements, ast.Stmt{
kind=kind,
span=span_from(name.span, parser.module.exprs[expr].span),
name=name.text,
name=name.symbol,
type=type_syntax,
immutable=immutable,
expr=expr,
@@ -352,7 +361,7 @@ parse_params :: proc(parser: ^Parser) -> []ast.Param {
}
type_syntax := parse_type(parser)
for name in names {
append(&params, ast.Param{name=name.text, span=name.span, type=type_syntax})
append(&params, ast.Param{name=name.symbol, span=name.span, type=type_syntax})
}
delete(names)
skip_newlines(parser)
@@ -404,7 +413,7 @@ parse_function :: proc(parser: ^Parser, name: token.Token, c_abi: bool) {
}
append(&parser.module.functions, ast.Function{
span=span_from(name.span, end.span),
name=name.text,
name=name.symbol,
pkg=parser.pkg,
file=parser.file,
c_abi=c_abi,
@@ -416,16 +425,17 @@ parse_function :: proc(parser: ^Parser, name: token.Token, c_abi: bool) {
}
decode_import_path :: proc(parser: ^Parser, tok: token.Token) -> string {
if len(tok.text) < 2 {
text := token_text(parser, tok)
if len(text) < 2 {
return fmt.aprintf("", allocator=parser.module.allocator)
}
builder := strings.builder_make(parser.module.allocator)
defer strings.builder_destroy(&builder)
for index := 1; index < len(tok.text)-1; index += 1 {
value := tok.text[index]
if value == '\\' && index+1 < len(tok.text)-1 {
for index := 1; index < len(text)-1; index += 1 {
value := text[index]
if value == '\\' && index+1 < len(text)-1 {
index += 1
value = tok.text[index]
value = text[index]
}
strings.write_byte(&builder, value)
}
@@ -442,7 +452,7 @@ parse_import :: proc(parser: ^Parser, alias: token.Token, start: token.Token) {
}
append(&parser.module.imports, ast.Import{
span=start.span,
alias=alias.text,
alias=alias.symbol,
pkg=parser.pkg,
file=parser.file,
target=-1,
@@ -455,7 +465,7 @@ parse_import :: proc(parser: ^Parser, alias: token.Token, start: token.Token) {
advance(parser)
append(&parser.module.imports, ast.Import{
span=span_from(start.span, path_token.span),
alias=alias.text,
alias=alias.symbol,
path=decode_import_path(parser, path_token),
pkg=parser.pkg,
file=parser.file,
@@ -519,7 +529,7 @@ parse_top_level :: proc(parser: ^Parser) {
expr := parse_expression(parser)
append(&parser.module.globals, ast.Global{
span=span_from(name.span, parser.module.exprs[expr].span),
name=name.text,
name=name.symbol,
pkg=parser.pkg,
file=parser.file,
type=type_syntax,
@@ -532,11 +542,13 @@ parse_top_level :: proc(parser: ^Parser) {
parse :: proc(
stream: ^token.Stream,
source_file: ^source.Source,
diagnostics: ^source.Diagnostics,
allocator := context.allocator,
) -> ast.Module {
parser := Parser{
tokens=stream,
source_file=source_file,
diagnostics=diagnostics,
module=ast.init_module(allocator),
}
@@ -550,12 +562,14 @@ parse :: proc(
parse_into :: proc(
stream: ^token.Stream,
source_file: ^source.Source,
diagnostics: ^source.Diagnostics,
module: ^ast.Module,
pkg, file: int,
) {
parser := Parser{
tokens=stream,
source_file=source_file,
diagnostics=diagnostics,
module=module^,
pkg=pkg,
+58
View File
@@ -0,0 +1,58 @@
package symbol
import "core:mem"
import "core:strings"
Id :: distinct u32
INVALID :: Id(0)
Table :: struct {
items: [dynamic]string,
lookup: map[string]Id,
stored_bytes: int,
allocator: mem.Allocator,
}
init_table :: proc(allocator := context.allocator) -> Table {
table: Table
table.items.allocator = allocator
table.lookup.allocator = allocator
table.allocator = allocator
return table
}
destroy_table :: proc(table: ^Table) {
delete(table.lookup)
for item in table.items {
delete(item, table.allocator)
}
delete(table.items)
}
intern :: proc(table: ^Table, text: string) -> Id {
if len(text) == 0 {
return INVALID
}
if id, ok := table.lookup[text]; ok {
return id
}
cloned := strings.clone(text, table.allocator)
id := Id(len(table.items) + 1)
append(&table.items, cloned)
table.lookup[cloned] = id
table.stored_bytes += len(cloned)
return id
}
resolve :: proc(table: ^Table, id: Id) -> string {
index := int(id) - 1
if index < 0 || index >= len(table.items) {
return ""
}
return table.items[index]
}
is_valid :: proc(id: Id) -> bool {
return id != INVALID
}
+2 -1
View File
@@ -1,6 +1,7 @@
package token
import "../source"
import "../symbol"
Kind :: enum {
Invalid,
@@ -34,7 +35,7 @@ Kind :: enum {
Token :: struct {
kind: Kind,
span: source.Span,
text: string,
symbol: symbol.Id,
diagnostic: int,
}