types reference store complete

This commit is contained in:
Peterino2 2026-12-02 00:27:40 -07:00
parent 235f605a05
commit 4650d31eba
1 changed files with 69 additions and 11 deletions

View File

@ -218,7 +218,19 @@ const Directive = enum {
import, import,
backing, backing,
}; };
const directives = @typeInfo(Directive).@"enum".field_names; const directives = @typeInfo(Directive).@"enum".field_names;
const TypeKind = union(enum(u8)) {
@"enum": u32,
alias: u32,
@"struct": u32,
@"fn": u32,
builtin: BuiltinType,
};
const TypeLink = struct {
name: []const u8,
kind: TypeKind,
};
pub const Module = struct { pub const Module = struct {
enums: ArrayList(EnumDefnition) = .empty, enums: ArrayList(EnumDefnition) = .empty,
@ -226,7 +238,7 @@ pub const Module = struct {
structs: ArrayList(StructDefinition) = .empty, structs: ArrayList(StructDefinition) = .empty,
functions: ArrayList(FunctionDefinition) = .empty, functions: ArrayList(FunctionDefinition) = .empty,
module: ?ModuleDefinition = null, module: ?ModuleDefinition = null,
typeRefs: ArrayList(TypeRef) = .empty, typeRefsByName: std.StringHashMap(TypeLink),
gpa: std.mem.Allocator, gpa: std.mem.Allocator,
lines: []Line, lines: []Line,
@ -375,6 +387,7 @@ pub const Module = struct {
pub fn lex(self: *@This(), gpa: std.mem.Allocator, file: []const u8, contents: []const u8) !void { pub fn lex(self: *@This(), gpa: std.mem.Allocator, file: []const u8, contents: []const u8) !void {
const log = log_tokenizer; const log = log_tokenizer;
self.* = .{ self.* = .{
.typeRefsByName = .init(gpa),
.file = file, .file = file,
.contents = contents, .contents = contents,
.gpa = gpa, .gpa = gpa,
@ -629,13 +642,10 @@ pub const Module = struct {
var isInstance: bool = false; var isInstance: bool = false;
// zig fmt: off if (lines[0].tokens[0].tokenKind.keyword == .@"fn") {
if(lines[0].tokens[0].tokenKind.keyword == .@"fn") if (lines[0].tokens[0].tokenKind.keyword == .fnptr)
{
if(lines[0].tokens[0].tokenKind.keyword == .fnptr)
isInstance = true; isInstance = true;
} }
// zig fmt: on
const indent = lines[0].indent + 1; const indent = lines[0].indent + 1;
const body = readBody(lines[1..], indent); const body = readBody(lines[1..], indent);
cursor.* += @as(u32, @intCast(body.len)); cursor.* += @as(u32, @intCast(body.len));
@ -717,22 +727,42 @@ pub const Module = struct {
.alias => { .alias => {
newdef = .{ .alias = try self.parseAlias(cursor) }; newdef = .{ .alias = try self.parseAlias(cursor) };
try self.aliases.append(self.gpa, newdef.alias); try self.aliases.append(self.gpa, newdef.alias);
try self.typeRefsByName.put(newdef.alias.name, .{
.name = newdef.alias.name,
.kind = .{ .alias = @intCast(self.aliases.items.len - 1) },
});
}, },
.@"enum" => { .@"enum" => {
newdef = .{ .@"enum" = try self.parseEnum(cursor) }; newdef = .{ .@"enum" = try self.parseEnum(cursor) };
try self.enums.append(self.gpa, newdef.@"enum"); try self.enums.append(self.gpa, newdef.@"enum");
try self.typeRefsByName.put(newdef.@"enum".name, .{
.name = newdef.@"enum".name,
.kind = .{ .@"enum" = @intCast(self.enums.items.len - 1) },
});
}, },
.fnptr => { .fnptr => {
newdef = .{ .@"fn" = try self.parseFunction(cursor) }; newdef = .{ .@"fn" = try self.parseFunction(cursor) };
try self.functions.append(self.gpa, newdef.@"fn"); try self.functions.append(self.gpa, newdef.@"fn");
try self.typeRefsByName.put(newdef.@"fn".name, .{
.name = newdef.@"fn".name,
.kind = .{ .@"fn" = @intCast(self.enums.items.len - 1) },
});
}, },
.@"fn" => { .@"fn" => {
newdef = .{ .@"fn" = try self.parseFunction(cursor) }; newdef = .{ .@"fn" = try self.parseFunction(cursor) };
try self.functions.append(self.gpa, newdef.@"fn"); try self.functions.append(self.gpa, newdef.@"fn");
try self.typeRefsByName.put(newdef.@"fn".name, .{
.name = newdef.@"fn".name,
.kind = .{ .@"fn" = @intCast(self.enums.items.len - 1) },
});
}, },
.@"struct" => { .@"struct" => {
newdef = .{ .@"struct" = try self.parseStruct(cursor) }; newdef = .{ .@"struct" = try self.parseStruct(cursor) };
try self.structs.append(self.gpa, newdef.@"struct"); try self.structs.append(self.gpa, newdef.@"struct");
try self.typeRefsByName.put(newdef.@"struct".name, .{
.name = newdef.@"struct".name,
.kind = .{ .@"struct" = @intCast(self.enums.items.len - 1) },
});
}, },
.module => { .module => {
newdef = .{ .module = try self.parseModule(cursor) }; newdef = .{ .module = try self.parseModule(cursor) };
@ -745,7 +775,7 @@ pub const Module = struct {
}, },
else => return error.UnexpectedToken, else => return error.UnexpectedToken,
} }
log.info("{f}", .{newdef}); log.info("{f} typeRefsCount: {d}", .{ newdef, self.typeRefsByName.count() });
return newdef; return newdef;
} }
@ -758,13 +788,40 @@ pub const Module = struct {
} }
} }
pub fn printSymbols(self: *@This()) void {
var iter = self.typeRefsByName.iterator();
while (iter.next()) |e| {
log_main.info("({s} {s})", .{ @tagName(e.value_ptr.kind), e.key_ptr.* });
}
}
pub const GenerateZigApiOptions = struct { pub const GenerateZigApiOptions = struct {
io: std.Io, io: std.Io,
output: []const u8, output: []const u8,
}; };
fn resolveTypeRef(self: *@This(), typeref: TypeRef) !void { fn resolveTypeRef(self: *@This(), typeref: *TypeRef) !void {
const line = self.lines[typeref.lineRef]; const line = self.lines[typeref.lineRef];
const tokens = typeref.tokens;
switch (tokens[0].tokenKind) {
.label => {
const name = tokens[0].slice(line.contents);
_ = name;
// examples: Slice
},
.keyword => |kw| {
switch (kw) {
.c_ptr => {},
.@"const" => {},
.fnptr => {},
else => {},
}
},
else => {
return error.ResolvedReferenceType;
},
}
for (typeref.tokens) |tok| { for (typeref.tokens) |tok| {
const x = tok.slice(line.contents); const x = tok.slice(line.contents);
@ -775,10 +832,10 @@ pub const Module = struct {
// link all references together // link all references together
pub fn resolve(self: *@This()) !void { pub fn resolve(self: *@This()) !void {
// walk over structs, then function fields in that order // walk over structs, then function fields in that order
for (self.structs.items) |st| { for (self.structs.items) |*st| {
log_main.warn("(struct {s})", .{st.name}); log_main.warn("(struct {s})", .{st.name});
for (st.fields.items) |field| { for (st.fields.items) |*field| {
try self.resolveTypeRef(field.typeReference); try self.resolveTypeRef(&field.typeReference);
} }
} }
} }
@ -819,6 +876,7 @@ pub fn compileAll(opts: TadModuleOptions) !void {
var mod: Module = undefined; var mod: Module = undefined;
try mod.lex(arena.allocator(), opts.path, normalized); try mod.lex(arena.allocator(), opts.path, normalized);
try mod.parse(); try mod.parse();
mod.printSymbols();
try mod.resolve(); // resolve all typeRefs try mod.resolve(); // resolve all typeRefs
// parse submodules here... // parse submodules here...