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...@@ -94,16 +94,8 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
94 comptime assertEqual(v.args.len, 1);94 comptime assertEqual(v.args.len, 1);
95 const x = search(v.args[0], data);95 const x = search(v.args[0], data);
96 switch (@typeInfo(@TypeOf(x))) {96 switch (@typeInfo(@TypeOf(x))) {
97 .Bool => if (x) {97 .Bool => try doif(writer, body, bottom, data, ctx, indent, flag1, x, true),
98 try do(writer, body, data, ctx, indent, flag1);98 .Optional => try docap(writer, body, bottom, data, ctx, indent, flag1, x, true),
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 },
107 else => unreachable,99 else => unreachable,
108 }100 }
109 },101 },
...@@ -111,16 +103,8 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype...@@ -111,16 +103,8 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
111 comptime assertEqual(v.args.len, 1);103 comptime assertEqual(v.args.len, 1);
112 const x = search(v.args[0], data);104 const x = search(v.args[0], data);
113 switch (@typeInfo(@TypeOf(x))) {105 switch (@typeInfo(@TypeOf(x))) {
114 .Bool => if (x) {106 .Bool => try doif(writer, body, bottom, data, ctx, indent, flag1, x, false),
115 try do(writer, bottom, data, ctx, indent, flag1);107 .Optional => try docap(writer, body, bottom, data, ctx, indent, flag1, x, false),
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 },
124 else => unreachable,108 else => unreachable,
125 }109 }
126 },110 },
...@@ -209,3 +193,19 @@ fn contains(haystack: []const []const u8, needle: []const u8) bool {...@@ -209,3 +193,19 @@ fn contains(haystack: []const []const u8, needle: []const u8) bool {
209 }193 }
210 return false;194 return false;
211}195}
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}