|
|
|
|
@ -18,7 +18,7 @@ fn normalizeContents(buffer: []u8) []u8 {
|
|
|
|
|
return buffer[0..wcursor];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const BuiltinType = enum(u8) { usize, int, uint, i8, u8, i32, u32, i64, u64, c_ptr, __none };
|
|
|
|
|
const BuiltinType = enum(u8) { usize, int, uint, @"opaque", i8, u8, i32, u32, i64, u64 };
|
|
|
|
|
|
|
|
|
|
const StructDefinition = struct {
|
|
|
|
|
name: []const u8,
|
|
|
|
|
@ -42,17 +42,20 @@ 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,
|
|
|
|
|
linkKind: TypeLinkKind,
|
|
|
|
|
pointer: PointerType,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub const TypeRef = struct {
|
|
|
|
|
lineRef: u32,
|
|
|
|
|
tokens: []Token,
|
|
|
|
|
resolved: ?ResolvedType = null,
|
|
|
|
|
resolved: ?TypeId = null,
|
|
|
|
|
|
|
|
|
|
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
|
|
|
|
|
try writer.print("(type line:{d}", .{self.lineRef});
|
|
|
|
|
@ -99,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" };
|
|
|
|
|
const Keyword = enum { @"fn", @"struct", @"enum", module, fnptr, alias, c_ptr, @"const" };
|
|
|
|
|
|
|
|
|
|
const TokenKind = union(enum(u8)) {
|
|
|
|
|
term: TerminalKind,
|
|
|
|
|
@ -215,20 +218,7 @@ 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,
|
|
|
|
|
@ -236,8 +226,7 @@ pub const Module = struct {
|
|
|
|
|
structs: ArrayList(StructDefinition) = .empty,
|
|
|
|
|
functions: ArrayList(FunctionDefinition) = .empty,
|
|
|
|
|
module: ?ModuleDefinition = null,
|
|
|
|
|
pointerType: ArrayList(PointerType) = .empty,
|
|
|
|
|
typeRefsByName: std.StringHashMap(TypeLink),
|
|
|
|
|
typeRefs: ArrayList(TypeRef) = .empty,
|
|
|
|
|
|
|
|
|
|
gpa: std.mem.Allocator,
|
|
|
|
|
lines: []Line,
|
|
|
|
|
@ -386,7 +375,6 @@ 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,
|
|
|
|
|
@ -641,10 +629,13 @@ pub const Module = struct {
|
|
|
|
|
|
|
|
|
|
var isInstance: bool = false;
|
|
|
|
|
|
|
|
|
|
if (lines[0].tokens[0].tokenKind.keyword == .@"fn") {
|
|
|
|
|
// zig fmt: off
|
|
|
|
|
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));
|
|
|
|
|
@ -726,56 +717,35 @@ 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",
|
|
|
|
|
=> {
|
|
|
|
|
.@"const", .c_ptr => {
|
|
|
|
|
return error.UnexpectedKeyword;
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
else => return error.UnexpectedToken,
|
|
|
|
|
}
|
|
|
|
|
log.info("{f} typeRefsCount: {d}", .{ newdef, self.typeRefsByName.count() });
|
|
|
|
|
log.info("{f}", .{newdef});
|
|
|
|
|
return newdef;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -788,94 +758,13 @@ 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 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];
|
|
|
|
|
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);
|
|
|
|
|
@ -886,10 +775,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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -930,7 +819,6 @@ 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...
|
|
|
|
|
|