authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-06-14 21:09:26 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-06-14 21:09:26 -07:00
log08ae93da6fde7392d66bac2c34c4a4f530fe7696
tree88ec00cbb9152b5fc5c8d845d7c195d2cf628eb5
parent3dbac838d6bf37842a478d47e8fbdd15cddbde7a

allow custom functions to be used in #if and #ifnot


2 files changed, 13 insertions(+), 0 deletions(-)

src/astgen.zig+3
......@@ -34,6 +34,7 @@ pub const Replacement = struct {
3434
3535pub const Block = struct {
3636 name: Type,
37 func: ?string,
3738 args: []const []const string,
3839 body: Body,
3940 bttm: Body,
......@@ -151,6 +152,7 @@ const Parser = struct {
151152 std.debug.assert(w.len > 0);
152153 std.debug.assert(w[0] != '_');
153154 if (std.meta.stringToEnum(Block.Type, w)) |name| {
155 const func = if (self.tryEatSymbol("#")) self.eat(.word) else null;
154156 const args = self.doArgs();
155157 var children: []const Value = &.{};
156158 var bottom: []const Value = &.{};
......@@ -171,6 +173,7 @@ const Parser = struct {
171173 self.eatSymbol("/");
172174 return Value{ .block = Block{
173175 .name = name,
176 .func = func,
174177 .args = args,
175178 .body = children,
176179 .bttm = bottom,
src/lib.zig+10
......@@ -137,6 +137,11 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V
137137 },
138138 .@"if" => {
139139 comptime assertEqual(v.args.len, 1);
140 if (v.func) |n| {
141 const x2 = try @field(opts.Ctx, "pek_" ++ n)(alloc, x);
142 try doif(alloc, writer, body, bottom, data, ctx, opts, x2);
143 return;
144 }
140145 if (comptime std.meta.trait.isIndexable(T)) {
141146 try doif(alloc, writer, body, bottom, data, ctx, opts, x.len > 0);
142147 return;
......@@ -149,6 +154,11 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V
149154 },
150155 .ifnot => {
151156 comptime assertEqual(v.args.len, 1);
157 if (v.func) |n| {
158 const x2 = try @field(opts.Ctx, "pek_" ++ n)(alloc, x);
159 try doif(alloc, writer, body, bottom, data, ctx, opts, !x2);
160 return;
161 }
152162 switch (comptime TI) {
153163 .Bool => try doif(alloc, writer, body, bottom, data, ctx, opts, !x),
154164 .Optional => try docap(alloc, writer, body, bottom, data, ctx, opts, !x),