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 {...@@ -34,6 +34,7 @@ pub const Replacement = struct {
3434
35pub const Block = struct {35pub const Block = struct {
36 name: Type,36 name: Type,
37 func: ?string,
37 args: []const []const string,38 args: []const []const string,
38 body: Body,39 body: Body,
39 bttm: Body,40 bttm: Body,
...@@ -151,6 +152,7 @@ const Parser = struct {...@@ -151,6 +152,7 @@ const Parser = struct {
151 std.debug.assert(w.len > 0);152 std.debug.assert(w.len > 0);
152 std.debug.assert(w[0] != '_');153 std.debug.assert(w[0] != '_');
153 if (std.meta.stringToEnum(Block.Type, w)) |name| {154 if (std.meta.stringToEnum(Block.Type, w)) |name| {
155 const func = if (self.tryEatSymbol("#")) self.eat(.word) else null;
154 const args = self.doArgs();156 const args = self.doArgs();
155 var children: []const Value = &.{};157 var children: []const Value = &.{};
156 var bottom: []const Value = &.{};158 var bottom: []const Value = &.{};
...@@ -171,6 +173,7 @@ const Parser = struct {...@@ -171,6 +173,7 @@ const Parser = struct {
171 self.eatSymbol("/");173 self.eatSymbol("/");
172 return Value{ .block = Block{174 return Value{ .block = Block{
173 .name = name,175 .name = name,
176 .func = func,
174 .args = args,177 .args = args,
175 .body = children,178 .body = children,
176 .bttm = bottom,179 .bttm = bottom,
src/lib.zig+10
...@@ -137,6 +137,11 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V...@@ -137,6 +137,11 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V
137 },137 },
138 .@"if" => {138 .@"if" => {
139 comptime assertEqual(v.args.len, 1);139 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 }
140 if (comptime std.meta.trait.isIndexable(T)) {145 if (comptime std.meta.trait.isIndexable(T)) {
141 try doif(alloc, writer, body, bottom, data, ctx, opts, x.len > 0);146 try doif(alloc, writer, body, bottom, data, ctx, opts, x.len > 0);
142 return;147 return;
...@@ -149,6 +154,11 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V...@@ -149,6 +154,11 @@ inline fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.V
149 },154 },
150 .ifnot => {155 .ifnot => {
151 comptime assertEqual(v.args.len, 1);156 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 }
152 switch (comptime TI) {162 switch (comptime TI) {
153 .Bool => try doif(alloc, writer, body, bottom, data, ctx, opts, !x),163 .Bool => try doif(alloc, writer, body, bottom, data, ctx, opts, !x),
154 .Optional => try docap(alloc, writer, body, bottom, data, ctx, opts, !x),164 .Optional => try docap(alloc, writer, body, bottom, data, ctx, opts, !x),