minor fixes
This commit is contained in:
parent
4fcbe20a15
commit
01a22ccc9e
@ -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 });
|
||||
}
|
||||
|
1
test.zig
1
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 {
|
||||
|
@ -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 .{
|
||||
|
Loading…
x
Reference in New Issue
Block a user