new types system aliases, comments in type resolution
This commit is contained in:
parent
0de15e008b
commit
235f605a05
|
|
@ -39,6 +39,7 @@ pub const ConcurrentQueueUnmanagedAdvanced = concurrent_queue.ConcurrentQueueUnm
|
||||||
|
|
||||||
pub const paged_vector = @import("structures/paged-vector.zig");
|
pub const paged_vector = @import("structures/paged-vector.zig");
|
||||||
pub const PagedVector = paged_vector.PagedVector;
|
pub const PagedVector = paged_vector.PagedVector;
|
||||||
|
pub const PagedVectorUnmanaged = paged_vector.PagedVectorUnmanaged;
|
||||||
|
|
||||||
pub const tag_pool = @import("structures/tag-pool.zig");
|
pub const tag_pool = @import("structures/tag-pool.zig");
|
||||||
pub const RawTag = tag_pool.RawTag;
|
pub const RawTag = tag_pool.RawTag;
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ struct SliceMutable
|
||||||
|
|
||||||
|
|
||||||
struct Slice
|
struct Slice
|
||||||
ptr: const_c_ptr u8
|
ptr: const c_ptr
|
||||||
len: usize
|
len: usize
|
||||||
|
|
||||||
struct Vec2
|
struct Vec2
|
||||||
|
|
@ -56,8 +56,8 @@ struct Rect
|
||||||
pos: Point
|
pos: Point
|
||||||
size: Size
|
size: Size
|
||||||
|
|
||||||
fn get_engine -> c_ptr opaque
|
fn get_engine -> c_ptr
|
||||||
fn get_tag_registry -> c_ptr opaque
|
fn get_tag_registry -> c_ptr # new rule: all c_ptr are ?*anyopaque
|
||||||
fn start -> void
|
fn start -> void
|
||||||
fn shutdown -> void
|
fn shutdown -> void
|
||||||
|
|
||||||
|
|
@ -70,7 +70,6 @@ fn log_puts -> void
|
||||||
verbosity: Verbosity
|
verbosity: Verbosity
|
||||||
string: Slice
|
string: Slice
|
||||||
|
|
||||||
|
|
||||||
# define a function type
|
# define a function type
|
||||||
fnptr on_log -> void
|
fnptr on_log -> void
|
||||||
verbosity: Verbosity
|
verbosity: Verbosity
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ fn normalizeContents(buffer: []u8) []u8 {
|
||||||
}
|
}
|
||||||
if (mem.eql(u8, buffer[cursor .. cursor + 2], "\r\n")) {
|
if (mem.eql(u8, buffer[cursor .. cursor + 2], "\r\n")) {
|
||||||
buffer[wcursor] = '\n';
|
buffer[wcursor] = '\n';
|
||||||
cursor += 2;
|
cursor += 1;
|
||||||
wcursor += 1;
|
wcursor += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -32,9 +32,30 @@ const FunctionDefinition = struct {
|
||||||
isInstance: bool, // if true this function definition is a function pointer type
|
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 {
|
pub const TypeRef = struct {
|
||||||
lineRef: u32,
|
lineRef: u32,
|
||||||
tokens: []Token,
|
tokens: []Token,
|
||||||
|
resolved: ?TypeId = 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});
|
||||||
|
|
@ -81,7 +102,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 };
|
const Keyword = enum { @"fn", @"struct", @"enum", module, fnptr, alias, c_ptr, @"const" };
|
||||||
|
|
||||||
const TokenKind = union(enum(u8)) {
|
const TokenKind = union(enum(u8)) {
|
||||||
term: TerminalKind,
|
term: TerminalKind,
|
||||||
|
|
@ -201,10 +222,11 @@ const directives = @typeInfo(Directive).@"enum".field_names;
|
||||||
|
|
||||||
pub const Module = struct {
|
pub const Module = struct {
|
||||||
enums: ArrayList(EnumDefnition) = .empty,
|
enums: ArrayList(EnumDefnition) = .empty,
|
||||||
|
aliases: ArrayList(AliasDefinition) = .empty,
|
||||||
structs: ArrayList(StructDefinition) = .empty,
|
structs: ArrayList(StructDefinition) = .empty,
|
||||||
functions: ArrayList(FunctionDefinition) = .empty,
|
functions: ArrayList(FunctionDefinition) = .empty,
|
||||||
module: ?ModuleDefinition = null,
|
module: ?ModuleDefinition = null,
|
||||||
typeRefs: ArrayList(TypeRef),
|
typeRefs: ArrayList(TypeRef) = .empty,
|
||||||
|
|
||||||
gpa: std.mem.Allocator,
|
gpa: std.mem.Allocator,
|
||||||
lines: []Line,
|
lines: []Line,
|
||||||
|
|
@ -390,6 +412,11 @@ pub const Module = struct {
|
||||||
self.lines = try lines.toOwnedSlice(gpa);
|
self.lines = try lines.toOwnedSlice(gpa);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const AliasDefinition = struct {
|
||||||
|
name: []const u8,
|
||||||
|
target: TypeRef,
|
||||||
|
};
|
||||||
|
|
||||||
const ModuleDefinition = struct {
|
const ModuleDefinition = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
imports: std.ArrayList([]const u8) = .empty,
|
imports: std.ArrayList([]const u8) = .empty,
|
||||||
|
|
@ -401,9 +428,13 @@ pub const Module = struct {
|
||||||
@"fn": FunctionDefinition,
|
@"fn": FunctionDefinition,
|
||||||
@"struct": StructDefinition,
|
@"struct": StructDefinition,
|
||||||
@"enum": EnumDefnition,
|
@"enum": EnumDefnition,
|
||||||
|
alias: AliasDefinition,
|
||||||
|
|
||||||
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
|
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
|
||||||
switch (self) {
|
switch (self) {
|
||||||
|
.alias => |alias| {
|
||||||
|
try writer.print("(alias {s} {f})", .{ alias.name, alias.target });
|
||||||
|
},
|
||||||
.module => |mod| {
|
.module => |mod| {
|
||||||
try writer.print("(module", .{});
|
try writer.print("(module", .{});
|
||||||
for (mod.imports.items) |i| {
|
for (mod.imports.items) |i| {
|
||||||
|
|
@ -467,18 +498,24 @@ pub const Module = struct {
|
||||||
.fields = .empty,
|
.fields = .empty,
|
||||||
};
|
};
|
||||||
|
|
||||||
for (body) |l| {
|
for (body, 1..) |l, i| {
|
||||||
if (l.tokens.len == 0) continue;
|
if (l.tokens.len == 0) continue;
|
||||||
switch (l.tokens[0].tokenKind) {
|
switch (l.tokens[0].tokenKind) {
|
||||||
.directive => return error.UnexpectedDirective, // can easily add directives support here
|
.directive => return error.UnexpectedDirective, // can easily add directives support here
|
||||||
.label => {
|
.label => {
|
||||||
try expectTerminal(l.tokens[1], .colon);
|
try expectTerminal(l.tokens[1], .colon);
|
||||||
|
|
||||||
|
const endsInComment = l.tokens[l.tokens.len - 1].tokenKind == .comment;
|
||||||
|
|
||||||
try s.fields.append(self.gpa, .{
|
try s.fields.append(self.gpa, .{
|
||||||
.docstring = null,
|
.docstring = null,
|
||||||
.name = l.tokens[0].slice(l.contents),
|
.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 => {},
|
.comment => {},
|
||||||
else => return error.UnexpectedStatement,
|
else => return error.UnexpectedStatement,
|
||||||
|
|
@ -565,10 +602,15 @@ pub const Module = struct {
|
||||||
.label => {
|
.label => {
|
||||||
try expectTerminal(l.tokens[1], .colon);
|
try expectTerminal(l.tokens[1], .colon);
|
||||||
|
|
||||||
|
const endsInComment = l.tokens[l.tokens.len - 1].tokenKind == .comment;
|
||||||
|
|
||||||
try func.args.append(self.gpa, .{
|
try func.args.append(self.gpa, .{
|
||||||
.docstring = null,
|
.docstring = null,
|
||||||
.name = l.tokens[0].slice(l.contents),
|
.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 => {},
|
.comment => {},
|
||||||
|
|
@ -617,6 +659,24 @@ pub const Module = struct {
|
||||||
return lines;
|
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 {
|
fn parseModule(self: @This(), cursor: *u32) !ModuleDefinition {
|
||||||
const log = log_tokenizer;
|
const log = log_tokenizer;
|
||||||
const line, const body = try self.setupBodyState(cursor, .module);
|
const line, const body = try self.setupBodyState(cursor, .module);
|
||||||
|
|
@ -655,7 +715,8 @@ pub const Module = struct {
|
||||||
.keyword => |kw| {
|
.keyword => |kw| {
|
||||||
switch (kw) {
|
switch (kw) {
|
||||||
.alias => {
|
.alias => {
|
||||||
// dont do anything right now
|
newdef = .{ .alias = try self.parseAlias(cursor) };
|
||||||
|
try self.aliases.append(self.gpa, newdef.alias);
|
||||||
},
|
},
|
||||||
.@"enum" => {
|
.@"enum" => {
|
||||||
newdef = .{ .@"enum" = try self.parseEnum(cursor) };
|
newdef = .{ .@"enum" = try self.parseEnum(cursor) };
|
||||||
|
|
@ -677,6 +738,9 @@ 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 => {
|
||||||
|
return error.UnexpectedKeyword;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
else => return error.UnexpectedToken,
|
else => return error.UnexpectedToken,
|
||||||
|
|
@ -699,6 +763,26 @@ pub const Module = struct {
|
||||||
output: []const u8,
|
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 {
|
fn fmt(self: *@This(), comptime f: []const u8, a: anytype) ?[]const u8 {
|
||||||
return self.gpa.print(f, a) catch return null;
|
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 {
|
pub const TadModuleOptions = struct {
|
||||||
io: std.Io,
|
io: std.Io,
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
|
|
@ -779,17 +816,14 @@ pub fn compileAll(opts: TadModuleOptions) !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(opts.allocator);
|
var arena = std.heap.ArenaAllocator.init(opts.allocator);
|
||||||
defer arena.deinit();
|
defer arena.deinit();
|
||||||
|
|
||||||
var store: Store = undefined;
|
|
||||||
try store.init(arena.allocator(), opts.path, normalized);
|
|
||||||
|
|
||||||
var mod: Module = undefined;
|
var mod: Module = undefined;
|
||||||
try mod.lex(arena.allocator(), opts.path, normalized);
|
try mod.lex(arena.allocator(), opts.path, normalized);
|
||||||
try mod.parse();
|
try mod.parse();
|
||||||
|
try mod.resolve(); // resolve all typeRefs
|
||||||
|
|
||||||
// parse submodules here...
|
// parse submodules here...
|
||||||
|
|
||||||
// generate
|
// generate
|
||||||
|
|
||||||
try mod.generateZigApi(.{ .io = opts.io, .output = "./zig-out/api/core.zig" });
|
try mod.generateZigApi(.{ .io = opts.io, .output = "./zig-out/api/core.zig" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue