type linking in tad complete
This commit is contained in:
parent
4650d31eba
commit
7799275511
|
|
@ -37,7 +37,7 @@ enum Verbosity
|
||||||
err
|
err
|
||||||
|
|
||||||
struct SliceMutable
|
struct SliceMutable
|
||||||
ptr: c_ptr u8
|
ptr: c_ptr
|
||||||
len: usize
|
len: usize
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ fn normalizeContents(buffer: []u8) []u8 {
|
||||||
return buffer[0..wcursor];
|
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 {
|
const StructDefinition = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
|
|
@ -42,20 +42,17 @@ pub const PointerType = struct {
|
||||||
pub const TypeId = u32;
|
pub const TypeId = u32;
|
||||||
|
|
||||||
pub const ResolvedType = struct {
|
pub const ResolvedType = struct {
|
||||||
|
isConst: bool = false,
|
||||||
kind: union(enum(u8)) {
|
kind: union(enum(u8)) {
|
||||||
@"struct": u32,
|
|
||||||
@"enum": u32,
|
|
||||||
@"fn": u32,
|
|
||||||
fnptr: u32,
|
|
||||||
builtin: BuiltinType,
|
builtin: BuiltinType,
|
||||||
pointer: PointerType,
|
linkKind: TypeLinkKind,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const TypeRef = struct {
|
pub const TypeRef = struct {
|
||||||
lineRef: u32,
|
lineRef: u32,
|
||||||
tokens: []Token,
|
tokens: []Token,
|
||||||
resolved: ?TypeId = null,
|
resolved: ?ResolvedType = null,
|
||||||
|
|
||||||
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
|
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
|
||||||
try writer.print("(type line:{d}", .{self.lineRef});
|
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;
|
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)) {
|
const TokenKind = union(enum(u8)) {
|
||||||
term: TerminalKind,
|
term: TerminalKind,
|
||||||
|
|
@ -220,16 +217,17 @@ const Directive = enum {
|
||||||
};
|
};
|
||||||
|
|
||||||
const directives = @typeInfo(Directive).@"enum".field_names;
|
const directives = @typeInfo(Directive).@"enum".field_names;
|
||||||
const TypeKind = union(enum(u8)) {
|
const TypeLinkKind = union(enum(u8)) {
|
||||||
@"enum": u32,
|
@"enum": u32,
|
||||||
alias: u32,
|
alias: u32,
|
||||||
@"struct": u32,
|
@"struct": u32,
|
||||||
@"fn": u32,
|
@"fn": u32,
|
||||||
builtin: BuiltinType,
|
builtin: BuiltinType,
|
||||||
};
|
};
|
||||||
|
|
||||||
const TypeLink = struct {
|
const TypeLink = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
kind: TypeKind,
|
kind: TypeLinkKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Module = struct {
|
pub const Module = struct {
|
||||||
|
|
@ -238,6 +236,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,
|
||||||
|
pointerType: ArrayList(PointerType) = .empty,
|
||||||
typeRefsByName: std.StringHashMap(TypeLink),
|
typeRefsByName: std.StringHashMap(TypeLink),
|
||||||
|
|
||||||
gpa: std.mem.Allocator,
|
gpa: std.mem.Allocator,
|
||||||
|
|
@ -768,7 +767,8 @@ pub const Module = struct {
|
||||||
newdef = .{ .module = try self.parseModule(cursor) };
|
newdef = .{ .module = try self.parseModule(cursor) };
|
||||||
self.module = newdef.module;
|
self.module = newdef.module;
|
||||||
},
|
},
|
||||||
.@"const", .c_ptr => {
|
.@"const",
|
||||||
|
=> {
|
||||||
return error.UnexpectedKeyword;
|
return error.UnexpectedKeyword;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -800,20 +800,74 @@ pub const Module = struct {
|
||||||
output: []const u8,
|
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 {
|
fn resolveTypeRef(self: *@This(), typeref: *TypeRef) !void {
|
||||||
const line = self.lines[typeref.lineRef];
|
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) {
|
switch (tokens[0].tokenKind) {
|
||||||
.label => {
|
.label => {
|
||||||
const name = tokens[0].slice(line.contents);
|
try self.resolveTokenType(typeref, line, tokens, false);
|
||||||
_ = name;
|
|
||||||
// examples: Slice
|
|
||||||
},
|
},
|
||||||
.keyword => |kw| {
|
.keyword => |kw| {
|
||||||
switch (kw) {
|
switch (kw) {
|
||||||
.c_ptr => {},
|
.@"const" => {
|
||||||
.@"const" => {},
|
try self.resolveTokenType(typeref, line, tokens[1..], true);
|
||||||
|
},
|
||||||
.fnptr => {},
|
.fnptr => {},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue