authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-13 23:08:14 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-13 23:08:14 -07:00
logd0308658232e7dc51983a870949ba0777fd8b501
treebd81dc67b2a4902d95556b14d1faf01852e685a4
parentd20900446be9506c189a7a8912d229e0f5690aa7

wrap if block evaluation in function


1 files changed, 20 insertions(+), 20 deletions(-)

src/lib.zig+20-20
......@@ -94,16 +94,8 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
9494 comptime assertEqual(v.args.len, 1);
9595 const x = search(v.args[0], data);
9696 switch (@typeInfo(@TypeOf(x))) {
97 .Bool => if (x) {
98 try do(writer, body, data, ctx, indent, flag1);
99 } else {
100 try do(writer, bottom, data, ctx, indent, flag1);
101 },
102 .Optional => if (x) |_| {
103 try do(writer, body, data, ctx, indent, flag1);
104 } else {
105 try do(writer, bottom, data, ctx, indent, flag1);
106 },
97 .Bool => try doif(writer, body, bottom, data, ctx, indent, flag1, x, true),
98 .Optional => try docap(writer, body, bottom, data, ctx, indent, flag1, x, true),
10799 else => unreachable,
108100 }
109101 },
......@@ -111,16 +103,8 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
111103 comptime assertEqual(v.args.len, 1);
112104 const x = search(v.args[0], data);
113105 switch (@typeInfo(@TypeOf(x))) {
114 .Bool => if (x) {
115 try do(writer, bottom, data, ctx, indent, flag1);
116 } else {
117 try do(writer, body, data, ctx, indent, flag1);
118 },
119 .Optional => if (x) |_| {
120 try do(writer, bottom, data, ctx, indent, flag1);
121 } else {
122 try do(writer, body, data, ctx, indent, flag1);
123 },
106 .Bool => try doif(writer, body, bottom, data, ctx, indent, flag1, x, false),
107 .Optional => try docap(writer, body, bottom, data, ctx, indent, flag1, x, false),
124108 else => unreachable,
125109 }
126110 },
......@@ -209,3 +193,19 @@ fn contains(haystack: []const []const u8, needle: []const u8) bool {
209193 }
210194 return false;
211195}
196
197fn doif(writer: anytype, comptime top: astgen.Value, comptime bottom: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool, flag2: bool, flag3: bool) anyerror!void {
198 if (flag2 == flag3) {
199 try do(writer, top, data, ctx, indent, flag1);
200 } else {
201 try do(writer, bottom, data, ctx, indent, flag1);
202 }
203}
204
205fn docap(writer: anytype, comptime top: astgen.Value, comptime bottom: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool, flag2: bool, flag3: bool) anyerror!void {
206 if (flag2 == flag3) {
207 try do(writer, top, data, ctx, indent, flag1);
208 } else {
209 try do(writer, bottom, data, ctx, indent, flag1);
210 }
211}