From d0308658232e7dc51983a870949ba0777fd8b501 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 13 Aug 2021 23:08:14 -0700 Subject: [PATCH] wrap if block evaluation in function --- src/lib.zig | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/lib.zig b/src/lib.zig index 356072a6b304798e927b2e131f389865033db16c..0b61fd8c2d8aadfb110dfd1dec6386ec70e8c980 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -94,16 +94,8 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype comptime assertEqual(v.args.len, 1); const x = search(v.args[0], data); switch (@typeInfo(@TypeOf(x))) { - .Bool => if (x) { - try do(writer, body, data, ctx, indent, flag1); - } else { - try do(writer, bottom, data, ctx, indent, flag1); - }, - .Optional => if (x) |_| { - try do(writer, body, data, ctx, indent, flag1); - } else { - try do(writer, bottom, data, ctx, indent, flag1); - }, + .Bool => try doif(writer, body, bottom, data, ctx, indent, flag1, x, true), + .Optional => try docap(writer, body, bottom, data, ctx, indent, flag1, x, true), else => unreachable, } }, @@ -111,16 +103,8 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype comptime assertEqual(v.args.len, 1); const x = search(v.args[0], data); switch (@typeInfo(@TypeOf(x))) { - .Bool => if (x) { - try do(writer, bottom, data, ctx, indent, flag1); - } else { - try do(writer, body, data, ctx, indent, flag1); - }, - .Optional => if (x) |_| { - try do(writer, bottom, data, ctx, indent, flag1); - } else { - try do(writer, body, data, ctx, indent, flag1); - }, + .Bool => try doif(writer, body, bottom, data, ctx, indent, flag1, x, false), + .Optional => try docap(writer, body, bottom, data, ctx, indent, flag1, x, false), else => unreachable, } }, @@ -209,3 +193,19 @@ fn contains(haystack: []const []const u8, needle: []const u8) bool { } return false; } + +fn 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 { + if (flag2 == flag3) { + try do(writer, top, data, ctx, indent, flag1); + } else { + try do(writer, bottom, data, ctx, indent, flag1); + } +} + +fn 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 { + if (flag2 == flag3) { + try do(writer, top, data, ctx, indent, flag1); + } else { + try do(writer, bottom, data, ctx, indent, flag1); + } +} -- 2.54.0