authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-07-06 03:41:43 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-07-06 03:41:43 -07:00
log7e545955bb4492f5beff995ff902c24cb91c7650
treecab676d4eb96d5583c8abac2ad9ca6c03386f1e3
parent8c6cafd94a5c0978ed534edc14d8847960941aa6

handle multi-arg if functions


1 files changed, 30 insertions(+), 4 deletions(-)

src/lib.zig+30-4
......@@ -135,12 +135,25 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V
135135 for (x) |item| try do(alloc, writer, body, item, ctx, opts);
136136 },
137137 .@"if" => {
138 comptime assertEqual(v.args.len, 1);
139138 if (v.func) |n| {
140 const x2 = try @field(opts.Ctx, "pek_" ++ n)(alloc, x);
139 const f = @field(opts.Ctx, "pek_" ++ n);
140 const x2 = try switch (@typeInfo(@TypeOf(f)).Fn.params.len) {
141 2 => f(alloc, x),
142 3 => blk: {
143 const y = resolveArg(v.args[1], data, ctx);
144 break :blk f(alloc, x, y);
145 },
146 4 => blk: {
147 const y = resolveArg(v.args[1], data, ctx);
148 const z = resolveArg(v.args[2], data, ctx);
149 break :blk f(alloc, x, y, z);
150 },
151 else => unreachable, // TODO
152 };
141153 try doif(alloc, writer, body, bottom, data, ctx, opts, x2);
142154 return;
143155 }
156 comptime assertEqual(v.args.len, 1);
144157 if (comptime std.meta.trait.isIndexable(T)) {
145158 try doif(alloc, writer, body, bottom, data, ctx, opts, x.len > 0);
146159 return;
......@@ -152,12 +165,25 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V
152165 }
153166 },
154167 .ifnot => {
155 comptime assertEqual(v.args.len, 1);
156168 if (v.func) |n| {
157 const x2 = try @field(opts.Ctx, "pek_" ++ n)(alloc, x);
169 const f = @field(opts.Ctx, "pek_" ++ n);
170 const x2 = try switch (@typeInfo(@TypeOf(f)).Fn.params.len) {
171 2 => f(alloc, x),
172 3 => blk: {
173 const y = resolveArg(v.args[1], data, ctx);
174 break :blk f(alloc, x, y);
175 },
176 4 => blk: {
177 const y = resolveArg(v.args[1], data, ctx);
178 const z = resolveArg(v.args[2], data, ctx);
179 break :blk f(alloc, x, y, z);
180 },
181 else => unreachable, // TODO
182 };
158183 try doif(alloc, writer, body, bottom, data, ctx, opts, !x2);
159184 return;
160185 }
186 comptime assertEqual(v.args.len, 1);
161187 switch (comptime TI) {
162188 .Bool => try doif(alloc, writer, body, bottom, data, ctx, opts, !x),
163189 .Optional => try docap(alloc, writer, body, bottom, data, ctx, opts, !x),