+ Build nzsl from sources

This commit is contained in:
REMqb
2024-04-30 01:47:30 +02:00
parent 57ae2d9c2f
commit 97c82cd688
10 changed files with 600 additions and 0 deletions

104
src/lib.zig git.filemode.normal_file
View File

@@ -0,0 +1,104 @@
//const nzsl = @cImport({
// @cInclude("CNZSL/CNZSL.h");
//});
const std = @import("std");
const cnzsl = @import("cimport.zig");
pub fn parseSource(source: []const u8) !Module {
const module = Module{.instance = null};
if(cnzsl.nzslParserParseSource(module.instance, source.ptr, source.len) != 0) {
defer module.release();
std.log.err("Error while parsing source: {s}", .{ module.getLastError() });
return error.FailedToParse;
}
return module;
}
pub fn parseSourceWithFilePath(source: []const u8, filePath: []const u8) !Module {
const module: ?*cnzsl.nzslModule = null;
if(cnzsl.nzslParserParseSourceWithFilePath(module, source.ptr, source.len, filePath.ptr, filePath.len) != 0) {
defer cnzsl.nzslModuleDestroy(module);
std.log.err("Error while parsing source: {s}", .{ module.getLastError() });
return error.FailedToParse;
}
if (module == null) {
std.log.err("Error while parsing source: Unknown error", .{ });
return error.FailedToParse;
}
return .{.instance = module};
}
pub fn parseFromFile(filePath: []const u8) !Module {
const module: ?*cnzsl.nzslModule = null;
if(cnzsl.nzslParserParseFromFile(module, filePath.ptr, filePath.len) != 0) {
defer cnzsl.nzslModuleDestroy(module);
std.log.err("Error while parsing source: {s}", .{ cnzsl.nzslModuleGetLastError(module) });
return error.FailedToParse;
}
if (module == null) {
std.log.err("Error while parsing source: Unknown error", .{ });
return error.FailedToParse;
}
return .{.instance = module};
}
pub const Module = struct {
instance: *cnzsl.nzslModule,
pub fn release(self: Module) void {
cnzsl.nzslModuleDestroy(self.instance);
}
pub fn getLastError(self: Module) [*c]const u8 {
return cnzsl.nzslModuleGetLastError(self.instance);
}
};
pub const GlslWriter = struct {
instance: *cnzsl.nzslGlslWriter,
pub fn create() GlslWriter {
return .{.instance = cnzsl.nzslGlslWriterCreate() orelse unreachable};
}
pub fn release(self: GlslWriter) void {
cnzsl.nzslGlslWriterDestroy(self.instance);
}
pub fn generate(self: GlslWriter, module: Module) !GlslOutput {
var output: ?*cnzsl.nzslGlslOutput = null;
output = cnzsl.nzslGlslWriterGenerate(self.instance, module.instance, null, null);
if(output == null) {
std.log.err("Failed to generate glsl output: {s}", .{ self.getLastError() });
return error.FailedToGenerateGlsl;
}
return .{.instance = output orelse unreachable};
}
pub fn getLastError(self: GlslWriter) [*c]const u8 {
return cnzsl.nzslGlslWriterGetLastError(self.instance);
}
};
pub const GlslOutput = struct {
instance: *cnzsl.nzslGlslOutput,
pub fn release(self: GlslOutput) void {
cnzsl.nzslGlslOutputDestroy(self.instance);
}
};

24
src/main.zig git.filemode.normal_file
View File

@@ -0,0 +1,24 @@
const std = @import("std");
pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("Run `zig build test` to run the tests.\n", .{});
try bw.flush(); // don't forget to flush!
}
test "simple test" {
var list = std.ArrayList(i32).init(std.testing.allocator);
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}

3
src/nzsl-c.zig git.filemode.normal_file
View File

@@ -0,0 +1,3 @@
pub usingnamespace @cImport({
@cInclude("CNZSL/CNZSL.h");
});

10
src/root.zig git.filemode.normal_file
View File

@@ -0,0 +1,10 @@
const std = @import("std");
const testing = std.testing;
export fn add(a: i32, b: i32) i32 {
return a + b;
}
test "basic add functionality" {
try testing.expect(add(3, 7) == 10);
}