Skip to content

Commit

Permalink
Output syntactically correct zig
Browse files Browse the repository at this point in the history
  • Loading branch information
dpwm committed Feb 25, 2025
1 parent e530fc4 commit 940b6dc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/capnp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ pub const StructReader = struct {
}
}

pub fn readVoid(_: StructBuilder) void {}

pub fn readStringField(self: StructReader, ptrNo: u16) Counter.Error![]u8 {
return (try self.readPtrField(ListReader(u8), ptrNo)).getString();
}
Expand Down Expand Up @@ -285,6 +287,8 @@ pub const StructBuilder = struct {
return @bitCast(self.readIntField(U, offset));
}

pub fn readVoid(_: StructBuilder) void {}

pub fn writeFloatField(self: StructBuilder, comptime T: type, offset: u32, value: T) void {
const U = comptime switch (T) {
f32 => u32,
Expand Down
25 changes: 22 additions & 3 deletions src/refactor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ pub fn Refactor(comptime W: type) type {
ctx.indenter.inc();
}

pub fn openVoidSetter(ctx: *WriteContext, name: []const u8) E!void {
try ctx.writeIndent();
try ctx.writer.print("pub fn set{s}(self: @This()) void {{\n", .{capitalized(name)});
ctx.indenter.inc();
}

pub fn closeGetter(ctx: *WriteContext) E!void {
ctx.indenter.dec();
try ctx.writeIndent();
Expand Down Expand Up @@ -433,7 +439,7 @@ pub fn Refactor(comptime W: type) type {
switch (t.which()) {
.void => {
{
try ctx.writer.writeAll("return void{};\n");
try ctx.writer.writeAll("return self.reader.readVoid();\n");
}
},
.bool => {
Expand Down Expand Up @@ -497,6 +503,12 @@ pub fn Refactor(comptime W: type) type {
try ctx.writeNodeNameById((try field.getSlot().?.getType()).getStruct().?.getTypeId());
try ctx.writer.print(", {d});\n", .{field.getSlot().?.getOffset()});
},
.anyPointer => {
switch (gt) {
.reader => try ctx.writer.print("return self.reader.readAnyPointer({});\n", .{field.getSlot().?.getOffset()}),
.builder => try ctx.writer.print("return self.builder.writeAnyPointer({});\n", .{field.getSlot().?.getOffset()}),
}
},
else => {},
}
},
Expand Down Expand Up @@ -527,9 +539,9 @@ pub fn Refactor(comptime W: type) type {

switch (t.which()) {
.void => {
try ctx.openSetter(try field.getName(), "void", "void");
try ctx.openVoidSetter(try field.getName());
try ctx.writeIndent();
try ctx.writer.writeAll("return;\n");
try ctx.writer.writeAll("_ = self; return;\n");
try ctx.closeSetter();
},

Expand Down Expand Up @@ -604,6 +616,13 @@ pub fn Refactor(comptime W: type) type {
try ctx.closeSetter();
},

.anyPointer => {
try ctx.openSetter(try field.getName(), "capnp.AnyPointerReader", "capnp.Error!void");
try ctx.writeIndent();
try ctx.writer.print("return self.builder.setAnyPointerField({d}, value);", .{field.getSlot().?.getOffset()});
try ctx.closeSetter();
},

else => {},
}
},
Expand Down

0 comments on commit 940b6dc

Please sign in to comment.