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...@@ -135,12 +135,25 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V
135 for (x) |item| try do(alloc, writer, body, item, ctx, opts);135 for (x) |item| try do(alloc, writer, body, item, ctx, opts);
136 },136 },
137 .@"if" => {137 .@"if" => {
138 comptime assertEqual(v.args.len, 1);
139 if (v.func) |n| {138 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 };
141 try doif(alloc, writer, body, bottom, data, ctx, opts, x2);153 try doif(alloc, writer, body, bottom, data, ctx, opts, x2);
142 return;154 return;
143 }155 }
156 comptime assertEqual(v.args.len, 1);
144 if (comptime std.meta.trait.isIndexable(T)) {157 if (comptime std.meta.trait.isIndexable(T)) {
145 try doif(alloc, writer, body, bottom, data, ctx, opts, x.len > 0);158 try doif(alloc, writer, body, bottom, data, ctx, opts, x.len > 0);
146 return;159 return;
...@@ -152,12 +165,25 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V...@@ -152,12 +165,25 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V
152 }165 }
153 },166 },
154 .ifnot => {167 .ifnot => {
155 comptime assertEqual(v.args.len, 1);
156 if (v.func) |n| {168 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 };
158 try doif(alloc, writer, body, bottom, data, ctx, opts, !x2);183 try doif(alloc, writer, body, bottom, data, ctx, opts, !x2);
159 return;184 return;
160 }185 }
186 comptime assertEqual(v.args.len, 1);
161 switch (comptime TI) {187 switch (comptime TI) {
162 .Bool => try doif(alloc, writer, body, bottom, data, ctx, opts, !x),188 .Bool => try doif(alloc, writer, body, bottom, data, ctx, opts, !x),
163 .Optional => try docap(alloc, writer, body, bottom, data, ctx, opts, !x),189 .Optional => try docap(alloc, writer, body, bottom, data, ctx, opts, !x),