diff --git a/reflection.zig b/reflection.zig index 24934f1..916c6ee 100644 --- a/reflection.zig +++ b/reflection.zig @@ -52,7 +52,11 @@ pub fn reflectT(self: *Self, comptime T: type, allocator: mem.Allocator, idx: us .int, .comptime_int => return @intFromFloat(number), .float, .comptime_float => return @floatCast(number), .@"struct" => |structInfo| switch (structInfo.layout) { - .@"packed" => return @bitCast(number), + .@"packed" => { + const my_int: structInfo.backing_integer.? = + @intFromFloat(number); + return @bitCast(my_int); + }, else => return error.TypeError, }, else => unreachable, @@ -158,7 +162,8 @@ test reflectT { \\{ \\ "age": 15, \\ "name": "Yuzu", - \\ "admin": true + \\ "admin": true, + \\ "flags": 0 \\} ; var self = try allocator.create(Self); @@ -168,13 +173,20 @@ test reflectT { defer self.deinit(allocator); const idx: usize = try self.parse(allocator); + + const UserFlags = packed struct { + is_cool: bool = false, + is_friendly: bool = false, + }; + const UserSchema = struct { age: f64, name: []const u8, admin: bool, + flags: UserFlags, }; - const root = try self.reflectT(UserSchema, allocator, idx); - errdefer allocator.free(root.name); - std.debug.print("my name is {s}\n", .{root.name}); + const root = try self.reflectT(UserSchema, allocator, idx); + + std.debug.print("is cool? {} is friendly? {}\n", .{ root.flags.is_cool, root.flags.is_friendly }); } diff --git a/test.zig b/test.zig index ad5dcb2..4941de5 100644 --- a/test.zig +++ b/test.zig @@ -45,6 +45,7 @@ test { _ = @import("language.zig"); _ = @import("strings.zig"); _ = @import("tokenizer.zig"); + _ = @import("reflection.zig"); } fn expectPass(comptime path: []const u8) !void { diff --git a/tokenizer.zig b/tokenizer.zig index 1455423..ad26c1e 100644 --- a/tokenizer.zig +++ b/tokenizer.zig @@ -47,7 +47,7 @@ stack: []usize, frame: usize, /// Initialize a new tokenizer -pub fn init(allocator: .mem.Allocator, text: []const u8) mem.Allocator.Error!Self { +pub fn init(allocator: mem.Allocator, text: []const u8) mem.Allocator.Error!Self { const stack = try allocator.alloc(usize, 0x100); errdefer allocator.free(stack); @memset(stack, 0); @@ -279,7 +279,7 @@ pub fn nextIdentifier(self: *Self, allocator: mem.Allocator) Error!Token { const ident = buffer[0..i]; // true - if (.mem.eql(u8, ident, "true")) { + if (mem.eql(u8, ident, "true")) { return .{ .type = .true, .value = null, @@ -289,7 +289,7 @@ pub fn nextIdentifier(self: *Self, allocator: mem.Allocator) Error!Token { } // false - if (.mem.eql(u8, ident, "false")) { + if (mem.eql(u8, ident, "false")) { return .{ .type = .false, .value = null, @@ -299,7 +299,7 @@ pub fn nextIdentifier(self: *Self, allocator: mem.Allocator) Error!Token { } // null - if (.mem.eql(u8, ident, "null")) { + if (mem.eql(u8, ident, "null")) { return .{ .type = .null, .value = null, @@ -387,7 +387,7 @@ pub fn nextString(self: *Self, allocator: mem.Allocator) Error!Token { switch (try self.lastChar()) { '"' => { - while (.mem.indexOfScalar(u8, buffer.items, 0x00)) |idx| + while (mem.indexOfScalar(u8, buffer.items, 0x00)) |idx| _ = buffer.swapRemove(idx); return .{