Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions src/genny/languages/zig.nim
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,15 @@ proc exportProc(
indent = false,
comments = ""
) =
let onClass = owner notin ["void", ""]
let indent =
if onClass:
true
else:
indent

if indent:
code.add " "
let
onClass = owner notin ["void", ""]
baseIndent =
if onClass or indent:
" "
else:
""

code.add &"extern fn {apiProcName}("
code.add &"{baseIndent}extern fn {apiProcName}("
for i, param in procParams:
if onClass and i == 0:
code.add "self"
Expand All @@ -126,7 +124,7 @@ proc exportProc(
code.add exportTypeZigAbi(param[1])
code.add &", "
code.removeSuffix ", "
code.add ") callconv(.C) "
code.add ") "
if procReturn != "":
code.add exportTypeZigAbi(procReturn);
else:
Expand All @@ -137,14 +135,9 @@ proc exportProc(
for line in comments.split("\n"):
var line = line
line.removePrefix("##")
if indent:
code.add " "
code.add "/// " & line.strip() & "\n"

if indent:
code.add " "
code.add &"{baseIndent}/// " & line.strip() & "\n"

code.add &"pub inline fn {procName}("
code.add &"{baseIndent}pub inline fn {procName}("
for i, param in procParams[0 .. ^1]:
if onClass and i == 0:
code.add "self"
Expand All @@ -155,17 +148,15 @@ proc exportProc(
code.add &", "
code.removeSuffix ", "
code.add ") "
if procRaises:
code.add "Error!"
if procReturn != "":
code.add procReturn;
else:
code.add "void"
code.add " "
code.add "{\n"

if indent:
code.add " "
code.add " return "

var call = ""
call.add apiProcName
call.add "("
Expand All @@ -180,11 +171,22 @@ proc exportProc(
call.add ", "
call.removeSuffix ", "
call.add &")"
code.add convertImportToZig(call, procReturn)
code.add ";\n"
if indent:
code.add " "
code.add "}\n\n"

if procRaises:
let hasReturn = procReturn notin ["", "void"]
if hasReturn:
code.add &"{baseIndent} const result = {call};\n"
else:
code.add &"{baseIndent} {call};\n"
code.add &"{baseIndent} if (checkError()) return error.$LibError;\n"
if hasReturn:
let resultValue = convertImportToZig("result", procReturn)
code.add &"{baseIndent} return {resultValue};\n"
else:
code.add &"{baseIndent} return;\n"
else:
code.add &"{baseIndent} return {convertImportToZig(call, procReturn)};\n"
code.add baseIndent & "}\n\n"

proc exportProcZig*(
sym: NimNode,
Expand Down Expand Up @@ -287,7 +289,7 @@ proc genRefObject(objName: string) =
code.add &"pub const {objName} = opaque " & "{\n"

let unrefLibProc = &"$lib_{toSnakeCase(objName)}_unref"
code.add &" extern fn {unrefLibProc}(self: *{objName}) callconv(.C) void;\n"
code.add &" extern fn {unrefLibProc}(self: *{objName}) void;\n"
code.add &" pub inline fn deinit(self: *{objName}) void " & "{\n"
code.add &" return {unrefLibProc}(self);\n"
code.add " }\n\n"
Expand Down Expand Up @@ -400,6 +402,10 @@ proc exportSeqZig*(sym: NimNode) =
const header = """
const std = @import("std");

pub const Error = error{
$LibError,
};

"""

proc writeZig*(dir, lib: string) =
Expand Down
76 changes: 40 additions & 36 deletions tests/generated/test.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const std = @import("std");

pub const Error = error{
testError,
};

pub const simple_const = 123;

pub const SimpleEnum = enum(u8) {
Expand All @@ -8,7 +12,7 @@ pub const SimpleEnum = enum(u8) {
third = 2,
};

extern fn test_simple_call(a: isize) callconv(.C) isize;
extern fn test_simple_call(a: isize) isize;
/// Returns the integer passed in.
pub inline fn simpleCall(a: isize) isize {
return test_simple_call(a);
Expand All @@ -27,131 +31,131 @@ pub const SimpleObj = extern struct {
};
}

extern fn test_simple_obj_eq(self: SimpleObj, other: SimpleObj) callconv(.C) bool;
extern fn test_simple_obj_eq(self: SimpleObj, other: SimpleObj) bool;
pub inline fn eql(self: SimpleObj, other: SimpleObj) bool {
return test_simple_obj_eq(self, other);
}
};

pub const SimpleRefObj = opaque {
extern fn test_simple_ref_obj_unref(self: *SimpleRefObj) callconv(.C) void;
extern fn test_simple_ref_obj_unref(self: *SimpleRefObj) void;
pub inline fn deinit(self: *SimpleRefObj) void {
return test_simple_ref_obj_unref(self);
}

extern fn test_new_simple_ref_obj() callconv(.C) *SimpleRefObj;
extern fn test_new_simple_ref_obj() *SimpleRefObj;
/// Creates new SimpleRefObj.
pub inline fn init() *SimpleRefObj {
return test_new_simple_ref_obj();
}

extern fn test_simple_ref_obj_get_simple_ref_a(self: *SimpleRefObj) callconv(.C) isize;
extern fn test_simple_ref_obj_get_simple_ref_a(self: *SimpleRefObj) isize;
pub inline fn getSimpleRefA(self: *SimpleRefObj) isize {
return test_simple_ref_obj_get_simple_ref_a(self);
}

extern fn test_simple_ref_obj_set_simple_ref_a(self: *SimpleRefObj, value: isize) callconv(.C) void;
extern fn test_simple_ref_obj_set_simple_ref_a(self: *SimpleRefObj, value: isize) void;
pub inline fn setSimpleRefA(self: *SimpleRefObj, value: isize) void {
return test_simple_ref_obj_set_simple_ref_a(self, value);
}

extern fn test_simple_ref_obj_get_simple_ref_b(self: *SimpleRefObj) callconv(.C) u8;
extern fn test_simple_ref_obj_get_simple_ref_b(self: *SimpleRefObj) u8;
pub inline fn getSimpleRefB(self: *SimpleRefObj) u8 {
return test_simple_ref_obj_get_simple_ref_b(self);
}

extern fn test_simple_ref_obj_set_simple_ref_b(self: *SimpleRefObj, value: u8) callconv(.C) void;
extern fn test_simple_ref_obj_set_simple_ref_b(self: *SimpleRefObj, value: u8) void;
pub inline fn setSimpleRefB(self: *SimpleRefObj, value: u8) void {
return test_simple_ref_obj_set_simple_ref_b(self, value);
}

extern fn test_simple_ref_obj_doit(self: *SimpleRefObj) callconv(.C) void;
extern fn test_simple_ref_obj_doit(self: *SimpleRefObj) void;
/// Does some thing with SimpleRefObj.
pub inline fn doit(self: *SimpleRefObj) void {
return test_simple_ref_obj_doit(self);
}
};

pub const SeqInt = opaque {
extern fn test_seq_int_unref(self: *SeqInt) callconv(.C) void;
extern fn test_seq_int_unref(self: *SeqInt) void;
pub inline fn deinit(self: *SeqInt) void {
return test_seq_int_unref(self);
}

extern fn test_new_seq_int() callconv(.C) *SeqInt;
extern fn test_new_seq_int() *SeqInt;
pub inline fn init() *SeqInt {
return test_new_seq_int();
}

extern fn test_seq_int_len(self: *SeqInt) callconv(.C) isize;
extern fn test_seq_int_len(self: *SeqInt) isize;
pub inline fn len(self: *SeqInt) isize {
return test_seq_int_len(self);
}

extern fn test_seq_int_get(self: *SeqInt, index: isize) callconv(.C) isize;
extern fn test_seq_int_get(self: *SeqInt, index: isize) isize;
pub inline fn get(self: *SeqInt, index: isize) isize {
return test_seq_int_get(self, index);
}

extern fn test_seq_int_set(self: *SeqInt, index: isize, value: isize) callconv(.C) void;
extern fn test_seq_int_set(self: *SeqInt, index: isize, value: isize) void;
pub inline fn set(self: *SeqInt, index: isize, value: isize) void {
return test_seq_int_set(self, index, value);
}

extern fn test_seq_int_add(self: *SeqInt, value: isize) callconv(.C) void;
extern fn test_seq_int_add(self: *SeqInt, value: isize) void;
pub inline fn append(self: *SeqInt, value: isize) void {
return test_seq_int_add(self, value);
}

extern fn test_seq_int_delete(self: *SeqInt, index: isize) callconv(.C) void;
extern fn test_seq_int_delete(self: *SeqInt, index: isize) void;
pub inline fn remove(self: *SeqInt, index: isize) void {
return test_seq_int_delete(self, index);
}

extern fn test_seq_int_clear(self: *SeqInt) callconv(.C) void;
extern fn test_seq_int_clear(self: *SeqInt) void;
pub inline fn clear(self: *SeqInt) void {
return test_seq_int_clear(self);
}
};

pub const RefObjWithSeq = opaque {
extern fn test_ref_obj_with_seq_unref(self: *RefObjWithSeq) callconv(.C) void;
extern fn test_ref_obj_with_seq_unref(self: *RefObjWithSeq) void;
pub inline fn deinit(self: *RefObjWithSeq) void {
return test_ref_obj_with_seq_unref(self);
}

extern fn test_new_ref_obj_with_seq() callconv(.C) *RefObjWithSeq;
extern fn test_new_ref_obj_with_seq() *RefObjWithSeq;
/// Creates new SimpleRefObj.
pub inline fn init() *RefObjWithSeq {
return test_new_ref_obj_with_seq();
}

extern fn test_ref_obj_with_seq_data_len(self: *RefObjWithSeq) callconv(.C) isize;
extern fn test_ref_obj_with_seq_data_len(self: *RefObjWithSeq) isize;
pub inline fn lenData(self: *RefObjWithSeq) isize {
return test_ref_obj_with_seq_data_len(self);
}

extern fn test_ref_obj_with_seq_data_get(self: *RefObjWithSeq, index: isize) callconv(.C) u8;
extern fn test_ref_obj_with_seq_data_get(self: *RefObjWithSeq, index: isize) u8;
pub inline fn getData(self: *RefObjWithSeq, index: isize) u8 {
return test_ref_obj_with_seq_data_get(self, index);
}

extern fn test_ref_obj_with_seq_data_set(self: *RefObjWithSeq, index: isize, value: u8) callconv(.C) void;
extern fn test_ref_obj_with_seq_data_set(self: *RefObjWithSeq, index: isize, value: u8) void;
pub inline fn setData(self: *RefObjWithSeq, index: isize, value: u8) void {
return test_ref_obj_with_seq_data_set(self, index, value);
}

extern fn test_ref_obj_with_seq_data_add(self: *RefObjWithSeq, value: u8) callconv(.C) void;
extern fn test_ref_obj_with_seq_data_add(self: *RefObjWithSeq, value: u8) void;
pub inline fn appendData(self: *RefObjWithSeq, value: u8) void {
return test_ref_obj_with_seq_data_add(self, value);
}

extern fn test_ref_obj_with_seq_data_delete(self: *RefObjWithSeq, index: isize) callconv(.C) void;
extern fn test_ref_obj_with_seq_data_delete(self: *RefObjWithSeq, index: isize) void;
pub inline fn removeData(self: *RefObjWithSeq, index: isize) void {
return test_ref_obj_with_seq_data_delete(self, index);
}

extern fn test_ref_obj_with_seq_data_clear(self: *RefObjWithSeq) callconv(.C) void;
extern fn test_ref_obj_with_seq_data_clear(self: *RefObjWithSeq) void;
pub inline fn clearData(self: *RefObjWithSeq) void {
return test_ref_obj_with_seq_data_clear(self);
}
Expand All @@ -170,60 +174,60 @@ pub const SimpleObjWithProc = extern struct {
};
}

extern fn test_simple_obj_with_proc_eq(self: SimpleObjWithProc, other: SimpleObjWithProc) callconv(.C) bool;
extern fn test_simple_obj_with_proc_eq(self: SimpleObjWithProc, other: SimpleObjWithProc) bool;
pub inline fn eql(self: SimpleObjWithProc, other: SimpleObjWithProc) bool {
return test_simple_obj_with_proc_eq(self, other);
}

extern fn test_simple_obj_with_proc_extra_proc(self: SimpleObjWithProc) callconv(.C) void;
extern fn test_simple_obj_with_proc_extra_proc(self: SimpleObjWithProc) void;
pub inline fn extraProc(self: SimpleObjWithProc) void {
return test_simple_obj_with_proc_extra_proc(self);
}
};

pub const SeqString = opaque {
extern fn test_seq_string_unref(self: *SeqString) callconv(.C) void;
extern fn test_seq_string_unref(self: *SeqString) void;
pub inline fn deinit(self: *SeqString) void {
return test_seq_string_unref(self);
}

extern fn test_new_seq_string() callconv(.C) *SeqString;
extern fn test_new_seq_string() *SeqString;
pub inline fn init() *SeqString {
return test_new_seq_string();
}

extern fn test_seq_string_len(self: *SeqString) callconv(.C) isize;
extern fn test_seq_string_len(self: *SeqString) isize;
pub inline fn len(self: *SeqString) isize {
return test_seq_string_len(self);
}

extern fn test_seq_string_get(self: *SeqString, index: isize) callconv(.C) [*:0]const u8;
extern fn test_seq_string_get(self: *SeqString, index: isize) [*:0]const u8;
pub inline fn get(self: *SeqString, index: isize) [:0]const u8 {
return std.mem.span(test_seq_string_get(self, index));
}

extern fn test_seq_string_set(self: *SeqString, index: isize, value: [*:0]const u8) callconv(.C) void;
extern fn test_seq_string_set(self: *SeqString, index: isize, value: [*:0]const u8) void;
pub inline fn set(self: *SeqString, index: isize, value: [:0]const u8) void {
return test_seq_string_set(self, index, value.ptr);
}

extern fn test_seq_string_add(self: *SeqString, value: [*:0]const u8) callconv(.C) void;
extern fn test_seq_string_add(self: *SeqString, value: [*:0]const u8) void;
pub inline fn append(self: *SeqString, value: [:0]const u8) void {
return test_seq_string_add(self, value.ptr);
}

extern fn test_seq_string_delete(self: *SeqString, index: isize) callconv(.C) void;
extern fn test_seq_string_delete(self: *SeqString, index: isize) void;
pub inline fn remove(self: *SeqString, index: isize) void {
return test_seq_string_delete(self, index);
}

extern fn test_seq_string_clear(self: *SeqString) callconv(.C) void;
extern fn test_seq_string_clear(self: *SeqString) void;
pub inline fn clear(self: *SeqString) void {
return test_seq_string_clear(self);
}
};

extern fn test_get_datas() callconv(.C) *SeqString;
extern fn test_get_datas() *SeqString;
pub inline fn getDatas() *SeqString {
return test_get_datas();
}
Expand Down
Loading
Loading