summaryrefslogtreecommitdiff
path: root/codegen
diff options
context:
space:
mode:
authorGravatar Jose Colon Rodriguez2024-02-14 12:02:02 -0400
committerGravatar Jose Colon Rodriguez2024-02-14 12:02:02 -0400
commit703a824c1bb7fc41535c9515c5a2209d04899d19 (patch)
tree0572d99ce590a644bf54444a41e20a9688fecb19 /codegen
parentRemoved unreachables from CodePointIterator (diff)
downloadzg-703a824c1bb7fc41535c9515c5a2209d04899d19.tar.gz
zg-703a824c1bb7fc41535c9515c5a2209d04899d19.tar.xz
zg-703a824c1bb7fc41535c9515c5a2209d04899d19.zip
Code reorg; Added UCD
Diffstat (limited to 'codegen')
-rw-r--r--codegen/emoji.zig93
-rw-r--r--codegen/grapheme_break.zig206
2 files changed, 299 insertions, 0 deletions
diff --git a/codegen/emoji.zig b/codegen/emoji.zig
new file mode 100644
index 0000000..acad0ca
--- /dev/null
+++ b/codegen/emoji.zig
@@ -0,0 +1,93 @@
1const std = @import("std");
2
3const emoji = @import("ziglyph").emoji;
4
5const block_size = 256;
6const Block = [block_size]bool;
7
8const BlockMap = std.HashMap(
9 Block,
10 u16,
11 struct {
12 pub fn hash(_: @This(), k: Block) u64 {
13 var hasher = std.hash.Wyhash.init(0);
14 std.hash.autoHashStrat(&hasher, k, .DeepRecursive);
15 return hasher.final();
16 }
17
18 pub fn eql(_: @This(), a: Block, b: Block) bool {
19 return std.mem.eql(bool, &a, &b);
20 }
21 },
22 std.hash_map.default_max_load_percentage,
23);
24
25pub fn main() !void {
26 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
27 defer arena.deinit();
28 const allocator = arena.allocator();
29
30 var blocks_map = BlockMap.init(allocator);
31 defer blocks_map.deinit();
32
33 var stage1 = std.ArrayList(u16).init(allocator);
34 defer stage1.deinit();
35
36 var stage2 = std.ArrayList(bool).init(allocator);
37 defer stage2.deinit();
38
39 var block: Block = [_]bool{false} ** block_size;
40 var block_len: u16 = 0;
41
42 for (0..0x10ffff + 1) |cp| {
43 const isEmoji = emoji.isExtendedPictographic(@intCast(cp));
44
45 block[block_len] = isEmoji;
46 block_len += 1;
47
48 if (block_len < block_size and cp != 0x10ffff) continue;
49
50 const gop = try blocks_map.getOrPut(block);
51 if (!gop.found_existing) {
52 gop.value_ptr.* = @intCast(stage2.items.len);
53 try stage2.appendSlice(&block);
54 }
55
56 try stage1.append(gop.value_ptr.*);
57 block_len = 0;
58 }
59
60 var args_iter = std.process.args();
61 _ = args_iter.skip();
62 const output_path = args_iter.next() orelse @panic("No output file arg!");
63
64 var out_file = try std.fs.cwd().createFile(output_path, .{});
65 defer out_file.close();
66 var out_buf = std.io.bufferedWriter(out_file.writer());
67 const writer = out_buf.writer();
68
69 try writer.print("const stage_1 = [{}]u16{{", .{stage1.items.len});
70 for (stage1.items) |v| {
71 _ = try writer.print("{},", .{v});
72 }
73 try writer.writeAll("};\n");
74
75 try writer.print("const stage_2 = [{}]bool{{", .{stage2.items.len});
76 for (stage2.items) |v| {
77 _ = try writer.print("{},", .{v});
78 }
79 try writer.writeAll("};\n");
80
81 const code =
82 \\pub inline fn isExtendedPictographic(cp: u21) bool {
83 \\ const stage_1_index = cp >> 8;
84 \\ const stage_2_index = stage_1[stage_1_index] + (cp & 0xff);
85 \\ return stage_2[stage_2_index];
86 \\}
87 \\
88 ;
89
90 try writer.writeAll(code);
91
92 try out_buf.flush();
93}
diff --git a/codegen/grapheme_break.zig b/codegen/grapheme_break.zig
new file mode 100644
index 0000000..ace875c
--- /dev/null
+++ b/codegen/grapheme_break.zig
@@ -0,0 +1,206 @@
1const std = @import("std");
2
3const gbp = @import("ziglyph").grapheme_break;
4
5const Prop = enum {
6 none,
7
8 control,
9 extend,
10 hangul_l,
11 hangul_lv,
12 hangul_lvt,
13 hangul_v,
14 hangul_t,
15 prepend,
16 regional,
17 spacing,
18 zwj,
19
20 fn forCodePoint(cp: u21) Prop {
21 if (gbp.isControl(cp)) return .control;
22 if (gbp.isExtend(cp)) return .extend;
23 if (gbp.isL(cp)) return .hangul_l;
24 if (gbp.isLv(cp)) return .hangul_lv;
25 if (gbp.isLvt(cp)) return .hangul_lvt;
26 if (gbp.isT(cp)) return .hangul_t;
27 if (gbp.isV(cp)) return .hangul_v;
28 if (gbp.isPrepend(cp)) return .prepend;
29 if (gbp.isRegionalIndicator(cp)) return .regional;
30 if (gbp.isSpacingmark(cp)) return .spacing;
31 if (gbp.isZwj(cp)) return .zwj;
32
33 return .none;
34 }
35};
36
37const block_size = 256;
38const Block = [block_size]u4;
39
40const BlockMap = std.HashMap(
41 Block,
42 u16,
43 struct {
44 pub fn hash(_: @This(), k: Block) u64 {
45 var hasher = std.hash.Wyhash.init(0);
46 std.hash.autoHashStrat(&hasher, k, .DeepRecursive);
47 return hasher.final();
48 }
49
50 pub fn eql(_: @This(), a: Block, b: Block) bool {
51 return std.mem.eql(u4, &a, &b);
52 }
53 },
54 std.hash_map.default_max_load_percentage,
55);
56
57pub fn main() !void {
58 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
59 defer arena.deinit();
60 const allocator = arena.allocator();
61
62 var blocks_map = BlockMap.init(allocator);
63 defer blocks_map.deinit();
64
65 var stage1 = std.ArrayList(u16).init(allocator);
66 defer stage1.deinit();
67
68 var stage2 = std.ArrayList(u4).init(allocator);
69 defer stage2.deinit();
70
71 var stage3 = std.ArrayList(Prop).init(allocator);
72 defer stage3.deinit();
73
74 var block: Block = [_]u4{0} ** block_size;
75 var block_len: u16 = 0;
76
77 for (0..0x10ffff + 1) |cp| {
78 const prop = Prop.forCodePoint(@intCast(cp));
79
80 const block_idx = blk: {
81 for (stage3.items, 0..) |item, i| {
82 if (item == prop) break :blk i;
83 }
84
85 const idx = stage3.items.len;
86 try stage3.append(prop);
87 break :blk idx;
88 };
89
90 block[block_len] = @intCast(block_idx);
91 block_len += 1;
92
93 if (block_len < block_size and cp != 0x10ffff) continue;
94
95 const gop = try blocks_map.getOrPut(block);
96 if (!gop.found_existing) {
97 gop.value_ptr.* = @intCast(stage2.items.len);
98 try stage2.appendSlice(&block);
99 }
100
101 try stage1.append(gop.value_ptr.*);
102 block_len = 0;
103 }
104
105 var args_iter = std.process.args();
106 _ = args_iter.skip();
107 const output_path = args_iter.next() orelse @panic("No output file arg!");
108
109 var out_file = try std.fs.cwd().createFile(output_path, .{});
110 defer out_file.close();
111 var out_buf = std.io.bufferedWriter(out_file.writer());
112 const writer = out_buf.writer();
113
114 const prop_code =
115 \\const Prop = enum {
116 \\ none,
117 \\
118 \\ control,
119 \\ extend,
120 \\ hangul_l,
121 \\ hangul_lv,
122 \\ hangul_lvt,
123 \\ hangul_v,
124 \\ hangul_t,
125 \\ prepend,
126 \\ regional,
127 \\ spacing,
128 \\ zwj,
129 \\};
130 \\
131 ;
132
133 try writer.writeAll(prop_code);
134
135 try writer.print("const stage_1 = [{}]u16{{", .{stage1.items.len});
136 for (stage1.items) |v| {
137 _ = try writer.print("{},", .{v});
138 }
139 try writer.writeAll("};\n");
140
141 try writer.print("const stage_2 = [{}]u4{{", .{stage2.items.len});
142 for (stage2.items) |v| {
143 _ = try writer.print("{},", .{v});
144 }
145 try writer.writeAll("};\n");
146
147 try writer.print("const stage_3 = [{}]Prop{{", .{stage3.items.len});
148 for (stage3.items) |v| {
149 _ = try writer.print(".{s},", .{@tagName(v)});
150 }
151 try writer.writeAll("};\n");
152
153 const code =
154 \\inline fn getProp(cp: u21) Prop {
155 \\ const stage_1_index = cp >> 8;
156 \\ const stage_2_index = stage_1[stage_1_index] + (cp & 0xff);
157 \\ const stage_3_index = stage_2[stage_2_index];
158 \\ return stage_3[stage_3_index];
159 \\}
160 \\
161 \\pub inline fn isControl(cp: u21) bool {
162 \\ return getProp(cp) == .control;
163 \\}
164 \\
165 \\pub inline fn isExtend(cp: u21) bool {
166 \\ return getProp(cp) == .extend;
167 \\}
168 \\
169 \\pub inline fn isL(cp: u21) bool {
170 \\ return getProp(cp) == .hangul_l;
171 \\}
172 \\pub inline fn isLv(cp: u21) bool {
173 \\ return getProp(cp) == .hangul_lv;
174 \\}
175 \\pub inline fn isLvt(cp: u21) bool {
176 \\ return getProp(cp) == .hangul_lvt;
177 \\}
178 \\pub inline fn isV(cp: u21) bool {
179 \\ return getProp(cp) == .hangul_v;
180 \\}
181 \\pub inline fn isT(cp: u21) bool {
182 \\ return getProp(cp) == .hangul_t;
183 \\}
184 \\
185 \\pub inline fn isPrepend(cp: u21) bool {
186 \\ return getProp(cp) == .prepend;
187 \\}
188 \\
189 \\pub inline fn isRegionalIndicator(cp: u21) bool {
190 \\ return getProp(cp) == .regional;
191 \\}
192 \\
193 \\pub inline fn isSpacingmark(cp: u21) bool {
194 \\ return getProp(cp) == .spacing;
195 \\}
196 \\
197 \\pub inline fn isZwj(cp: u21) bool {
198 \\ return getProp(cp) == .zwj;
199 \\}
200 \\
201 ;
202
203 try writer.writeAll(code);
204
205 try out_buf.flush();
206}