type linking in tad complete
This commit is contained in:
parent
4650d31eba
commit
7799275511
|
|
@ -37,7 +37,7 @@ enum Verbosity
|
|||
err
|
||||
|
||||
struct SliceMutable
|
||||
ptr: c_ptr u8
|
||||
ptr: c_ptr
|
||||
len: usize
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
@ -220,16 +217,17 @@ const Directive = enum {
|
|||
};
|
||||
|
||||
const directives = @typeInfo(Directive).@"enum".field_names;
|
||||
const TypeKind = union(enum(u8)) {
|
||||
const TypeLinkKind = union(enum(u8)) {
|
||||
@"enum": u32,
|
||||
alias: u32,
|
||||
@"struct": u32,
|
||||
@"fn": u32,
|
||||
builtin: BuiltinType,
|
||||
};
|
||||
|
||||
const TypeLink = struct {
|
||||
name: []const u8,
|
||||
kind: TypeKind,
|
||||
kind: TypeLinkKind,
|
||||
};
|
||||
|
||||
pub const Module = struct {
|
||||
|
|
@ -238,6 +236,7 @@ pub const Module = struct {
|
|||
structs: ArrayList(StructDefinition) = .empty,
|
||||
functions: ArrayList(FunctionDefinition) = .empty,
|
||||
module: ?ModuleDefinition = null,
|
||||
pointerType: ArrayList(PointerType) = .empty,
|
||||
typeRefsByName: std.StringHashMap(TypeLink),
|
||||
|
||||
gpa: std.mem.Allocator,
|
||||
|
|
@ -768,7 +767,8 @@ pub const Module = struct {
|
|||
newdef = .{ .module = try self.parseModule(cursor) };
|
||||
self.module = newdef.module;
|
||||
},
|
||||
.@"const", .c_ptr => {
|
||||
.@"const",
|
||||
=> {
|
||||
return error.UnexpectedKeyword;
|
||||
},
|
||||
}
|
||||
|
|
@ -800,20 +800,74 @@ pub const Module = struct {
|
|||
output: []const u8,
|
||||
};
|
||||
|
||||
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];
|
||||
const tokens = typeref.tokens;
|
||||
var tokens = typeref.tokens;
|
||||
if (tokens[tokens.len - 1].tokenKind == .comment) tokens = tokens[0 .. tokens.len - 1];
|
||||
|
||||
switch (tokens[0].tokenKind) {
|
||||
.label => {
|
||||
const name = tokens[0].slice(line.contents);
|
||||
_ = name;
|
||||
// examples: Slice
|
||||
try self.resolveTokenType(typeref, line, tokens, false);
|
||||
},
|
||||
.keyword => |kw| {
|
||||
switch (kw) {
|
||||
.c_ptr => {},
|
||||
.@"const" => {},
|
||||
.@"const" => {
|
||||
try self.resolveTokenType(typeref, line, tokens[1..], true);
|
||||
},
|
||||
.fnptr => {},
|
||||
else => {},
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue