Files
brolang/compiler/ast/ast.odin
T
2026-06-10 20:54:14 +02:00

155 lines
2.7 KiB
Odin

package ast
import "../source"
import "core:mem"
INVALID_ID :: -1
Type_Syntax :: enum {
Invalid,
Int,
I8,
I16,
I32,
I64,
Void,
}
Expr_Kind :: enum {
Invalid,
Integer,
Name,
Add,
Call,
}
Expr :: struct {
kind: Expr_Kind,
span: source.Span,
qualifier: string,
text: string,
integer: i64,
left: int,
right: int,
args: []int,
diagnostic: int,
}
Param :: struct {
name: string,
span: source.Span,
type: Type_Syntax,
}
Stmt_Kind :: enum {
Invalid,
Declaration,
Assignment,
Return,
Expression,
}
Stmt :: struct {
kind: Stmt_Kind,
span: source.Span,
name: string,
type: Type_Syntax,
immutable: bool,
expr: int,
diagnostic: int,
}
Function :: struct {
span: source.Span,
name: string,
pkg: int,
file: int,
c_abi: bool,
params: []Param,
result: Type_Syntax,
body: []int,
diagnostic: int,
}
Global :: struct {
span: source.Span,
name: string,
pkg: int,
file: int,
type: Type_Syntax,
immutable: bool,
expr: int,
diagnostic: int,
}
Import :: struct {
span: source.Span,
alias: string,
path: string,
pkg: int,
file: int,
target: int,
valid: bool,
used: bool,
diagnostic: int,
}
File :: struct {
source: int,
pkg: int,
}
Package :: struct {
path: string,
name: string,
available: bool,
}
Module :: struct {
exprs: [dynamic]Expr,
statements: [dynamic]Stmt,
functions: [dynamic]Function,
globals: [dynamic]Global,
imports: [dynamic]Import,
files: [dynamic]File,
packages: [dynamic]Package,
allocator: mem.Allocator,
}
init_module :: proc(allocator := context.allocator) -> Module {
module: Module
module.allocator = allocator
module.exprs.allocator = allocator
module.statements.allocator = allocator
module.functions.allocator = allocator
module.globals.allocator = allocator
module.imports.allocator = allocator
module.files.allocator = allocator
module.packages.allocator = allocator
return module
}
destroy_module :: proc(module: ^Module) {
for expr in module.exprs {
delete(expr.args, module.allocator)
}
for function in module.functions {
delete(function.params, module.allocator)
delete(function.body, module.allocator)
}
for import_item in module.imports {
delete(import_item.path, module.allocator)
}
for pkg in module.packages {
delete(pkg.path, module.allocator)
delete(pkg.name, module.allocator)
}
delete(module.exprs)
delete(module.statements)
delete(module.functions)
delete(module.globals)
delete(module.imports)
delete(module.files)
delete(module.packages)
}