new types system aliases, comments in type resolution

This commit is contained in:
Peterino2 2026-12-01 23:18:32 -07:00
parent 0de15e008b
commit 235f605a05
3 changed files with 96 additions and 62 deletions

View File

@ -39,6 +39,7 @@ pub const ConcurrentQueueUnmanagedAdvanced = concurrent_queue.ConcurrentQueueUnm
pub const paged_vector = @import("structures/paged-vector.zig");
pub const PagedVector = paged_vector.PagedVector;
pub const PagedVectorUnmanaged = paged_vector.PagedVectorUnmanaged;
pub const tag_pool = @import("structures/tag-pool.zig");
pub const RawTag = tag_pool.RawTag;

View File

@ -42,7 +42,7 @@ struct SliceMutable
struct Slice
ptr: const_c_ptr u8
ptr: const c_ptr
len: usize
struct Vec2
@ -56,8 +56,8 @@ struct Rect
pos: Point
size: Size
fn get_engine -> c_ptr opaque
fn get_tag_registry -> c_ptr opaque
fn get_engine -> c_ptr
fn get_tag_registry -> c_ptr # new rule: all c_ptr are ?*anyopaque
fn start -> void
fn shutdown -> void
@ -70,7 +70,6 @@ fn log_puts -> void
verbosity: Verbosity
string: Slice
# define a function type
fnptr on_log -> void
verbosity: Verbosity

View File

