mirror of
https://codeberg.org/fairyglade/ly.git
synced 2026-08-09 07:09:13 +02:00
feat: WIP add lua config parser
Non-working implementation.
This commit is contained in:
parent
7c2ae2738c
commit
f457b148f9
3 changed files with 70 additions and 0 deletions
|
|
@ -21,6 +21,13 @@ pub fn build(b: *std.Build) void {
|
|||
const zigini = b.dependency("zigini", .{ .target = target, .optimize = optimize });
|
||||
mod.addImport("zigini", zigini.module("zigini"));
|
||||
|
||||
const zlua = b.dependency("zlua", .{
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.lang = .luajit,
|
||||
});
|
||||
mod.addImport("zlua", zlua.module("zlua"));
|
||||
|
||||
const translate_c = b.dependency("translate_c", .{
|
||||
.target = target,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -12,6 +12,10 @@
|
|||
.url = "git+https://codeberg.org/ziglang/translate-c?ref=zig-0.16.x#6fe0ffc4549f15c5f2d9432c2b4460ba90ff85ac",
|
||||
.hash = "translate_c-1.0.0-Q_BUWo_5BgD4flHdUhA31zOz0XvZk9k7lQv1ouzyNXj2",
|
||||
},
|
||||
.zlua = .{
|
||||
.url = "git+https://github.com/natecraddock/ziglua?ref=zig-0.16#8f271c82baa5fc43aa02a72f6da020c2025d9436",
|
||||
.hash = "zlua-0.1.0-hGRpC2aABQD4D9PBVH3wAW8k32-I4969MRQ0CpOwoley",
|
||||
},
|
||||
},
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub const ini = @import("zigini");
|
||||
pub const zlua = @import("zlua");
|
||||
pub const Lua = zlua.Lua;
|
||||
|
||||
pub const interop = @import("interop.zig");
|
||||
pub const UidRange = @import("UidRange.zig");
|
||||
|
|
@ -76,3 +78,60 @@ pub fn IniParser(comptime Struct: type) type {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn LuaParser(comptime Struct: type) type {
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
structure: Struct,
|
||||
|
||||
pub fn init(
|
||||
allocator: std.mem.Allocator,
|
||||
io: std.Io,
|
||||
path: []const u8,
|
||||
) !Self {
|
||||
var lua: Lua = try .init(allocator);
|
||||
defer lua.deinit();
|
||||
|
||||
lua.openLibs();
|
||||
|
||||
// convert to sentinel terminated slice
|
||||
const spath: [:0]const u8 = try allocator.dupeSentinel(u8, path, 0);
|
||||
lua.doFile(spath) catch {
|
||||
const error_str = lua.toString(-1) catch unreachable;
|
||||
const lua_str = try allocator.dupeSentinel(u8, error_str, 0);
|
||||
_ = lua_str; // TODO add error logging
|
||||
};
|
||||
var data: Struct = .{};
|
||||
switch (@typeInfo(Struct)) {
|
||||
.@"struct" => |struc| {
|
||||
inline for (struc.fields) |field| {
|
||||
try setField(allocator, lua, field, &data);
|
||||
}
|
||||
},
|
||||
else => @compileError("Expected a struct."),
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
pub fn setField(allocator: std.mem.Allocator, lua: *Lua, comptime field: std.builtin.Type.StructField, data: *Struct) !void {
|
||||
try lua.getGlobal("ly");
|
||||
defer lua.pop(1); // pop ly table
|
||||
switch (@typeInfo(field.type)) {
|
||||
.int => {
|
||||
_ = lua.getField(-1, field.name);
|
||||
const value = try lua.toInteger(-1);
|
||||
lua.pop(1);
|
||||
@field(data, field.name) = @intCast(@as(field.type, @truncate(value)));
|
||||
},
|
||||
.float => {
|
||||
_ = lua.getField(-1, field.name);
|
||||
const value = try lua.toNumber(-1);
|
||||
lua.pop(1);
|
||||
@field(data, field.name) = @floatCast(@as(field.type, value));
|
||||
},
|
||||
else => @compileError("unimplemented!"),
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue