Compare commits

..

2 Commits

Author SHA1 Message Date
Peterino2 7799275511 type linking in tad complete 2026-12-02 01:52:25 -07:00
Peterino2 4650d31eba types reference store complete 2026-12-02 00:27:40 -07:00
2 changed files with 133 additions and 21 deletions

View File

@ -37,7 +37,7 @@ enum Verbosity
err
struct SliceMutable
ptr: c_ptr u8
ptr: c_ptr
len: usize

View File

@ -18,7 +18,7 @@ fn normalizeContents(buffer: []u8) []u8 {
return buffer[0..wcursor];
}
const BuiltinType = enum(u8) { usize, int, uint, @"opaque", i8, u8, i32, u32, i64, u64 };
const BuiltinType = enum(u8) { usize, int, uint, i8, u8, i32, u32, i64, u64, c_ptr, __none };
const StructDefinition = struct {
name: []const u8,
@ -42,20 +42,17 @@ pub const PointerType = struct {
pub const TypeId = u32;
pub const ResolvedType = struct {
isConst: bool = false,
kind: union(enum(u8)) {
@"struct": u32,
@"enum": u32,
@"fn": u32,
fnptr: u32,
builtin: BuiltinType,
pointer: PointerType,
linkKind: TypeLinkKind,
},
};
pub const TypeRef = struct {
lineRef: u32,
tokens: []Token,
resolved: ?TypeId = null,
resolved: ?ResolvedType = null,
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
try writer.print("(type line:{d}", .{self.lineRef});
@ -102,7 +99,7 @@ fn parseInt(x: []const u8) ?i64 {
return std.fmt.parseInt(i64, x, 0) catch null;
}
const Keyword = enum { @"fn", @"struct", @"enum", module, fnptr, alias, c_ptr, @"const" };
const Keyword = enum { @"fn", @"struct", @"enum", module, fnptr, alias, @"const" };
const TokenKind = union(enum(u8)) {
term: TerminalKind,
@ -218,7 +215,20 @@ const Directive = enum {
import,
backing,
};
const directives = @typeInfo(Directive).@"enum".field_names;
const TypeLinkKind = union(enum(u8)) {
@"enum": u32,
alias: u32,
@"struct": u32,
@"fn": u32,
builtin: BuiltinType,
};
const TypeLink = struct {
name: []const u8,
kind: TypeLinkKind,
};
pub const Module = struct {
enums: ArrayList(EnumDefnition) = .empty,
@ -226,7 +236,8 @@ pub const Module = struct {
structs: ArrayList(StructDefinition) = .empty,
functions: ArrayList(FunctionDefinition) = .empty,
module: ?ModuleDefinition = null,
typeRefs: ArrayList(TypeRef) = .empty,
pointerType: ArrayList(PointerType) = .empty,
typeRefsByName: std.StringHashMap(TypeLink),
gpa: std.mem.Allocator,
lines: []Line,
@ -375,6 +386,7 @@ pub const Module = struct {
pub fn lex(self: *@This(), gpa: std.mem.Allocator, file: []const u8, contents: []const u8) !void {
const log = log_tokenizer;
self.* = .{
.typeRefsByName = .init(gpa),
.file = file,
.contents = contents,
.gpa = gpa,
@ -629,13 +641,10 @@ pub const Module = struct {
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)
isInstance = true;
}
// zig fmt: on
const indent = lines[0].indent + 1;
const body = readBody(lines[1..], indent);
cursor.* += @as(u32, @intCast(body.len));
@ -717,35 +726,56 @@ pub const Module = struct {
.alias => {
newdef = .{ .alias = try self.parseAlias(cursor) };
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" => {
newdef = .{ .@"enum" = try self.parseEnum(cursor) };
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 => {
newdef = .{ .@"fn" = try self.parseFunction(cursor) };
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" => {
newdef = .{ .@"fn" = try self.parseFunction(cursor) };
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" => {
newdef = .{ .@"struct" = try self.parseStruct(cursor) };
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 => {
newdef = .{ .module = try self.parseModule(cursor) };
self.module = newdef.module;
},
.@"const", .c_ptr => {
.@"const",
=> {
return error.UnexpectedKeyword;
},
}
},
else => return error.UnexpectedToken,
}
log.info("{f}", .{newdef});
log.info("{f} typeRefsCount: {d}", .{ newdef, self.typeRefsByName.count() });
return newdef;
}
@ -758,13 +788,94 @@ 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 {
io: std.Io,
output: []const u8,
};
fn resolveTypeRef(self: *@This(), typeref: TypeRef) !void {
fn checkBuiltin(segment: []const u8) ?BuiltinType {
const keywords = @typeInfo(BuiltinType).@"enum".field_names;
inline for (keywords, 0..) |k, i| {
if (mem.eql(u8, segment, k)) {
const kw: BuiltinType = @fromBackingInt(@intCast(i));
return kw;
}
}
return null;
}
fn getTypeRefByName(self: *@This(), name: []const u8) ?ResolvedType {
// const name = tokens[0].slice(line.contents);
const builtin = checkBuiltin(name);
const ref = self.typeRefsByName.get(name);
if (ref) |r| {
return .{ .kind = .{ .linkKind = r.kind } };
}
if (builtin) |b| {
return .{ .kind = .{ .builtin = b } };
}
log_main.warn("(name:{s} builtin:{s})", .{ name, @tagName(builtin orelse .__none) });
return null;
}
fn resolveTokenType(self: *@This(), typeref: *TypeRef, line: Line, tokens: []Token, isConst: bool) !void {
if (tokens.len > 1) {
log_main.err("extra tokens after type, found '{s}'\nline: {d}\n{s}", .{
tokens[1].slice(line.contents),
line.num,
line.contents,
});
return error.UnexpectedTokens;
}
const name = tokens[0].slice(line.contents);
const ref = self.getTypeRefByName(name);
if (ref) |r| {
typeref.resolved = r;
typeref.resolved.?.isConst = isConst;
} else {
log_main.err("unrecognized symbol {s}\nline: {d}\n{s}", .{
name,
line.num,
line.contents,
});
return error.UnrecognizedSymbol;
}
log_main.warn("type resolved {s}", .{line.contents});
}
fn resolveTypeRef(self: *@This(), typeref: *TypeRef) !void {
const line = self.lines[typeref.lineRef];
var tokens = typeref.tokens;
if (tokens[tokens.len - 1].tokenKind == .comment) tokens = tokens[0 .. tokens.len - 1];
switch (tokens[0].tokenKind) {
.label => {
try self.resolveTokenType(typeref, line, tokens, false);
},
.keyword => |kw| {
switch (kw) {
.@"const" => {
try self.resolveTokenType(typeref, line, tokens[1..], true);
},
.fnptr => {},
else => {},
}
},
else => {
return error.ResolvedReferenceType;
},
}
for (typeref.tokens) |tok| {
const x = tok.slice(line.contents);
@ -775,10 +886,10 @@ pub const Module = struct {
// link all references together
pub fn resolve(self: *@This()) !void {
// 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});
for (st.fields.items) |field| {
try self.resolveTypeRef(field.typeReference);
for (st.fields.items) |*field| {
try self.resolveTypeRef(&field.typeReference);
}
}
}
@ -819,6 +930,7 @@ pub fn compileAll(opts: TadModuleOptions) !void {
var mod: Module = undefined;
try mod.lex(arena.allocator(), opts.path, normalized);
try mod.parse();
mod.printSymbols();
try mod.resolve(); // resolve all typeRefs
// parse submodules here...