@ -8,7 +8,7 @@ fn normalizeContents(buffer: []u8) []u8 {
}
if (mem.eql(u8, buffer[cursor .. cursor + 2], "\r\n")) {
buffer[wcursor] = '\n';
cursor += 2;
cursor += 1;
wcursor += 1;
continue;
}
@ -32,9 +32,30 @@ const FunctionDefinition = struct {
isInstance: bool, // if true this function definition is a function pointer type
};
pub const PointerType = struct {
alignment: u8,
size: u32,
isConst: bool,
target: TypeId,
};
pub const TypeId = u32;
pub const ResolvedType = struct {
kind: union(enum(u8)) {
@"struct": u32,
@"enum": u32,
@"fn": u32,
fnptr: u32,
builtin: BuiltinType,
pointer: PointerType,
},
};
pub const TypeRef = struct {
lineRef: u32,
tokens: []Token,
resolved: ?TypeId = null,
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
try writer.print("(type line:{d}", .{self.lineRef});
@ -81,7 +102,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 };
const Keyword = enum { @"fn", @"struct", @"enum", module, fnptr, alias, c_ptr, @"const" };
const TokenKind = union(enum(u8)) {
term: TerminalKind,
@ -201,10 +222,11 @@ const directives = @typeInfo(Directive).@"enum".field_names;
pub const Module = struct {
enums: ArrayList(EnumDefnition) = .empty,
aliases: ArrayList(AliasDefinition) = .empty,
structs: ArrayList(StructDefinition) = .empty,
functions: ArrayList(FunctionDefinition) = .empty,
module: ?ModuleDefinition = null,
typeRefs: ArrayList(TypeRef),
typeRefs: ArrayList(TypeRef) = .empty,
gpa: std.mem.Allocator,
lines: []Line,
@ -390,6 +412,11 @@ pub const Module = struct {
self.lines = try lines.toOwnedSlice(gpa);
}
const AliasDefinition = struct {
name: []const u8,
target: TypeRef,
};
const ModuleDefinition = struct {
name: []const u8,
imports: std.ArrayList([]const u8) = .empty,
@ -401,9 +428,13 @@ pub const Module = struct {
@"fn": FunctionDefinition,
@"struct": StructDefinition,
@"enum": EnumDefnition,
alias: AliasDefinition,
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
switch (self) {
.alias => |alias| {
try writer.print("(alias {s} {f})", .{ alias.name, alias.target });
},
.module => |mod| {
try writer.print("(module", .{});
for (mod.imports.items) |i| {
@ -467,18 +498,24 @@ pub const Module = struct {
.fields = .empty,
};
for (body) |l| {
for (body, 1..) |l, i| {
if (l.tokens.len == 0) continue;
switch (l.tokens[0].tokenKind) {
.directive => return error.UnexpectedDirective, // can easily add directives support here
.label => {
try expectTerminal(l.tokens[1], .colon);
const endsInComment = l.tokens[l.tokens.len - 1].tokenKind == .comment;
try s.fields.append(self.gpa, .{
.docstring = null,
.name = l.tokens[0].slice(l.contents),
.typeReference = .{ .lineRef = lineRef, .tokens = l.tokens[2..] },
.typeReference = .{
.lineRef = lineRef + @as(u32, @intCast(i)),
.tokens = l.tokens[2..if (endsInComment) l.tokens.len - 1 else l.tokens.len],
},
});
log_tokenizer.warn("{d} {f}", .{ i, s.fields.last().?.typeReference });
},
.comment => {},
else => return error.UnexpectedStatement,
@ -565,10 +602,15 @@ pub const Module = struct {
.label => {
try expectTerminal(l.tokens[1], .colon);
const endsInComment = l.tokens[l.tokens.len - 1].tokenKind == .comment;
try func.args.append(self.gpa, .{
.docstring = null,
.name = l.tokens[0].slice(l.contents),
.typeReference = .{ .lineRef = lineRef, .tokens = l.tokens[2..] },
.typeReference = .{
.lineRef = lineRef,
.tokens = l.tokens[2..if (endsInComment) l.tokens.len - 1 else l.tokens.len],
},
});
},
.comment => {},
@ -617,6 +659,24 @@ pub const Module = struct {
return lines;
}
fn parseAlias(self: @This(), cursor: *u32) !AliasDefinition {
const lineRef = cursor.*;
const line = self.lines[cursor.*];
try expectKeyword(line.tokens[0], .alias);
if (line.tokens[1].tokenKind != .label) return error.ExpectedLabel;
if (line.tokens[2].tokenKind != .term or line.tokens[2].tokenKind.term != .equals) return error.ExpectedToken;
const endsInComment = line.tokens[line.tokens.len - 1].tokenKind == .comment;
return .{
.name = line.tokens[1].slice(line.contents),
.target = .{
.lineRef = lineRef,
.tokens = line.tokens[2..if (endsInComment) line.tokens.len - 1 else line.tokens.len],
},
};
}
fn parseModule(self: @This(), cursor: *u32) !ModuleDefinition {
const log = log_tokenizer;
const line, const body = try self.setupBodyState(cursor, .module);
@ -655,7 +715,8 @@ pub const Module = struct {
.keyword => |kw| {
switch (kw) {
.alias => {
// dont do anything right now
newdef = .{ .alias = try self.parseAlias(cursor) };
try self.aliases.append(self.gpa, newdef.alias);
},
.@"enum" => {
newdef = .{ .@"enum" = try self.parseEnum(cursor) };
@ -677,6 +738,9 @@ pub const Module = struct {
newdef = .{ .module = try self.parseModule(cursor) };
self.module = newdef.module;
},
.@"const", .c_ptr => {
return error.UnexpectedKeyword;
},
}
},
else => return error.UnexpectedToken,
@ -699,6 +763,26 @@ pub const Module = struct {
output: []const u8,
};
fn resolveTypeRef(self: *@This(), typeref: TypeRef) !void {
const line = self.lines[typeref.lineRef];
for (typeref.tokens) |tok| {
const x = tok.slice(line.contents);
log_main.warn(" ({s})", .{x});
}
}
// link all references together
pub fn resolve(self: *@This()) !void {
// walk over structs, then function fields in that order
for (self.structs.items) |st| {
log_main.warn("(struct {s})", .{st.name});
for (st.fields.items) |field| {
try self.resolveTypeRef(field.typeReference);
}
}
}
fn fmt(self: *@This(), comptime f: []const u8, a: anytype) ?[]const u8 {
return self.gpa.print(f, a) catch return null;
}
@ -719,53 +803,6 @@ pub const Module = struct {
}
};
const ModuleId = u32;
pub const PointerId = u32;
pub const EnumId = u32;
pub const StructId = u32;
pub const TokensLink = struct {
module: u32,
id: u32,
};
pub const StructNode = struct {
name: []const u8,
fields: std.ArrayList(Field),
};
pub const EnumNode = struct {
name: []const u8,
values: std.ArrayList(EnumValue),
};
pub const AliasNode = struct {};
pub const FunctionNode = struct {};
pub const StoreNode = struct {
id: u32, // index in the nodes paged vector
link: TokensLink,
node: union(enum(u8)) {
@"struct": StructNode,
@"enum": EnumNode,
alias: AliasNode,
function: FunctionNode,
},
};
// new store operator
pub const Store = struct {
gpa: std.mem.Allocator,
nodes: p2.PagedVectorUnmanaged(StoreNode) = .empty,
pub fn init(self: *@This(), gpa: std.mem.Allocator) !void {
self.* = .{
.gpa = gpa,
};
}
};
pub const TadModuleOptions = struct {
io: std.Io,
allocator: std.mem.Allocator,
@ -779,17 +816,14 @@ pub fn compileAll(opts: TadModuleOptions) !void {
var arena = std.heap.ArenaAllocator.init(opts.allocator);
defer arena.deinit();
var store: Store = undefined;
try store.init(arena.allocator(), opts.path, normalized);
var mod: Module = undefined;
try mod.lex(arena.allocator(), opts.path, normalized);
try mod.parse();
try mod.resolve(); // resolve all typeRefs
// parse submodules here...
// generate
try mod.generateZigApi(.{ .io = opts.io, .output = "./zig-out/api/core.zig" });
}