minor fixes

This commit is contained in:
yuzu 2025-05-30 01:07:33 -05:00
parent 4fcbe20a15
commit 01a22ccc9e
3 changed files with 23 additions and 10 deletions

View File

@ -52,7 +52,11 @@ pub fn reflectT(self: *Self, comptime T: type, allocator: mem.Allocator, idx: us
.int, .comptime_int => return @intFromFloat(number), .int, .comptime_int => return @intFromFloat(number),
.float, .comptime_float => return @floatCast(number), .float, .comptime_float => return @floatCast(number),
.@"struct" => |structInfo| switch (structInfo.layout) { .@"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 => return error.TypeError,
}, },
else => unreachable, else => unreachable,
@ -158,7 +162,8 @@ test reflectT {
\\{ \\{
\\ "age": 15, \\ "age": 15,
\\ "name": "Yuzu", \\ "name": "Yuzu",
\\ "admin": true \\ "admin": true,
\\ "flags": 0
\\} \\}
; ;
var self = try allocator.create(Self); var self = try allocator.create(Self);
@ -168,13 +173,20 @@ test reflectT {
defer self.deinit(allocator); defer self.deinit(allocator);
const idx: usize = try self.parse(allocator); const idx: usize = try self.parse(allocator);
const UserFlags = packed struct {
is_cool: bool = false,
is_friendly: bool = false,
};
const UserSchema = struct { const UserSchema = struct {
age: f64, age: f64,
name: []const u8, name: []const u8,
admin: bool, 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 });
} }

View File

@ -45,6 +45,7 @@ test {
_ = @import("language.zig"); _ = @import("language.zig");
_ = @import("strings.zig"); _ = @import("strings.zig");
_ = @import("tokenizer.zig"); _ = @import("tokenizer.zig");
_ = @import("reflection.zig");
} }
fn expectPass(comptime path: []const u8) !void { fn expectPass(comptime path: []const u8) !void {

View File

@ -47,7 +47,7 @@ stack: []usize,
frame: usize, frame: usize,
/// Initialize a new tokenizer /// 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); const stack = try allocator.alloc(usize, 0x100);
errdefer allocator.free(stack); errdefer allocator.free(stack);
@memset(stack, 0); @memset(stack, 0);
@ -279,7 +279,7 @@ pub fn nextIdentifier(self: *Self, allocator: mem.Allocator) Error!Token {
const ident = buffer[0..i]; const ident = buffer[0..i];
// true // true
if (.mem.eql(u8, ident, "true")) { if (mem.eql(u8, ident, "true")) {
return .{ return .{
.type = .true, .type = .true,
.value = null, .value = null,
@ -289,7 +289,7 @@ pub fn nextIdentifier(self: *Self, allocator: mem.Allocator) Error!Token {
} }
// false // false
if (.mem.eql(u8, ident, "false")) { if (mem.eql(u8, ident, "false")) {
return .{ return .{
.type = .false, .type = .false,
.value = null, .value = null,
@ -299,7 +299,7 @@ pub fn nextIdentifier(self: *Self, allocator: mem.Allocator) Error!Token {
} }
// null // null
if (.mem.eql(u8, ident, "null")) { if (mem.eql(u8, ident, "null")) {
return .{ return .{
.type = .null, .type = .null,
.value = null, .value = null,
@ -387,7 +387,7 @@ pub fn nextString(self: *Self, allocator: mem.Allocator) Error!Token {
switch (try self.lastChar()) { switch (try self.lastChar()) {
'"' => { '"' => {
while (.mem.indexOfScalar(u8, buffer.items, 0x00)) |idx| while (mem.indexOfScalar(u8, buffer.items, 0x00)) |idx|
_ = buffer.swapRemove(idx); _ = buffer.swapRemove(idx);
return .{ return .{