forked from natecraddock/ziglua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefine.zig
More file actions
172 lines (151 loc) · 5.1 KB
/
define.zig
File metadata and controls
172 lines (151 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const std = @import("std");
const ArrayList = std.ArrayListUnmanaged; // this should be deleted when zig 0.14 support is no longer necessary
const StringHashMap = std.StringHashMapUnmanaged; // this should be deleted when zig 0.14 support is no longer necessary
const String = ArrayList(u8);
const Database = StringHashMap(void);
pub const DefineState = struct {
database: Database,
definitions: ArrayList(String),
pub const empty: DefineState = .{
.database = .empty,
.definitions = .empty,
};
pub fn deinit(self: *DefineState, gpa: std.mem.Allocator) void {
for (self.definitions.items) |*def| {
def.deinit(gpa);
}
self.database.deinit(gpa);
self.definitions.deinit(gpa);
self.* = undefined;
}
};
pub fn define(
gpa: std.mem.Allocator,
absolute_output_path: []const u8,
comptime to_define: []const type,
) !void {
var state: DefineState = .empty;
defer state.deinit(gpa);
inline for (to_define) |T| {
_ = try addClass(&state, gpa, T);
}
var file = try std.fs.createFileAbsolute(absolute_output_path, .{});
defer file.close();
try file.seekTo(0);
try file.writeAll(file_header);
for (state.definitions.items) |def| {
try file.writeAll(def.items);
try file.writeAll("\n");
}
try file.setEndPos(try file.getPos());
}
const file_header: []const u8 =
\\---@meta
\\
\\--- This is an autogenerated file,
\\--- Do not modify
\\
\\
;
fn name(comptime T: type) []const u8 {
return (comptime std.fs.path.extension(@typeName(T)))[1..];
}
fn addEnum(
state: *DefineState,
gpa: std.mem.Allocator,
comptime T: type,
) !void {
if (state.database.contains(@typeName(T)) == false) {
try state.database.put(gpa, @typeName(T), {});
const item = try state.definitions.addOne(gpa);
item.* = .empty;
try item.appendSlice(gpa, "---@alias ");
try item.appendSlice(gpa, name(T));
try item.appendSlice(gpa, "\n");
inline for (@typeInfo(T).@"enum".fields) |field| {
try item.appendSlice(gpa, "---|\' \"");
try item.appendSlice(gpa, field.name);
try item.appendSlice(gpa, "\" \'\n");
}
}
}
pub fn addClass(
state: *DefineState,
gpa: std.mem.Allocator,
comptime T: type,
) !void {
if (state.database.contains(@typeName(T)) == false) {
try state.database.put(gpa, @typeName(T), {});
const item = try state.definitions.addOne(gpa);
const idx = state.definitions.items.len - 1;
item.* = .empty;
try item.appendSlice(gpa, "---@class (exact) ");
try item.appendSlice(gpa, name(T));
try item.appendSlice(gpa, "\n");
inline for (@typeInfo(T).@"struct".fields) |field| {
try item.appendSlice(gpa, "---@field ");
try item.appendSlice(gpa, field.name);
if (field.defaultValue() != null) {
try item.appendSlice(gpa, "?");
}
try item.appendSlice(gpa, " ");
try luaTypeName(state, gpa, idx, field.type);
try state.definitions.items[idx].appendSlice(gpa, "\n");
}
}
}
fn luaTypeName(
state: *DefineState,
gpa: std.mem.Allocator,
index: usize,
comptime T: type,
) !void {
switch (@typeInfo(T)) {
.@"struct" => {
try state.definitions.items[index].appendSlice(gpa, name(T));
try addClass(state, gpa, T);
},
.pointer => |info| {
if (info.child == u8 and info.size == .slice) {
try state.definitions.items[index].appendSlice(gpa, "string");
} else switch (info.size) {
.one => {
try state.definitions.items[index].appendSlice(gpa, "lightuserdata");
},
.c, .many, .slice => {
try luaTypeName(state, gpa, index, info.child);
try state.definitions.items[index].appendSlice(gpa, "[]");
},
}
},
.array => |info| {
try luaTypeName(state, gpa, index, info.child);
try state.definitions.items[index].appendSlice(gpa, "[]");
},
.vector => |info| {
try luaTypeName(state, gpa, index, info.child);
try state.definitions.items[index].appendSlice(gpa, "[]");
},
.optional => |info| {
try luaTypeName(state, gpa, index, info.child);
try state.definitions.items[index].appendSlice(gpa, " | nil");
},
.@"enum" => {
try state.definitions.items[index].appendSlice(gpa, name(T));
try addEnum(state, gpa, T);
},
.int => {
try state.definitions.items[index].appendSlice(gpa, "integer");
},
.float => {
try state.definitions.items[index].appendSlice(gpa, "number");
},
.bool => {
try state.definitions.items[index].appendSlice(gpa, "boolean");
},
else => {
@compileLog(T);
@compileError("Type not supported");
},
}
}